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 { export interface PatchAction {
apply(db: Database): void; apply(db: Database): void;
assign_object(obj: Record<string, unknown>):void;
} }

View File

@ -7,6 +7,7 @@
import { Snippet } from '../../Snippet'; import { Snippet } from '../../Snippet';
import { MainMenu } from './menu/MainMenu'; import { MainMenu } from './menu/MainMenu';
import { Database as DatabaseClass } from './classes/Database';
export default class Database implements Snippet { export default class Database implements Snippet {
public is_active (): boolean { public is_active (): boolean {
@ -14,7 +15,7 @@ export default class Database implements Snippet {
} }
public async start (): Promise<void> { public async start (): Promise<void> {
await (new MainMenu) await (new MainMenu (new DatabaseClass))
.run (); .run ();
} }
} }

View File

@ -11,7 +11,9 @@ import { Menu } from './Menu';
export class MainMenu extends Menu { export class MainMenu extends Menu {
public constructor (db: Database) { public constructor (db: Database) {
super ('Snippeteer Database'); super ('Snippeteer Database');
this.register_option ('quit', () => { /* noop */ }); this.register_option ('quit', () => {
// noop
});
this.register_option ('create table', () => { this.register_option ('create table', () => {
console.log ('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 * Created by Timo Hocker <timo@scode.ovh>, June 2020
*/ */
export { RenameColumn } from './RenameColumn'; import { PatchAction } from '../classes/PatchAction';
export { AddColumn } from './AddColumn'; import { ActionFactory } from './ActionFactory';
export { DropColumn } from './DropColumn'; import { RenameColumn } from './RenameColumn';
export { AddRelation } from './AddRelation'; import { AddTable } from './AddTable';
export { DropRelation } from './DropRelation';
export { SetColumnType } from './SetColumnType';
export { AddTable } from './AddTable'; /*
export { DropTable } from './DropTable'; * export { AddColumn } from './AddColumn';
export { RenameTable } from './RenameTable'; * export { DropColumn } from './DropColumn';
export { InsertData } from './InsertData'; * export { AddRelation } from './AddRelation';
export { UpdateData } from './UpdateData'; * export { DropRelation } from './DropRelation';
export { MutateDate } from './MutateDate'; * export { SetColumnType } from './SetColumnType';
export { DeleteData } from './DeleteData'; * 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 { export class RenameColumn extends Persistent implements PatchAction {
public get table (): string { public get table (): string {
return this.get ('table'); return this.get ('table') as string;
} }
public get column (): string { public get column (): string {
return this.get ('column'); return this.get ('column') as string;
} }
public get new_name (): string { public get new_name (): string {
return this.get ('new_name'); return this.get ('new_name') as string;
} }
public constructor ( public constructor (
column: string, column: string,
new_name: string|null = null, new_name: string,
table: string|null = null table: string
) { ) {
this.properties = { column: 'string', table: 'string', new_name: 'string' }; super ();
this.properties.column = 'string';
if (new_name === null || table === null) { this.properties.table = 'string';
const parsed = JSON.parse (column); this.properties.new_name = 'string';
this.assign_object (parsed);
return;
}
this.set ('column', column); this.set ('column', column);
this.set ('new_name', new_name); this.set ('new_name', new_name);