serialize classes
This commit is contained in:
parent
c287ac2cb8
commit
473fc38457
3 changed files with 103 additions and 4 deletions
87
src/services/storage/serializationHelpers.ts
Normal file
87
src/services/storage/serializationHelpers.ts
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
import { FindOperator, LessThan, MoreThan, LessThanOrEqual, MoreThanOrEqual, Equal, Like, ILike, Between, In, Any, IsNull, Not, FindOptionsWhere } from 'typeorm';
|
||||
export type WhereCondition<T> = FindOptionsWhere<T> | FindOptionsWhere<T>[]
|
||||
|
||||
type SerializedFindOperator = {
|
||||
_type: 'FindOperator'
|
||||
type: string
|
||||
value: any
|
||||
}
|
||||
|
||||
export function serializeFindOperator(operator: FindOperator<any>): SerializedFindOperator {
|
||||
return {
|
||||
_type: 'FindOperator',
|
||||
type: operator['type'],
|
||||
value: operator['value'],
|
||||
};
|
||||
}
|
||||
|
||||
export function deserializeFindOperator(serialized: SerializedFindOperator): FindOperator<any> {
|
||||
switch (serialized.type) {
|
||||
case 'lessThan':
|
||||
return LessThan(serialized.value);
|
||||
case 'moreThan':
|
||||
return MoreThan(serialized.value);
|
||||
case 'lessThanOrEqual':
|
||||
return LessThanOrEqual(serialized.value);
|
||||
case 'moreThanOrEqual':
|
||||
return MoreThanOrEqual(serialized.value);
|
||||
case 'equal':
|
||||
return Equal(serialized.value);
|
||||
case 'like':
|
||||
return Like(serialized.value);
|
||||
case 'ilike':
|
||||
return ILike(serialized.value);
|
||||
case 'between':
|
||||
return Between(serialized.value[0], serialized.value[1]);
|
||||
case 'in':
|
||||
return In(serialized.value);
|
||||
case 'any':
|
||||
return Any(serialized.value);
|
||||
case 'isNull':
|
||||
return IsNull();
|
||||
case 'not':
|
||||
return Not(deserializeFindOperator(serialized.value));
|
||||
default:
|
||||
throw new Error(`Unknown FindOperator type: ${serialized.type}`);
|
||||
}
|
||||
}
|
||||
|
||||
export function serializeRequest<T>(r: object): T {
|
||||
if (!r || typeof r !== 'object') {
|
||||
return r;
|
||||
}
|
||||
|
||||
if (r instanceof FindOperator) {
|
||||
return serializeFindOperator(r) as any;
|
||||
}
|
||||
|
||||
if (Array.isArray(r)) {
|
||||
return r.map(item => serializeRequest(item)) as any;
|
||||
}
|
||||
|
||||
const result: any = {};
|
||||
for (const [key, value] of Object.entries(r)) {
|
||||
result[key] = serializeRequest(value);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export function deserializeRequest<T>(r: object): T {
|
||||
if (!r || typeof r !== 'object') {
|
||||
return r;
|
||||
}
|
||||
|
||||
if (Array.isArray(r)) {
|
||||
return r.map(item => deserializeRequest(item)) as any;
|
||||
}
|
||||
|
||||
if (r && typeof r === 'object' && (r as any)._type === 'FindOperator') {
|
||||
return deserializeFindOperator(r as any) as any;
|
||||
}
|
||||
|
||||
const result: any = {};
|
||||
for (const [key, value] of Object.entries(r)) {
|
||||
result[key] = deserializeRequest(value);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
@ -10,9 +10,9 @@ import {
|
|||
IncrementOperation,
|
||||
DecrementOperation,
|
||||
SumOperation,
|
||||
WhereCondition
|
||||
} from './storageProcessor.js';
|
||||
import { PickKeysByType } from 'typeorm/common/PickKeysByType.js';
|
||||
import { serializeRequest, WhereCondition } from './serializationHelpers.js';
|
||||
|
||||
export type TX<T> = (txId: string) => Promise<T>
|
||||
|
||||
|
|
@ -147,10 +147,18 @@ export class StorageInterface extends EventEmitter {
|
|||
resolve(response.data);
|
||||
}
|
||||
this.once(op.opId, responseHandler)
|
||||
this.process.send(op)
|
||||
this.process.send(this.serializeOperation(op))
|
||||
})
|
||||
}
|
||||
|
||||
private serializeOperation(operation: IStorageOperation): IStorageOperation {
|
||||
const serialized = { ...operation };
|
||||
if ('q' in serialized) {
|
||||
(serialized as any).q = serializeRequest((serialized as any).q);
|
||||
}
|
||||
return serialized;
|
||||
}
|
||||
|
||||
private checkConnected() {
|
||||
if (!this.isConnected) {
|
||||
throw new Error('Storage processor is not connected');
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
import { DataSource, EntityManager, DeepPartial, FindOptionsWhere, FindOptionsOrder } from 'typeorm';
|
||||
import { DataSource, EntityManager, DeepPartial, FindOptionsWhere, FindOptionsOrder, FindOperator } from 'typeorm';
|
||||
import NewDB, { DbSettings, MainDbEntities, MainDbNames, newMetricsDb } from './db.js';
|
||||
import { PubLogger, getLogger } from '../helpers/logger.js';
|
||||
import { allMetricsMigrations, allMigrations } from './migrations/runner.js';
|
||||
import transactionsQueue from './transactionsQueue.js';
|
||||
import { PickKeysByType } from 'typeorm/common/PickKeysByType';
|
||||
import { deserializeRequest, WhereCondition } from './serializationHelpers.js';
|
||||
|
||||
|
||||
export type WhereCondition<T> = FindOptionsWhere<T> | FindOptionsWhere<T>[]
|
||||
export type QueryOptions<T> = {
|
||||
where?: WhereCondition<T>
|
||||
order?: FindOptionsOrder<T>
|
||||
|
|
@ -154,6 +155,9 @@ class StorageProcessor {
|
|||
private async handleOperation(operation: StorageOperation<any>) {
|
||||
try {
|
||||
const opId = operation.opId;
|
||||
if ((operation as any).q) {
|
||||
(operation as any).q = deserializeRequest((operation as any).q)
|
||||
}
|
||||
switch (operation.type) {
|
||||
case 'connect':
|
||||
return this.handleConnect(operation);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue