From 858d778039a2ac19cbd34e9cc987b8526a1df463 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Sat, 18 Apr 2020 20:43:02 +0200 Subject: [PATCH] database structure --- lib/snippets/database/classes/Column.ts | 13 ++++++++++ lib/snippets/database/classes/ColumnType.ts | 20 ++++++++++++++ lib/snippets/database/classes/Database.ts | 5 ++++ lib/snippets/database/classes/PatchActions.ts | 15 +++++++++++ lib/snippets/database/classes/Relation.ts | 9 +++++++ lib/snippets/database/classes/Serializable.ts | 3 +++ lib/snippets/database/classes/Table.ts | 10 +++++++ lib/snippets/database/index.ts | 7 +++++ .../database/patch_actions/RenameColumn.ts | 26 +++++++++++++++++++ 9 files changed, 108 insertions(+) create mode 100644 lib/snippets/database/classes/Column.ts create mode 100644 lib/snippets/database/classes/ColumnType.ts create mode 100644 lib/snippets/database/classes/Database.ts create mode 100644 lib/snippets/database/classes/PatchActions.ts create mode 100644 lib/snippets/database/classes/Relation.ts create mode 100644 lib/snippets/database/classes/Serializable.ts create mode 100644 lib/snippets/database/classes/Table.ts create mode 100644 lib/snippets/database/index.ts create mode 100644 lib/snippets/database/patch_actions/RenameColumn.ts diff --git a/lib/snippets/database/classes/Column.ts b/lib/snippets/database/classes/Column.ts new file mode 100644 index 0000000..78e01e5 --- /dev/null +++ b/lib/snippets/database/classes/Column.ts @@ -0,0 +1,13 @@ +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; + } +} diff --git a/lib/snippets/database/classes/ColumnType.ts b/lib/snippets/database/classes/ColumnType.ts new file mode 100644 index 0000000..4ecc8c1 --- /dev/null +++ b/lib/snippets/database/classes/ColumnType.ts @@ -0,0 +1,20 @@ +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' +} diff --git a/lib/snippets/database/classes/Database.ts b/lib/snippets/database/classes/Database.ts new file mode 100644 index 0000000..254447e --- /dev/null +++ b/lib/snippets/database/classes/Database.ts @@ -0,0 +1,5 @@ +import { Table } from './Table'; + +export class Database { + public tables: Array = []; +} diff --git a/lib/snippets/database/classes/PatchActions.ts b/lib/snippets/database/classes/PatchActions.ts new file mode 100644 index 0000000..0f52c6f --- /dev/null +++ b/lib/snippets/database/classes/PatchActions.ts @@ -0,0 +1,15 @@ +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/Relation.ts b/lib/snippets/database/classes/Relation.ts new file mode 100644 index 0000000..89cb134 --- /dev/null +++ b/lib/snippets/database/classes/Relation.ts @@ -0,0 +1,9 @@ +export class Relation { + public column: string; + public table: string; + + public constructor (table: string, column: string) { + this.column = column; + this.table = table; + } +} diff --git a/lib/snippets/database/classes/Serializable.ts b/lib/snippets/database/classes/Serializable.ts new file mode 100644 index 0000000..7d5a8d3 --- /dev/null +++ b/lib/snippets/database/classes/Serializable.ts @@ -0,0 +1,3 @@ +export interface Serializable { + serialize(): string; +} diff --git a/lib/snippets/database/classes/Table.ts b/lib/snippets/database/classes/Table.ts new file mode 100644 index 0000000..d05cfaa --- /dev/null +++ b/lib/snippets/database/classes/Table.ts @@ -0,0 +1,10 @@ +import { Column } from './Column'; + +export class Table { + public name: string; + public columns: Array = []; + + public constructor (name: string) { + this.name = name; + } +} diff --git a/lib/snippets/database/index.ts b/lib/snippets/database/index.ts new file mode 100644 index 0000000..91e2110 --- /dev/null +++ b/lib/snippets/database/index.ts @@ -0,0 +1,7 @@ +import { Snippet } from '../../Snippet'; + +export default class Database implements Snippet { + public start (): Promise { + // noop + } +} diff --git a/lib/snippets/database/patch_actions/RenameColumn.ts b/lib/snippets/database/patch_actions/RenameColumn.ts new file mode 100644 index 0000000..09e4a50 --- /dev/null +++ b/lib/snippets/database/patch_actions/RenameColumn.ts @@ -0,0 +1,26 @@ +import { Serializable } from '../classes/Serializable'; + +export class RenameColumn implements Serializable { + public column: string; + public new_name: string; + + public constructor (column: string, new_name = null) { + if (new_name === null) { + const regex = /(?[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.column = res.groups.column; + this.new_name = res.groups.new_name; + return; + } + + this.column = column; + this.new_name = new_name as unknown as string; + } + + public serialize (): string { + return `${this.column} ${this.new_name}`; + } +}