@egi/smart-db 2.0.3 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +212 -0
- package/README.md +2 -0
- package/assets/mysql/{database-init.sql → smart-db-core-init.sql} +0 -0
- package/assets/sqlite3/{database-init.sql → smart-db-core-init.sql} +0 -0
- package/bin/copy-assets +6 -0
- package/bin/extract-db-api +19 -0
- package/package.json +57 -28
- package/src/drivers/smart-db-better-sqlite3.ts +284 -0
- package/src/drivers/smart-db-mysql.ts +159 -0
- package/src/drivers/smart-db-mysql2.ts +159 -0
- package/src/drivers/smart-db-sqlite3.ts +198 -0
- package/src/helpers/extract-db-api.ts +465 -0
- package/src/helpers/terser-tree.ts +39 -0
- package/src/models/abstract-model.ts +209 -0
- package/src/models/smart-db-core-table-model.ts +161 -0
- package/src/models/smart-db-dictionary.ts +28 -0
- package/src/models/smart-db-log-model.ts +316 -0
- package/src/models/smart-db-version-model.ts +316 -0
- package/src/models/smart-db-version-view-model.ts +347 -0
- package/src/models/sqlite-master-model.ts +140 -0
- package/src/models/sqlite-sequence-model.ts +91 -0
- package/src/smart-db-api.ts +15 -0
- package/src/smart-db-globals.ts +136 -0
- package/src/smart-db-interfaces.ts +218 -0
- package/src/smart-db-log.ts +262 -0
- package/src/smart-db-sql-build-data.ts +28 -0
- package/src/smart-db-upgrade-manager.ts +171 -0
- package/src/smart-db.ts +854 -0
- package/test/data/sql-engine-tests.json +232 -0
- package/test/db/mysql/database-init.sql +11 -0
- package/test/db/sqlite3/database-init.sql +11 -0
- package/test/exer.js +28 -0
- package/test/model/smart-db-dictionary.ts +19 -0
- package/test/model/test-table-model.ts +214 -0
- package/test/test.js +273 -0
- package/test/test2.js +252 -0
- package/tsconfig.json +32 -0
- package/tsconfig.pro.json +32 -0
- package/tsconfig.test-model.json +23 -0
- package/bin/extract-db-interface +0 -18
- package/drivers/smart-db-better-sqlite3.d.ts +0 -34
- package/drivers/smart-db-better-sqlite3.js +0 -1
- package/drivers/smart-db-mysql.d.ts +0 -24
- package/drivers/smart-db-mysql.js +0 -1
- package/drivers/smart-db-mysql2.d.ts +0 -24
- package/drivers/smart-db-mysql2.js +0 -1
- package/drivers/smart-db-sqlite3.d.ts +0 -28
- package/drivers/smart-db-sqlite3.js +0 -1
- package/helpers/extract-db-interface.d.ts +0 -1
- package/helpers/extract-db-interface.js +0 -1
- package/index.d.ts +0 -10
- package/index.js +0 -1
- package/model/abstract-model.d.ts +0 -44
- package/model/abstract-model.js +0 -1
- package/model/smart-db-core-table-model.d.ts +0 -33
- package/model/smart-db-core-table-model.js +0 -1
- package/model/smart-db-dictionary.d.ts +0 -15
- package/model/smart-db-dictionary.js +0 -1
- package/model/smart-db-log-model.d.ts +0 -63
- package/model/smart-db-log-model.js +0 -1
- package/model/smart-db-version-model.d.ts +0 -63
- package/model/smart-db-version-model.js +0 -1
- package/model/smart-db-version-view-model.d.ts +0 -70
- package/model/smart-db-version-view-model.js +0 -1
- package/model/sqlite-master-model.d.ts +0 -29
- package/model/sqlite-master-model.js +0 -1
- package/model/sqlite-sequence-model.d.ts +0 -24
- package/model/sqlite-sequence-model.js +0 -1
- package/smart-db-interface.d.ts +0 -173
- package/smart-db-interface.js +0 -1
- package/smart-db-log.d.ts +0 -32
- package/smart-db-log.js +0 -1
- package/smart-db-upgrade-manager.d.ts +0 -13
- package/smart-db-upgrade-manager.js +0 -1
- package/smart-db.d.ts +0 -74
- package/smart-db.js +0 -1
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/member-ordering */
|
|
2
|
+
// noinspection JSUnusedGlobalSymbols
|
|
3
|
+
|
|
4
|
+
import {ModelAttributeMap} from "../smart-db-interfaces";
|
|
5
|
+
import {AbstractModel} from "./abstract-model";
|
|
6
|
+
|
|
7
|
+
export interface SqliteMasterModelData extends Record<string, any> {
|
|
8
|
+
name?: string;
|
|
9
|
+
type?: string;
|
|
10
|
+
rootpage?: number;
|
|
11
|
+
sql?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
export class SqliteMasterModel extends AbstractModel<SqliteMasterModel, SqliteMasterModelData> {
|
|
16
|
+
private _type?: string;
|
|
17
|
+
private _name: string;
|
|
18
|
+
private _tbl_name?: string;
|
|
19
|
+
private _rootpage?: number;
|
|
20
|
+
private _sql?: string;
|
|
21
|
+
|
|
22
|
+
static readonly attributeMap: ModelAttributeMap = {
|
|
23
|
+
type: {
|
|
24
|
+
type: "string",
|
|
25
|
+
attribute: "_type"
|
|
26
|
+
},
|
|
27
|
+
name: {
|
|
28
|
+
type: "string",
|
|
29
|
+
attribute: "_name"
|
|
30
|
+
},
|
|
31
|
+
tbl_name: {
|
|
32
|
+
type: "string",
|
|
33
|
+
attribute: "_tbl_name"
|
|
34
|
+
},
|
|
35
|
+
tblName: {
|
|
36
|
+
alias: "tbl_name",
|
|
37
|
+
type: "string",
|
|
38
|
+
attribute: "_tbl_name"
|
|
39
|
+
},
|
|
40
|
+
rootpage: {
|
|
41
|
+
type: "number",
|
|
42
|
+
attribute: "_rootpage"
|
|
43
|
+
},
|
|
44
|
+
sql: {
|
|
45
|
+
type: "string",
|
|
46
|
+
attribute: "_sql"
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
static getClassName(): string {
|
|
51
|
+
return "SqliteMasterModel";
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static getTableName(): string {
|
|
55
|
+
return "sqlite_master";
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
static from(other: SqliteMasterModel): SqliteMasterModel {
|
|
59
|
+
let value: SqliteMasterModel = null;
|
|
60
|
+
if (other) {
|
|
61
|
+
value = new SqliteMasterModel();
|
|
62
|
+
if (other instanceof SqliteMasterModel) {
|
|
63
|
+
Object.assign(value, other);
|
|
64
|
+
} else {
|
|
65
|
+
value.assign(other);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return value;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public clone(): SqliteMasterModel {
|
|
72
|
+
return SqliteMasterModel.from(this);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public getClassName(): string {
|
|
76
|
+
return "SqliteMasterModel";
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public getTableName(): string {
|
|
80
|
+
return "sqlite_master";
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public getAttributeMap(): ModelAttributeMap {
|
|
84
|
+
return SqliteMasterModel.attributeMap;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
public getPrimaryKey(): string {
|
|
88
|
+
return "name";
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
get name(): string {
|
|
92
|
+
return this._name;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
set name(name: string) {
|
|
96
|
+
this._name = name;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
get rootpage(): number {
|
|
100
|
+
return this._rootpage;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
set rootpage(rootpage: number) {
|
|
104
|
+
this._rootpage = rootpage;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
get sql(): string {
|
|
108
|
+
return this._sql;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
set sql(sql: string) {
|
|
112
|
+
this._sql = sql;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
get tblName(): string {
|
|
116
|
+
return this._tbl_name;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
set tblName(tblNname: string) {
|
|
120
|
+
this._tbl_name = tblNname;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// noinspection FunctionNamingConventionJS
|
|
124
|
+
get tbl_name(): string {
|
|
125
|
+
return this._tbl_name;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// noinspection FunctionNamingConventionJS
|
|
129
|
+
set tbl_name(tblNname: string) {
|
|
130
|
+
this._tbl_name = tblNname;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
get type(): string {
|
|
134
|
+
return this._type;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
set type(type: string) {
|
|
138
|
+
this._type = type;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/member-ordering */
|
|
2
|
+
// noinspection JSUnusedGlobalSymbols
|
|
3
|
+
|
|
4
|
+
import {GenericModelData, ModelAttributeMap, SqlValueType} from "../smart-db-interfaces";
|
|
5
|
+
import {AbstractModel} from "./abstract-model";
|
|
6
|
+
|
|
7
|
+
export interface SqliteSequenceModelData extends Record<string, any> {
|
|
8
|
+
name?: SqlValueType;
|
|
9
|
+
seq?: SqlValueType;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class SqliteSequenceModel extends AbstractModel <SqliteSequenceModel, SqliteSequenceModelData> {
|
|
13
|
+
private _name?: SqlValueType;
|
|
14
|
+
private _seq?: SqlValueType;
|
|
15
|
+
|
|
16
|
+
static readonly attributeMap: ModelAttributeMap = {
|
|
17
|
+
name: {
|
|
18
|
+
physical: true,
|
|
19
|
+
typeScriptStyle: true,
|
|
20
|
+
type: "SqlValueType",
|
|
21
|
+
attribute: "_name"
|
|
22
|
+
},
|
|
23
|
+
seq: {
|
|
24
|
+
physical: true,
|
|
25
|
+
typeScriptStyle: true,
|
|
26
|
+
type: "SqlValueType",
|
|
27
|
+
attribute: "_seq"
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
static getClassName(): string {
|
|
32
|
+
return "SqliteSequenceModel";
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static getTableName(): string {
|
|
36
|
+
return "sqlite_sequence";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static getPrimaryKey(): string {
|
|
40
|
+
return "";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static from(other: SqliteSequenceModel | GenericModelData): SqliteSequenceModel {
|
|
44
|
+
let value: SqliteSequenceModel = null;
|
|
45
|
+
if (other) {
|
|
46
|
+
value = new SqliteSequenceModel();
|
|
47
|
+
if (other instanceof SqliteSequenceModel) {
|
|
48
|
+
Object.assign(value, other);
|
|
49
|
+
} else {
|
|
50
|
+
value.assign(other);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public clone(): SqliteSequenceModel {
|
|
57
|
+
return SqliteSequenceModel.from(this);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public getClassName(): string {
|
|
61
|
+
return "SqliteSequenceModel";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public getTableName(): string {
|
|
65
|
+
return "sqlite_sequence";
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public getPrimaryKey(): string {
|
|
69
|
+
return "";
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public getAttributeMap(): ModelAttributeMap {
|
|
73
|
+
return SqliteSequenceModel.attributeMap;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
get name(): SqlValueType {
|
|
77
|
+
return this._name;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
set name(name: SqlValueType) {
|
|
81
|
+
this._name = name;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
get seq(): SqlValueType {
|
|
85
|
+
return this._seq;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
set seq(seq: SqlValueType) {
|
|
89
|
+
this._seq = seq;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// noinspection JSUnusedGlobalSymbols
|
|
2
|
+
|
|
3
|
+
export * from "./smart-db";
|
|
4
|
+
export * from "./models/abstract-model";
|
|
5
|
+
export * from "./models/smart-db-core-table-model";
|
|
6
|
+
export * from "./models/smart-db-dictionary";
|
|
7
|
+
export * from "./models/smart-db-log-model";
|
|
8
|
+
export * from "./models/smart-db-version-model";
|
|
9
|
+
export * from "./models/smart-db-version-view-model";
|
|
10
|
+
export * from "./models/sqlite-master-model";
|
|
11
|
+
export * from "./models/sqlite-sequence-model";
|
|
12
|
+
export * from "./smart-db-log";
|
|
13
|
+
export * from "./smart-db-globals";
|
|
14
|
+
export * from "./smart-db-interfaces";
|
|
15
|
+
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import _ from "lodash";
|
|
2
|
+
import {SqlFieldDescriptor, SqlFieldOperationType, SqlOperation, SqlOperationType, SqlValueType} from "./smart-db-interfaces";
|
|
3
|
+
|
|
4
|
+
export const OP = (op: SqlOperationType, value?: SqlValueType | SqlValueType[], key?: string, literalOp?: SqlOperationType): SqlOperation => {
|
|
5
|
+
return key ? {
|
|
6
|
+
operation: op,
|
|
7
|
+
value: value,
|
|
8
|
+
key: key,
|
|
9
|
+
literalOperation: literalOp
|
|
10
|
+
} : {
|
|
11
|
+
operation: op,
|
|
12
|
+
value: value
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
// noinspection JSUnusedGlobalSymbols
|
|
16
|
+
export const VALUE = (value: SqlValueType, alias?: string, literal?: boolean): SqlFieldDescriptor => {
|
|
17
|
+
return {
|
|
18
|
+
operation: SqlFieldOperationType.VALUE,
|
|
19
|
+
value: value,
|
|
20
|
+
alias: alias,
|
|
21
|
+
literal: literal
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
// noinspection JSUnusedGlobalSymbols
|
|
25
|
+
export const FIELD = (value: string, alias?: string): SqlFieldDescriptor => {
|
|
26
|
+
return {
|
|
27
|
+
operation: SqlFieldOperationType.FIELD,
|
|
28
|
+
value: value,
|
|
29
|
+
alias: alias
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
// noinspection JSUnusedGlobalSymbols
|
|
33
|
+
export const COUNT = (value?: string, alias?: string): SqlFieldDescriptor => {
|
|
34
|
+
return {
|
|
35
|
+
operation: SqlFieldOperationType.COUNT,
|
|
36
|
+
value: value,
|
|
37
|
+
alias: alias
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
// noinspection JSUnusedGlobalSymbols
|
|
41
|
+
export const COALESCE = (values: (SqlFieldDescriptor | SqlValueType)[], alias?: string): SqlFieldDescriptor => {
|
|
42
|
+
return {
|
|
43
|
+
operation: SqlFieldOperationType.COALESCE,
|
|
44
|
+
value: values,
|
|
45
|
+
alias: alias
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
// noinspection JSUnusedGlobalSymbols
|
|
49
|
+
export const NVL = (values: (SqlFieldDescriptor | SqlValueType)[], alias?: string): SqlFieldDescriptor => {
|
|
50
|
+
return COALESCE(values, alias);
|
|
51
|
+
};
|
|
52
|
+
// noinspection JSUnusedGlobalSymbols
|
|
53
|
+
export const LITERAL = (key: string, value: SqlValueType, op?: SqlOperationType): SqlOperation => {
|
|
54
|
+
return OP(SqlOperationType.LITERAL, value, key, op || SqlOperationType.EQ);
|
|
55
|
+
};
|
|
56
|
+
// noinspection JSUnusedGlobalSymbols
|
|
57
|
+
export const GT = (value: SqlValueType): SqlOperation => {
|
|
58
|
+
return OP(SqlOperationType.GT, value);
|
|
59
|
+
};
|
|
60
|
+
// noinspection JSUnusedGlobalSymbols
|
|
61
|
+
export const GE = (value: SqlValueType): SqlOperation => {
|
|
62
|
+
return OP(SqlOperationType.GE, value);
|
|
63
|
+
};
|
|
64
|
+
// noinspection JSUnusedGlobalSymbols
|
|
65
|
+
export const LT = (value: SqlValueType): SqlOperation => {
|
|
66
|
+
return OP(SqlOperationType.LT, value);
|
|
67
|
+
};
|
|
68
|
+
// noinspection JSUnusedGlobalSymbols
|
|
69
|
+
export const LE = (value: SqlValueType): SqlOperation => {
|
|
70
|
+
return OP(SqlOperationType.LE, value);
|
|
71
|
+
};
|
|
72
|
+
// noinspection JSUnusedGlobalSymbols
|
|
73
|
+
export const EQ = (value: SqlValueType): SqlOperation => {
|
|
74
|
+
return OP(SqlOperationType.EQ, value);
|
|
75
|
+
};
|
|
76
|
+
// noinspection JSUnusedGlobalSymbols
|
|
77
|
+
export const NE = (value: any): SqlOperation => {
|
|
78
|
+
return OP(SqlOperationType.NE, value);
|
|
79
|
+
};
|
|
80
|
+
// noinspection JSUnusedGlobalSymbols
|
|
81
|
+
export const IN = (value: SqlValueType[]): SqlOperation => {
|
|
82
|
+
return OP(SqlOperationType.IN, value);
|
|
83
|
+
};
|
|
84
|
+
// noinspection JSUnusedGlobalSymbols
|
|
85
|
+
export const IS_NULL = (): SqlOperation => {
|
|
86
|
+
return OP(SqlOperationType.IS_NULL);
|
|
87
|
+
};
|
|
88
|
+
// noinspection JSUnusedGlobalSymbols
|
|
89
|
+
export const IS_NOT_NULL = (): SqlOperation => {
|
|
90
|
+
return OP(SqlOperationType.IS_NOT_NULL);
|
|
91
|
+
};
|
|
92
|
+
export const SmartDbDateRegexp = /^(\d{4}-[01]\d-[0-3]\d [0-2]\d:[0-5]\d:[0-5]\d)(Z| GMT| GMT[-+]\d{1,2})?$/;
|
|
93
|
+
export const SmartDbTimestampRegexp = /^(\d{4}-[01]\d-[0-3]\d [0-2]\d:[0-5]\d:[0-5]\d\.\d{3})(Z| GMT| GMT[-+]\d{1,2})?$/;
|
|
94
|
+
|
|
95
|
+
export function toSmartDbDate(d: Date | number): string {
|
|
96
|
+
let dateString: string;
|
|
97
|
+
if (d) {
|
|
98
|
+
if (_.isNumber(d)) {
|
|
99
|
+
d = new Date(d);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
dateString = d.toISOString().substr(0, 19).replace("T", " ");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return dateString;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function toSmartDbTimestamp(d: Date | number): string {
|
|
109
|
+
let timestampString: string;
|
|
110
|
+
if (d) {
|
|
111
|
+
if (_.isNumber(d)) {
|
|
112
|
+
d = new Date(d);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
timestampString = d.toISOString().substr(0, 23).replace("T", " ");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return timestampString;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// noinspection JSUnusedGlobalSymbols
|
|
122
|
+
export function smartDbToDate(d: Date | number | string): Date | null {
|
|
123
|
+
let date: Date = null;
|
|
124
|
+
|
|
125
|
+
if (_.isDate(d)) {
|
|
126
|
+
date = d;
|
|
127
|
+
} else if (_.isNumber(d)) {
|
|
128
|
+
date = new Date(d);
|
|
129
|
+
} else if (_.isString(d)) {
|
|
130
|
+
if (d.match(SmartDbDateRegexp) || d.match(SmartDbTimestampRegexp)) {
|
|
131
|
+
date = new Date(d);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return date;
|
|
136
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import {AbstractModel} from "./models/abstract-model";
|
|
2
|
+
import {SmartDbDictionary} from "./models/smart-db-dictionary";
|
|
3
|
+
import {SmartDbVersionViewModel} from "./models/smart-db-version-view-model";
|
|
4
|
+
import {SmartDbSqlBuildData} from "./smart-db-sql-build-data";
|
|
5
|
+
|
|
6
|
+
export interface SmartDbOptions {
|
|
7
|
+
module: string;
|
|
8
|
+
sqlFilesDirectory: string;
|
|
9
|
+
smartDbDictionary?: typeof SmartDbDictionary | any[];
|
|
10
|
+
skipAutoUpgrade?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface SmartDbRunResult {
|
|
14
|
+
lastId: number;
|
|
15
|
+
changes: number;
|
|
16
|
+
affected: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface SmartDbTableInfo {
|
|
20
|
+
name: string;
|
|
21
|
+
fields: SmartDbFieldDescription[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface SmartDbFieldDescription {
|
|
25
|
+
cid?: string | number;
|
|
26
|
+
name: string;
|
|
27
|
+
type: string;
|
|
28
|
+
virtual?: boolean;
|
|
29
|
+
notNull?: boolean;
|
|
30
|
+
defaultValue?: number | string | boolean;
|
|
31
|
+
isPk?: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface SmartDbSqlComparison {
|
|
35
|
+
compare: string | SqlFieldDescriptor,
|
|
36
|
+
with: string | SqlFieldDescriptor;
|
|
37
|
+
operation?: SqlOperationType;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type SqlValueType = string | number | boolean | Date;
|
|
41
|
+
|
|
42
|
+
export enum FieldNamingStyle { Database, TypeScript, All}
|
|
43
|
+
|
|
44
|
+
export type GenericModelData = Record<string, SqlValueType>;
|
|
45
|
+
|
|
46
|
+
export interface AttributeInfo {
|
|
47
|
+
attribute: string;
|
|
48
|
+
type: string;
|
|
49
|
+
alias?: string;
|
|
50
|
+
physical?: boolean;
|
|
51
|
+
virtual?: boolean;
|
|
52
|
+
alternative?: boolean;
|
|
53
|
+
typeScriptStyle?: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface ModelAttributeMap {
|
|
57
|
+
[key: string]: AttributeInfo;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface AbstractModelGlobals<T extends AbstractModel<T, D>, D extends GenericModelData> {
|
|
61
|
+
attributeMap: ModelAttributeMap;
|
|
62
|
+
getTableName: () => string;
|
|
63
|
+
from: (other: T | D) => T;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface SqlWhere {
|
|
67
|
+
or?: SqlWhere | SqlWhere[];
|
|
68
|
+
and?: SqlWhere | SqlWhere[];
|
|
69
|
+
expression?: SmartDbSqlComparison[];
|
|
70
|
+
|
|
71
|
+
[key: string]: SqlValueType | SqlValueType[] | SqlOperation | SqlWhere | SqlWhere[] | SmartDbSqlComparison[];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export type SqlUpdateValues = Record<string, SqlValueType>;
|
|
75
|
+
export type SqlOrderBy = string | string[];
|
|
76
|
+
type SqlGroupBy = string | string[];
|
|
77
|
+
|
|
78
|
+
export interface SqlLimit {
|
|
79
|
+
offset?: number;
|
|
80
|
+
limit?: number;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface SectionDescription<T extends AbstractModel<T, D>, D extends GenericModelData> {
|
|
84
|
+
model: (new() => T) | string;
|
|
85
|
+
where?: SqlWhere;
|
|
86
|
+
fields?: string | SqlFieldDescriptor | (string | SqlFieldDescriptor)[] | null;
|
|
87
|
+
groupBy?: SqlGroupBy;
|
|
88
|
+
limit?: SqlLimit;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface SmartDbSqlOptions<T extends AbstractModel<T, D>, D extends GenericModelData> {
|
|
92
|
+
firstOnly?: boolean;
|
|
93
|
+
where?: SqlWhere;
|
|
94
|
+
fields?: string | SqlFieldDescriptor | (string | SqlFieldDescriptor)[] | null;
|
|
95
|
+
orderBy?: SqlOrderBy;
|
|
96
|
+
groupBy?: SqlGroupBy;
|
|
97
|
+
limit?: SqlLimit;
|
|
98
|
+
count?: boolean;
|
|
99
|
+
distinct?: boolean;
|
|
100
|
+
collapseRow?: boolean;
|
|
101
|
+
indexedBy?: string;
|
|
102
|
+
union?: SectionDescription<T, D> | SectionDescription<T, D>[];
|
|
103
|
+
minus?: SectionDescription<T, D> | SectionDescription<T, D>[];
|
|
104
|
+
intersect?: SectionDescription<T, D> | SectionDescription<T, D>[];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export enum SqlOperationType {
|
|
108
|
+
// noinspection JSUnusedGlobalSymbols,LocalVariableNamingConventionJS
|
|
109
|
+
// eslint-disable-next-line no-shadow
|
|
110
|
+
GT = ">", GE = ">=", LT = "<", LE = "<=", EQ = "=", NE = "!=",
|
|
111
|
+
// eslint-disable-next-line no-shadow
|
|
112
|
+
IN = "IN", LIKE = "LIKE", NOT_LIKE = "NOT LIKE",
|
|
113
|
+
// eslint-disable-next-line no-shadow
|
|
114
|
+
IS_NULL = "IS NULL", IS_NOT_NULL = "IS NOT NULL",
|
|
115
|
+
// eslint-disable-next-line no-shadow
|
|
116
|
+
LITERAL = "LITERAL",
|
|
117
|
+
VALUE = "VALUE"
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export enum SqlFieldOperationType {
|
|
121
|
+
VALUE = "VALUE",
|
|
122
|
+
FIELD = "FIELD",
|
|
123
|
+
COUNT = "COUNT",
|
|
124
|
+
COALESCE = "COALESCE"
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface SqlFieldDescriptor {
|
|
128
|
+
operation: SqlFieldOperationType,
|
|
129
|
+
value: SqlFieldDescriptor | SqlValueType | (SqlFieldDescriptor | SqlValueType)[],
|
|
130
|
+
alias?: string,
|
|
131
|
+
literal?: boolean;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export interface SqlOperation {
|
|
135
|
+
operation: SqlOperationType;
|
|
136
|
+
value: SqlValueType | SqlValueType[];
|
|
137
|
+
key?: string;
|
|
138
|
+
literalOperation?: SqlOperationType;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export interface KeyValueList {
|
|
142
|
+
[key: string]: SqlValueType;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export interface IndexedGenericModelData<T extends AbstractModel<T, D>, D extends GenericModelData> {
|
|
146
|
+
[key: string]: T | GenericModelData;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export interface SmartDbSqlBuildDataResults {
|
|
150
|
+
sql: string;
|
|
151
|
+
values: SqlValueType[];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export interface SmartDbApi {
|
|
155
|
+
initDb(appOptions: SmartDbOptions): Promise<SmartDbVersionViewModel[]>;
|
|
156
|
+
|
|
157
|
+
exists<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), type?: "view" | "table" | "index", indexTableName?: string): Promise<boolean>;
|
|
158
|
+
|
|
159
|
+
existsSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), type?: "view" | "table" | "index", indexTableName?: string): boolean;
|
|
160
|
+
|
|
161
|
+
get<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), options: SmartDbSqlOptions<T, D>): Promise<(T | D)[] | T | D | SqlValueType | IndexedGenericModelData<T, D> | string | string[]>;
|
|
162
|
+
|
|
163
|
+
getSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), options: SmartDbSqlOptions<T, D>): (T | D)[] | T | D | IndexedGenericModelData<T, D> | SqlValueType | string[] | false;
|
|
164
|
+
|
|
165
|
+
getFirst<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), where?: SqlWhere, fields?: string | string[] | null, orderBy?: SqlOrderBy): Promise<T>;
|
|
166
|
+
|
|
167
|
+
getFirstSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), where?: SqlWhere, fields?: string | string[] | null, orderBy?: SqlOrderBy): T | false;
|
|
168
|
+
|
|
169
|
+
getAll<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), where?: SqlWhere, fields?: string | string[] | null, orderBy?: SqlOrderBy, limit?: SqlLimit): Promise<T[]>;
|
|
170
|
+
|
|
171
|
+
getAllSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), where?: SqlWhere, fields?: string | string[] | null, orderBy?: SqlOrderBy, limit?: SqlLimit): T[] | false;
|
|
172
|
+
|
|
173
|
+
delete<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), where?: SqlWhere): Promise<number>;
|
|
174
|
+
|
|
175
|
+
deleteSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), where?: SqlWhere): number | false;
|
|
176
|
+
|
|
177
|
+
update<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), values: SqlUpdateValues | T, where?: SqlWhere): Promise<number>;
|
|
178
|
+
|
|
179
|
+
updateSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), values: SqlUpdateValues | T, where?: SqlWhere): number | false;
|
|
180
|
+
|
|
181
|
+
insert<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), values: SqlUpdateValues | T): Promise<number>;
|
|
182
|
+
|
|
183
|
+
insertSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), values: SqlUpdateValues | T): number | false;
|
|
184
|
+
|
|
185
|
+
exec(script: string): Promise<void>;
|
|
186
|
+
|
|
187
|
+
execSync(script: string): SmartDbDatabase | false;
|
|
188
|
+
|
|
189
|
+
getLastError(): any;
|
|
190
|
+
|
|
191
|
+
getLastBuildData(): SmartDbSqlBuildData;
|
|
192
|
+
|
|
193
|
+
getDbQuote(): string;
|
|
194
|
+
|
|
195
|
+
getTableInfo(table: string): Promise<SmartDbTableInfo>;
|
|
196
|
+
|
|
197
|
+
toDate(d: Date | number | string): Date | null;
|
|
198
|
+
|
|
199
|
+
getDbConnector(): string | SmartDbConnector;
|
|
200
|
+
|
|
201
|
+
toDbTimestamp(d: Date | number): string;
|
|
202
|
+
|
|
203
|
+
toDbDate(d: Date | number): string;
|
|
204
|
+
|
|
205
|
+
hasConcurrentTransactions(): boolean;
|
|
206
|
+
|
|
207
|
+
closeSync(): boolean;
|
|
208
|
+
|
|
209
|
+
close(): Promise<void>;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export interface SmartDbDatabase {
|
|
213
|
+
// empty placeholder interface
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export interface SmartDbConnector {
|
|
217
|
+
// empty placeholder interface
|
|
218
|
+
}
|