@nymphjs/driver-sqlite3 1.0.0-beta.0 → 1.0.0-beta.1

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.
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
@@ -27,54 +18,48 @@ class SQLite3Driver extends nymph_1.NymphDriver {
27
18
  super();
28
19
  this.connected = false;
29
20
  this.transactionsStarted = 0;
30
- this.config = Object.assign(Object.assign({}, conf_1.SQLite3DriverConfigDefaults), config);
21
+ this.config = { ...conf_1.SQLite3DriverConfigDefaults, ...config };
31
22
  this.prefix = this.config.prefix;
32
23
  this.connect();
33
24
  }
34
- connect() {
35
- return __awaiter(this, void 0, void 0, function* () {
36
- const { filename, fileMustExist, timeout, readonly, verbose } = this.config;
37
- if (!this.connected) {
38
- try {
39
- this.link = new better_sqlite3_1.default(filename, {
40
- readonly,
41
- fileMustExist,
42
- timeout,
43
- verbose,
44
- });
45
- this.connected = true;
46
- this.link.pragma('encoding = "UTF-8";');
47
- this.link.pragma('foreign_keys = 1;');
48
- this.link.pragma('case_sensitive_like = 1;');
49
- this.link.function('regexp', { deterministic: true }, (pattern, subject) => this.posixRegexMatch(pattern, subject) ? 1 : 0);
25
+ async connect() {
26
+ const { filename, fileMustExist, timeout, readonly, verbose } = this.config;
27
+ if (!this.connected) {
28
+ try {
29
+ this.link = new better_sqlite3_1.default(filename, {
30
+ readonly,
31
+ fileMustExist,
32
+ timeout,
33
+ verbose,
34
+ });
35
+ this.connected = true;
36
+ this.link.pragma('encoding = "UTF-8";');
37
+ this.link.pragma('foreign_keys = 1;');
38
+ this.link.pragma('case_sensitive_like = 1;');
39
+ this.link.function('regexp', { deterministic: true }, (pattern, subject) => this.posixRegexMatch(pattern, subject) ? 1 : 0);
40
+ }
41
+ catch (e) {
42
+ this.connected = false;
43
+ if (filename === ':memory:') {
44
+ throw new nymph_1.NotConfiguredError("It seems the config hasn't been set up correctly.");
50
45
  }
51
- catch (e) {
52
- this.connected = false;
53
- if (filename === ':memory:') {
54
- throw new nymph_1.NotConfiguredError("It seems the config hasn't been set up correctly.");
55
- }
56
- else {
57
- throw new nymph_1.UnableToConnectError('Could not connect: ' + (e === null || e === void 0 ? void 0 : e.message));
58
- }
46
+ else {
47
+ throw new nymph_1.UnableToConnectError('Could not connect: ' + e?.message);
59
48
  }
60
49
  }
61
- return this.connected;
62
- });
50
+ }
51
+ return this.connected;
63
52
  }
64
- disconnect() {
65
- return __awaiter(this, void 0, void 0, function* () {
66
- if (this.connected) {
67
- this.link.exec('PRAGMA optimize;');
68
- this.link.close();
69
- this.connected = false;
70
- }
71
- return this.connected;
72
- });
53
+ async disconnect() {
54
+ if (this.connected) {
55
+ this.link.exec('PRAGMA optimize;');
56
+ this.link.close();
57
+ this.connected = false;
58
+ }
59
+ return this.connected;
73
60
  }
74
- inTransaction() {
75
- return __awaiter(this, void 0, void 0, function* () {
76
- return this.transactionsStarted > 0;
77
- });
61
+ async inTransaction() {
62
+ return this.transactionsStarted > 0;
78
63
  }
79
64
  isConnected() {
80
65
  return this.connected;
@@ -124,8 +109,8 @@ class SQLite3Driver extends nymph_1.NymphDriver {
124
109
  return runQuery();
125
110
  }
126
111
  catch (e) {
127
- const errorCode = e === null || e === void 0 ? void 0 : e.code;
128
- const errorMsg = e === null || e === void 0 ? void 0 : e.message;
112
+ const errorCode = e?.code;
113
+ const errorMsg = e?.message;
129
114
  if (errorCode === 'SQLITE_ERROR' &&
130
115
  errorMsg.match(/^no such table: /) &&
131
116
  this.createTables()) {
@@ -136,11 +121,11 @@ class SQLite3Driver extends nymph_1.NymphDriver {
136
121
  return runQuery();
137
122
  }
138
123
  catch (e2) {
139
- throw new nymph_1.QueryFailedError('Query failed: ' + (e2 === null || e2 === void 0 ? void 0 : e2.code) + ' - ' + (e2 === null || e2 === void 0 ? void 0 : e2.message), query);
124
+ throw new nymph_1.QueryFailedError('Query failed: ' + e2?.code + ' - ' + e2?.message, query);
140
125
  }
141
126
  }
142
127
  else {
143
- throw new nymph_1.QueryFailedError('Query failed: ' + (e === null || e === void 0 ? void 0 : e.code) + ' - ' + (e === null || e === void 0 ? void 0 : e.message), query);
128
+ throw new nymph_1.QueryFailedError('Query failed: ' + e?.code + ' - ' + e?.message, query);
144
129
  }
145
130
  }
146
131
  }
@@ -153,144 +138,135 @@ class SQLite3Driver extends nymph_1.NymphDriver {
153
138
  queryRun(query, { etypes = [], params = {}, } = {}) {
154
139
  return this.query(() => this.link.prepare(query).run(params), `${query} -- ${JSON.stringify(params)}`, etypes);
155
140
  }
156
- commit(name) {
157
- return __awaiter(this, void 0, void 0, function* () {
158
- if (name == null || typeof name !== 'string' || name.length === 0) {
159
- throw new nymph_1.InvalidParametersError('Transaction commit attempted without a name.');
160
- }
161
- if (this.transactionsStarted === 0) {
162
- return true;
163
- }
164
- this.queryRun(`RELEASE SAVEPOINT ${SQLite3Driver.escape(name)};`);
165
- this.transactionsStarted--;
141
+ async commit(name) {
142
+ if (name == null || typeof name !== 'string' || name.length === 0) {
143
+ throw new nymph_1.InvalidParametersError('Transaction commit attempted without a name.');
144
+ }
145
+ if (this.transactionsStarted === 0) {
166
146
  return true;
167
- });
168
- }
169
- deleteEntityByID(guid, className) {
170
- return __awaiter(this, void 0, void 0, function* () {
171
- let EntityClass;
172
- if (typeof className === 'string' || className == null) {
173
- const GetEntityClass = this.nymph.getEntityClass(className !== null && className !== void 0 ? className : 'Entity');
174
- EntityClass = GetEntityClass;
175
- }
176
- else {
177
- EntityClass = className;
178
- }
179
- const etype = EntityClass.ETYPE;
180
- this.checkReadOnlyMode();
181
- yield this.startTransaction('nymph-delete');
182
- try {
183
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} WHERE "guid"=@guid;`, {
184
- etypes: [etype],
185
- params: {
186
- guid,
187
- },
188
- });
189
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} WHERE "guid"=@guid;`, {
190
- etypes: [etype],
191
- params: {
192
- guid,
193
- },
194
- });
195
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} WHERE "guid"=@guid;`, {
196
- etypes: [etype],
197
- params: {
198
- guid,
199
- },
200
- });
201
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}references_${etype}`)} WHERE "guid"=@guid;`, {
202
- etypes: [etype],
203
- params: {
204
- guid,
205
- },
206
- });
207
- yield this.commit('nymph-delete');
208
- if (this.nymph.config.cache) {
209
- this.cleanCache(guid);
210
- }
211
- return true;
212
- }
213
- catch (e) {
214
- yield this.rollback('nymph-delete');
215
- throw e;
216
- }
217
- });
147
+ }
148
+ this.queryRun(`RELEASE SAVEPOINT ${SQLite3Driver.escape(name)};`);
149
+ this.transactionsStarted--;
150
+ return true;
218
151
  }
219
- deleteUID(name) {
220
- return __awaiter(this, void 0, void 0, function* () {
221
- if (!name) {
222
- throw new nymph_1.InvalidParametersError('Name not given for UID');
223
- }
224
- this.checkReadOnlyMode();
225
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}uids`)} WHERE "name"=@name;`, {
152
+ async deleteEntityByID(guid, className) {
153
+ let EntityClass;
154
+ if (typeof className === 'string' || className == null) {
155
+ const GetEntityClass = this.nymph.getEntityClass(className ?? 'Entity');
156
+ EntityClass = GetEntityClass;
157
+ }
158
+ else {
159
+ EntityClass = className;
160
+ }
161
+ const etype = EntityClass.ETYPE;
162
+ this.checkReadOnlyMode();
163
+ await this.startTransaction('nymph-delete');
164
+ try {
165
+ this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} WHERE "guid"=@guid;`, {
166
+ etypes: [etype],
226
167
  params: {
227
- name,
168
+ guid,
169
+ },
170
+ });
171
+ this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} WHERE "guid"=@guid;`, {
172
+ etypes: [etype],
173
+ params: {
174
+ guid,
175
+ },
176
+ });
177
+ this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} WHERE "guid"=@guid;`, {
178
+ etypes: [etype],
179
+ params: {
180
+ guid,
181
+ },
182
+ });
183
+ this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}references_${etype}`)} WHERE "guid"=@guid;`, {
184
+ etypes: [etype],
185
+ params: {
186
+ guid,
228
187
  },
229
188
  });
189
+ await this.commit('nymph-delete');
190
+ if (this.nymph.config.cache) {
191
+ this.cleanCache(guid);
192
+ }
230
193
  return true;
194
+ }
195
+ catch (e) {
196
+ await this.rollback('nymph-delete');
197
+ throw e;
198
+ }
199
+ }
200
+ async deleteUID(name) {
201
+ if (!name) {
202
+ throw new nymph_1.InvalidParametersError('Name not given for UID');
203
+ }
204
+ this.checkReadOnlyMode();
205
+ this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}uids`)} WHERE "name"=@name;`, {
206
+ params: {
207
+ name,
208
+ },
231
209
  });
