-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlCrud.ts
108 lines (86 loc) · 2.66 KB
/
sqlCrud.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { PowerSQL, PowerSQLDefaults, PowerSQLTable, PowerSQLTableColumn } from "powersql";
import { Crud } from "..";
import DbInterface from "./dbInterface";
import checkSQLIdentifierName from "./nameCheck";
import ObjectModel from "./objectModel";
import VirtualType from "./virtualType";
export type DataInputHandler<T> = (data: T) => any | Promise<any>;
export type DataOutputHandler<T> = (data: any) => T | Promise<T>;
export interface SQLSearchCondition {
[field: string]: {
value: any;
compare: string;
}
}
export type SQLBooleanComparsion = 'AND' | 'OR';
export type SQLSearch = Array<SQLSearchCondition|SQLBooleanComparsion>;
export default abstract class SQLCrud<T> extends Crud<T> {
/**
* The data model
*/
public abstract get model(): ObjectModel;
/**
* The database to store/retreive data
*/
public abstract get database(): DbInterface;
public abstract set database(value: DbInterface);
/**
* The table built based on the model
*/
public abstract get table(): PowerSQLTable;
/**
* @depreceated Use setup() instead
*/
public createTableIfNotExists(): Promise<void> {
return this.setup();
}
/**
* Creates the table
*/
public abstract setup(): Promise<void>;
/**
* Query the database and returns the output
* @param sql The SQL query
*/
protected abstract query(sql: string): Promise<T[]>;
/**
* Inserts data to the database
* @param data The data to insert
*/
public abstract insert(data: T): Promise<void>;
/**
*
* @param searchKeys The search keys
*/
public abstract get(searchKeys: any): Promise<T>;
/**
*
* @param searchKeys The search keys to define the rows to be updated
* @param dataToUpdate The data to update
*/
public abstract update(searchKeys: any, dataToUpdate: any): Promise<void>;
/**
* Deletes database rows
* @param searchKeys The search keys to delete
*/
public abstract delete(searchKeys: any): Promise<void>;
/**
* Inserts each item of the data array into the database
* @param data The data array to insert into the database
*/
public abstract insertMultiple(data: T[]): Promise<void>;
/**
* Get multiple data from the database
* @param searchKeys The keys to search
*/
public abstract getMultiple(searchKeys: any): Promise<T[]>;
/**
* Returns all database data
*/
public abstract getAll(): Promise<T[]>;
/**
* Custom SQL search
* @param search The SQL search
*/
public abstract search(search: SQLSearch): Promise<T[]>;
}