@egi/smart-db 2.3.2 → 2.3.4
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/drivers/smart-db-better-sqlite3.d.ts +36 -0
- package/drivers/smart-db-better-sqlite3.js +1 -0
- package/drivers/smart-db-mysql.d.ts +26 -0
- package/drivers/smart-db-mysql.js +1 -0
- package/drivers/smart-db-mysql2.d.ts +26 -0
- package/drivers/smart-db-mysql2.js +1 -0
- package/drivers/smart-db-sqlite3.d.ts +30 -0
- package/drivers/smart-db-sqlite3.js +1 -0
- package/helpers/extract-db-api.d.ts +1 -0
- package/helpers/extract-db-api.js +1 -0
- package/models/abstract-model.d.ts +22 -0
- package/models/abstract-model.js +1 -0
- package/models/smart-db-core-table-model.d.ts +35 -0
- package/models/smart-db-core-table-model.js +1 -0
- package/models/smart-db-dictionary.d.ts +13 -0
- package/models/smart-db-dictionary.js +1 -0
- package/models/smart-db-log-model.d.ts +65 -0
- package/models/smart-db-log-model.js +1 -0
- package/models/smart-db-version-model.d.ts +65 -0
- package/models/smart-db-version-model.js +1 -0
- package/models/smart-db-version-view-model.d.ts +71 -0
- package/models/smart-db-version-view-model.js +1 -0
- package/models/sqlite-master-model.d.ts +36 -0
- package/models/sqlite-master-model.js +1 -0
- package/models/sqlite-sequence-model.d.ts +24 -0
- package/models/sqlite-sequence-model.js +1 -0
- package/package.json +1 -1
- package/{src/smart-db-api.ts → smart-db-api.d.ts} +0 -3
- package/smart-db-api.js +1 -0
- package/smart-db-globals.d.ts +22 -0
- package/smart-db-globals.js +1 -0
- package/{src/smart-db-interfaces.ts → smart-db-interfaces.d.ts} +34 -82
- package/smart-db-interfaces.js +1 -0
- package/smart-db-log.d.ts +58 -0
- package/smart-db-log.js +1 -0
- package/smart-db-sql-build-data.d.ts +9 -0
- package/smart-db-sql-build-data.js +1 -0
- package/smart-db-upgrade-manager.d.ts +13 -0
- package/smart-db-upgrade-manager.js +1 -0
- package/smart-db.d.ts +67 -0
- package/smart-db.js +1 -0
- package/.eslintrc.json +0 -212
- package/README.md +0 -2
- package/bin/copy-assets +0 -6
- package/src/drivers/smart-db-better-sqlite3.ts +0 -284
- package/src/drivers/smart-db-mysql.ts +0 -159
- package/src/drivers/smart-db-mysql2.ts +0 -159
- package/src/drivers/smart-db-sqlite3.ts +0 -198
- package/src/helpers/extract-db-api.ts +0 -465
- package/src/helpers/terser-tree.ts +0 -39
- package/src/models/abstract-model.ts +0 -209
- package/src/models/smart-db-core-table-model.ts +0 -161
- package/src/models/smart-db-dictionary.ts +0 -28
- package/src/models/smart-db-log-model.ts +0 -316
- package/src/models/smart-db-version-model.ts +0 -316
- package/src/models/smart-db-version-view-model.ts +0 -347
- package/src/models/sqlite-master-model.ts +0 -140
- package/src/models/sqlite-sequence-model.ts +0 -91
- package/src/smart-db-globals.ts +0 -136
- package/src/smart-db-log.ts +0 -262
- package/src/smart-db-sql-build-data.ts +0 -28
- package/src/smart-db-upgrade-manager.ts +0 -171
- package/src/smart-db.ts +0 -854
- package/test/data/sql-engine-tests.json +0 -232
- package/test/db/mysql/database-init.sql +0 -11
- package/test/db/sqlite3/database-init.sql +0 -11
- package/test/exer.js +0 -28
- package/test/model/smart-db-dictionary.ts +0 -19
- package/test/model/test-table-model.ts +0 -214
- package/test/test.js +0 -273
- package/test/test2.js +0 -252
- package/tsconfig.json +0 -32
- package/tsconfig.pro.json +0 -32
- package/tsconfig.test-model.json +0 -23
|
@@ -1,140 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,91 +0,0 @@
|
|
|
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
|
-
}
|
package/src/smart-db-globals.ts
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
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
|
-
}
|
package/src/smart-db-log.ts
DELETED
|
@@ -1,262 +0,0 @@
|
|
|
1
|
-
// noinspection JSUnusedGlobalSymbols
|
|
2
|
-
|
|
3
|
-
import _ from "lodash";
|
|
4
|
-
import {SmartDbLogModel, SmartDbLogModelData} from "./models/smart-db-log-model";
|
|
5
|
-
import {SmartDbDateRegexp, SmartDbTimestampRegexp, toSmartDbTimestamp} from "./smart-db-globals";
|
|
6
|
-
import {SmartDbApi} from "./smart-db-interfaces";
|
|
7
|
-
|
|
8
|
-
export const SmartDbLogLocation = {
|
|
9
|
-
Frontend: "frontend",
|
|
10
|
-
Backend: "backend",
|
|
11
|
-
Database: "database",
|
|
12
|
-
UpgradeManager: "upgrade-manager",
|
|
13
|
-
Other: "other"
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export type SmartDbLogLocationType = typeof SmartDbLogLocation;
|
|
17
|
-
export type SmartDbLogLocationKey = keyof SmartDbLogLocationType;
|
|
18
|
-
export type SmartDbLogLocationValue = SmartDbLogLocationType[keyof SmartDbLogLocationType];
|
|
19
|
-
|
|
20
|
-
export const SeverityCode = {
|
|
21
|
-
Fatal: "F", Error: "E", Warning: "W", Info: "I", Debug: "D", Local: "L"
|
|
22
|
-
} as const;
|
|
23
|
-
|
|
24
|
-
export type SeverityCodeType = typeof SeverityCode;
|
|
25
|
-
export type SeverityCodeKey = keyof SeverityCodeType;
|
|
26
|
-
export type SeverityCodeValue = SeverityCodeType[keyof SeverityCodeType];
|
|
27
|
-
|
|
28
|
-
export const SeverityLevel = {
|
|
29
|
-
None: 0, Fatal: 1, Error: 2, Warning: 3, Info: 4, Debug: 5, All: 9
|
|
30
|
-
} as const;
|
|
31
|
-
|
|
32
|
-
export type SeverityLevelType = typeof SeverityLevel;
|
|
33
|
-
export type SeverityLevelKey = keyof SeverityLevelType;
|
|
34
|
-
export type SeverityLevelValue = SeverityLevelType[keyof SeverityLevelType];
|
|
35
|
-
|
|
36
|
-
export class SmartDbLog {
|
|
37
|
-
private db: SmartDbApi;
|
|
38
|
-
private dbLogLevel: SeverityLevelValue; // = SeverityLevel.Info;
|
|
39
|
-
private consoleLogLevel: SeverityLevelValue; //= SeverityLevel.Info;
|
|
40
|
-
private userId: string | number = null;
|
|
41
|
-
private dbLogging: boolean = true;
|
|
42
|
-
private isLogging: boolean = false;
|
|
43
|
-
|
|
44
|
-
constructor(db?: SmartDbApi) {
|
|
45
|
-
this.db = db;
|
|
46
|
-
this.dbLogLevel = SeverityLevel.Info;
|
|
47
|
-
this.consoleLogLevel = SeverityLevel.Info;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// noinspection JSUnusedGlobalSymbols
|
|
51
|
-
public getConsoleLogLevel(): SeverityLevelValue {
|
|
52
|
-
return this.consoleLogLevel;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
public setConsoleLogLevel(consoleLogLevel: SeverityLevelValue): void {
|
|
56
|
-
this.consoleLogLevel = consoleLogLevel;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// noinspection JSUnusedGlobalSymbols
|
|
60
|
-
public getDbLogLevel(): SeverityLevelValue {
|
|
61
|
-
return this.dbLogLevel;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
public setDbLogLevel(dbLogLevel: SeverityLevelValue): void {
|
|
65
|
-
this.dbLogLevel = dbLogLevel;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
public setDb(db: SmartDbApi): void {
|
|
69
|
-
this.db = db;
|
|
70
|
-
|
|
71
|
-
if (this.db) {
|
|
72
|
-
this.db.exists(SmartDbLogModel).then((exists) => {
|
|
73
|
-
this.dbLogging = exists;
|
|
74
|
-
}).catch((err) => {
|
|
75
|
-
throw err;
|
|
76
|
-
});
|
|
77
|
-
} else {
|
|
78
|
-
this.dbLogging = false;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
public setUserId(userId: string | number): void {
|
|
83
|
-
this.userId = userId;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
public setDbLogging(dbLogging: boolean) {
|
|
87
|
-
this.dbLogging = dbLogging;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
public getDbLogging(): boolean {
|
|
91
|
-
return this.dbLogging;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
public logFatal(type: SmartDbLogLocationValue, location: string, message: string | Error, data?: any): void {
|
|
95
|
-
this.writeLog(type, location, SeverityCode.Fatal, message, data);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
public logError(type: SmartDbLogLocationValue, location: string, message: string | Error, data?: any): void {
|
|
99
|
-
this.writeLog(type, location, SeverityCode.Error, message, data);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
public logWarning(type: SmartDbLogLocationValue, location: string, message: string | Error, data?: any): void {
|
|
103
|
-
this.writeLog(type, location, SeverityCode.Warning, message, data);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
public logInfo(type: SmartDbLogLocationValue, location: string, message: string | Error, data?: any): void {
|
|
107
|
-
this.writeLog(type, location, SeverityCode.Info, message, data);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
public logDebug(type: SmartDbLogLocationValue, location: string, message: string | Error, data?: any): void {
|
|
111
|
-
this.writeLog(type, location, SeverityCode.Debug, message, data);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// noinspection OverlyComplexFunctionJS,FunctionTooLongJS
|
|
115
|
-
public writeLog(type: SmartDbLogLocationValue, location: string, severity: SeverityCodeValue, message: string | Error, data: unknown, timestamp?: Date | string | number): void {
|
|
116
|
-
try {
|
|
117
|
-
if (this.isLogging) {
|
|
118
|
-
console.error("recursive logging error:", type, location, severity, message, data);
|
|
119
|
-
} else {
|
|
120
|
-
this.isLogging = true;
|
|
121
|
-
|
|
122
|
-
let logLevel: SeverityLevelValue = SeverityLevel.None;
|
|
123
|
-
let logFn: (message?: any, ...args: any[]) => void;
|
|
124
|
-
|
|
125
|
-
switch (severity) {
|
|
126
|
-
case SeverityCode.Fatal:
|
|
127
|
-
logFn = console.error;
|
|
128
|
-
logLevel = SeverityLevel.Fatal;
|
|
129
|
-
break;
|
|
130
|
-
|
|
131
|
-
case SeverityCode.Error:
|
|
132
|
-
logFn = console.error;
|
|
133
|
-
logLevel = SeverityLevel.Error;
|
|
134
|
-
break;
|
|
135
|
-
|
|
136
|
-
case SeverityCode.Warning:
|
|
137
|
-
logFn = console.warn;
|
|
138
|
-
logLevel = SeverityLevel.Warning;
|
|
139
|
-
break;
|
|
140
|
-
|
|
141
|
-
case SeverityCode.Info:
|
|
142
|
-
logFn = console.info;
|
|
143
|
-
logLevel = SeverityLevel.Info;
|
|
144
|
-
break;
|
|
145
|
-
|
|
146
|
-
case SeverityCode.Debug:
|
|
147
|
-
logFn = console.debug;
|
|
148
|
-
logLevel = SeverityLevel.Debug;
|
|
149
|
-
break;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
let timestampString: string;
|
|
153
|
-
|
|
154
|
-
if (_.isString(timestamp)) {
|
|
155
|
-
const timestampMatch = timestamp.match(SmartDbTimestampRegexp);
|
|
156
|
-
const dateMatch = timestampMatch && timestamp.match(SmartDbDateRegexp);
|
|
157
|
-
if (timestampMatch) {
|
|
158
|
-
timestampString = timestampMatch[1];
|
|
159
|
-
} else if (dateMatch) {
|
|
160
|
-
timestampString = dateMatch[1] + ".000";
|
|
161
|
-
} else {
|
|
162
|
-
timestampString = toSmartDbTimestamp(new Date());
|
|
163
|
-
}
|
|
164
|
-
} else if (_.isDate(timestamp)) {
|
|
165
|
-
timestampString = toSmartDbTimestamp(timestamp);
|
|
166
|
-
} else if (_.isNumber(timestamp)) {
|
|
167
|
-
timestampString = toSmartDbTimestamp(new Date(timestamp));
|
|
168
|
-
} else {
|
|
169
|
-
timestampString = toSmartDbTimestamp(new Date());
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
if (_.isObjectLike(message)) {
|
|
173
|
-
if (message instanceof Error) {
|
|
174
|
-
if (!data) {
|
|
175
|
-
data = message;
|
|
176
|
-
}
|
|
177
|
-
message = message.message;
|
|
178
|
-
} else {
|
|
179
|
-
try {
|
|
180
|
-
message = JSON.stringify(message);
|
|
181
|
-
} catch (err) {
|
|
182
|
-
message = message.toString();
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
} else if (typeof <any>message === "boolean") {
|
|
186
|
-
message = message ? "<true>" : "<false>";
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
if (data) {
|
|
190
|
-
if (data instanceof Error) {
|
|
191
|
-
data = data.stack;
|
|
192
|
-
} else if (_.isObject(data)) {
|
|
193
|
-
try {
|
|
194
|
-
data = JSON.stringify(data);
|
|
195
|
-
} catch (err) {
|
|
196
|
-
data = data.toString();
|
|
197
|
-
|
|
198
|
-
if (this.db && this.dbLogLevel >= logLevel) {
|
|
199
|
-
console.error("unable to stringify log data", data);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
} else if (data.toString) {
|
|
203
|
-
data = data.toString();
|
|
204
|
-
} else {
|
|
205
|
-
data = null;
|
|
206
|
-
|
|
207
|
-
if (this.db && this.dbLogLevel >= logLevel) {
|
|
208
|
-
console.error("unable to store log data", data);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
if (data) {
|
|
213
|
-
message += ` (${data})`;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
if (this.consoleLogLevel >= logLevel) {
|
|
218
|
-
const shortType = type.substr(0, 1).toUpperCase();
|
|
219
|
-
if (type == SmartDbLogLocation.Database && this.db) {
|
|
220
|
-
logFn(`${severity}-${shortType}-${timestampString}: last statement:`, this.db.getLastBuildData());
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
logFn(`${severity}-${shortType}-${timestampString}: ${message}`);
|
|
224
|
-
|
|
225
|
-
if (this.dbLogging) {
|
|
226
|
-
try {
|
|
227
|
-
if (type == SmartDbLogLocation.Database) {
|
|
228
|
-
if (!data) {
|
|
229
|
-
data = JSON.stringify(this.db.getLastBuildData());
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
this.db.insertSync(SmartDbLogModel, <SmartDbLogModelData>{
|
|
234
|
-
severity: severity,
|
|
235
|
-
type: type,
|
|
236
|
-
location: location,
|
|
237
|
-
info: message || "<empty>",
|
|
238
|
-
data: data as string,
|
|
239
|
-
user: this.userId,
|
|
240
|
-
timestamp: new Date(timestampString)
|
|
241
|
-
});
|
|
242
|
-
} catch (error) {
|
|
243
|
-
console.error(`F-B-${timestampString}: unable to write the log statement below to database`, error, error.code, error.name, error.message);
|
|
244
|
-
try {
|
|
245
|
-
logFn(`F-B-${timestampString}: last statement:`, this.db.getLastBuildData());
|
|
246
|
-
} catch {
|
|
247
|
-
logFn(`F-B-${timestampString}: last statement not available`);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
this.isLogging = false;
|
|
255
|
-
} catch (error) {
|
|
256
|
-
this.isLogging = false;
|
|
257
|
-
console.error("fatal logging error", error);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
export const smartDbLog: SmartDbLog = new SmartDbLog();
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import _ from "lodash";
|
|
2
|
-
import {SmartDbSqlBuildDataResults, SqlValueType} from "./smart-db-interfaces";
|
|
3
|
-
|
|
4
|
-
export class SmartDbSqlBuildData {
|
|
5
|
-
public sql: string;
|
|
6
|
-
public values: SqlValueType[];
|
|
7
|
-
|
|
8
|
-
constructor(sql?: string, values?: SqlValueType[]) {
|
|
9
|
-
this.sql = sql || "";
|
|
10
|
-
this.values = values || [];
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
public append(data: SmartDbSqlBuildData): void {
|
|
14
|
-
this.sql = this.sql.trim() + " " + data.sql.trim();
|
|
15
|
-
this.values = _.concat(this.values, data.values);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
public toString() {
|
|
19
|
-
return this.sql + " <= (" + this.values.join(", ") + ")";
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
public results(): SmartDbSqlBuildDataResults {
|
|
23
|
-
return {
|
|
24
|
-
sql: this.sql,
|
|
25
|
-
values: this.values
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
}
|