Compare commits
No commits in common. "20c3780f8eb976d7f049152b2655326f9271d14c" and "043759f707d7532c438f44297d5073bd5b9d2831" have entirely different histories.
20c3780f8e
...
043759f707
20
lib/snippets/database/classes/Column.ts
Normal file
20
lib/snippets/database/classes/Column.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>, 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;
|
||||||
|
}
|
||||||
|
}
|
27
lib/snippets/database/classes/ColumnType.ts
Normal file
27
lib/snippets/database/classes/ColumnType.ts
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* 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'
|
||||||
|
}
|
21
lib/snippets/database/classes/Database.ts
Normal file
21
lib/snippets/database/classes/Database.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
13
lib/snippets/database/classes/PatchAction.ts
Normal file
13
lib/snippets/database/classes/PatchAction.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
}
|
16
lib/snippets/database/classes/Relation.ts
Normal file
16
lib/snippets/database/classes/Relation.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
26
lib/snippets/database/classes/Table.ts
Normal file
26
lib/snippets/database/classes/Table.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
21
lib/snippets/database/index.ts
Normal file
21
lib/snippets/database/index.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
* 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 ();
|
||||||
|
}
|
||||||
|
}
|
21
lib/snippets/database/menu/MainMenu.ts
Normal file
21
lib/snippets/database/menu/MainMenu.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
16
lib/snippets/database/patch_actions/ActionFactory.ts
Normal file
16
lib/snippets/database/patch_actions/ActionFactory.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
29
lib/snippets/database/patch_actions/AddTable.ts
Normal file
29
lib/snippets/database/patch_actions/AddTable.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
50
lib/snippets/database/patch_actions/PatchActions.ts
Normal file
50
lib/snippets/database/patch_actions/PatchActions.ts
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
};
|
56
lib/snippets/database/patch_actions/RenameColumn.ts
Normal file
56
lib/snippets/database/patch_actions/RenameColumn.ts
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user