2020-06-28 17:02:46 +02:00

37 lines
1.1 KiB
TypeScript

/*
* 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 { 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);
}
}