重构了一下异步上下文的saveOpRecord行为国
This commit is contained in:
parent
1d61db4592
commit
1fd17a6932
|
|
@ -3,11 +3,11 @@ import { EntityDict, RowStore, OperateOption, OperationResult, SelectOption, Con
|
||||||
import { IncomingHttpHeaders } from "http";
|
import { IncomingHttpHeaders } from "http";
|
||||||
export declare abstract class AsyncContext<ED extends EntityDict> implements Context {
|
export declare abstract class AsyncContext<ED extends EntityDict> implements Context {
|
||||||
rowStore: AsyncRowStore<ED, this>;
|
rowStore: AsyncRowStore<ED, this>;
|
||||||
clusterInfo?: ClusterInfo;
|
|
||||||
private uuid?;
|
private uuid?;
|
||||||
opRecords: OpRecord<ED>[];
|
opRecords: OpRecord<ED>[];
|
||||||
private scene?;
|
private scene?;
|
||||||
private headers?;
|
headers?: IncomingHttpHeaders;
|
||||||
|
clusterInfo?: ClusterInfo;
|
||||||
private message?;
|
private message?;
|
||||||
events: {
|
events: {
|
||||||
commit: Array<() => Promise<void>>;
|
commit: Array<() => Promise<void>>;
|
||||||
|
|
@ -18,12 +18,12 @@ export declare abstract class AsyncContext<ED extends EntityDict> implements Con
|
||||||
*/
|
*/
|
||||||
abstract refineOpRecords(): Promise<void>;
|
abstract refineOpRecords(): Promise<void>;
|
||||||
constructor(store: AsyncRowStore<ED, AsyncContext<ED>>, headers?: IncomingHttpHeaders, clusterInfo?: ClusterInfo);
|
constructor(store: AsyncRowStore<ED, AsyncContext<ED>>, headers?: IncomingHttpHeaders, clusterInfo?: ClusterInfo);
|
||||||
setHeaders(headers: IncomingHttpHeaders): void;
|
|
||||||
getHeader(key: string): string | string[] | undefined;
|
getHeader(key: string): string | string[] | undefined;
|
||||||
getScene(): string | undefined;
|
getScene(): string | undefined;
|
||||||
setScene(scene?: string): void;
|
setScene(scene?: string): void;
|
||||||
private resetEvents;
|
private resetEvents;
|
||||||
on(event: 'commit' | 'rollback', callback: () => Promise<void>): void;
|
on(event: 'commit' | 'rollback', callback: () => Promise<void>): void;
|
||||||
|
saveOpRecord<T extends keyof ED>(entity: T, operation: ED[T]['Operation']): void;
|
||||||
/**
|
/**
|
||||||
* 一个context中不应该有并发的事务,这里将事务串行化,使用的时候千万要注意不要自己等自己
|
* 一个context中不应该有并发的事务,这里将事务串行化,使用的时候千万要注意不要自己等自己
|
||||||
* @param options
|
* @param options
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,15 @@
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
exports.AsyncContext = void 0;
|
exports.AsyncContext = void 0;
|
||||||
const tslib_1 = require("tslib");
|
const tslib_1 = require("tslib");
|
||||||
|
const action_1 = require("../actions/action");
|
||||||
const assert_1 = tslib_1.__importDefault(require("assert"));
|
const assert_1 = tslib_1.__importDefault(require("assert"));
|
||||||
class AsyncContext {
|
class AsyncContext {
|
||||||
rowStore;
|
rowStore;
|
||||||
clusterInfo;
|
|
||||||
uuid;
|
uuid;
|
||||||
opRecords;
|
opRecords;
|
||||||
scene;
|
scene;
|
||||||
headers;
|
headers;
|
||||||
|
clusterInfo;
|
||||||
message;
|
message;
|
||||||
events;
|
events;
|
||||||
constructor(store, headers, clusterInfo) {
|
constructor(store, headers, clusterInfo) {
|
||||||
|
|
@ -24,9 +25,6 @@ class AsyncContext {
|
||||||
this.headers = headers;
|
this.headers = headers;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setHeaders(headers) {
|
|
||||||
this.headers = headers;
|
|
||||||
}
|
|
||||||
getHeader(key) {
|
getHeader(key) {
|
||||||
if (this.headers) {
|
if (this.headers) {
|
||||||
return this.headers[key];
|
return this.headers[key];
|
||||||
|
|
@ -47,6 +45,39 @@ class AsyncContext {
|
||||||
on(event, callback) {
|
on(event, callback) {
|
||||||
this.uuid && this.events[event].push(callback);
|
this.uuid && this.events[event].push(callback);
|
||||||
}
|
}
|
||||||
|
saveOpRecord(entity, operation) {
|
||||||
|
const { action, data, filter, id } = operation;
|
||||||
|
switch (action) {
|
||||||
|
case 'create': {
|
||||||
|
this.opRecords.push({
|
||||||
|
id,
|
||||||
|
a: 'c',
|
||||||
|
e: entity,
|
||||||
|
d: data
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'remove': {
|
||||||
|
this.opRecords.push({
|
||||||
|
id,
|
||||||
|
a: 'r',
|
||||||
|
e: entity,
|
||||||
|
f: filter,
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
(0, assert_1.default)(!action_1.readOnlyActions.includes(action));
|
||||||
|
this.opRecords.push({
|
||||||
|
id,
|
||||||
|
a: 'u',
|
||||||
|
e: entity,
|
||||||
|
d: data,
|
||||||
|
f: filter,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* 一个context中不应该有并发的事务,这里将事务串行化,使用的时候千万要注意不要自己等自己
|
* 一个context中不应该有并发的事务,这里将事务串行化,使用的时候千万要注意不要自己等自己
|
||||||
* @param options
|
* @param options
|
||||||
|
|
|
||||||
|
|
@ -1133,11 +1133,12 @@ class CascadeStore extends RowStore_1.RowStore {
|
||||||
await createInner(operation);
|
await createInner(operation);
|
||||||
}
|
}
|
||||||
if (!option.dontCollect) {
|
if (!option.dontCollect) {
|
||||||
context.opRecords.push({
|
context.saveOpRecord(entity, operation);
|
||||||
|
/* context.opRecords.push({
|
||||||
a: 'c',
|
a: 'c',
|
||||||
e: entity,
|
e: entity,
|
||||||
d: data,
|
d: data as ED[T]['OpSchema'] | ED[T]['OpSchema'][],
|
||||||
});
|
}); */
|
||||||
}
|
}
|
||||||
if (!option.dontCreateOper && !['oper', 'operEntity', 'modiEntity', 'modi'].includes(entity)) {
|
if (!option.dontCreateOper && !['oper', 'operEntity', 'modiEntity', 'modi'].includes(entity)) {
|
||||||
// 按照框架要求生成Oper和OperEntity这两个内置的对象
|
// 按照框架要求生成Oper和OperEntity这两个内置的对象
|
||||||
|
|
@ -1319,7 +1320,17 @@ class CascadeStore extends RowStore_1.RowStore {
|
||||||
};
|
};
|
||||||
if (action === 'remove') {
|
if (action === 'remove') {
|
||||||
if (!option.dontCollect) {
|
if (!option.dontCollect) {
|
||||||
context.opRecords.push({
|
context.saveOpRecord(entity, {
|
||||||
|
id: operId,
|
||||||
|
action,
|
||||||
|
data: {},
|
||||||
|
filter: {
|
||||||
|
id: {
|
||||||
|
$in: ids,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
/* context.opRecords.push({
|
||||||
a: 'r',
|
a: 'r',
|
||||||
e: entity,
|
e: entity,
|
||||||
f: {
|
f: {
|
||||||
|
|
@ -1327,7 +1338,7 @@ class CascadeStore extends RowStore_1.RowStore {
|
||||||
$in: ids,
|
$in: ids,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
}); */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
@ -1338,16 +1349,26 @@ class CascadeStore extends RowStore_1.RowStore {
|
||||||
$$updateAt$$: now,
|
$$updateAt$$: now,
|
||||||
});
|
});
|
||||||
if (!option.dontCollect) {
|
if (!option.dontCollect) {
|
||||||
context.opRecords.push({
|
context.saveOpRecord(entity, {
|
||||||
a: 'u',
|
id: operId,
|
||||||
e: entity,
|
action,
|
||||||
d: data,
|
data: data,
|
||||||
f: {
|
filter: {
|
||||||
id: {
|
id: {
|
||||||
$in: ids,
|
$in: ids,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
/* context.opRecords.push({
|
||||||
|
a: 'u',
|
||||||
|
e: entity,
|
||||||
|
d: data as ED[T]['Update']['data'],
|
||||||
|
f: {
|
||||||
|
id: {
|
||||||
|
$in: ids,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}); */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (action !== 'update') {
|
else if (action !== 'update') {
|
||||||
|
|
|
||||||
|
|
@ -143,17 +143,20 @@ type RemoveOperationData = {
|
||||||
export type RemoveOperation = Operation<'remove', RemoveOperationData, Filter, Sorter>;
|
export type RemoveOperation = Operation<'remove', RemoveOperationData, Filter, Sorter>;
|
||||||
export type CUDOperation = CreateOperation | UpdateOperation | RemoveOperation;
|
export type CUDOperation = CreateOperation | UpdateOperation | RemoveOperation;
|
||||||
export type CreateOpResult<ED extends EntityDict, T extends keyof ED> = {
|
export type CreateOpResult<ED extends EntityDict, T extends keyof ED> = {
|
||||||
|
id?: string;
|
||||||
a: 'c';
|
a: 'c';
|
||||||
e: T;
|
e: T;
|
||||||
d: ED[T]['OpSchema'] | ED[T]['OpSchema'][];
|
d: ED[T]['OpSchema'] | ED[T]['OpSchema'][];
|
||||||
};
|
};
|
||||||
export type UpdateOpResult<ED extends EntityDict, T extends keyof ED> = {
|
export type UpdateOpResult<ED extends EntityDict, T extends keyof ED> = {
|
||||||
|
id?: string;
|
||||||
a: 'u';
|
a: 'u';
|
||||||
e: T;
|
e: T;
|
||||||
d: UpdateOperationData;
|
d: UpdateOperationData;
|
||||||
f?: Filter;
|
f?: Filter;
|
||||||
};
|
};
|
||||||
export type RemoveOpResult<ED extends EntityDict, T extends keyof ED> = {
|
export type RemoveOpResult<ED extends EntityDict, T extends keyof ED> = {
|
||||||
|
id?: string;
|
||||||
a: 'r';
|
a: 'r';
|
||||||
e: T;
|
e: T;
|
||||||
f?: Filter;
|
f?: Filter;
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,16 @@
|
||||||
|
|
||||||
import { EntityDict, RowStore, OperateOption, OperationResult, SelectOption, Context, TxnOption, OpRecord, AggregationResult, ClusterInfo } from "../types";
|
import { EntityDict, RowStore, OperateOption, OperationResult, SelectOption, Context, TxnOption, OpRecord, AggregationResult, ClusterInfo } from "../types";
|
||||||
|
import { readOnlyActions } from '../actions/action';
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import { IncomingHttpHeaders } from "http";
|
import { IncomingHttpHeaders } from "http";
|
||||||
|
|
||||||
export abstract class AsyncContext<ED extends EntityDict> implements Context {
|
export abstract class AsyncContext<ED extends EntityDict> implements Context {
|
||||||
rowStore: AsyncRowStore<ED, this>;
|
rowStore: AsyncRowStore<ED, this>;
|
||||||
clusterInfo?: ClusterInfo;
|
|
||||||
private uuid?: string;
|
private uuid?: string;
|
||||||
opRecords: OpRecord<ED>[];
|
opRecords: OpRecord<ED>[];
|
||||||
private scene?: string;
|
private scene?: string;
|
||||||
private headers?: IncomingHttpHeaders;
|
headers?: IncomingHttpHeaders;
|
||||||
|
clusterInfo?: ClusterInfo;
|
||||||
private message?: string;
|
private message?: string;
|
||||||
events: {
|
events: {
|
||||||
commit: Array<() => Promise<void>>;
|
commit: Array<() => Promise<void>>;
|
||||||
|
|
@ -34,10 +35,6 @@ export abstract class AsyncContext<ED extends EntityDict> implements Context {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setHeaders(headers: IncomingHttpHeaders) {
|
|
||||||
this.headers = headers;
|
|
||||||
}
|
|
||||||
|
|
||||||
getHeader(key: string): string | string[] | undefined {
|
getHeader(key: string): string | string[] | undefined {
|
||||||
if (this.headers) {
|
if (this.headers) {
|
||||||
return this.headers[key];
|
return this.headers[key];
|
||||||
|
|
@ -61,6 +58,40 @@ export abstract class AsyncContext<ED extends EntityDict> implements Context {
|
||||||
this.uuid && this.events[event].push(callback);
|
this.uuid && this.events[event].push(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
saveOpRecord<T extends keyof ED>(entity: T, operation: ED[T]['Operation']) {
|
||||||
|
const { action, data, filter, id } = operation;
|
||||||
|
switch (action) {
|
||||||
|
case 'create': {
|
||||||
|
this.opRecords.push({
|
||||||
|
id,
|
||||||
|
a: 'c',
|
||||||
|
e: entity,
|
||||||
|
d: data as ED[T]['OpSchema']
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'remove': {
|
||||||
|
this.opRecords.push({
|
||||||
|
id,
|
||||||
|
a: 'r',
|
||||||
|
e: entity,
|
||||||
|
f: filter,
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
assert(!readOnlyActions.includes(action));
|
||||||
|
this.opRecords.push({
|
||||||
|
id,
|
||||||
|
a: 'u',
|
||||||
|
e: entity,
|
||||||
|
d: data as ED[T]['Update']['data'],
|
||||||
|
f: filter,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 一个context中不应该有并发的事务,这里将事务串行化,使用的时候千万要注意不要自己等自己
|
* 一个context中不应该有并发的事务,这里将事务串行化,使用的时候千万要注意不要自己等自己
|
||||||
* @param options
|
* @param options
|
||||||
|
|
|
||||||
|
|
@ -1382,11 +1382,12 @@ export abstract class CascadeStore<ED extends EntityDict & BaseEntityDict> exten
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!option.dontCollect) {
|
if (!option.dontCollect) {
|
||||||
context.opRecords.push({
|
context.saveOpRecord(entity, operation);
|
||||||
|
/* context.opRecords.push({
|
||||||
a: 'c',
|
a: 'c',
|
||||||
e: entity,
|
e: entity,
|
||||||
d: data as ED[T]['OpSchema'] | ED[T]['OpSchema'][],
|
d: data as ED[T]['OpSchema'] | ED[T]['OpSchema'][],
|
||||||
});
|
}); */
|
||||||
}
|
}
|
||||||
if (!option.dontCreateOper && !['oper', 'operEntity', 'modiEntity', 'modi'].includes(entity as string)) {
|
if (!option.dontCreateOper && !['oper', 'operEntity', 'modiEntity', 'modi'].includes(entity as string)) {
|
||||||
// 按照框架要求生成Oper和OperEntity这两个内置的对象
|
// 按照框架要求生成Oper和OperEntity这两个内置的对象
|
||||||
|
|
@ -1581,7 +1582,17 @@ export abstract class CascadeStore<ED extends EntityDict & BaseEntityDict> exten
|
||||||
};
|
};
|
||||||
if (action === 'remove') {
|
if (action === 'remove') {
|
||||||
if (!option.dontCollect) {
|
if (!option.dontCollect) {
|
||||||
context.opRecords.push({
|
context.saveOpRecord(entity, {
|
||||||
|
id: operId,
|
||||||
|
action,
|
||||||
|
data: {},
|
||||||
|
filter: {
|
||||||
|
id: {
|
||||||
|
$in: ids,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
/* context.opRecords.push({
|
||||||
a: 'r',
|
a: 'r',
|
||||||
e: entity,
|
e: entity,
|
||||||
f: {
|
f: {
|
||||||
|
|
@ -1589,7 +1600,7 @@ export abstract class CascadeStore<ED extends EntityDict & BaseEntityDict> exten
|
||||||
$in: ids,
|
$in: ids,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
}); */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
@ -1600,7 +1611,17 @@ export abstract class CascadeStore<ED extends EntityDict & BaseEntityDict> exten
|
||||||
$$updateAt$$: now,
|
$$updateAt$$: now,
|
||||||
});
|
});
|
||||||
if (!option.dontCollect) {
|
if (!option.dontCollect) {
|
||||||
context.opRecords.push({
|
context.saveOpRecord(entity, {
|
||||||
|
id: operId,
|
||||||
|
action,
|
||||||
|
data: data as ED[T]['Update']['data'],
|
||||||
|
filter: {
|
||||||
|
id: {
|
||||||
|
$in: ids,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
/* context.opRecords.push({
|
||||||
a: 'u',
|
a: 'u',
|
||||||
e: entity,
|
e: entity,
|
||||||
d: data as ED[T]['Update']['data'],
|
d: data as ED[T]['Update']['data'],
|
||||||
|
|
@ -1609,7 +1630,7 @@ export abstract class CascadeStore<ED extends EntityDict & BaseEntityDict> exten
|
||||||
$in: ids,
|
$in: ids,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
}); */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (action !== 'update') {
|
else if (action !== 'update') {
|
||||||
|
|
|
||||||
|
|
@ -187,12 +187,14 @@ export type RemoveOperation = Operation<'remove', RemoveOperationData, Filter, S
|
||||||
export type CUDOperation = CreateOperation | UpdateOperation | RemoveOperation;
|
export type CUDOperation = CreateOperation | UpdateOperation | RemoveOperation;
|
||||||
|
|
||||||
export type CreateOpResult<ED extends EntityDict, T extends keyof ED> = {
|
export type CreateOpResult<ED extends EntityDict, T extends keyof ED> = {
|
||||||
|
id?: string;
|
||||||
a: 'c';
|
a: 'c';
|
||||||
e: T;
|
e: T;
|
||||||
d: ED[T]['OpSchema'] | ED[T]['OpSchema'][];
|
d: ED[T]['OpSchema'] | ED[T]['OpSchema'][];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type UpdateOpResult<ED extends EntityDict, T extends keyof ED> = {
|
export type UpdateOpResult<ED extends EntityDict, T extends keyof ED> = {
|
||||||
|
id?: string;
|
||||||
a: 'u',
|
a: 'u',
|
||||||
e: T;
|
e: T;
|
||||||
d: UpdateOperationData;
|
d: UpdateOperationData;
|
||||||
|
|
@ -200,6 +202,7 @@ export type UpdateOpResult<ED extends EntityDict, T extends keyof ED> = {
|
||||||
};
|
};
|
||||||
|
|
||||||
export type RemoveOpResult<ED extends EntityDict, T extends keyof ED> = {
|
export type RemoveOpResult<ED extends EntityDict, T extends keyof ED> = {
|
||||||
|
id?: string;
|
||||||
a: 'r',
|
a: 'r',
|
||||||
e: T;
|
e: T;
|
||||||
f?: Filter;
|
f?: Filter;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue