mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 13:39:24 +08:00
* feat: create nocobase app with simple & quickstart option * chore: delete template file * create-nocobase-app: add env API_PORT fallback * chore: log * env default fallback * move config dir * change has yarn * chore: prettier * fix: npm running issue * database testing support sqlite * once... * chore: typo * fix: sqlite test * update readme * feat: copy .env.example to .env at create-nocobase-app * create-nocobase-app: change sqlite3 to github master * create-nocobase-app: .env template * create-nocobase-app: update .env * chore: typo * update README * chore: Application constructor * feat: sqlite demo data support * fix test * fix: application error * chore: plugin-client run sql * fix: application createCli * fix: can choose whether to register actions * chore: model compile error * fix: support sqlite * fix: demo data set index sequence on postgresql * chore: code reduce * fix: operators are compatible with sqlite * add impor demo option to init command * update env Co-authored-by: chenos <chenlinxh@gmail.com>
54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
import Database from '../database';
|
|
import { Dialect } from 'sequelize';
|
|
import path from 'path';
|
|
import { uid } from '../utils';
|
|
|
|
function getTestKey() {
|
|
const { id } = require.main;
|
|
const key = id
|
|
// .replace(process.env.PWD, '')
|
|
.replace(`${process.env.PWD}/packages`, '')
|
|
.replace(/src\/__tests__/g, '')
|
|
.replace('.test.ts', '')
|
|
.replace(/[^\w]/g, '_');
|
|
return key;
|
|
}
|
|
|
|
let config: any = {
|
|
username: process.env.DB_USER,
|
|
password: process.env.DB_PASSWORD,
|
|
database: process.env.DB_DATABASE,
|
|
host: process.env.DB_HOST,
|
|
port: Number.parseInt(process.env.DB_PORT, 10),
|
|
dialect: process.env.DB_DIALECT as Dialect,
|
|
logging: process.env.DB_LOG_SQL === 'on' ? console.log : false,
|
|
};
|
|
|
|
if (process.env.DB_DIALECT === 'sqlite') {
|
|
config = {
|
|
dialect: process.env.DB_DIALECT as Dialect,
|
|
storage: path.resolve(__dirname, `./.db/${uid()}.sqlite`),
|
|
logging: process.env.DB_LOG_SQL === 'on' ? console.log : false,
|
|
};
|
|
}
|
|
|
|
export function getDatabase() {
|
|
return new Database({
|
|
...config,
|
|
sync: {
|
|
force: true,
|
|
},
|
|
hooks: {
|
|
beforeDefine(model, options) {
|
|
// @ts-ignore
|
|
options.tableNamePrefix = `${getTestKey()}_`;
|
|
// @ts-ignore
|
|
options.hookModelName = options.tableName;
|
|
options.tableName = `${getTestKey()}_${
|
|
options.tableName || options.name.plural
|
|
}`;
|
|
},
|
|
},
|
|
});
|
|
}
|