initial menu design, simpler patch serialization
This commit is contained in:
		| @@ -1,22 +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 PatchActions { | ||||
|   rename_column = 'rc', | ||||
|   add_column = 'ac', | ||||
|   drop_column = 'dc', | ||||
|   add_relation = 'ar', | ||||
|   drop_relation = 'dr', | ||||
|   set_column_type = 'tc', | ||||
|   add_table = 'at', | ||||
|   drop_table = 'dt', | ||||
|   rename_table = 'rt', | ||||
|   insert_data = 'id', | ||||
|   update_data = 'ud', | ||||
|   mutate_data = 'md', | ||||
|   delete_data = 'dd' | ||||
| } | ||||
| @@ -1,10 +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 interface Serializable { | ||||
|   serialize(): string; | ||||
| } | ||||
| @@ -6,13 +6,15 @@ | ||||
|  */ | ||||
|  | ||||
| import { Snippet } from '../../Snippet'; | ||||
| import { MainMenu } from './menu/MainMenu'; | ||||
|  | ||||
| export default class Database implements Snippet { | ||||
|   public is_active (): boolean { | ||||
|     return false; | ||||
|   } | ||||
|  | ||||
|   public start (): Promise<void> { | ||||
|     return Promise.resolve (); | ||||
|   public async start (): Promise<void> { | ||||
|     await (new MainMenu) | ||||
|       .run (); | ||||
|   } | ||||
| } | ||||
|   | ||||
							
								
								
									
										19
									
								
								lib/snippets/database/menu/MainMenu.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								lib/snippets/database/menu/MainMenu.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | ||||
| /* | ||||
|  * 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'); | ||||
|     }); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										35
									
								
								lib/snippets/database/menu/Menu.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								lib/snippets/database/menu/Menu.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,35 @@ | ||||
| /* | ||||
|  * 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; | ||||
|   } | ||||
| } | ||||
							
								
								
									
										20
									
								
								lib/snippets/database/patch_actions/PatchActions.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								lib/snippets/database/patch_actions/PatchActions.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| /* | ||||
|  * 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 | ||||
|  */ | ||||
|  | ||||
| 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'; | ||||
| @@ -5,40 +5,39 @@ | ||||
|  * Created by Timo Hocker <timo@scode.ovh>, May 2020 | ||||
|  */ | ||||
|  | ||||
| import { Serializable } from '../classes/Serializable'; | ||||
| import { Persistent } from '@sapphirecode/modelling'; | ||||
| import { PatchAction } from '../classes/PatchAction'; | ||||
| import { Database } from '../classes/Database'; | ||||
|  | ||||
| export class RenameColumn implements Serializable, PatchAction { | ||||
|   public table: string; | ||||
|   public column: string; | ||||
|   public new_name: string; | ||||
| export class RenameColumn extends Persistent implements PatchAction { | ||||
|   public get table (): string { | ||||
|     return this.get ('table'); | ||||
|   } | ||||
|  | ||||
|   public get column (): string { | ||||
|     return this.get ('column'); | ||||
|   } | ||||
|  | ||||
|   public get new_name (): string { | ||||
|     return this.get ('new_name'); | ||||
|   } | ||||
|  | ||||
|   public constructor ( | ||||
|     column: string, | ||||
|     new_name: string|null = null, | ||||
|     table: string|null = null | ||||
|   ) { | ||||
|     if (new_name === null || table === null) { | ||||
|       const regex | ||||
|       = /(?<table>[a-z_]+) (?<column>[a-z_]+) (?<new_name>[a-z_]+)/iu; | ||||
|       const res = regex.exec (column); | ||||
|       if (res === null || typeof res.groups === 'undefined') | ||||
|         throw new Error ('invalid string to deserialize'); | ||||
|     this.properties = { column: 'string', table: 'string', new_name: 'string' }; | ||||
|  | ||||
|       this.column = res.groups.column; | ||||
|       this.new_name = res.groups.new_name; | ||||
|       this.table = res.groups.table; | ||||
|     if (new_name === null || table === null) { | ||||
|       const parsed = JSON.parse (column); | ||||
|       this.assign_object (parsed); | ||||
|       return; | ||||
|     } | ||||
|  | ||||
|     this.column = column; | ||||
|     this.table = table; | ||||
|     this.new_name = new_name; | ||||
|   } | ||||
|  | ||||
|   public serialize (): string { | ||||
|     return `${this.table} ${this.column} ${this.new_name}`; | ||||
|     this.set ('column', column); | ||||
|     this.set ('new_name', new_name); | ||||
|     this.set ('table', table); | ||||
|   } | ||||
|  | ||||
|   public apply (db: Database): void { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user