@egi/smart-db 2.3.0 → 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.
Files changed (74) hide show
  1. package/.eslintrc.json +212 -0
  2. package/README.md +2 -0
  3. package/bin/copy-assets +6 -0
  4. package/package.json +4 -4
  5. package/src/drivers/smart-db-better-sqlite3.ts +284 -0
  6. package/src/drivers/smart-db-mysql.ts +159 -0
  7. package/src/drivers/smart-db-mysql2.ts +159 -0
  8. package/src/drivers/smart-db-sqlite3.ts +198 -0
  9. package/src/helpers/extract-db-api.ts +465 -0
  10. package/src/helpers/terser-tree.ts +39 -0
  11. package/src/models/abstract-model.ts +209 -0
  12. package/src/models/smart-db-core-table-model.ts +161 -0
  13. package/src/models/smart-db-dictionary.ts +28 -0
  14. package/src/models/smart-db-log-model.ts +316 -0
  15. package/src/models/smart-db-version-model.ts +316 -0
  16. package/src/models/smart-db-version-view-model.ts +347 -0
  17. package/src/models/sqlite-master-model.ts +140 -0
  18. package/src/models/sqlite-sequence-model.ts +91 -0
  19. package/{smart-db-api.d.ts → src/smart-db-api.ts} +3 -0
  20. package/src/smart-db-globals.ts +136 -0
  21. package/{smart-db-interfaces.d.ts → src/smart-db-interfaces.ts} +82 -34
  22. package/src/smart-db-log.ts +262 -0
  23. package/src/smart-db-sql-build-data.ts +28 -0
  24. package/src/smart-db-upgrade-manager.ts +171 -0
  25. package/src/smart-db.ts +854 -0
  26. package/test/data/sql-engine-tests.json +232 -0
  27. package/test/db/mysql/database-init.sql +11 -0
  28. package/test/db/sqlite3/database-init.sql +11 -0
  29. package/test/exer.js +28 -0
  30. package/test/model/smart-db-dictionary.ts +19 -0
  31. package/test/model/test-table-model.ts +214 -0
  32. package/test/test.js +273 -0
  33. package/test/test2.js +252 -0
  34. package/tsconfig.json +32 -0
  35. package/tsconfig.pro.json +32 -0
  36. package/tsconfig.test-model.json +23 -0
  37. package/drivers/smart-db-better-sqlite3.d.ts +0 -36
  38. package/drivers/smart-db-better-sqlite3.js +0 -1
  39. package/drivers/smart-db-mysql.d.ts +0 -26
  40. package/drivers/smart-db-mysql.js +0 -1
  41. package/drivers/smart-db-mysql2.d.ts +0 -26
  42. package/drivers/smart-db-mysql2.js +0 -1
  43. package/drivers/smart-db-sqlite3.d.ts +0 -30
  44. package/drivers/smart-db-sqlite3.js +0 -1
  45. package/helpers/extract-db-api.d.ts +0 -1
  46. package/helpers/extract-db-api.js +0 -1
  47. package/models/abstract-model.d.ts +0 -22
  48. package/models/abstract-model.js +0 -1
  49. package/models/smart-db-core-table-model.d.ts +0 -35
  50. package/models/smart-db-core-table-model.js +0 -1
  51. package/models/smart-db-dictionary.d.ts +0 -13
  52. package/models/smart-db-dictionary.js +0 -1
  53. package/models/smart-db-log-model.d.ts +0 -65
  54. package/models/smart-db-log-model.js +0 -1
  55. package/models/smart-db-version-model.d.ts +0 -65
  56. package/models/smart-db-version-model.js +0 -1
  57. package/models/smart-db-version-view-model.d.ts +0 -71
  58. package/models/smart-db-version-view-model.js +0 -1
  59. package/models/sqlite-master-model.d.ts +0 -36
  60. package/models/sqlite-master-model.js +0 -1
  61. package/models/sqlite-sequence-model.d.ts +0 -24
  62. package/models/sqlite-sequence-model.js +0 -1
  63. package/smart-db-api.js +0 -1
  64. package/smart-db-globals.d.ts +0 -22
  65. package/smart-db-globals.js +0 -1
  66. package/smart-db-interfaces.js +0 -1
  67. package/smart-db-log.d.ts +0 -58
  68. package/smart-db-log.js +0 -1
  69. package/smart-db-sql-build-data.d.ts +0 -9
  70. package/smart-db-sql-build-data.js +0 -1
  71. package/smart-db-upgrade-manager.d.ts +0 -13
  72. package/smart-db-upgrade-manager.js +0 -1
  73. package/smart-db.d.ts +0 -67
  74. package/smart-db.js +0 -1
