tachybase_todo/packages/core/database/src/mock-database.ts
ChengLei Shao 89c1d39b6b
Fix/pg schema with inherit (#1446)
* fix: pg schema in sync runner

* fix: restore with pg schema

* chore: databse utils

* fix: test

* Update nocobase-test.yml

* fix: test

* chore: db schema test options

* feat: collection add schema table name method

* fix: update to bigint test

* fix: schema test

* chore: show constraints query path

* chore: show constraints query path

* Update nocobase-test.yml

* chore: test

* fix: test

* fix: test

* fix: test table name

* fix: inherited collection test
2023-02-16 10:53:04 +08:00

43 lines
1.3 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 || 'sqlite',
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,
underscored: process.env.DB_UNDERSCORED === 'true',
schema: process.env.DB_SCHEMA !== 'public' ? process.env.DB_SCHEMA : undefined,
};
}
export function mockDatabase(options: IDatabaseOptions = {}): MockDatabase {
const dbOptions = merge(getConfigByEnv(), options) as any;
return new MockDatabase(dbOptions);
}