feat: improve database.sync()
This commit is contained in:
parent
c5f089d7b7
commit
1242ba5aed
@ -2,6 +2,7 @@ import { getDatabase } from './';
|
|||||||
import Database from '../database';
|
import Database from '../database';
|
||||||
import Table from '../table';
|
import Table from '../table';
|
||||||
import Model from '../model';
|
import Model from '../model';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
let db: Database;
|
let db: Database;
|
||||||
|
|
||||||
@ -243,6 +244,24 @@ describe('tables', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe.only('#import()', () => {
|
||||||
|
it('import tables', () => {
|
||||||
|
const tables = db.import({
|
||||||
|
directory: path.resolve(__dirname, './tables'),
|
||||||
|
});
|
||||||
|
expect([...tables.keys()]).toEqual(['demos', 'examples', 'tests']);
|
||||||
|
expect(db.getTable('demos').getOptions()).toEqual({
|
||||||
|
name: 'demos', actions: [ { name: 'create' }, { name: 'list' } ]
|
||||||
|
});
|
||||||
|
expect(db.getTable('examples').getOptions()).toEqual({
|
||||||
|
name: 'examples', actions: [ { name: 'create' } ]
|
||||||
|
});
|
||||||
|
expect(db.getTable('tests').getOptions()).toEqual({
|
||||||
|
name: 'tests', actions: [ { name: 'list' } ]
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('associations', () => {
|
describe('associations', () => {
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
});
|
});
|
||||||
|
8
packages/database/src/__tests__/tables/demos.ts
Normal file
8
packages/database/src/__tests__/tables/demos.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
export default {
|
||||||
|
name: 'demos',
|
||||||
|
actions: [
|
||||||
|
{
|
||||||
|
name: 'create',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
8
packages/database/src/__tests__/tables/examples.json
Normal file
8
packages/database/src/__tests__/tables/examples.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"name": "examples",
|
||||||
|
"actions": [
|
||||||
|
{
|
||||||
|
"name": "create"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
12
packages/database/src/__tests__/tables/extends.ts
Normal file
12
packages/database/src/__tests__/tables/extends.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { extend } from "../../database";
|
||||||
|
|
||||||
|
export default extend({
|
||||||
|
name: 'demos',
|
||||||
|
actions: [
|
||||||
|
{
|
||||||
|
name: 'list',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}, {
|
||||||
|
arrayMerge: (t, s) => t.concat(s),
|
||||||
|
});
|
8
packages/database/src/__tests__/tables/tests.js
Normal file
8
packages/database/src/__tests__/tables/tests.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
module.exports = {
|
||||||
|
name: 'tests',
|
||||||
|
actions: [
|
||||||
|
{
|
||||||
|
name: 'create',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
8
packages/database/src/__tests__/tables/tests2.ts
Normal file
8
packages/database/src/__tests__/tables/tests2.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
export default {
|
||||||
|
name: 'tests',
|
||||||
|
actions: [
|
||||||
|
{
|
||||||
|
name: 'list',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
@ -35,6 +35,19 @@ export interface DatabaseOptions extends Options {
|
|||||||
|
|
||||||
export type HookType = 'beforeTableInit' | 'afterTableInit' | 'beforeAddField' | 'afterAddField';
|
export type HookType = 'beforeTableInit' | 'afterTableInit' | 'beforeAddField' | 'afterAddField';
|
||||||
|
|
||||||
|
export class Extend {
|
||||||
|
tableOptions: TableOptions;
|
||||||
|
mergeOptions: MergeOptions
|
||||||
|
constructor(tableOptions: TableOptions, mergeOptions?: MergeOptions) {
|
||||||
|
this.tableOptions = tableOptions;
|
||||||
|
this.mergeOptions = mergeOptions;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function extend(tableOptions: TableOptions, mergeOptions: MergeOptions = {}) {
|
||||||
|
return new Extend(tableOptions, mergeOptions);
|
||||||
|
}
|
||||||
|
|
||||||
export default class Database {
|
export default class Database {
|
||||||
|
|
||||||
public readonly sequelize: Sequelize;
|
public readonly sequelize: Sequelize;
|
||||||
@ -80,7 +93,13 @@ export default class Database {
|
|||||||
const tables = new Map<string, Table>();
|
const tables = new Map<string, Table>();
|
||||||
files.forEach((file: string) => {
|
files.forEach((file: string) => {
|
||||||
const result = requireModule(file);
|
const result = requireModule(file);
|
||||||
this.extend(typeof result === 'function' ? result(this) : result);
|
if (result instanceof Extend) {
|
||||||
|
const table = this.extend(result.tableOptions, result.mergeOptions);
|
||||||
|
tables.set(table.getName(), table);
|
||||||
|
} else {
|
||||||
|
const table = this.extend(typeof result === 'function' ? result(this) : result);
|
||||||
|
tables.set(table.getName(), table);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return tables;
|
return tables;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user