From db3285d4524e3459f4aecb543e10f733a78f817b Mon Sep 17 00:00:00 2001 From: chenos Date: Sun, 13 Feb 2022 13:41:17 +0800 Subject: [PATCH] fix(database): mock db --- .../database/src/__tests__/collection.test.ts | 3 +- packages/database/src/__tests__/index.ts | 76 ++++++++++++++++--- 2 files changed, 69 insertions(+), 10 deletions(-) diff --git a/packages/database/src/__tests__/collection.test.ts b/packages/database/src/__tests__/collection.test.ts index df1f2c399..52817ded7 100644 --- a/packages/database/src/__tests__/collection.test.ts +++ b/packages/database/src/__tests__/collection.test.ts @@ -1,6 +1,6 @@ -import { generatePrefixByPath, mockDatabase } from './index'; import { Collection } from '../collection'; import { Database } from '../database'; +import { generatePrefixByPath, mockDatabase } from './index'; test('collection disable authGenId', async () => { const db = mockDatabase(); @@ -15,6 +15,7 @@ test('collection disable authGenId', async () => { await db.sync(); expect(model.rawAttributes['id']).toBeUndefined(); + await db.close(); }); test('new collection', async () => { diff --git a/packages/database/src/__tests__/index.ts b/packages/database/src/__tests__/index.ts index a702c8949..aa23280f1 100644 --- a/packages/database/src/__tests__/index.ts +++ b/packages/database/src/__tests__/index.ts @@ -1,4 +1,6 @@ +import { uid } from '@nocobase/utils'; import merge from 'deepmerge'; +import { Sequelize } from 'sequelize'; import { Database, DatabaseOptions } from '../database'; export function generatePrefixByPath() { @@ -12,12 +14,15 @@ export function generatePrefixByPath() { return key; } -export function getConfig(config = {}, options?: any): DatabaseOptions { - return merge( - { - dialect: 'sqlite', - storage: options?.storage || ':memory:', - logging: options?.logging || false, +export function getConfig(config: any = {}, options?: any): DatabaseOptions { + if (process.env.DB_DIALECT === 'sqlite') { + const defaults = { + dialect: process.env.DB_DIALECT as any, + storage: ':memory:', + logging: process.env.DB_LOG_SQL === 'on' ? console.log : false, + // sync: { + // force: true, + // }, hooks: { beforeDefine(model, options) { options.tableName = `${generatePrefixByPath()}_${ @@ -25,10 +30,63 @@ export function getConfig(config = {}, options?: any): DatabaseOptions { }`; }, }, + }; + return merge(defaults, config, options); + } + const database = `mock_${uid()}`; + let dbExists = false; + const defaults = { + username: process.env.DB_USER, + password: process.env.DB_PASSWORD, + database: process.env.DB_DATABASE, + host: process.env.DB_HOST, + port: process.env.DB_PORT as any, + dialect: process.env.DB_DIALECT as any, + logging: process.env.DB_LOG_SQL === 'on' ? console.log : false, + dialectOptions: { + charset: 'utf8mb4', + collate: 'utf8mb4_unicode_ci', }, - config || {}, - options, - ) as any; + hooks: { + beforeDefine(model, options) { + options.tableName = `${generatePrefixByPath()}_${ + options.tableName || options.modelName || options.name.plural + }`; + }, + async beforeSync({ sequelize }: any) { + if (config.database) { + return; + } + if (dbExists) { + return; + } + return; + const db = new Sequelize({ + username: process.env.DB_USER, + password: process.env.DB_PASSWORD, + database: process.env.DB_DATABASE, + host: process.env.DB_HOST, + port: process.env.DB_PORT as any, + dialect: process.env.DB_DIALECT as any, + logging: process.env.DB_LOG_SQL === 'on' ? console.log : false, + dialectOptions: { + charset: 'utf8mb4', + collate: 'utf8mb4_unicode_ci', + }, + }); + await db.query(`CREATE DATABASE "${database}";`); + await db.close(); + sequelize.options.database = database; + sequelize.config.database = database; + const ConnectionManager = sequelize.dialect.connectionManager.constructor; + const connectionManager = new ConnectionManager(sequelize.dialect, sequelize); + sequelize.dialect.connectionManager = connectionManager; + sequelize.connectionManager = connectionManager; + dbExists = true; + }, + }, + }; + return merge(defaults, config, options); } export function mockDatabase(options?: DatabaseOptions): Database {