@egi/smart-db 2.3.1 → 2.3.2
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/bin/copy-assets +6 -0
- package/package.json +1 -1
- 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/{smart-db-api.d.ts → src/smart-db-api.ts} +3 -0
- package/src/smart-db-globals.ts +136 -0
- package/{smart-db-interfaces.d.ts → src/smart-db-interfaces.ts} +82 -34
- 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/drivers/smart-db-better-sqlite3.d.ts +0 -36
- package/drivers/smart-db-better-sqlite3.js +0 -1
- package/drivers/smart-db-mysql.d.ts +0 -26
- package/drivers/smart-db-mysql.js +0 -1
- package/drivers/smart-db-mysql2.d.ts +0 -26
- package/drivers/smart-db-mysql2.js +0 -1
- package/drivers/smart-db-sqlite3.d.ts +0 -30
- package/drivers/smart-db-sqlite3.js +0 -1
- package/helpers/extract-db-api.d.ts +0 -1
- package/helpers/extract-db-api.js +0 -1
- package/models/abstract-model.d.ts +0 -22
- package/models/abstract-model.js +0 -1
- package/models/smart-db-core-table-model.d.ts +0 -35
- package/models/smart-db-core-table-model.js +0 -1
- package/models/smart-db-dictionary.d.ts +0 -13
- package/models/smart-db-dictionary.js +0 -1
- package/models/smart-db-log-model.d.ts +0 -65
- package/models/smart-db-log-model.js +0 -1
- package/models/smart-db-version-model.d.ts +0 -65
- package/models/smart-db-version-model.js +0 -1
- package/models/smart-db-version-view-model.d.ts +0 -71
- package/models/smart-db-version-view-model.js +0 -1
- package/models/sqlite-master-model.d.ts +0 -36
- package/models/sqlite-master-model.js +0 -1
- package/models/sqlite-sequence-model.d.ts +0 -24
- package/models/sqlite-sequence-model.js +0 -1
- package/smart-db-api.js +0 -1
- package/smart-db-globals.d.ts +0 -22
- package/smart-db-globals.js +0 -1
- package/smart-db-interfaces.js +0 -1
- package/smart-db-log.d.ts +0 -58
- package/smart-db-log.js +0 -1
- package/smart-db-sql-build-data.d.ts +0 -9
- package/smart-db-sql-build-data.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 -67
- 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
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// noinspection JSUnusedGlobalSymbols
|
|
2
|
+
|
|
1
3
|
export * from "./smart-db";
|
|
2
4
|
export * from "./models/abstract-model";
|
|
3
5
|
export * from "./models/smart-db-core-table-model";
|
|
@@ -10,3 +12,4 @@ export * from "./models/sqlite-sequence-model";
|
|
|
10
12
|
export * from "./smart-db-log";
|
|
11
13
|
export * from "./smart-db-globals";
|
|
12
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
|
+
}
|
|
@@ -1,22 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
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
|
+
|
|
5
6
|
export interface SmartDbOptions {
|
|
6
7
|
module: string;
|
|
7
8
|
sqlFilesDirectory: string;
|
|
8
9
|
smartDbDictionary?: typeof SmartDbDictionary | any[];
|
|
9
10
|
skipAutoUpgrade?: boolean;
|
|
10
11
|
}
|
|
12
|
+
|
|
11
13
|
export interface SmartDbRunResult {
|
|
12
14
|
lastId: number;
|
|
13
15
|
changes: number;
|
|
14
16
|
affected: number;
|
|
15
17
|
}
|
|
18
|
+
|
|
16
19
|
export interface SmartDbTableInfo {
|
|
17
20
|
name: string;
|
|
18
21
|
fields: SmartDbFieldDescription[];
|
|
19
22
|
}
|
|
23
|
+
|
|
20
24
|
export interface SmartDbFieldDescription {
|
|
21
25
|
cid?: string | number;
|
|
22
26
|
name: string;
|
|
@@ -26,18 +30,19 @@ export interface SmartDbFieldDescription {
|
|
|
26
30
|
defaultValue?: number | string | boolean;
|
|
27
31
|
isPk?: boolean;
|
|
28
32
|
}
|
|
33
|
+
|
|
29
34
|
export interface SmartDbSqlComparison {
|
|
30
|
-
compare: string | SqlFieldDescriptor
|
|
35
|
+
compare: string | SqlFieldDescriptor,
|
|
31
36
|
with: string | SqlFieldDescriptor;
|
|
32
37
|
operation?: SqlOperationType;
|
|
33
38
|
}
|
|
34
|
-
|
|
35
|
-
export
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
+
|
|
41
46
|
export interface AttributeInfo {
|
|
42
47
|
attribute: string;
|
|
43
48
|
type: string;
|
|
@@ -47,34 +52,42 @@ export interface AttributeInfo {
|
|
|
47
52
|
alternative?: boolean;
|
|
48
53
|
typeScriptStyle?: boolean;
|
|
49
54
|
}
|
|
55
|
+
|
|
50
56
|
export interface ModelAttributeMap {
|
|
51
57
|
[key: string]: AttributeInfo;
|
|
52
58
|
}
|
|
59
|
+
|
|
53
60
|
export interface AbstractModelGlobals<T extends AbstractModel<T, D>, D extends GenericModelData> {
|
|
54
61
|
attributeMap: ModelAttributeMap;
|
|
55
62
|
getTableName: () => string;
|
|
56
63
|
from: (other: T | D) => T;
|
|
57
64
|
}
|
|
65
|
+
|
|
58
66
|
export interface SqlWhere {
|
|
59
67
|
or?: SqlWhere | SqlWhere[];
|
|
60
68
|
and?: SqlWhere | SqlWhere[];
|
|
61
69
|
expression?: SmartDbSqlComparison[];
|
|
70
|
+
|
|
62
71
|
[key: string]: SqlValueType | SqlValueType[] | SqlOperation | SqlWhere | SqlWhere[] | SmartDbSqlComparison[];
|
|
63
72
|
}
|
|
64
|
-
|
|
65
|
-
export
|
|
66
|
-
|
|
73
|
+
|
|
74
|
+
export type SqlUpdateValues = Record<string, SqlValueType>;
|
|
75
|
+
export type SqlOrderBy = string | string[];
|
|
76
|
+
type SqlGroupBy = string | string[];
|
|
77
|
+
|
|
67
78
|
export interface SqlLimit {
|
|
68
79
|
offset?: number;
|
|
69
80
|
limit?: number;
|
|
70
81
|
}
|
|
82
|
+
|
|
71
83
|
export interface SectionDescription<T extends AbstractModel<T, D>, D extends GenericModelData> {
|
|
72
|
-
model: (new
|
|
84
|
+
model: (new() => T) | string;
|
|
73
85
|
where?: SqlWhere;
|
|
74
86
|
fields?: string | SqlFieldDescriptor | (string | SqlFieldDescriptor)[] | null;
|
|
75
87
|
groupBy?: SqlGroupBy;
|
|
76
88
|
limit?: SqlLimit;
|
|
77
89
|
}
|
|
90
|
+
|
|
78
91
|
export interface SmartDbSqlOptions<T extends AbstractModel<T, D>, D extends GenericModelData> {
|
|
79
92
|
firstOnly?: boolean;
|
|
80
93
|
where?: SqlWhere;
|
|
@@ -90,81 +103,116 @@ export interface SmartDbSqlOptions<T extends AbstractModel<T, D>, D extends Gene
|
|
|
90
103
|
minus?: SectionDescription<T, D> | SectionDescription<T, D>[];
|
|
91
104
|
intersect?: SectionDescription<T, D> | SectionDescription<T, D>[];
|
|
92
105
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
LE = "<=",
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
IS_NULL = "IS NULL",
|
|
104
|
-
IS_NOT_NULL = "IS NOT NULL",
|
|
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
|
|
105
116
|
LITERAL = "LITERAL",
|
|
106
117
|
VALUE = "VALUE"
|
|
107
118
|
}
|
|
108
|
-
|
|
119
|
+
|
|
120
|
+
export enum SqlFieldOperationType {
|
|
109
121
|
VALUE = "VALUE",
|
|
110
122
|
FIELD = "FIELD",
|
|
111
123
|
COUNT = "COUNT",
|
|
112
124
|
COALESCE = "COALESCE"
|
|
113
125
|
}
|
|
126
|
+
|
|
114
127
|
export interface SqlFieldDescriptor {
|
|
115
|
-
operation: SqlFieldOperationType
|
|
116
|
-
value: SqlFieldDescriptor | SqlValueType | (SqlFieldDescriptor | SqlValueType)[]
|
|
117
|
-
alias?: string
|
|
128
|
+
operation: SqlFieldOperationType,
|
|
129
|
+
value: SqlFieldDescriptor | SqlValueType | (SqlFieldDescriptor | SqlValueType)[],
|
|
130
|
+
alias?: string,
|
|
118
131
|
literal?: boolean;
|
|
119
132
|
}
|
|
133
|
+
|
|
120
134
|
export interface SqlOperation {
|
|
121
135
|
operation: SqlOperationType;
|
|
122
136
|
value: SqlValueType | SqlValueType[];
|
|
123
137
|
key?: string;
|
|
124
138
|
literalOperation?: SqlOperationType;
|
|
125
139
|
}
|
|
140
|
+
|
|
126
141
|
export interface KeyValueList {
|
|
127
142
|
[key: string]: SqlValueType;
|
|
128
143
|
}
|
|
144
|
+
|
|
129
145
|
export interface IndexedGenericModelData<T extends AbstractModel<T, D>, D extends GenericModelData> {
|
|
130
146
|
[key: string]: T | GenericModelData;
|
|
131
147
|
}
|
|
148
|
+
|
|
132
149
|
export interface SmartDbSqlBuildDataResults {
|
|
133
150
|
sql: string;
|
|
134
151
|
values: SqlValueType[];
|
|
135
152
|
}
|
|
153
|
+
|
|
136
154
|
export interface SmartDbApi {
|
|
137
155
|
initDb(appOptions: SmartDbOptions): Promise<SmartDbVersionViewModel[]>;
|
|
156
|
+
|
|
138
157
|
exists<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), type?: "view" | "table" | "index", indexTableName?: string): Promise<boolean>;
|
|
158
|
+
|
|
139
159
|
existsSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), type?: "view" | "table" | "index", indexTableName?: string): boolean;
|
|
160
|
+
|
|
140
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
|
+
|
|
141
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
|
+
|
|
142
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
|
+
|
|
143
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
|
+
|
|
144
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
|
+
|
|
145
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
|
+
|
|
146
173
|
delete<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), where?: SqlWhere): Promise<number>;
|
|
174
|
+
|
|
147
175
|
deleteSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), where?: SqlWhere): number | false;
|
|
176
|
+
|
|
148
177
|
update<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), values: SqlUpdateValues | T, where?: SqlWhere): Promise<number>;
|
|
178
|
+
|
|
149
179
|
updateSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), values: SqlUpdateValues | T, where?: SqlWhere): number | false;
|
|
180
|
+
|
|
150
181
|
insert<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), values: SqlUpdateValues | T): Promise<number>;
|
|
182
|
+
|
|
151
183
|
insertSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), values: SqlUpdateValues | T): number | false;
|
|
184
|
+
|
|
152
185
|
exec(script: string): Promise<void>;
|
|
186
|
+
|
|
153
187
|
execSync(script: string): SmartDbDatabase | false;
|
|
188
|
+
|
|
154
189
|
getLastError(): any;
|
|
190
|
+
|
|
155
191
|
getLastBuildData(): SmartDbSqlBuildData;
|
|
192
|
+
|
|
156
193
|
getDbQuote(): string;
|
|
194
|
+
|
|
157
195
|
getTableInfo(table: string): Promise<SmartDbTableInfo>;
|
|
196
|
+
|
|
158
197
|
toDate(d: Date | number | string): Date | null;
|
|
198
|
+
|
|
159
199
|
getDbConnector(): string | SmartDbConnector;
|
|
200
|
+
|
|
160
201
|
toDbTimestamp(d: Date | number): string;
|
|
202
|
+
|
|
161
203
|
toDbDate(d: Date | number): string;
|
|
204
|
+
|
|
162
205
|
hasConcurrentTransactions(): boolean;
|
|
206
|
+
|
|
163
207
|
closeSync(): boolean;
|
|
208
|
+
|
|
164
209
|
close(): Promise<void>;
|
|
165
210
|
}
|
|
211
|
+
|
|
166
212
|
export interface SmartDbDatabase {
|
|
213
|
+
// empty placeholder interface
|
|
167
214
|
}
|
|
215
|
+
|
|
168
216
|
export interface SmartDbConnector {
|
|
169
|
-
|
|
170
|
-
|
|
217
|
+
// empty placeholder interface
|
|
218
|
+
}
|