210
+ return true;
232
211
  }
233
- exportEntities(writeLine) {
234
- return __awaiter(this, void 0, void 0, function* () {
235
- writeLine('#nex2');
236
- writeLine('# Nymph Entity Exchange v2');
237
- writeLine('# http://nymph.io');
238
- writeLine('#');
239
- writeLine('# Generation Time: ' + new Date().toLocaleString());
240
- writeLine('');
241
- writeLine('#');
242
- writeLine('# UIDs');
243
- writeLine('#');
244
- writeLine('');
245
- let uids = this.queryIter(`SELECT * FROM ${SQLite3Driver.escape(`${this.prefix}uids`)} ORDER BY "name";`);
246
- for (const uid of uids) {
247
- writeLine(`<${uid.name}>[${uid.cur_uid}]`);
248
- }
249
- writeLine('');
250
- writeLine('#');
251
- writeLine('# Entities');
252
- writeLine('#');
253
- writeLine('');
254
- const tables = this.queryIter("SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name;");
255
- const etypes = [];
256
- for (const table of tables) {
257
- if (table.name.startsWith(this.prefix + 'entities_')) {
258
- etypes.push(table.name.substr((this.prefix + 'entities_').length));
259
- }
212
+ async exportEntities(writeLine) {
213
+ writeLine('#nex2');
214
+ writeLine('# Nymph Entity Exchange v2');
215
+ writeLine('# http://nymph.io');
216
+ writeLine('#');
217
+ writeLine('# Generation Time: ' + new Date().toLocaleString());
218
+ writeLine('');
219
+ writeLine('#');
220
+ writeLine('# UIDs');
221
+ writeLine('#');
222
+ writeLine('');
223
+ let uids = this.queryIter(`SELECT * FROM ${SQLite3Driver.escape(`${this.prefix}uids`)} ORDER BY "name";`);
224
+ for (const uid of uids) {
225
+ writeLine(`<${uid.name}>[${uid.cur_uid}]`);
226
+ }
227
+ writeLine('');
228
+ writeLine('#');
229
+ writeLine('# Entities');
230
+ writeLine('#');
231
+ writeLine('');
232
+ const tables = this.queryIter("SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name;");
233
+ const etypes = [];
234
+ for (const table of tables) {
235
+ if (table.name.startsWith(this.prefix + 'entities_')) {
236
+ etypes.push(table.name.substr((this.prefix + 'entities_').length));
260
237
  }
261
- for (const etype of etypes) {
262
- const dataIterator = this.queryIter(`SELECT e.*, d."name" AS "dname", d."value" AS "dvalue", c."string", c."number" FROM ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} e LEFT JOIN ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} d USING ("guid") INNER JOIN ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} c USING ("guid", "name") ORDER BY e."guid";`)[Symbol.iterator]();
263
- let datum = dataIterator.next();
264
- while (!datum.done) {
265
- const guid = datum.value.guid;
266
- const tags = datum.value.tags.slice(1, -1);
267
- const cdate = datum.value.cdate;
268
- const mdate = datum.value.mdate;
269
- writeLine(`{${guid}}<${etype}>[${tags}]`);
270
- writeLine(`\tcdate=${JSON.stringify(cdate)}`);
271
- writeLine(`\tmdate=${JSON.stringify(mdate)}`);
272
- if (datum.value.dname != null) {
273
- do {
274
- const value = datum.value.dvalue === 'N'
275
- ? JSON.stringify(datum.value.number)
276
- : datum.value.dvalue === 'S'
277
- ? JSON.stringify(datum.value.string)
278
- : datum.value.dvalue;
279
- writeLine(`\t${datum.value.dname}=${value}`);
280
- datum = dataIterator.next();
281
- } while (!datum.done && datum.value.guid === guid);
282
- }
283
- else {
238
+ }
239
+ for (const etype of etypes) {
240
+ const dataIterator = this.queryIter(`SELECT e.*, d."name" AS "dname", d."value" AS "dvalue", c."string", c."number" FROM ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} e LEFT JOIN ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} d USING ("guid") INNER JOIN ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} c USING ("guid", "name") ORDER BY e."guid";`)[Symbol.iterator]();
241
+ let datum = dataIterator.next();
242
+ while (!datum.done) {
243
+ const guid = datum.value.guid;
244
+ const tags = datum.value.tags.slice(1, -1);
245
+ const cdate = datum.value.cdate;
246
+ const mdate = datum.value.mdate;
247
+ writeLine(`{${guid}}<${etype}>[${tags}]`);
248
+ writeLine(`\tcdate=${JSON.stringify(cdate)}`);
249
+ writeLine(`\tmdate=${JSON.stringify(mdate)}`);
250
+ if (datum.value.dname != null) {
251
+ do {
252
+ const value = datum.value.dvalue === 'N'
253
+ ? JSON.stringify(datum.value.number)
254
+ : datum.value.dvalue === 'S'
255
+ ? JSON.stringify(datum.value.string)
256
+ : datum.value.dvalue;
257
+ writeLine(`\t${datum.value.dname}=${value}`);
284
258
  datum = dataIterator.next();
285
- }
259
+ } while (!datum.done && datum.value.guid === guid);
260
+ }
261
+ else {
262
+ datum = dataIterator.next();
286
263
  }
287
264
  }
288
- return;
289
- });
265
+ }
266
+ return;
290
267
  }
291
268
  makeEntityQuery(options, formattedSelectors, etype, count = { i: 0 }, params = {}, subquery = false, tableSuffix = '', etypes = []) {
292
- var _a, _b;
293
- if (typeof ((_a = options.class) === null || _a === void 0 ? void 0 : _a.alterOptions) === 'function') {
269
+ if (typeof options.class?.alterOptions === 'function') {
294
270
  options = options.class.alterOptions(options);
295
271
  }
296
272
  const eTable = `e${tableSuffix}`;
@@ -298,7 +274,7 @@ class SQLite3Driver extends nymph_1.NymphDriver {
298
274
  const cTable = `c${tableSuffix}`;
299
275
  const fTable = `f${tableSuffix}`;
300
276
  const ieTable = `ie${tableSuffix}`;
301
- const sort = (_b = options.sort) !== null && _b !== void 0 ? _b : 'cdate';
277
+ const sort = options.sort ?? 'cdate';
302
278
  const queryParts = this.iterateSelectorsForQuery(formattedSelectors, (key, value, typeIsOr, typeIsNot) => {
303
279
  const clauseNot = key.startsWith('!');
304
280
  let curQuery = '';
@@ -1021,7 +997,7 @@ class SQLite3Driver extends nymph_1.NymphDriver {
1021
997
  const [qrefOptions, ...qrefSelectors] = curValue[1];
1022
998
  const QrefEntityClass = qrefOptions.class;
1023
999
  etypes.push(QrefEntityClass.ETYPE);
1024
- const qrefQuery = this.makeEntityQuery(Object.assign(Object.assign({}, qrefOptions), { return: 'guid', class: QrefEntityClass }), qrefSelectors, QrefEntityClass.ETYPE, count, params, false, (0, guid_1.makeTableSuffix)(), etypes);
1000
+ const qrefQuery = this.makeEntityQuery({ ...qrefOptions, return: 'guid', class: QrefEntityClass }, qrefSelectors, QrefEntityClass.ETYPE, count, params, false, (0, guid_1.makeTableSuffix)(), etypes);
1025
1001
  if (curQuery) {
1026
1002
  curQuery += typeIsOr ? ' OR ' : ' AND ';
1027
1003
  }
@@ -1198,10 +1174,8 @@ class SQLite3Driver extends nymph_1.NymphDriver {
1198
1174
  result,
1199
1175
  };
1200
1176
  }
1201
- getEntities(options = {}, ...selectors) {
1202
- return __awaiter(this, void 0, void 0, function* () {
1203
- return this.getEntitiesSync(options, ...selectors);
1204
- });
1177
+ async getEntities(options = {}, ...selectors) {
1178
+ return this.getEntitiesSync(options, ...selectors);
1205
1179
  }
1206
1180
  getEntitiesSync(options = {}, ...selectors) {
1207
1181
  const { result, process } = this.getEntitesRowLike(options, selectors, (options, formattedSelectors, etype) => this.performQuery(options, formattedSelectors, etype), () => {
@@ -1221,337 +1195,319 @@ class SQLite3Driver extends nymph_1.NymphDriver {
1221
1195
  }));
1222
1196
  return process();
1223
1197
  }
1224
- getUID(name) {
1225
- var _a;
1226
- return __awaiter(this, void 0, void 0, function* () {
1227
- if (name == null) {
1228
- throw new nymph_1.InvalidParametersError('Name not given for UID.');
1229
- }
1230
- const result = this.queryGet(`SELECT "cur_uid" FROM ${SQLite3Driver.escape(`${this.prefix}uids`)} WHERE "name"=@name;`, {
1231
- params: {
1232
- name: name,
1233
- },
1234
- });
1235
- return (_a = result === null || result === void 0 ? void 0 : result.cur_uid) !== null && _a !== void 0 ? _a : null;
1198
+ async getUID(name) {
1199
+ if (name == null) {
1200
+ throw new nymph_1.InvalidParametersError('Name not given for UID.');
1201
+ }
1202
+ const result = this.queryGet(`SELECT "cur_uid" FROM ${SQLite3Driver.escape(`${this.prefix}uids`)} WHERE "name"=@name;`, {
1203
+ params: {
1204
+ name: name,
1205
+ },
1236
1206
  });
1207
+ return result?.cur_uid ?? null;
1237
1208
  }
1238
- import(filename) {
1239
- return __awaiter(this, void 0, void 0, function* () {
1240
- this.checkReadOnlyMode();
1241
- try {
1242
- return this.importFromFile(filename, (guid, tags, sdata, etype) => __awaiter(this, void 0, void 0, function* () {
1243
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} WHERE "guid"=@guid;`, {
1244
- etypes: [etype],
1245
- params: {
1246
- guid,
1247
- },
1248
- });
1249
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} WHERE "guid"=@guid;`, {
1250
- etypes: [etype],
1251
- params: {
1252
- guid,
1253
- },
1254
- });
1255
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} WHERE "guid"=@guid;`, {
1256
- etypes: [etype],
1257
- params: {
1258
- guid,
1259
- },
1260
- });
1261
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}references_${etype}`)} WHERE "guid"=@guid;`, {
1209
+ async import(filename) {
1210
+ this.checkReadOnlyMode();
1211
+ try {
1212
+ return this.importFromFile(filename, async (guid, tags, sdata, etype) => {
1213
+ this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} WHERE "guid"=@guid;`, {
1214
+ etypes: [etype],
1215
+ params: {
1216
+ guid,
1217
+ },
1218
+ });
1219
+ this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} WHERE "guid"=@guid;`, {
1220
+ etypes: [etype],
1221
+ params: {
1222
+ guid,
1223
+ },
1224
+ });
1225
+ this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} WHERE "guid"=@guid;`, {
1226
+ etypes: [etype],
1227
+ params: {
1228
+ guid,
1229
+ },
1230
+ });
1231
+ this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}references_${etype}`)} WHERE "guid"=@guid;`, {
1232
+ etypes: [etype],
1233
+ params: {
1234
+ guid,
1235
+ },
1236
+ });
1237
+ this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} ("guid", "tags", "cdate", "mdate") VALUES (@guid, @tags, @cdate, @mdate);`, {
1238
+ etypes: [etype],
1239
+ params: {
1240
+ guid,
1241
+ tags: ',' + tags.join(',') + ',',
1242
+ cdate: Number(JSON.parse(sdata.cdate)),
1243
+ mdate: Number(JSON.parse(sdata.mdate)),
1244
+ },
1245
+ });
1246
+ delete sdata.cdate;
1247
+ delete sdata.mdate;
1248
+ for (const name in sdata) {
1249
+ const value = sdata[name];
1250
+ const uvalue = JSON.parse(value);
1251
+ if (value === undefined) {
1252
+ continue;
1253
+ }
1254
+ const storageValue = typeof uvalue === 'number'
1255
+ ? 'N'
1256
+ : typeof uvalue === 'string'
1257
+ ? 'S'
1258
+ : value;
1259
+ this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("guid", "name", "value") VALUES (@guid, @name, @storageValue);`, {
1262
1260
  etypes: [etype],
1263
1261
  params: {
1264
1262
  guid,
1263
+ name,
1264
+ storageValue,
1265
1265
  },
1266
1266
  });
1267
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} ("guid", "tags", "cdate", "mdate") VALUES (@guid, @tags, @cdate, @mdate);`, {
1267
+ this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} ("guid", "name", "truthy", "string", "number") VALUES (@guid, @name, @truthy, @string, @number);`, {
1268
1268
  etypes: [etype],
1269
1269
  params: {
1270
1270
  guid,
1271
- tags: ',' + tags.join(',') + ',',
1272
- cdate: Number(JSON.parse(sdata.cdate)),
1273
- mdate: Number(JSON.parse(sdata.mdate)),
1271
+ name,
1272
+ truthy: uvalue ? 1 : 0,
1273
+ string: `${uvalue}`,
1274
+ number: Number(uvalue),
1274
1275
  },
1275
1276
  });
1276
- delete sdata.cdate;
1277
- delete sdata.mdate;
1278
- for (const name in sdata) {
1279
- const value = sdata[name];
1280
- const uvalue = JSON.parse(value);
1281
- if (value === undefined) {
1282
- continue;
1283
- }
1284
- const storageValue = typeof uvalue === 'number'
1285
- ? 'N'
1286
- : typeof uvalue === 'string'
1287
- ? 'S'
1288
- : value;
1289
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("guid", "name", "value") VALUES (@guid, @name, @storageValue);`, {
1290
- etypes: [etype],
1291
- params: {
1292
- guid,
1293
- name,
1294
- storageValue,
1295
- },
1296
- });
1297
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} ("guid", "name", "truthy", "string", "number") VALUES (@guid, @name, @truthy, @string, @number);`, {
1277
+ const references = this.findReferences(value);
1278
+ for (const reference of references) {
1279
+ this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}references_${etype}`)} ("guid", "name", "reference") VALUES (@guid, @name, @reference);`, {
1298
1280
  etypes: [etype],
1299
1281
  params: {
1300
1282
  guid,
1301
1283
  name,
1302
- truthy: uvalue ? 1 : 0,
1303
- string: `${uvalue}`,
1304
- number: Number(uvalue),
1284
+ reference,
1305
1285
  },
1306
1286
  });
1307
- const references = this.findReferences(value);
1308
- for (const reference of references) {
1309
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}references_${etype}`)} ("guid", "name", "reference") VALUES (@guid, @name, @reference);`, {
1310
- etypes: [etype],
1311
- params: {
1312
- guid,
1313
- name,
1314
- reference,
1315
- },
1316
- });
1317
- }
1318
1287
  }
1319
- }), (name, curUid) => __awaiter(this, void 0, void 0, function* () {
1320
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}uids`)} WHERE "name"=@name;`, {
1321
- params: {
1322
- name,
1323
- },
1324
- });
1325
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}uids`)} ("name", "cur_uid") VALUES (@name, @curUid);`, {
1326
- params: {
1327
- name,
1328
- curUid,
1329
- },
1330
- });
1331
- }), () => __awaiter(this, void 0, void 0, function* () {
1332
- yield this.startTransaction('nymph-import');
1333
- }), () => __awaiter(this, void 0, void 0, function* () {
1334
- yield this.commit('nymph-import');
1335
- }));
1336
- }
1337
- catch (e) {
1338
- yield this.rollback('nymph-import');
1339
- throw e;
1340
- }
1341
- });
1288
+ }
1289
+ }, async (name, curUid) => {
1290
+ this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}uids`)} WHERE "name"=@name;`, {
1291
+ params: {
1292
+ name,
1293
+ },
1294
+ });
1295
+ this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}uids`)} ("name", "cur_uid") VALUES (@name, @curUid);`, {
1296
+ params: {
1297
+ name,
1298
+ curUid,
1299
+ },
1300
+ });
1301
+ }, async () => {
1302
+ await this.startTransaction('nymph-import');
1303
+ }, async () => {
1304
+ await this.commit('nymph-import');
1305
+ });
1306
+ }
1307
+ catch (e) {
1308
+ await this.rollback('nymph-import');
1309
+ throw e;
1310
+ }
1342
1311
  }
