diff --git a/lib/snippets/database/classes/PatchAction.ts b/lib/snippets/database/classes/PatchAction.ts index 185ebf2..2f4799b 100644 --- a/lib/snippets/database/classes/PatchAction.ts +++ b/lib/snippets/database/classes/PatchAction.ts @@ -9,4 +9,5 @@ import { Database } from './Database'; export interface PatchAction { apply(db: Database): void; + assign_object(obj: Record):void; } diff --git a/lib/snippets/database/index.ts b/lib/snippets/database/index.ts index 53741a3..90a666d 100644 --- a/lib/snippets/database/index.ts +++ b/lib/snippets/database/index.ts @@ -7,6 +7,7 @@ import { Snippet } from '../../Snippet'; import { MainMenu } from './menu/MainMenu'; +import { Database as DatabaseClass } from './classes/Database'; export default class Database implements Snippet { public is_active (): boolean { @@ -14,7 +15,7 @@ export default class Database implements Snippet { } public async start (): Promise { - await (new MainMenu) + await (new MainMenu (new DatabaseClass)) .run (); } } diff --git a/lib/snippets/database/menu/MainMenu.ts b/lib/snippets/database/menu/MainMenu.ts index 24f67c0..be26786 100644 --- a/lib/snippets/database/menu/MainMenu.ts +++ b/lib/snippets/database/menu/MainMenu.ts @@ -11,7 +11,9 @@ import { Menu } from './Menu'; export class MainMenu extends Menu { public constructor (db: Database) { super ('Snippeteer Database'); - this.register_option ('quit', () => { /* noop */ }); + this.register_option ('quit', () => { + // noop + }); this.register_option ('create table', () => { console.log ('table'); }); diff --git a/lib/snippets/database/patch_actions/ActionFactory.ts b/lib/snippets/database/patch_actions/ActionFactory.ts new file mode 100644 index 0000000..704afb9 --- /dev/null +++ b/lib/snippets/database/patch_actions/ActionFactory.ts @@ -0,0 +1,16 @@ +import { PatchAction } from '../classes/PatchAction'; + +export class ActionFactory { + private static _actions: RecordPatchAction> = {}; + + public static get (name:string, json:string): PatchAction { + return this._actions[name] (json); + } + + public static register ( + name:string, + constr: (json:string)=>PatchAction + ):void { + this._actions[name] = constr; + } +} diff --git a/lib/snippets/database/patch_actions/AddTable.ts b/lib/snippets/database/patch_actions/AddTable.ts new file mode 100644 index 0000000..cf629f3 --- /dev/null +++ b/lib/snippets/database/patch_actions/AddTable.ts @@ -0,0 +1,29 @@ +import { Persistent } from '@sapphirecode/modelling'; +import { PatchAction } from '../classes/PatchAction'; +import { Database } from '../classes/Database'; +import { Table } from '../classes/Table'; +import { Column } from '../classes/Column'; +import { ColumnType } from '../classes/ColumnType'; + +export class AddTable extends Persistent implements PatchAction { + public get name ():string { + return this.get ('name') as string; + } + + public constructor (name:string) { + super (); + this.properties.name = 'string'; + + this.set ('name', name); + } + + public apply (db: Database):void { + if (typeof db.get_table (this.name) !== 'undefined') + throw new Error ('table already exists'); + + const table = new Table (this.name); + table.columns.push (new Column ('id', ColumnType.increments)); + + db.tables.push (table); + } +} diff --git a/lib/snippets/database/patch_actions/PatchActions.ts b/lib/snippets/database/patch_actions/PatchActions.ts index 248519b..2c5af90 100644 --- a/lib/snippets/database/patch_actions/PatchActions.ts +++ b/lib/snippets/database/patch_actions/PatchActions.ts @@ -5,16 +5,46 @@ * Created by Timo Hocker , June 2020 */ -export { RenameColumn } from './RenameColumn'; -export { AddColumn } from './AddColumn'; -export { DropColumn } from './DropColumn'; -export { AddRelation } from './AddRelation'; -export { DropRelation } from './DropRelation'; -export { SetColumnType } from './SetColumnType'; -export { AddTable } from './AddTable'; -export { DropTable } from './DropTable'; -export { RenameTable } from './RenameTable'; -export { InsertData } from './InsertData'; -export { UpdateData } from './UpdateData'; -export { MutateDate } from './MutateDate'; -export { DeleteData } from './DeleteData'; +import { PatchAction } from '../classes/PatchAction'; +import { ActionFactory } from './ActionFactory'; +import { RenameColumn } from './RenameColumn'; +import { AddTable } from './AddTable'; + + +/* + * export { AddColumn } from './AddColumn'; + * export { DropColumn } from './DropColumn'; + * export { AddRelation } from './AddRelation'; + * export { DropRelation } from './DropRelation'; + * export { SetColumnType } from './SetColumnType'; + * export { DropTable } from './DropTable'; + * export { RenameTable } from './RenameTable'; + * export { InsertData } from './InsertData'; + * export { UpdateData } from './UpdateData'; + * export { MutateDate } from './MutateDate'; + * export { DeleteData } from './DeleteData'; + */ + +function assign_json (patch:PatchAction, json:string):PatchAction { + const obj = JSON.parse (json); + patch.assign_object (obj); + return patch; +} + +function init ():void { + ActionFactory.register ( + 'rename_column', + (json:string) => assign_json (new RenameColumn ('', '', ''), json) + ); + + ActionFactory.register ( + 'add_table', + (json:string) => assign_json (new AddTable (''), json) + ); +} + +export { + init, + RenameColumn, + AddTable +}; diff --git a/lib/snippets/database/patch_actions/RenameColumn.ts b/lib/snippets/database/patch_actions/RenameColumn.ts index db797f8..a1cf8e8 100644 --- a/lib/snippets/database/patch_actions/RenameColumn.ts +++ b/lib/snippets/database/patch_actions/RenameColumn.ts @@ -11,29 +11,26 @@ import { Database } from '../classes/Database'; export class RenameColumn extends Persistent implements PatchAction { public get table (): string { - return this.get ('table'); + return this.get ('table') as string; } public get column (): string { - return this.get ('column'); + return this.get ('column') as string; } public get new_name (): string { - return this.get ('new_name'); + return this.get ('new_name') as string; } public constructor ( column: string, - new_name: string|null = null, - table: string|null = null + new_name: string, + table: string ) { - this.properties = { column: 'string', table: 'string', new_name: 'string' }; - - if (new_name === null || table === null) { - const parsed = JSON.parse (column); - this.assign_object (parsed); - return; - } + super (); + this.properties.column = 'string'; + this.properties.table = 'string'; + this.properties.new_name = 'string'; this.set ('column', column); this.set ('new_name', new_name);