增加了ports初始化的检查

This commit is contained in:
Xu Chang 2023-01-08 18:29:34 +08:00
parent d48b258198
commit 78154763d4
6 changed files with 30 additions and 5 deletions

4
lib/index.d.ts vendored
View File

@ -1,7 +1,7 @@
import { operate, select, fetchRows, count, aggregate } from './crud';
import { amap } from './amap';
import { getTranslations } from './locales';
import { registerPorts, importEntity, exportEntity } from './port';
import { registerPorts, clearPorts, importEntity, exportEntity } from './port';
declare const aspectDict: {
operate: typeof operate;
select: typeof select;
@ -15,4 +15,4 @@ declare const aspectDict: {
};
export default aspectDict;
export * from './AspectDict';
export { registerPorts, };
export { registerPorts, clearPorts, };

View File

@ -1,12 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerPorts = void 0;
exports.clearPorts = exports.registerPorts = void 0;
const tslib_1 = require("tslib");
const crud_1 = require("./crud");
const amap_1 = require("./amap");
const locales_1 = require("./locales");
const port_1 = require("./port");
Object.defineProperty(exports, "registerPorts", { enumerable: true, get: function () { return port_1.registerPorts; } });
Object.defineProperty(exports, "clearPorts", { enumerable: true, get: function () { return port_1.clearPorts; } });
const aspectDict = {
operate: crud_1.operate,
select: crud_1.select,

1
lib/port.d.ts vendored
View File

@ -2,6 +2,7 @@
import { Importation, Exportation, EntityDict } from 'oak-domain/lib/types/Entity';
import { AsyncContext } from 'oak-domain/lib/store/AsyncRowStore';
export declare function registerPorts<ED extends EntityDict>(importations: Importation<ED, keyof ED, any>[], exportations: Exportation<ED, keyof ED, any>[]): void;
export declare function clearPorts(): void;
export declare function importEntity<ED extends EntityDict, Cxt extends AsyncContext<ED>>(params: FormData, context: Cxt): Promise<void>;
export declare function exportEntity<ED extends EntityDict, T extends keyof ED, Cxt extends AsyncContext<ED>>(params: {
entity: T;

View File

@ -1,23 +1,34 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.exportEntity = exports.importEntity = exports.registerPorts = void 0;
exports.exportEntity = exports.importEntity = exports.clearPorts = exports.registerPorts = void 0;
const tslib_1 = require("tslib");
const assert_1 = tslib_1.__importDefault(require("assert"));
const Importations = {};
const Exportations = {};
function registerPorts(importations, exportations) {
for (const i of importations) {
(0, assert_1.default)(!Importations.hasOwnProperty(i.id), `Importation中的id【${i.id}】重复了`);
Object.assign(Importations, {
[i.id]: i,
});
}
for (const e of exportations) {
(0, assert_1.default)(!Exportations.hasOwnProperty(e.id), `Exportation中的id【${e.id}】重复了`);
Object.assign(Exportations, {
[e.id]: e,
});
}
}
exports.registerPorts = registerPorts;
function clearPorts() {
for (const i in Importations) {
delete Importations[i];
}
for (const e in Exportations) {
delete Exportations[e];
}
}
exports.clearPorts = clearPorts;
function getImportation(id) {
(0, assert_1.default)(Importations.hasOwnProperty(id), `id为[${id}]的importation不存在`);
return Importations[id];

View File

@ -1,7 +1,7 @@
import { operate, select, fetchRows, count, aggregate } from './crud';
import { amap } from './amap';
import { getTranslations } from './locales';
import { registerPorts, importEntity, exportEntity } from './port';
import { registerPorts, clearPorts, importEntity, exportEntity } from './port';
const aspectDict = {
operate,
select,
@ -18,4 +18,5 @@ export default aspectDict;
export * from './AspectDict';
export {
registerPorts,
clearPorts,
};

View File

@ -7,18 +7,29 @@ const Exportations: Record<string, any> = {};
export function registerPorts<ED extends EntityDict>(importations: Importation<ED, keyof ED, any>[], exportations: Exportation<ED, keyof ED, any>[]) {
for (const i of importations) {
assert(!Importations.hasOwnProperty(i.id), `Importation中的id【${i.id}】重复了`);
Object.assign(Importations, {
[i.id]: i,
});
}
for (const e of exportations) {
assert(!Exportations.hasOwnProperty(e.id), `Exportation中的id【${e.id}】重复了`);
Object.assign(Exportations, {
[e.id]: e,
});
}
}
export function clearPorts() {
for (const i in Importations) {
delete Importations[i];
}
for (const e in Exportations) {
delete Exportations[e];
}
}
function getImportation<ED extends EntityDict, T extends keyof ED>(id: string) {
assert(Importations.hasOwnProperty(id), `id为[${id}]的importation不存在`);
return Importations[id] as Importation<ED, T, any>;