1343
- newUID(name) {
1344
- var _a, _b;
1345
- return __awaiter(this, void 0, void 0, function* () {
1346
- if (name == null) {
1347
- throw new nymph_1.InvalidParametersError('Name not given for UID.');
1348
- }
1349
- this.checkReadOnlyMode();
1350
- yield this.startTransaction('nymph-newuid');
1351
- try {
1352
- let curUid = (_b = (_a = this.queryGet(`SELECT "cur_uid" FROM ${SQLite3Driver.escape(`${this.prefix}uids`)} WHERE "name"=@name;`, {
1312
+ async newUID(name) {
1313
+ if (name == null) {
1314
+ throw new nymph_1.InvalidParametersError('Name not given for UID.');
1315
+ }
1316
+ this.checkReadOnlyMode();
1317
+ await this.startTransaction('nymph-newuid');
1318
+ try {
1319
+ let curUid = this.queryGet(`SELECT "cur_uid" FROM ${SQLite3Driver.escape(`${this.prefix}uids`)} WHERE "name"=@name;`, {
1320
+ params: {
1321
+ name,
1322
+ },
1323
+ })?.cur_uid ?? null;
1324
+ if (curUid == null) {
1325
+ curUid = 1;
1326
+ this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}uids`)} ("name", "cur_uid") VALUES (@name, @curUid);`, {
1353
1327
  params: {
1354
1328
  name,
1329
+ curUid,
1355
1330
  },
1356
- })) === null || _a === void 0 ? void 0 : _a.cur_uid) !== null && _b !== void 0 ? _b : null;
1357
- if (curUid == null) {
1358
- curUid = 1;
1359
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}uids`)} ("name", "cur_uid") VALUES (@name, @curUid);`, {
1360
- params: {
1361
- name,
1362
- curUid,
1363
- },
1364
- });
1365
- }
1366
- else {
1367
- curUid++;
1368
- this.queryRun(`UPDATE ${SQLite3Driver.escape(`${this.prefix}uids`)} SET "cur_uid"=@curUid WHERE "name"=@name;`, {
1369
- params: {
1370
- curUid,
1371
- name,
1372
- },
1373
- });
1374
- }
1375
- yield this.commit('nymph-newuid');
1376
- return curUid;
1331
+ });
1377
1332
  }
1378
- catch (e) {
1379
- yield this.rollback('nymph-newuid');
1380
- throw e;
1333
+ else {
1334
+ curUid++;
1335
+ this.queryRun(`UPDATE ${SQLite3Driver.escape(`${this.prefix}uids`)} SET "cur_uid"=@curUid WHERE "name"=@name;`, {
1336
+ params: {
1337
+ curUid,
1338
+ name,
1339
+ },
1340
+ });
1381
1341
  }
1382
- });
1342
+ await this.commit('nymph-newuid');
1343
+ return curUid;
1344
+ }
1345
+ catch (e) {
1346
+ await this.rollback('nymph-newuid');
1347
+ throw e;
1348
+ }
1383
1349
  }
