This commit is contained in:
Timo Hocker 2020-06-28 16:55:44 +02:00
parent 146c9b661f
commit 3e773b5327
7 changed files with 103 additions and 27 deletions

View File

@ -9,4 +9,5 @@ import { Database } from './Database';
export interface PatchAction {
apply(db: Database): void;
assign_object(obj: Record<string, unknown>):void;
}

View File

@ -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<void> {
await (new MainMenu)
await (new MainMenu (new DatabaseClass))
.run ();
}
}

View File

@ -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');
});

View File

@ -0,0 +1,16 @@
import { PatchAction } from '../classes/PatchAction';
export class ActionFactory {
private static _actions: Record<string, (json:string)=>PatchAction> = {};
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;
}
}

View File

@ -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);
}
}

View File

@ -5,16 +5,46 @@
* Created by Timo Hocker <timo@scode.ovh>, 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
};

View File

@ -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);