* v0.6 * plugin-ui-schema: insert && getJsonSchema * plugin-ui-schema: insert schema with sort * plugin-ui-schema: node with x-index * insert adjacent method * chore: insert * typo * insert with x-uid * fix: getSchema by subtree * add ui-schema actions * fix: mysql compatibility * remove ui-schema when remove node tree * ui schema patch * ui_schemas.create * test cases * test cases * fix(database): reset changed before update * feat: insert ui schema node after created * feat: patch ui schema node after updated * fix: sqlite error * uid * cleanup * test cases * feat: ui_schema items type support * fix: insert items node * fix: get inner type * change items struct * add insert return value * add insert return value Co-authored-by: chenos <chenlinxh@gmail.com>
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import merge from 'deepmerge';
|
|
import Database, { DatabaseOptions } from '@nocobase/database';
|
|
|
|
export function generatePrefixByPath() {
|
|
const { id } = require.main;
|
|
const key = id
|
|
.replace(`${process.env.PWD}/packages`, '')
|
|
.replace(/src\/__tests__/g, '')
|
|
.replace('.test.ts', '')
|
|
.replace(/[^\w]/g, '_')
|
|
.replace(/_+/g, '_');
|
|
return key;
|
|
}
|
|
|
|
export function getConfig(config = {}, options?: any): DatabaseOptions {
|
|
return merge(
|
|
{
|
|
username: process.env.DB_USER,
|
|
password: process.env.DB_PASSWORD,
|
|
storage: process.env.DB_STORAGE,
|
|
database: process.env.DB_DATABASE,
|
|
host: process.env.DB_HOST,
|
|
port: process.env.DB_PORT,
|
|
dialect: process.env.DB_DIALECT,
|
|
logging: process.env.DB_LOG_SQL === 'on',
|
|
sync: {
|
|
force: true,
|
|
alter: {
|
|
drop: true,
|
|
},
|
|
},
|
|
hooks: {
|
|
beforeDefine(model, options) {
|
|
options.tableName = `${generatePrefixByPath()}_${options.tableName || options.name.plural}`;
|
|
},
|
|
},
|
|
},
|
|
config || {},
|
|
options,
|
|
) as any;
|
|
}
|
|
|
|
export function mockDatabase(options?: DatabaseOptions): Database {
|
|
return new Database(getConfig(options));
|
|
}
|