1384
- renameUID(oldName, newName) {
1385
- return __awaiter(this, void 0, void 0, function* () {
1386
- if (oldName == null || newName == null) {
1387
- throw new nymph_1.InvalidParametersError('Name not given for UID.');
1388
- }
1389
- this.checkReadOnlyMode();
1390
- this.queryRun(`UPDATE ${SQLite3Driver.escape(`${this.prefix}uids`)} SET "name"=@newName WHERE "name"=@oldName;`, {
1391
- params: {
1392
- newName,
1393
- oldName,
1394
- },
1395
- });
1396
- return true;
1350
+ async renameUID(oldName, newName) {
1351
+ if (oldName == null || newName == null) {
1352
+ throw new nymph_1.InvalidParametersError('Name not given for UID.');
1353
+ }
1354
+ this.checkReadOnlyMode();
1355
+ this.queryRun(`UPDATE ${SQLite3Driver.escape(`${this.prefix}uids`)} SET "name"=@newName WHERE "name"=@oldName;`, {
1356
+ params: {
1357
+ newName,
1358
+ oldName,
1359
+ },
1397
1360
  });
1361
+ return true;
1398
1362
  }
1399
- rollback(name) {
1400
- return __awaiter(this, void 0, void 0, function* () {
1401
- if (name == null || typeof name !== 'string' || name.length === 0) {
1402
- throw new nymph_1.InvalidParametersError('Transaction rollback attempted without a name.');
1403
- }
1404
- if (this.transactionsStarted === 0) {
1405
- return true;
1406
- }
1407
- this.queryRun(`ROLLBACK TO SAVEPOINT ${SQLite3Driver.escape(name)};`);
1408
- this.transactionsStarted--;
1363
+ async rollback(name) {
1364
+ if (name == null || typeof name !== 'string' || name.length === 0) {
1365
+ throw new nymph_1.InvalidParametersError('Transaction rollback attempted without a name.');
1366
+ }
1367
+ if (this.transactionsStarted === 0) {
1409
1368
  return true;
1410
- });
1369
+ }
1370
+ this.queryRun(`ROLLBACK TO SAVEPOINT ${SQLite3Driver.escape(name)};`);
1371
+ this.transactionsStarted--;
1372
+ return true;
1411
1373
  }
1412
- saveEntity(entity) {
1413
- return __awaiter(this, void 0, void 0, function* () {
1414
- this.checkReadOnlyMode();
1415
- const insertData = (guid, data, sdata, etype) => {
1416
- const runInsertQuery = (name, value, svalue) => {
1417
- if (value === undefined) {
1418
- return;
1419
- }
1420
- const storageValue = typeof value === 'number'
1421
- ? 'N'
1422
- : typeof value === 'string'
1423
- ? 'S'
1424
- : svalue;
1425
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("guid", "name", "value") VALUES (@guid, @name, @storageValue);`, {
1374
+ async saveEntity(entity) {
1375
+ this.checkReadOnlyMode();
1376
+ const insertData = (guid, data, sdata, etype) => {
1377
+ const runInsertQuery = (name, value, svalue) => {
1378
+ if (value === undefined) {
1379
+ return;
1380
+ }
1381
+ const storageValue = typeof value === 'number'
1382
+ ? 'N'
1383
+ : typeof value === 'string'
1384
+ ? 'S'
1385
+ : svalue;
1386
+ this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} ("guid", "name", "value") VALUES (@guid, @name, @storageValue);`, {
1387
+ etypes: [etype],
1388
+ params: {
1389
+ guid,
1390
+ name,
1391
+ storageValue,
1392
+ },
1393
+ });
1394
+ this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} ("guid", "name", "truthy", "string", "number") VALUES (@guid, @name, @truthy, @string, @number);`, {
1395
+ etypes: [etype],
1396
+ params: {
1397
+ guid,
1398
+ name,
1399
+ truthy: value ? 1 : 0,
1400
+ string: `${value}`,
1401
+ number: Number(value),
1402
+ },
1403
+ });
1404
+ const references = this.findReferences(svalue);
1405
+ for (const reference of references) {
1406
+ this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}references_${etype}`)} ("guid", "name", "reference") VALUES (@guid, @name, @reference);`, {
1426
1407
  etypes: [etype],
1427
1408
  params: {
1428
1409
  guid,
1429
1410
  name,
1430
- storageValue,
1411
+ reference,
1431
1412
  },
1432
1413
  });
1433
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} ("guid", "name", "truthy", "string", "number") VALUES (@guid, @name, @truthy, @string, @number);`, {
1414
+ }
1415
+ };
1416
+ for (const name in data) {
1417
+ runInsertQuery(name, data[name], JSON.stringify(data[name]));
1418
+ }
1419
+ for (const name in sdata) {
1420
+ runInsertQuery(name, JSON.parse(sdata[name]), sdata[name]);
1421
+ }
1422
+ };
1423
+ try {
1424
+ return this.saveEntityRowLike(entity, async (_entity, guid, tags, data, sdata, cdate, etype) => {
1425
+ this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} ("guid", "tags", "cdate", "mdate") VALUES (@guid, @tags, @cdate, @cdate);`, {
1426
+ etypes: [etype],
1427
+ params: {
1428
+ guid,
1429
+ tags: ',' + tags.join(',') + ',',
1430
+ cdate,
1431
+ },
1432
+ });
1433
+ insertData(guid, data, sdata, etype);
1434
+ return true;
1435
+ }, async (entity, guid, tags, data, sdata, mdate, etype) => {
1436
+ const info = this.queryRun(`UPDATE ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} SET "tags"=@tags, "mdate"=@mdate WHERE "guid"=@guid AND "mdate" <= @emdate;`, {
1437
+ etypes: [etype],
1438
+ params: {
1439
+ tags: ',' + tags.join(',') + ',',
1440
+ mdate,
1441
+ guid,
1442
+ emdate: Number(entity.mdate),
1443
+ },
1444
+ });
1445
+ let success = false;
1446
+ if (info.changes === 1) {
1447
+ this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} WHERE "guid"=@guid;`, {
1434
1448
  etypes: [etype],
1435
1449
  params: {
1436
1450
  guid,
1437
- name,
1438
- truthy: value ? 1 : 0,
1439
- string: `${value}`,
1440
- number: Number(value),
1441
1451
  },
1442
1452
  });
1443
- const references = this.findReferences(svalue);
1444
- for (const reference of references) {
1445
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}references_${etype}`)} ("guid", "name", "reference") VALUES (@guid, @name, @reference);`, {
1446
- etypes: [etype],
1447
- params: {
1448
- guid,
1449
- name,
1450
- reference,
1451
- },
1452
- });
1453
- }
1454
- };
1455
- for (const name in data) {
1456
- runInsertQuery(name, data[name], JSON.stringify(data[name]));
1457
- }
1458
- for (const name in sdata) {
1459
- runInsertQuery(name, JSON.parse(sdata[name]), sdata[name]);
1460
- }
1461
- };
1462
- try {
1463
- return this.saveEntityRowLike(entity, (_entity, guid, tags, data, sdata, cdate, etype) => __awaiter(this, void 0, void 0, function* () {
1464
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} ("guid", "tags", "cdate", "mdate") VALUES (@guid, @tags, @cdate, @cdate);`, {
1453
+ this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} WHERE "guid"=@guid;`, {
1465
1454
  etypes: [etype],
1466
1455
  params: {
1467
1456
  guid,
1468
- tags: ',' + tags.join(',') + ',',
1469
- cdate,
1470
1457
  },
1471
1458
  });
1472
- insertData(guid, data, sdata, etype);
1473
- return true;
1474
- }), (entity, guid, tags, data, sdata, mdate, etype) => __awaiter(this, void 0, void 0, function* () {
1475
- const info = this.queryRun(`UPDATE ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} SET "tags"=@tags, "mdate"=@mdate WHERE "guid"=@guid AND "mdate" <= @emdate;`, {
1459
+ this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}references_${etype}`)} WHERE "guid"=@guid;`, {
1476
1460
  etypes: [etype],
1477
1461
  params: {
1478
- tags: ',' + tags.join(',') + ',',
1479
- mdate,
1480
1462
  guid,
1481
- emdate: Number(entity.mdate),
1482
1463
  },
1483
1464
  });
1484
- let success = false;
1485
- if (info.changes === 1) {
1486
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}data_${etype}`)} WHERE "guid"=@guid;`, {
1487
- etypes: [etype],
1488
- params: {
1489
- guid,
1490
- },
1491
- });
1492
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}comparisons_${etype}`)} WHERE "guid"=@guid;`, {
1493
- etypes: [etype],
1494
- params: {
1495
- guid,
1496
- },
1497
- });
1498
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}references_${etype}`)} WHERE "guid"=@guid;`, {
1499
- etypes: [etype],
1500
- params: {
1501
- guid,
1502
- },
1503
- });
1504
- insertData(guid, data, sdata, etype);
1505
- success = true;
1506
- }
1507
- return success;
1508
- }), () => __awaiter(this, void 0, void 0, function* () {
1509
- yield this.startTransaction('nymph-save');
1510
- }), (success) => __awaiter(this, void 0, void 0, function* () {
1511
- if (success) {
1512
- yield this.commit('nymph-save');
1513
- }
1514
- else {
1515
- yield this.rollback('nymph-save');
1516
- }
1517
- return success;
1518
- }));
1519
- }
1520
- catch (e) {
1521
- yield this.rollback('nymph-save');
1522
- throw e;
1523
- }
1524
- });
1525
- }
1526
- setUID(name, curUid) {
1527
- return __awaiter(this, void 0, void 0, function* () {
1528
- if (name == null) {
1529
- throw new nymph_1.InvalidParametersError('Name not given for UID.');
1530
- }
1531
- this.checkReadOnlyMode();
1532
- this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}uids`)} WHERE "name"=@name;`, {
1533
- params: {
1534
- name,
1535
- },
1536
- });
1537
- this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}uids`)} ("name", "cur_uid") VALUES (@name, @curUid);`, {
1538
- params: {
1539
- name,
1540
- curUid,
1541
- },
1465
+ insertData(guid, data, sdata, etype);
1466
+ success = true;
1467
+ }
1468
+ return success;
1469
+ }, async () => {
1470
+ await this.startTransaction('nymph-save');
1471
+ }, async (success) => {
1472
+ if (success) {
1473
+ await this.commit('nymph-save');
1474
+ }
1475
+ else {
1476
+ await this.rollback('nymph-save');
1477
+ }
1478
+ return success;
1542
1479
  });
