mirror of
https://github.com/nocobase/nocobase
synced 2024-11-16 03:35:10 +00:00
cec5733260
* fix: env DB_TABLE_PREFIX doesn't get applied * fix: remove before define * fix: test error * fix: table prefix * test: ci * fix: test error * test: ci * test: ci * fix: test error * fix: test error * chore: update node ci
41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import { merge, uid } from '@nocobase/utils';
|
|
import { resolve } from 'path';
|
|
import { Database, IDatabaseOptions } from './database';
|
|
|
|
export class MockDatabase extends Database {
|
|
constructor(options: IDatabaseOptions) {
|
|
super({
|
|
storage: ':memory:',
|
|
tablePrefix: `mock_${uid(6)}_`,
|
|
dialect: 'sqlite',
|
|
...options,
|
|
});
|
|
}
|
|
}
|
|
|
|
export function getConfigByEnv() {
|
|
return {
|
|
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,
|
|
dialect: process.env.DB_DIALECT,
|
|
logging: process.env.DB_LOGGING === 'on' ? console.log : false,
|
|
storage:
|
|
process.env.DB_STORAGE && process.env.DB_STORAGE !== ':memory:'
|
|
? resolve(process.cwd(), process.env.DB_STORAGE)
|
|
: ':memory:',
|
|
define: {
|
|
charset: 'utf8mb4',
|
|
collate: 'utf8mb4_unicode_ci',
|
|
},
|
|
timezone: process.env.DB_TIMEZONE,
|
|
};
|
|
}
|
|
|
|
export function mockDatabase(options: IDatabaseOptions = {}): MockDatabase {
|
|
const dbOptions = merge(getConfigByEnv(), options);
|
|
return new MockDatabase(dbOptions);
|
|
}
|