From ff8fe544226403a65704203e21b6901f54902b17 Mon Sep 17 00:00:00 2001 From: richardo2016 Date: Tue, 2 May 2023 03:52:34 +0800 Subject: [PATCH] feat: support hook `afterOrmSyncFinished` --- demo/test/integration/hooks.js | 136 ++++++++++++++++++++++++--------- src/Typo/app.ts | 2 + src/db.ts | 10 +++ typings/Typo/app.d.ts | 1 + 4 files changed, 114 insertions(+), 35 deletions(-) diff --git a/demo/test/integration/hooks.js b/demo/test/integration/hooks.js index 992df2e..e7cbc01 100644 --- a/demo/test/integration/hooks.js +++ b/demo/test/integration/hooks.js @@ -1,7 +1,9 @@ const test = require('test'); test.setup(); -describe('Hooks', () => { +const coroutine = require('coroutine'); + +describe.only('Hooks', () => { var tappInfo function setup(opts) { tappInfo = require('../support/spec_helper').getRandomSqliteBasedApp({ @@ -10,12 +12,13 @@ describe('Hooks', () => { } var triggered = { - beforeSetupRoute: false + beforeSetupRoute: false, + afterOrmSyncFinished: false }; - afterEach(() => { + const resetTriggered = () => { Object.keys(triggered).forEach(k => triggered[k] = false) - }) + } const apis = [ 'get', @@ -42,49 +45,112 @@ describe('Hooks', () => { assert.isFunction(app.filterRequest) } - - it('[beforeSetupRoute] triggered after app instanced', () => { - setup({ - hooks: { - beforeSetupRoute () { - assert.isFalse(triggered.beforeSetupRoute) - triggered.beforeSetupRoute = true; - - assert.exist(this) - assert.isObject(this) - assert.isObject(this.api) - assert_apis_exist(this) - } - } + describe('beforeSetupRoute', () => { + afterEach(() => { + resetTriggered(); }) - assert.isTrue(triggered.beforeSetupRoute); - assert_apis_exist(tappInfo.app); - }); - - it('[beforeSetupRoute] triggered after app instanced - next', () => { - setup({ - hooks: { - beforeSetupRoute (next) { - setTimeout(() => { + it('triggered after app instanced', () => { + setup({ + hooks: { + beforeSetupRoute () { assert.isFalse(triggered.beforeSetupRoute) triggered.beforeSetupRoute = true; - + assert.exist(this) assert.isObject(this) - + assert.isObject(this.api) assert_apis_exist(this) - - next() - }, 100) + } + } + }) + + assert.isTrue(triggered.beforeSetupRoute); + assert_apis_exist(tappInfo.app); + }); + + it('triggered after app instanced - next', () => { + setup({ + hooks: { + beforeSetupRoute (next) { + setTimeout(() => { + assert.isFalse(triggered.beforeSetupRoute) + triggered.beforeSetupRoute = true; + + assert.exist(this) + assert.isObject(this) + + assert.isObject(this.api) + assert_apis_exist(this) + + next() + }, 100) + } } - } + }) + + assert.isTrue(triggered.beforeSetupRoute); + assert_apis_exist(tappInfo.app); + }); + }); + + describe('afterOrmSyncFinished', () => { + afterEach(() => { + resetTriggered(); }) - assert.isTrue(triggered.beforeSetupRoute); - assert_apis_exist(tappInfo.app); + it('triggered after app orm instanced', () => { + setup({ + hooks: { + afterOrmSyncFinished () { + assert.isFalse(triggered.afterOrmSyncFinished) + triggered.afterOrmSyncFinished = true; + + assert.exist(this) + assert.isObject(this) + + assert.isObject(this.api) + assert_apis_exist(this) + } + } + }) + + assert.isFalse(triggered.afterOrmSyncFinished); + tappInfo.app.ormPool(orm => { + }) + assert.isTrue(triggered.afterOrmSyncFinished); + assert_apis_exist(tappInfo.app); + }); + + it('triggered after app orm instanced - next', () => { + setup({ + hooks: { + afterOrmSyncFinished (next) { + setTimeout(() => { + assert.isFalse(triggered.afterOrmSyncFinished) + triggered.afterOrmSyncFinished = true; + + assert.exist(this) + assert.isObject(this) + + assert.isObject(this.api) + assert_apis_exist(this) + + // assert.isUndefined(next) + next() + }, 100) + } + } + }) + + assert.isFalse(triggered.afterOrmSyncFinished); + tappInfo.app.ormPool(orm => {}) + coroutine.sleep(100); + assert.isTrue(triggered.afterOrmSyncFinished); + assert_apis_exist(tappInfo.app); + }); }); }) diff --git a/src/Typo/app.ts b/src/Typo/app.ts index 17ae1fb..6edbd4d 100644 --- a/src/Typo/app.ts +++ b/src/Typo/app.ts @@ -607,6 +607,8 @@ export namespace FibApp { } export interface Hooks { beforeSetupRoute?: FxOrmHook.HookActionCallback + afterOrmSyncFinished?: FxOrmHook.HookActionCallback + // afterOrmSyncFinished?: FxOrmHook.HookActionWithCtxCallback } export interface GetTestRoutingOptions { diff --git a/src/db.ts b/src/db.ts index 869d88b..2e57ebd 100644 --- a/src/db.ts +++ b/src/db.ts @@ -5,6 +5,7 @@ import coroutine = require('coroutine'); import Pool = require('fib-pool'); import graphql = require('./http/graphql'); +import Hook = require('./http/hook'); import orm_utils = require('./utils/orm') import orm_plugins = require('./orm_plugins') @@ -50,6 +51,15 @@ export = (app: FibApp.FibAppClass, connStr: string, opts: FibApp.FibAppDbSetupOp sync_info.lock.release(); } + Hook.wait( + app as any, + app.__opts.hooks.afterOrmSyncFinished as any, + function (err: orm.FxOrmError.ExtendedError) { + if (err) throw err; + }, + // { app, orm: ormInstance }, + ); + // only init graphql when models is not empty if (ormInstance.models && Object.keys(ormInstance.models).length) graphql(app, ormInstance); diff --git a/typings/Typo/app.d.ts b/typings/Typo/app.d.ts index de4e49b..2170a2d 100644 --- a/typings/Typo/app.d.ts +++ b/typings/Typo/app.d.ts @@ -475,6 +475,7 @@ export declare namespace FibApp { } export interface Hooks { beforeSetupRoute?: FxOrmHook.HookActionCallback; + afterOrmSyncFinished?: FxOrmHook.HookActionCallback; } export interface GetTestRoutingOptions { initRouting?: {