1543
- return true;
1544
- });
1480
+ }
1481
+ catch (e) {
1482
+ await this.rollback('nymph-save');
1483
+ throw e;
1484
+ }
1545
1485
  }
1546
- startTransaction(name) {
1547
- return __awaiter(this, void 0, void 0, function* () {
1548
- if (name == null || typeof name !== 'string' || name.length === 0) {
1549
- throw new nymph_1.InvalidParametersError('Transaction start attempted without a name.');
1550
- }
1551
- this.queryRun(`SAVEPOINT ${SQLite3Driver.escape(name)};`);
1552
- this.transactionsStarted++;
1553
- return this.nymph;
1486
+ async setUID(name, curUid) {
1487
+ if (name == null) {
1488
+ throw new nymph_1.InvalidParametersError('Name not given for UID.');
1489
+ }
1490
+ this.checkReadOnlyMode();
1491
+ this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}uids`)} WHERE "name"=@name;`, {
1492
+ params: {
1493
+ name,
1494
+ },
1495
+ });
1496
+ this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}uids`)} ("name", "cur_uid") VALUES (@name, @curUid);`, {
1497
+ params: {
1498
+ name,
1499
+ curUid,
1500
+ },
1554
1501
  });
1502
+ return true;
1503
+ }
1504
+ async startTransaction(name) {
1505
+ if (name == null || typeof name !== 'string' || name.length === 0) {
1506
+ throw new nymph_1.InvalidParametersError('Transaction start attempted without a name.');
1507
+ }
1508
+ this.queryRun(`SAVEPOINT ${SQLite3Driver.escape(name)};`);
1509
+ this.transactionsStarted++;
1510
+ return this.nymph;
1555
1511
  }
1556
1512
  }
1557
1513
  exports.default = SQLite3Driver;