fix(database): mock db

This commit is contained in:
chenos 2022-02-13 13:41:17 +08:00
parent 01e5e1cbac
commit db3285d452
2 changed files with 69 additions and 10 deletions

View File

@ -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 () => {

View File

@ -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 {