diff --git a/lib/snippets/database/classes/PatchActions.ts b/lib/snippets/database/classes/PatchActions.ts deleted file mode 100644 index b9da465..0000000 --- a/lib/snippets/database/classes/PatchActions.ts +++ /dev/null @@ -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 , 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' -} diff --git a/lib/snippets/database/classes/Serializable.ts b/lib/snippets/database/classes/Serializable.ts deleted file mode 100644 index 3a25705..0000000 --- a/lib/snippets/database/classes/Serializable.ts +++ /dev/null @@ -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 , May 2020 - */ - -export interface Serializable { - serialize(): string; -} diff --git a/lib/snippets/database/index.ts b/lib/snippets/database/index.ts index 9794721..53741a3 100644 --- a/lib/snippets/database/index.ts +++ b/lib/snippets/database/index.ts @@ -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 { - return Promise.resolve (); + public async start (): Promise { + await (new MainMenu) + .run (); } } diff --git a/lib/snippets/database/menu/MainMenu.ts b/lib/snippets/database/menu/MainMenu.ts new file mode 100644 index 0000000..24f67c0 --- /dev/null +++ b/lib/snippets/database/menu/MainMenu.ts @@ -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 , 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'); + }); + } +} diff --git a/lib/snippets/database/menu/Menu.ts b/lib/snippets/database/menu/Menu.ts new file mode 100644 index 0000000..87d0bfe --- /dev/null +++ b/lib/snippets/database/menu/Menu.ts @@ -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 , June 2020 + */ + +import { StringOption } from '@sapphirecode/console-app'; + +export class Menu { + private _options: Recordvoid|Promise> = {}; + private _title: string; + + public constructor (title: string) { + this._title = title; + } + + public async run (): Promise { + 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 { + this._options[name] = callback; + } +} diff --git a/lib/snippets/database/patch_actions/PatchActions.ts b/lib/snippets/database/patch_actions/PatchActions.ts new file mode 100644 index 0000000..248519b --- /dev/null +++ b/lib/snippets/database/patch_actions/PatchActions.ts @@ -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 , 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'; diff --git a/lib/snippets/database/patch_actions/RenameColumn.ts b/lib/snippets/database/patch_actions/RenameColumn.ts index c1bff12..db797f8 100644 --- a/lib/snippets/database/patch_actions/RenameColumn.ts +++ b/lib/snippets/database/patch_actions/RenameColumn.ts @@ -5,40 +5,39 @@ * Created by Timo Hocker , 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 - = /(?[a-z_]+) (?[a-z_]+) (?[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 { diff --git a/package.json b/package.json index 6491c6d..78dd74c 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,8 @@ "typescript": "^3.8.3" }, "dependencies": { + "@sapphirecode/console-app": "^2.0.6", + "@sapphirecode/modelling": "^1.1.6", "@sapphirecode/standard": "^1.0.1", "enquirer": "^2.3.5", "fs-extra": "^9.0.0", diff --git a/yarn.lock b/yarn.lock index ccb1cc9..5615eb7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -43,6 +43,18 @@ resolved "https://registry.yarnpkg.com/@ovyerus/licenses/-/licenses-6.4.4.tgz#596e3ace46ab7c70bcf0e2b17f259796a4bedf9f" integrity sha512-IHjc31WXciQT3hfvdY+M59jBkQp70Fpr04tNDVO5rez2PNv4u8tE6w//CkU+GeBoO9k2ahneSqzjzvlgjyjkGw== +"@sapphirecode/console-app@^2.0.6": + version "2.0.6" + resolved "https://registry.yarnpkg.com/@sapphirecode/console-app/-/console-app-2.0.6.tgz#5034a2beddd9d0cb5f29e5b9ffff4c36cc647b31" + integrity sha512-dceIFY+hTMzCXpPH2ZIy+zVA7qwt1N7maNL9RjnkZNIoiJicyPnnkF3W8JHubh/i00ejzepXoBf6gJEgDeHclw== + dependencies: + "@sapphirecode/modelling" "^1.0.35" + "@sapphirecode/utilities" "^1.3.4" + enquirer "^2.3.5" + fs-extra "^9.0.0" + hjson "^3.2.1" + yargs "^15.3.1" + "@sapphirecode/eslint-config-es6@^1.1.1": version "1.1.12" resolved "https://registry.yarnpkg.com/@sapphirecode/eslint-config-es6/-/eslint-config-es6-1.1.12.tgz#cc38dcbf59c450ac09d8042f3d69cd784005aec9" @@ -69,11 +81,30 @@ eslint-plugin-node "^11.1.0" eslint-plugin-sort-requires-by-path "^1.0.2" +"@sapphirecode/modelling@^1.0.35": + version "1.1.5" + resolved "https://registry.yarnpkg.com/@sapphirecode/modelling/-/modelling-1.1.5.tgz#71b1ec6a8938ad4e8a3ea13bd6bd9d54eaabedcd" + integrity sha512-5MkawJexd0FzcFDlD+bt/mmNuwtjRyMqntHHKpieov2pU4HJxJoQL1k5gnrDmKittXZsl2OOweNi/Xa2cYxmCA== + dependencies: + "@sapphirecode/utilities" "^1.3.2" + +"@sapphirecode/modelling@^1.1.6": + version "1.1.6" + resolved "https://registry.yarnpkg.com/@sapphirecode/modelling/-/modelling-1.1.6.tgz#84b1b760e07a8fa3da326b7b973b23f52e149a0e" + integrity sha512-WxuMUNHtcC0wbPXtgzi5JX1mjFZbmoWpwCP3iICja3K6ecu4ptk7npnDM3klL+kThq9CMWAIffOheo4y5b4Cgg== + dependencies: + "@sapphirecode/utilities" "^1.3.2" + "@sapphirecode/standard@^1.0.1": version "1.1.8" resolved "https://registry.yarnpkg.com/@sapphirecode/standard/-/standard-1.1.8.tgz#07c48ea91c7d1555d0110419d5eeeb6f2d11b246" integrity sha512-O3dO6fujxlnR/7DoRpLB5NtTG4FMnRgDN0574rAT9oaUsL2rf3rpinlyeB2V36C8wtpihbcAw7zA5FSEPO3sdw== +"@sapphirecode/utilities@^1.3.2", "@sapphirecode/utilities@^1.3.4": + version "1.5.1" + resolved "https://registry.yarnpkg.com/@sapphirecode/utilities/-/utilities-1.5.1.tgz#d7e5a3791aa558bf968bb201ab3cc34b60d3e650" + integrity sha512-CU19QMj6WjUuup5mpKK6QOY/NROIUBpoQ1pOokmD7J3wd7NND9xntU9bnzSxrPc5OfhqY5r+S30+hdrEsoAXbA== + "@types/color-name@^1.1.1": version "1.1.1" resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0" @@ -789,6 +820,11 @@ has@^1.0.3: dependencies: function-bind "^1.1.1" +hjson@^3.2.1: + version "3.2.1" + resolved "https://registry.yarnpkg.com/hjson/-/hjson-3.2.1.tgz#20de41dc87fc9a10d1557d0230b0e02afb1b09ac" + integrity sha512-OhhrFMeC7dVuA1xvxuXGTv/yTdhTvbe8hz+3LgVNsfi9+vgz0sF/RrkuX8eegpKaMc9cwYwydImBH6iePoJtdQ== + hosted-git-info@^2.1.4: version "2.8.8" resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.8.8.tgz#7539bd4bc1e0e0a895815a2e0262420b12858488"