remove database snippet
This commit is contained in:
		| @@ -1,20 +0,0 @@ | ||||
| /* | ||||
|  * Copyright (C) SapphireCode - All Rights Reserved | ||||
|  * This file is part of Snippeteer which is released under BSD-3-Clause. | ||||
|  * See file 'LICENSE' for full license details. | ||||
|  * Created by Timo Hocker <timo@scode.ovh>, May 2020 | ||||
|  */ | ||||
|  | ||||
| import { ColumnType } from './ColumnType'; | ||||
| import { Relation } from './Relation'; | ||||
|  | ||||
| export class Column { | ||||
|   public name: string; | ||||
|   public type: ColumnType; | ||||
|   public relation?: Relation; | ||||
|  | ||||
|   public constructor (name: string, type: ColumnType) { | ||||
|     this.name = name; | ||||
|     this.type = type; | ||||
|   } | ||||
| } | ||||
| @@ -1,27 +0,0 @@ | ||||
| /* | ||||
|  * Copyright (C) SapphireCode - All Rights Reserved | ||||
|  * This file is part of Snippeteer which is released under BSD-3-Clause. | ||||
|  * See file 'LICENSE' for full license details. | ||||
|  * Created by Timo Hocker <timo@scode.ovh>, May 2020 | ||||
|  */ | ||||
|  | ||||
| export enum ColumnType { | ||||
|   string = 'string', | ||||
|   text = 'text', | ||||
|   integer = 'integer', | ||||
|   big_integer = 'bigInteger', | ||||
|   float = 'float', | ||||
|   decimal = 'decimal', | ||||
|   increments = 'increments', | ||||
|   big_increments = 'bigIncrements', | ||||
|   boolean = 'boolean', | ||||
|   date = 'date', | ||||
|   date_time ='datetime', | ||||
|   time = 'time', | ||||
|   timestamp = 'timestamp', | ||||
|   binary = 'binary', | ||||
|   enum = 'enu', | ||||
|   json='json', | ||||
|   jsonb='jsonb', | ||||
|   uuid='uuid' | ||||
| } | ||||
| @@ -1,21 +0,0 @@ | ||||
| /* | ||||
|  * Copyright (C) SapphireCode - All Rights Reserved | ||||
|  * This file is part of Snippeteer which is released under BSD-3-Clause. | ||||
|  * See file 'LICENSE' for full license details. | ||||
|  * Created by Timo Hocker <timo@scode.ovh>, May 2020 | ||||
|  */ | ||||
|  | ||||
| import { Table } from './Table'; | ||||
|  | ||||
| export class Database { | ||||
|   public tables: Array<Table> = []; | ||||
|  | ||||
|   public get_table (name: string): Table|null { | ||||
|     for (const table of this.tables) { | ||||
|       if (table.name === name) | ||||
|         return table; | ||||
|     } | ||||
|  | ||||
|     return null; | ||||
|   } | ||||
| } | ||||
| @@ -1,13 +0,0 @@ | ||||
| /* | ||||
|  * Copyright (C) SapphireCode - All Rights Reserved | ||||
|  * This file is part of Snippeteer which is released under BSD-3-Clause. | ||||
|  * See file 'LICENSE' for full license details. | ||||
|  * Created by Timo Hocker <timo@scode.ovh>, May 2020 | ||||
|  */ | ||||
|  | ||||
| import { Database } from './Database'; | ||||
|  | ||||
| export interface PatchAction { | ||||
|   apply(db: Database): void; | ||||
|   assign_object(obj: Record<string, unknown>):void; | ||||
| } | ||||
| @@ -1,16 +0,0 @@ | ||||
| /* | ||||
|  * Copyright (C) SapphireCode - All Rights Reserved | ||||
|  * This file is part of Snippeteer which is released under BSD-3-Clause. | ||||
|  * See file 'LICENSE' for full license details. | ||||
|  * Created by Timo Hocker <timo@scode.ovh>, May 2020 | ||||
|  */ | ||||
|  | ||||
| export class Relation { | ||||
|   public column: string; | ||||
|   public table: string; | ||||
|  | ||||
|   public constructor (table: string, column: string) { | ||||
|     this.column = column; | ||||
|     this.table = table; | ||||
|   } | ||||
| } | ||||
| @@ -1,26 +0,0 @@ | ||||
| /* | ||||
|  * Copyright (C) SapphireCode - All Rights Reserved | ||||
|  * This file is part of Snippeteer which is released under BSD-3-Clause. | ||||
|  * See file 'LICENSE' for full license details. | ||||
|  * Created by Timo Hocker <timo@scode.ovh>, May 2020 | ||||
|  */ | ||||
|  | ||||
| import { Column } from './Column'; | ||||
|  | ||||
| export class Table { | ||||
|   public name: string; | ||||
|   public columns: Array<Column> = []; | ||||
|  | ||||
|   public constructor (name: string) { | ||||
|     this.name = name; | ||||
|   } | ||||
|  | ||||
|   public get_column (name: string): Column|null { | ||||
|     for (const col of this.columns) { | ||||
|       if (col.name === name) | ||||
|         return col; | ||||
|     } | ||||
|  | ||||
|     return null; | ||||
|   } | ||||
| } | ||||
| @@ -1,21 +0,0 @@ | ||||
| /* | ||||
|  * Copyright (C) SapphireCode - All Rights Reserved | ||||
|  * This file is part of Snippeteer which is released under BSD-3-Clause. | ||||
|  * See file 'LICENSE' for full license details. | ||||
|  * Created by Timo Hocker <timo@scode.ovh>, May 2020 | ||||
|  */ | ||||
|  | ||||
| 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 { | ||||
|     return false; | ||||
|   } | ||||
|  | ||||
|   public async start (): Promise<void> { | ||||
|     await (new MainMenu (new DatabaseClass)) | ||||
|       .run (); | ||||
|   } | ||||
| } | ||||
| @@ -1,21 +0,0 @@ | ||||
| /* | ||||
|  * Copyright (C) SapphireCode - All Rights Reserved | ||||
|  * This file is part of Snippeteer which is released under BSD-3-Clause. | ||||
|  * See file 'LICENSE' for full license details. | ||||
|  * Created by Timo Hocker <timo@scode.ovh>, June 2020 | ||||
|  */ | ||||
|  | ||||
| import { Database } from '../classes/Database'; | ||||
| import { Menu } from './Menu'; | ||||
|  | ||||
| export class MainMenu extends Menu { | ||||
|   public constructor (db: Database) { | ||||
|     super ('Snippeteer Database'); | ||||
|     this.register_option ('quit', () => { | ||||
|       // noop | ||||
|     }); | ||||
|     this.register_option ('create table', () => { | ||||
|       console.log ('table'); | ||||
|     }); | ||||
|   } | ||||
| } | ||||
| @@ -1,35 +0,0 @@ | ||||
| /* | ||||
|  * Copyright (C) SapphireCode - All Rights Reserved | ||||
|  * This file is part of Snippeteer which is released under BSD-3-Clause. | ||||
|  * See file 'LICENSE' for full license details. | ||||
|  * Created by Timo Hocker <timo@scode.ovh>, June 2020 | ||||
|  */ | ||||
|  | ||||
| import { StringOption } from '@sapphirecode/console-app'; | ||||
|  | ||||
| export class Menu { | ||||
|   private _options: Record<string, ()=>void|Promise<void>> = {}; | ||||
|   private _title: string; | ||||
|  | ||||
|   public constructor (title: string) { | ||||
|     this._title = title; | ||||
|   } | ||||
|  | ||||
|   public async run (): Promise<void> { | ||||
|     const options = Object.keys (this._options); | ||||
|     const selected = await (new StringOption ({ | ||||
|       name:              'menu_select', | ||||
|       message:           this._title, | ||||
|       sources:           { console: false }, | ||||
|       exit_on_interrupt: true, | ||||
|       preset:            options | ||||
|     })) | ||||
|       .parse (); | ||||
|     if (typeof this._options[selected] !== 'undefined') | ||||
|       await new Promise (this._options[selected]); | ||||
|   } | ||||
|  | ||||
|   public register_option (name:string, callback:()=>void|Promise<void>): void { | ||||
|     this._options[name] = callback; | ||||
|   } | ||||
| } | ||||
| @@ -1,23 +0,0 @@ | ||||
| /* | ||||
|  * Copyright (C) SapphireCode - All Rights Reserved | ||||
|  * This file is part of Snippeteer which is released under BSD-3-Clause. | ||||
|  * See file 'LICENSE' for full license details. | ||||
|  * Created by Timo Hocker <timo@scode.ovh>, June 2020 | ||||
|  */ | ||||
|  | ||||
| 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; | ||||
|   } | ||||
| } | ||||
| @@ -1,36 +0,0 @@ | ||||
| /* | ||||
|  * Copyright (C) SapphireCode - All Rights Reserved | ||||
|  * This file is part of Snippeteer which is released under BSD-3-Clause. | ||||
|  * See file 'LICENSE' for full license details. | ||||
|  * Created by Timo Hocker <timo@scode.ovh>, June 2020 | ||||
|  */ | ||||
|  | ||||
| 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); | ||||
|   } | ||||
| } | ||||
| @@ -1,50 +0,0 @@ | ||||
| /* | ||||
|  * Copyright (C) SapphireCode - All Rights Reserved | ||||
|  * This file is part of Snippeteer which is released under BSD-3-Clause. | ||||
|  * See file 'LICENSE' for full license details. | ||||
|  * Created by Timo Hocker <timo@scode.ovh>, June 2020 | ||||
|  */ | ||||
|  | ||||
| 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 | ||||
| }; | ||||
| @@ -1,56 +0,0 @@ | ||||
| /* | ||||
|  * Copyright (C) SapphireCode - All Rights Reserved | ||||
|  * This file is part of Snippeteer which is released under BSD-3-Clause. | ||||
|  * See file 'LICENSE' for full license details. | ||||
|  * Created by Timo Hocker <timo@scode.ovh>, May 2020 | ||||
|  */ | ||||
|  | ||||
| import { Persistent } from '@sapphirecode/modelling'; | ||||
| import { PatchAction } from '../classes/PatchAction'; | ||||
| import { Database } from '../classes/Database'; | ||||
|  | ||||
| export class RenameColumn extends Persistent implements PatchAction { | ||||
|   public get table (): string { | ||||
|     return this.get ('table') as string; | ||||
|   } | ||||
|  | ||||
|   public get column (): string { | ||||
|     return this.get ('column') as string; | ||||
|   } | ||||
|  | ||||
|   public get new_name (): string { | ||||
|     return this.get ('new_name') as string; | ||||
|   } | ||||
|  | ||||
|   public constructor ( | ||||
|     column: string, | ||||
|     new_name: string, | ||||
|     table: string | ||||
|   ) { | ||||
|     super (); | ||||
|     this.properties.column = 'string'; | ||||
|     this.properties.table = 'string'; | ||||
|     this.properties.new_name = 'string'; | ||||
|  | ||||
|     this.set ('column', column); | ||||
|     this.set ('new_name', new_name); | ||||
|     this.set ('table', table); | ||||
|   } | ||||
|  | ||||
|   public apply (db: Database): void { | ||||
|     const table = db.get_table (this.table); | ||||
|     if (typeof table === 'undefined' || table === null) { | ||||
|       throw new Error ( | ||||
|         `table ${this.table} not found` | ||||
|       ); | ||||
|     } | ||||
|     const column = table.get_column (this.column); | ||||
|     if (typeof column === 'undefined' || column === null) { | ||||
|       throw new Error ( | ||||
|         `column ${this.column} not found in table ${this.table}` | ||||
|       ); | ||||
|     } | ||||
|  | ||||
|     column.name = this.new_name; | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user