tachybase_todo/packages/core/database/src/mock-database.ts
chenos cec5733260
fix: DB_TABLE_PREFIX doesn't get applied (#710)
* 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
2022-08-04 21:27:55 +08:00

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);
}