test volatile trigger

This commit is contained in:
Xu Chang 2023-09-02 19:08:37 +08:00
parent 80e51d39ef
commit cf0497bab4
5 changed files with 113 additions and 18 deletions

View File

@ -12,6 +12,7 @@
"scripts": {
"copy-files": "copyfiles -u 1 src/**/*.json lib/",
"test": "ts-node test/test.ts",
"test2": "ts-node test/testDbStore.ts",
"build": "tsc && npm run copy-files"
},
"dependencies": {

25
test/meta/Context.ts Normal file
View File

@ -0,0 +1,25 @@
import { EntityDict } from 'oak-domain/lib/base-app-domain';
import { AsyncContext } from 'oak-domain/lib/store/AsyncRowStore';
class Context extends AsyncContext<EntityDict> {
async refineOpRecords(): Promise<void> {
return;
}
isRoot(): boolean {
return true;
}
getCurrentUserId(allowUnloggedIn?: boolean | undefined): string | undefined {
throw 'bbb';
}
toString(): string {
return '';
}
allowUserUpdate(): boolean {
throw new Error('Method not implemented.');
}
openRootMode(): () => void {
return () => undefined;
}
};
export default Context;

12
test/meta/dbConfig.ts Normal file
View File

@ -0,0 +1,12 @@
import { MySQLConfiguration } from "oak-db/lib/MySQL/types/Configuration";
const config : MySQLConfiguration = {
host: 'localhost',
user: 'root',
database: 'obb',
charset: 'utf8mb4_general_ci',
connectionLimit: 20,
password: 'root',
};
export default config;

View File

@ -1,21 +1,20 @@
import { initialize } from '../src/index';
import { Configuration } from '../src/types/Configuration';
import { AppLoader } from '../src/AppLoader';
import { EntityDict } from '../../jichuang/src/oak-app-domain';
import Context from './meta/Context';
import dbConfig from './meta/dbConfig';
const configuration: Configuration = {
mysql: {
host: 'localhost',
user: 'root',
database: 'obb',
charset: 'utf8mb4_general_ci',
connectionLimit: 20,
password: '',
},
async function main() {
const appLoader = new AppLoader<EntityDict, Context>(`${__dirname}/../../jichuang`, () => async (store) => new Context(store), dbConfig);
await appLoader.mount(true);
await appLoader.initialize(true);
await appLoader.unmount();
console.log('data initialized');
}
initialize(`${__dirname}/../../bangzuxia`, configuration, true)
main()
.then(
() => console.log('success')
)
.catch(
).catch(
(err) => console.error(err)
);

58
test/testDbStore.ts Normal file
View File

@ -0,0 +1,58 @@
import { CreateTrigger } from 'oak-domain/lib/types';
import { DbStore } from '../src/DbStore';
import Context from './meta/Context';
import dbConfig from './meta/dbConfig';
import { EntityDict, storageSchema, ActionCascadePathGraph, RelationCascadePathGraph } from 'oak-domain/lib/base-app-domain';
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
const store = new DbStore<EntityDict, Context>(storageSchema, () => async (store) => new Context(store), dbConfig, ActionCascadePathGraph, RelationCascadePathGraph, {});
async function init() {
store.connect();
await store.initialize(true);
store.disconnect();
}
async function testVolatileTrigger() {
store.connect();
let execCount = 0;
store.registerTrigger({
name: 'ttt',
entity: 'user',
action: 'create',
when: 'commit',
strict: 'makeSure',
fn: async(event, context) => {
const { operation } = event;
console.log(JSON.stringify(operation));
execCount ++;
return 1;
}
} as CreateTrigger<EntityDict, 'user', Context>);
const cxt = new Context(store);
await cxt.begin();
await store.operate('user', {
id: await generateNewIdAsync(),
action: 'create',
data: {
id: await generateNewIdAsync(),
nickname: 'xxx',
name: 'ccc',
}
}, cxt, {});
await cxt.commit();
console.log('execCount', execCount);
}
init()
.then(
() => testVolatileTrigger()
);