diff --git a/package.json b/package.json index 371901e..6d95183 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/meta/Context.ts b/test/meta/Context.ts new file mode 100644 index 0000000..d48c08b --- /dev/null +++ b/test/meta/Context.ts @@ -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 { + async refineOpRecords(): Promise { + 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; \ No newline at end of file diff --git a/test/meta/dbConfig.ts b/test/meta/dbConfig.ts new file mode 100644 index 0000000..a932d1f --- /dev/null +++ b/test/meta/dbConfig.ts @@ -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; \ No newline at end of file diff --git a/test/test.ts b/test/test.ts index 20b3bcc..75a49c5 100644 --- a/test/test.ts +++ b/test/test.ts @@ -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(`${__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) -.then( - () => console.log('success') -) -.catch( - (err) => console.error(err) -); \ No newline at end of file + +main() + .then( + () => console.log('success') + ).catch( + (err) => console.error(err) + ); diff --git a/test/testDbStore.ts b/test/testDbStore.ts new file mode 100644 index 0000000..b2f051c --- /dev/null +++ b/test/testDbStore.ts @@ -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(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); + + 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() + ); \ No newline at end of file