package/test/test.js ADDED
@@ -0,0 +1,273 @@
1
+ const {SmartDbMysql} = require("../dist/drivers/smart-db-mysql");
2
+ const {SmartDbSqlite3} = require("../dist/drivers/smart-db-sqlite3");
3
+ const {SmartDbBetterSqlite3} = require("../dist/drivers/smart-db-better-sqlite3");
4
+ const {SmartDbDictionary} = require("./model/smart-db-dictionary");
5
+
6
+ const {COUNT, COALESCE, FIELD, VALUE} = require("../dist/smart-db-interface");
7
+
8
+ const assert = require("assert");
9
+ const {describe, it, done, before, after} = require("mocha");
10
+ const {TestTableModel} = require("./model/test-table-model");
11
+ const fs = require("fs");
12
+ const _ = require("lodash");
13
+
14
+ const LEARN = true;
15
+ const engineTestConfigFile = "test/data/sql-engine-tests.json";
16
+
17
+ let engineTestConfig;
18
+ try {
19
+ if (fs.existsSync(engineTestConfigFile)) {
20
+ engineTestConfig = JSON.parse(fs.readFileSync(engineTestConfigFile).toString());
21
+ } else {
22
+ engineTestConfig = null;
23
+ }
24
+ } catch (err) {
25
+ console.error("Unable to parse engine test configuration file");
26
+ console.error(err);
27
+ engineTestConfig = null;
28
+ }
29
+
30
+ function verify(buildData, dbType, testId) {
31
+ const testData = buildData.results();
32
+
33
+ if (LEARN && !engineTestConfig.results) {
34
+ engineTestConfig.results = {};
35
+ }
36
+
37
+ if (LEARN && !engineTestConfig.results[testId]) {
38
+ engineTestConfig.results[testId] = {};
39
+ }
40
+
41
+ if (LEARN || !engineTestConfig.results[testId][dbType]) {
42
+ engineTestConfig.results[testId][dbType] = testData;
43
+ } else {
44
+ assert(_.isEqual(testData, engineTestConfig.results[testId][dbType]), JSON.stringify({
45
+ id: testId,
46
+ expected: engineTestConfig.results[testId][dbType],
47
+ actual: testData
48
+ }, null, 2));
49
+ }
50
+ }
51
+
52
+ describe("Databases Setup", () => {
53
+ if (fs.existsSync("test/db/sqlite3.db")) {
54
+ fs.unlinkSync("test/db/sqlite3.db");
55
+ }
56
+
57
+ it("should return Sqlite3 DB handle for asynchronous tests", () => {
58
+ let db = new SmartDbSqlite3("test/db/sqlite3.db");
59
+ assert(!!db);
60
+
61
+ genericTests(db, "sqlite3", {
62
+ module: "smart-db-test",
63
+ sqlFilesDirectory: "test/db/sqlite3",
64
+ smartDbDictionary: SmartDbDictionary
65
+
66
+ });
67
+
68
+ sqlEngineTests(db, "sqlite3");
69
+ });
70
+
71
+ if (fs.existsSync("test/db/better-sqlite3.db")) {
72
+ fs.unlinkSync("test/db/better-sqlite3.db");
73
+ }
74
+
75
+ it("should return Better Sqlite3 DB handle for asynchronous tests", () => {
76
+ let db = new SmartDbBetterSqlite3("test/db/better-sqlite3.db");
77
+ assert(!!db);
78
+
79
+ genericTests(db, "better-sqlite3", {
80
+ module: "smart-db-test",
81
+ sqlFilesDirectory: "test/db/sqlite3",
82
+ smartDbDictionary: SmartDbDictionary
83
+ });
84
+ });
85
+
86
+ if (fs.existsSync("test/db/better-sqlite3-sync.db")) {
87
+ fs.unlinkSync("test/db/better-sqlite3-sync.db");
88
+ }
89
+
90
+ it("should return Better Sqlite3 DB handle for synchronous tests", () => {
91
+ let db = new SmartDbBetterSqlite3("test/db/better-sqlite3-sync.db");
92
+ assert(!!db);
93
+
94
+ genericSyncTests(db, "better-sqlite3", {
95
+ module: "smart-db-test",
96
+ sqlFilesDirectory: "test/db/sqlite3",
97
+ smartDbDictionary: SmartDbDictionary
98
+ });
99
+
100
+ sqlEngineTests(db, "better-sqlite3");
101
+ });
102
+
103
+ let mysqlDb;
104
+ it("should return Mysql DB handle", () => {
105
+ mysqlDb = new SmartDbMysql({
106
+ host: "localhost",
107
+ user: "root",
108
+ password: "foobar",
109
+ multipleStatements: true
110
+ });
111
+
112
+ assert(!!mysqlDb);
113
+ });
114
+
115
+ it("should cleanup previous test schema", () => {
116
+ return mysqlDb.exec("drop schema if exists `smart-db-test`");
117
+ });
118
+
119
+ it("should create test schema", () => {
120
+ return mysqlDb.exec("create schema `smart-db-test`");
121
+ });
122
+
123
+ it("should select test schema", () => {
124
+ return mysqlDb.exec("use `smart-db-test`").then(() => {
125
+ genericTests(mysqlDb, "mysql", {
126
+ module: "smart-db-test",
127
+ sqlFilesDirectory: "test/db/mysql",
128
+ smartDbDictionary: SmartDbDictionary
129
+ });
130
+
131
+ sqlEngineTests(mysqlDb, "mysql");
132
+ });
133
+ });
134
+ });
135
+
136
+ function genericTests(db, dbType, initOptions) {
137
+ describe("Common Asynchronous Tests for " + dbType, () => {
138
+ let moduleVersions;
139
+ let id1;
140
+
141
+ it("should run init script", () => {
142
+ return db.initDb(initOptions).then((versions) => {
143
+ moduleVersions = versions;
144
+ });
145
+ });
146
+
147
+ it("should contain module 'smart-db-core'", () => {
148
+ const hasCore = moduleVersions.find((version) => {
149
+ return version.module == "smart-db-core";
150
+ });
151
+
152
+ assert(!!hasCore);
153
+ });
154
+
155
+ it("should contain module 'smart-db-test'", () => {
156
+ const hasTest = moduleVersions.find((version) => {
157
+ return version.module == "smart-db-core";
158
+ });
159
+
160
+ assert(!!hasTest);
161
+ });
162
+
163
+ it("should insert value", () => {
164
+ return db.insert(TestTableModel, {
165
+ string: "test-string",
166
+ number: 42,
167
+ date: new Date(),
168
+ boolean: true
169
+ }).then((id) => {
170
+ id1 = id;
171
+ assert(id1 !== false, db.getLastError());
172
+ assert(id1 > 0, db.getLastBuildData().toString());
173
+ });
174
+ });
175
+
176
+ it("should select row", () => {
177
+ return db.getFirst(TestTableModel, {
178
+ id: id1
179
+ }).then((row) => {
180
+ assert(row !== false, db.getLastError());
181
+ assert(!!row, db.getLastBuildData().toString());
182
+ assert(typeof row.string == "string", "string type " + (typeof row.string));
183
+ assert(typeof row.number == "number", "number type " + (typeof row.number));
184
+ assert(typeof row.date == "object", "date type " + (typeof row.date));
185
+ assert(typeof row.boolean == "boolean", "boolean type " + (typeof row.boolean));
186
+ });
187
+ });
188
+
189
+ it("should truncate table", () => {
190
+ return db.delete(TestTableModel).then((n) => {
191
+ assert(n !== false, db.getLastError());
192
+ assert(n >= 0, db.getLastBuildData().toString());
193
+ });
194
+ });
195
+ });
196
+ }
197
+
198
+
199
+ function genericSyncTests(db, dbType, initOptions) {
200
+ describe("Common Synchronous Tests for " + dbType, () => {
201
+ let moduleVersions;
202
+ let id1;
203
+
204
+ it("should run init script", () => {
205
+ return db.initDb(initOptions).then((versions) => {
206
+ moduleVersions = versions;
207
+ });
208
+ });
209
+
210
+ it("should contain module 'smart-db-core'", () => {
211
+ const hasCore = moduleVersions.find((version) => {
212
+ return version.module == "smart-db-core";
213
+ });
214
+
215
+ assert(!!hasCore);
216
+ });
217
+
218
+ it("should contain module 'smart-db-test'", () => {
219
+ const hasTest = moduleVersions.find((version) => {
220
+ return version.module == "smart-db-core";
221
+ });
222
+
223
+ assert(!!hasTest);
224
+ });
225
+
226
+ it("should insert value", () => {
227
+ id1 = db.insertSync(TestTableModel, {
228
+ string: "test-string",
229
+ number: 42,
230
+ date: new Date(),
231
+ boolean: true
232
+ });
233
+ assert(id1 !== false, db.getLastError());
234
+ assert(id1 > 0, db.getLastBuildData().toString());
235
+ });
236
+
237
+ it("should select row", () => {
238
+ let row = db.getFirstSync(TestTableModel, {
239
+ id: id1
240
+ });
241
+
242
+ assert(row !== false, db.getLastError());
243
+ assert(!!row, db.getLastBuildData().toString());
244
+ assert(typeof row.string == "string", "string type " + (typeof row.string));
245
+ assert(typeof row.number == "number", "number type " + (typeof row.number));
246
+ assert(typeof row.date == "object", "date type " + (typeof row.date));
247
+ assert(typeof row.boolean == "boolean", "boolean type " + (typeof row.boolean));
248
+ });
249
+
250
+ it("should truncate table", () => {
251
+ let n = db.deleteSync(TestTableModel);
252
+ assert(n !== false, db.getLastError());
253
+ assert(n >= 0, db.getLastBuildData().toString());
254
+ });
255
+ });
256
+ }
257
+
258
+ function sqlEngineTests(db, dbType) {
259
+ if (engineTestConfig && engineTestConfig.tests) {
260
+ describe("SQL Engine Tests for " + dbType, () => {
261
+ _.forEach(engineTestConfig.tests, (testData, testId) => {
262
+ it("should perform test: " + testId, () => {
263
+ let buildData = db.buildSelectStatement(testData.table, testData.query);
264
+ verify(buildData, dbType, testId);
265
+ });
266
+ });
267
+
268
+ it("finalize SQL engine results", () => {
269
+ fs.writeFileSync(engineTestConfigFile, JSON.stringify(engineTestConfig, null, 2));
270
+ });
271
+ });
272
+ }
273
+ }
package/test/test2.js ADDED
@@ -0,0 +1,252 @@
1
+ const {SmartDbMysql} = require("../dist/drivers/smart-db-mysql");
2
+ const {SmartDbBetterSqlite3} = require("../dist/drivers/smart-db-better-sqlite3");
3
+ const {smartDbLog} = require("../dist");
4
+
5
+ const {COUNT, COALESCE, FIELD, VALUE} = require("../dist/smart-db-interface");
6
+
7
+ const assert = require("assert");
8
+ const {describe, it, done, before, after} = require("mocha");
9
+ const {TestTableModel} = require("./model/test-table-model");
10
+ const fs = require("fs");
11
+
12
+ describe("Databases Setup", () => {
13
+ if (fs.existsSync("test/test.sqlite3")) {
14
+ fs.unlinkSync("test/test.sqlite3");
15
+ }
16
+
17
+ it("should return Sqlite3 DB handle for asynchronous tests", () => {
18
+ let db = new SmartDbBetterSqlite3("test/test.sqlite3");
19
+ assert(!!db);
20
+
21
+ genericTests(db, "sqlite3", {
22
+ module: "smart-db-test",
23
+ sqlFilesDirectory: "test/sqlite3"
24
+ });
25
+ });
26
+
27
+ it("should return Sqlite3 DB handle for synchronous tests", () => {
28
+ let db = new SmartDbBetterSqlite3("test/test-sync.sqlite3");
29
+ assert(!!db);
30
+
31
+ genericSyncTests(db, "sqlite3", {
32
+ module: "smart-db-test",
33
+ sqlFilesDirectory: "test/sqlite3"
34
+ });
35
+ });
36
+
37
+ let mysqlDb;
38
+ it("should return Mysql DB handle", () => {
39
+ let db = new SmartDbMysql({
40
+ host: "localhost",
41
+ user: "root",
42
+ password: "foobar",
43
+ multipleStatements: true
44
+ });
45
+
46
+ assert(!!db);
47
+ });
48
+
49
+ it("should cleanup previous test schema", () => {
50
+ return mysqlDb.exec("drop schema if exists `smart-db-test`");
51
+ });
52
+
53
+ it("should create test schema", () => {
54
+ return mysqlDb.exec("create schema `smart-db-test`");
55
+ });
56
+
57
+ it("should select test schema", () => {
58
+ return mysqlDb.exec("use `smart-db-test`").then(() => {
59
+ genericTests(mysqlDb, "mysql", {
60
+ module: "smart-db-test",
61
+ sqlFilesDirectory: "test/mysql"
62
+ });
63
+ });
64
+ });
65
+ });
66
+
67
+
68
+ function genericTests(db, dbType, initOptions) {
69
+ describe("Common Asynchronous Tests for " + dbType, () => {
70
+ let moduleVersions;
71
+ let id1;
72
+
73
+ it("should run init script", () => {
74
+ return db.initDb(initOptions).then((versions) => {
75
+ moduleVersions = versions;
76
+ });
77
+ });
78
+
79
+ it("should contain module 'smart-db-core'", () => {
80
+ const hasCore = moduleVersions.find((version) => {
81
+ return version.module == "smart-db-core";
82
+ });
83
+
84
+ assert(!!hasCore);
85
+ });
86
+
87
+ it("should contain module 'smart-db-test'", () => {
88
+ const hasTest = moduleVersions.find((version) => {
89
+ return version.module == "smart-db-core";
90
+ });
91
+
92
+ assert(!!hasTest);
93
+ });
94
+
95
+ it("should insert value", () => {
96
+ return db.insert(TestTableModel, {
97
+ string: "test-string",
98
+ number: 42,
99
+ date: new Date(),
100
+ boolean: true
101
+ }).then((id) => {
102
+ id1 = id;
103
+ assert(id1 !== false, db.getLastError());
104
+ assert(id1 > 0, db.getLastBuildData().toString());
105
+ });
106
+ });
107
+
108
+ it("show generate correct SQL", () => {
109
+ return db.getFirst(TestTableModel, {
110
+ expression: [{
111
+ compare: "number",
112
+ with: 42
113
+ }, {
114
+ compare: "number",
115
+ with: 21
116
+ }],
117
+ or: {
118
+ number: 22,
119
+ expression: [{
120
+ compare: "number",
121
+ with: 42
122
+ }, {
123
+ compare: "string",
124
+ with: VALUE("ss")
125
+ }]
126
+ }
127
+ }, [
128
+ "number",
129
+ FIELD("boolean", "t1"),
130
+ VALUE("ss", "test"),
131
+ COUNT("*", "bla"),
132
+ COALESCE(["number", 43], "xset")
133
+ ]).then((row) => {
134
+ console.log(db.getLastBuildData());
135
+ });
136
+ });
137
+
138
+ it("should select row", () => {
139
+ return db.getFirst(TestTableModel, {
140
+ id: id1
141
+ }).then((row) => {
142
+ assert(row !== false, db.getLastError());
143
+ assert(!!row, db.getLastBuildData().toString());
144
+ assert(typeof row.string == "string", "string type " + (typeof row.string));
145
+ assert(typeof row.number == "number", "number type " + (typeof row.number));
146
+ assert(typeof row.date == "object", "date type " + (typeof row.date));
147
+ assert(typeof row.boolean == "boolean", "boolean type " + (typeof row.boolean));
148
+ });
149
+ });
150
+
151
+ it("should truncate table", () => {
152
+ return db.delete(TestTableModel).then((n) => {
153
+ assert(n !== false, db.getLastError());
154
+ assert(n >= 0, db.getLastBuildData().toString());
155
+ });
156
+ });
157
+ });
158
+ }
159
+
160
+
161
+ function genericSyncTests(db, dbType, initOptions) {
162
+ describe("Common Synchronous Tests for " + dbType, () => {
163
+ let moduleVersions;
164
+ let id1;
165
+
166
+ it("should run init script", () => {
167
+ return db.initDb(initOptions).then((versions) => {
168
+ moduleVersions = versions;
169
+ });
170
+ });
171
+
172
+ it("should contain module 'smart-db-core'", () => {
173
+ const hasCore = moduleVersions.find((version) => {
174
+ return version.module == "smart-db-core";
175
+ });
176
+
177
+ assert(!!hasCore);
178
+ });
179
+
180
+ it("should contain module 'smart-db-test'", () => {
181
+ const hasTest = moduleVersions.find((version) => {
182
+ return version.module == "smart-db-core";
183
+ });
184
+
185
+ assert(!!hasTest);
186
+ });
187
+
188
+ it("should insert value", () => {
189
+ return db.insertSync(TestTableModel, {
190
+ string: "test-string",
191
+ number: 42,
192
+ date: new Date(),
193
+ boolean: true
194
+ }).then((id) => {
195
+ id1 = id;
196
+ assert(id1 !== false, db.getLastError());
197
+ assert(id1 > 0, db.getLastBuildData().toString());
198
+ });
199
+ });
200
+
201
+ it("show generate correct SQL", () => {
202
+ return db.getFirst(TestTableModel, {
203
+ expression: [{
204
+ compare: "number",
205
+ with: 42
206
+ }, {
207
+ compare: "number",
208
+ with: 21
209
+ }],
210
+ or: {
211
+ number: 22,
212
+ expression: [{
213
+ compare: "number",
214
+ with: 42
215
+ }, {
216
+ compare: "string",
217
+ with: VALUE("ss")
218
+ }]
219
+ }
220
+ }, [
221
+ "number",
222
+ FIELD("boolean", "t1"),
223
+ VALUE("ss", "test"),
224
+ COUNT("*", "bla"),
225
+ COALESCE(["number", 43], "xset")
226
+ ]).then((row) => {
227
+ console.log(db.getLastBuildData());
228
+ });
229
+ });
230
+
231
+ it("should select row", () => {
232
+ return db.getFirst(TestTableModel, {
233
+ id: id1
234
+ }).then((row) => {
235
+ assert(row !== false, db.getLastError());
236
+ assert(!!row, db.getLastBuildData().toString());
237
+ assert(typeof row.string == "string", "string type " + (typeof row.string));
238
+ assert(typeof row.number == "number", "number type " + (typeof row.number));
239
+ assert(typeof row.date == "object", "date type " + (typeof row.date));
240
+ assert(typeof row.boolean == "boolean", "boolean type " + (typeof row.boolean));
241
+ });
242
+ });
243
+
244
+ it("should truncate table", () => {
245
+ return db.delete(TestTableModel).then((n) => {
246
+ assert(n !== false, db.getLastError());
247
+ assert(n >= 0, db.getLastBuildData().toString());
248
+ });
249
+ });
250
+ });
251
+ }
252
+
package/tsconfig.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "compileOnSave": false,
3
+ "compilerOptions": {
4
+ "baseUrl": ".",
5
+ "outDir": "dist",
6
+ "sourceMap": true,
7
+ "declaration": true,
8
+ "downlevelIteration": true,
9
+ "experimentalDecorators": true,
10
+ "moduleResolution": "node",
11
+ "module": "ES2020",
12
+ "inlineSources": true,
13
+ //"importHelpers": true,
14
+ "target": "ES2020",
15
+ "allowSyntheticDefaultImports": true,
16
+ //"esModuleInterop": true,
17
+ "lib": [
18
+ "es6",
19
+ "ES2020",
20
+ "dom"
21
+ ],
22
+ "paths": {
23
+ "*": [
24
+ "node_modules/*"
25
+ ]
26
+ }
27
+ },
28
+ "include": [
29
+ "src/*.ts",
30
+ "src/**/*.ts"
31
+ ]
32
+ }
@@ -0,0 +1,32 @@
1
+ {
2
+ "compileOnSave": false,
3
+ "compilerOptions": {
4
+ "baseUrl": ".",
5
+ "outDir": "dist",
6
+ "sourceMap": false,
7
+ "declaration": true,
8
+ "downlevelIteration": true,
9
+ "experimentalDecorators": true,
10
+ "moduleResolution": "node",
11
+ "module": "ES2020",
12
+ "inlineSources": false,
13
+ //"importHelpers": true,
14
+ "target": "ES2020",
15
+ "allowSyntheticDefaultImports": true,
16
+ //"esModuleInterop": true,
17
+ "lib": [
18
+ "es6",
19
+ "ES2020",
20
+ "dom"
21
+ ],
22
+ "paths": {
23
+ "*": [
24
+ "node_modules/*"
25
+ ]
26
+ }
27
+ },
28
+ "include": [
29
+ "src/*.ts",
30
+ "src/**/*.ts"
31
+ ]
32
+ }
@@ -0,0 +1,23 @@
1
+ {
2
+ "compileOnSave": false,
3
+ "compilerOptions": {
4
+ "module": "commonjs",
5
+ "esModuleInterop": true,
6
+ "target": "es5",
7
+ "noImplicitAny": true,
8
+ "moduleResolution": "node",
9
+ "declaration": true,
10
+ "sourceMap": true,
11
+ "baseUrl": ".",
12
+ "allowSyntheticDefaultImports": true,
13
+ "paths": {
14
+ "*": [
15
+ "node_modules/*"
16
+ ]
17
+ }
18
+ },
19
+ "include": [
20
+ "test/model/*.ts"
21
+ ]
22
+ }
23
+
@@ -1,36 +0,0 @@
1
- import BetterSqlite3 from "better-sqlite3";
2
- import { SmartDbSqlBuildData } from "../smart-db-sql-build-data";
3
- import { AbstractModel } from "../models/abstract-model";
4
- import { SmartDb } from "../smart-db";
5
- import { GenericModelData, SmartDbConnector, SmartDbRunResult, SmartDbTableInfo } from "../smart-db-interfaces";
6
- declare type VariableArgFunction = (...params: any[]) => any;
7
- export declare class SmartDbBetterSqlite3 extends SmartDb {
8
- protected db: BetterSqlite3.Database;
9
- constructor(connectorOrDb: string | BetterSqlite3.Database, options?: BetterSqlite3.Options);
10
- getDatabaseType(): string;
11
- getDbQuote(): string;
12
- getDbConnector(): string | SmartDbConnector;
13
- hasConcurrentTransactions(): boolean;
14
- exists<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), type?: "view" | "table" | "index", indexTableName?: string): Promise<boolean>;
15
- existsSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), type?: "view" | "table" | "index", indexTableName?: string): boolean;
16
- exec(script: string): Promise<any>;
17
- execSync(script: string): BetterSqlite3.Database | false;
18
- transaction<F extends VariableArgFunction>(fn: F): BetterSqlite3.Transaction | false;
19
- pragma(source: string, options?: BetterSqlite3.PragmaOptions): unknown;
20
- checkpoint(databaseName?: string): BetterSqlite3.Database | false;
21
- func(name: string, options: BetterSqlite3.RegistrationOptions, cb: VariableArgFunction): BetterSqlite3.Database | false;
22
- aggregate(name: string, options: BetterSqlite3.AggregateOptions): BetterSqlite3.Database | false;
23
- loadExtension(path: string): BetterSqlite3.Database | false;
24
- closeSync(): boolean;
25
- defaultSafeIntegers(toggleState?: boolean): BetterSqlite3.Database | false;
26
- backup(destinationFile: string, options?: BetterSqlite3.BackupOptions): Promise<BetterSqlite3.BackupMetadata> | false;
27
- getTableInfo(tableName: string): Promise<SmartDbTableInfo>;
28
- protected statementRun(buildData: SmartDbSqlBuildData, retry?: number): Promise<SmartDbRunResult>;
29
- protected statementRunSync(buildData: SmartDbSqlBuildData): SmartDbRunResult;
30
- protected statementGet(buildData: SmartDbSqlBuildData): Promise<any>;
31
- protected statementGetSync(buildData: SmartDbSqlBuildData): any;
32
- protected statementGetAll(buildData: SmartDbSqlBuildData): Promise<any[]>;
33
- protected statementGetAllSync(buildData: SmartDbSqlBuildData): any[];
34
- close(): Promise<void>;
35
- }
36
- export {};
@@ -1 +0,0 @@
1
- import BetterSqlite3 from"better-sqlite3";import _ from"lodash";import{SqliteMasterModel}from"../models/sqlite-master-model";import{SmartDb}from"../smart-db";export class SmartDbBetterSqlite3 extends SmartDb{constructor(t,e){_.isString(t)?(super(t),this.db=new BetterSqlite3(t,e)):(super(null),this.db=t,this.db.inTransaction)}getDatabaseType(){return"sqlite3"}getDbQuote(){return'"'}getDbConnector(){return this.dbConnector}hasConcurrentTransactions(){return!1}exists(t,e,s){return new Promise((r,n)=>{const a=_.isString(t)?t:t.getTableName();this.getFirst(SqliteMasterModel,{name:a,type:e,tblName:s}).then(t=>{null===t?n(this.getLastError()):r(!!t)}).catch(t=>{n(t)})})}existsSync(t,e,s){let r=!1;const n=_.isString(t)?t:t.getTableName(),a=this.getFirstSync(SqliteMasterModel,{name:n,type:e,tblName:s});if(!1===a)throw this.getLastError();return r=!!a,r}exec(t){return new Promise((e,s)=>{const r=this.execSync(t);!1===r?s("unable to execute script"):e(r)})}execSync(t){return this.saveExecute(()=>this.db.exec(t))}transaction(t){return this.saveExecute(()=>this.db.transaction(t))}pragma(t,e){return this.saveExecute(()=>this.db.pragma(t,e))}checkpoint(t){return this.saveExecute(()=>this.db.checkpoint(t))}func(t,e,s){return this.saveExecute(()=>{let r;return r=_.isFunction(e)?this.db.function(t,e):this.db.function(t,e,s),r})}aggregate(t,e){return this.saveExecute(()=>this.db.aggregate(t,e))}loadExtension(t){return this.saveExecute(()=>this.db.loadExtension(t))}closeSync(){return this.saveExecute(()=>(this.smartDbLog.setDb(null),!!this.db.close()))}defaultSafeIntegers(t){return this.saveExecute(()=>this.db.defaultSafeIntegers(t))}backup(t,e){return this.saveExecute(()=>this.db.backup(t,e))}getTableInfo(t){return new Promise(e=>{const s=this.pragma(`table_info(${t})`);let r=[];s&&(r=s.map(t=>({cid:t.cid,name:t.name,type:t.type,notNull:0!==t.notnull,defaultValue:t.dflt_value,isPk:0!==t.pk})),e({name:t,fields:r}))})}statementRun(t,e=0){return new Promise((s,r)=>{try{s(this.statementRunSync(t))}catch(n){"SQLITE_BUSY"==n.code&&e<10?setTimeout(()=>{this.statementRun(t,e+1).then(t=>{s(t)}).catch(t=>{r(t)})},100):r(n)}})}statementRunSync(t){const e=this.db.prepare(t.sql),s=e&&e.run(t.values);return s&&{changes:s.changes,affected:s.changes,lastId:s.lastInsertRowid}}statementGet(t){return new Promise((e,s)=>{try{e(this.db.prepare(t.sql).get(t.values))}catch(t){s(t)}})}statementGetSync(t){const e=this.db.prepare(t.sql);return e&&e.get(t.values)}statementGetAll(t){return new Promise((e,s)=>{try{e(this.db.prepare(t.sql).all(t.values))}catch(t){s(t)}})}statementGetAllSync(t){const e=this.db.prepare(t.sql);return e&&e.all(t.values)}close(){return new Promise((t,e)=>{try{this.smartDbLog.setDb(null),this.db.close(),t()}catch(t){e(t)}})}}
@@ -1,26 +0,0 @@
1
- import Mysql, { Connection, ConnectionConfig } from "mysql";
2
- import { SmartDbSqlBuildData } from "../smart-db-sql-build-data";
3
- import { AbstractModel } from "../models/abstract-model";
4
- import { SmartDb } from "../smart-db";
5
- import { GenericModelData, SmartDbRunResult, SmartDbTableInfo } from "../smart-db-interfaces";
6
- export declare class SmartDbMysql extends SmartDb {
7
- protected db: Mysql.Connection;
8
- constructor(connectorOrDb: string | ConnectionConfig | Connection);
9
- getDatabaseType(): string;
10
- getDbConnector(): string | Mysql.ConnectionConfig;
11
- getDbQuote(): string;
12
- hasConcurrentTransactions(): boolean;
13
- closeSync(): boolean;
14
- exists<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), type?: "view" | "table" | "index", indexTableName?: string): Promise<boolean>;
15
- existsSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), type?: "view" | "table" | "index", indexTableName?: string): boolean;
16
- exec(script: string): Promise<void>;
17
- execSync(script: string): boolean;
18
- getTableInfo(table: string): Promise<SmartDbTableInfo>;
19
- protected statementGet(buildData: SmartDbSqlBuildData): Promise<any>;
20
- protected statementGetSync(buildData: SmartDbSqlBuildData): any;
21
- protected statementGetAll(buildData: SmartDbSqlBuildData): Promise<any[]>;
22
- protected statementGetAllSync(buildData: SmartDbSqlBuildData): any[];
23
- protected statementRun(buildData: SmartDbSqlBuildData): Promise<SmartDbRunResult>;
24
- protected statementRunSync(buildData: SmartDbSqlBuildData): SmartDbRunResult;
25
- close(): Promise<void>;
26
- }