initial menu design, simpler patch serialization

This commit is contained in:
Timo Hocker 2020-06-28 14:51:37 +02:00
parent 214b113865
commit 146c9b661f
9 changed files with 136 additions and 55 deletions

View File

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

View File

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

View File

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

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

View 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;
}
}

View 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';

View File

@ -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 {

View File

@ -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",

View File

@ -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"