@nymphjs/driver-sqlite3 1.0.0-beta.5 → 1.0.0-beta.50
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/CHANGELOG.md +198 -0
- package/dist/SQLite3Driver.d.ts +13 -13
- package/dist/SQLite3Driver.js +205 -82
- package/dist/SQLite3Driver.js.map +1 -1
- package/dist/SQLite3Driver.test.js +39 -5
- package/dist/SQLite3Driver.test.js.map +1 -1
- package/dist/conf/d.d.ts +2 -1
- package/dist/conf/defaults.js +2 -1
- package/dist/conf/defaults.js.map +1 -1
- package/package.json +13 -13
- package/src/SQLite3Driver.test.ts +41 -6
- package/src/SQLite3Driver.ts +424 -284
- package/src/conf/d.ts +21 -2
- package/src/conf/defaults.ts +2 -1
- package/typedoc.json +4 -0
package/dist/SQLite3Driver.js
CHANGED
|
@@ -7,6 +7,13 @@ const better_sqlite3_1 = __importDefault(require("better-sqlite3"));
|
|
|
7
7
|
const nymph_1 = require("@nymphjs/nymph");
|
|
8
8
|
const guid_1 = require("@nymphjs/guid");
|
|
9
9
|
const conf_1 = require("./conf");
|
|
10
|
+
class InternalStore {
|
|
11
|
+
constructor(link) {
|
|
12
|
+
this.connected = false;
|
|
13
|
+
this.transactionsStarted = 0;
|
|
14
|
+
this.link = link;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
10
17
|
class SQLite3Driver extends nymph_1.NymphDriver {
|
|
11
18
|
static escape(input) {
|
|
12
19
|
if (input.indexOf('\x00') !== -1) {
|
|
@@ -14,63 +21,126 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
14
21
|
}
|
|
15
22
|
return '"' + input.replace(/"/g, () => '""') + '"';
|
|
16
23
|
}
|
|
17
|
-
constructor(config) {
|
|
24
|
+
constructor(config, store) {
|
|
18
25
|
super();
|
|
19
|
-
this.connected = false;
|
|
20
|
-
this.transactionsStarted = 0;
|
|
21
26
|
this.config = { ...conf_1.SQLite3DriverConfigDefaults, ...config };
|
|
27
|
+
if (this.config.filename === ':memory:') {
|
|
28
|
+
this.config.explicitWrite = true;
|
|
29
|
+
}
|
|
22
30
|
this.prefix = this.config.prefix;
|
|
23
|
-
|
|
31
|
+
if (store) {
|
|
32
|
+
this.store = store;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
this.connect();
|
|
36
|
+
}
|
|
24
37
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
38
|
+
clone() {
|
|
39
|
+
return new SQLite3Driver(this.config, this.store);
|
|
40
|
+
}
|
|
41
|
+
connect() {
|
|
42
|
+
if (this.store && this.store.connected) {
|
|
43
|
+
return Promise.resolve(true);
|
|
44
|
+
}
|
|
45
|
+
this._connect(false);
|
|
46
|
+
return Promise.resolve(this.store.connected);
|
|
47
|
+
}
|
|
48
|
+
_connect(write) {
|
|
49
|
+
const { filename, fileMustExist, timeout, explicitWrite, wal, verbose } = this.config;
|
|
50
|
+
try {
|
|
51
|
+
const setOptions = (link) => {
|
|
52
|
+
if (wal) {
|
|
53
|
+
link.pragma('journal_mode = WAL;');
|
|
54
|
+
}
|
|
55
|
+
link.pragma('encoding = "UTF-8";');
|
|
56
|
+
link.pragma('foreign_keys = 1;');
|
|
57
|
+
link.pragma('case_sensitive_like = 1;');
|
|
58
|
+
link.function('regexp', { deterministic: true }, ((pattern, subject) => (this.posixRegexMatch(pattern, subject) ? 1 : 0)));
|
|
59
|
+
};
|
|
60
|
+
let link;
|
|
28
61
|
try {
|
|
29
|
-
|
|
30
|
-
readonly,
|
|
62
|
+
link = new better_sqlite3_1.default(filename, {
|
|
63
|
+
readonly: !explicitWrite && !write,
|
|
31
64
|
fileMustExist,
|
|
32
65
|
timeout,
|
|
33
66
|
verbose,
|
|
34
67
|
});
|
|
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
68
|
}
|
|
41
69
|
catch (e) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
70
|
+
if (e.code === 'SQLITE_CANTOPEN' &&
|
|
71
|
+
!explicitWrite &&
|
|
72
|
+
!write &&
|
|
73
|
+
!this.config.fileMustExist) {
|
|
74
|
+
const writeLink = new better_sqlite3_1.default(filename, {
|
|
75
|
+
readonly: false,
|
|
76
|
+
fileMustExist,
|
|
77
|
+
timeout,
|
|
78
|
+
verbose,
|
|
79
|
+
});
|
|
80
|
+
setOptions(writeLink);
|
|
81
|
+
writeLink.close();
|
|
82
|
+
link = new better_sqlite3_1.default(filename, {
|
|
83
|
+
readonly: true,
|
|
84
|
+
fileMustExist,
|
|
85
|
+
timeout,
|
|
86
|
+
verbose,
|
|
87
|
+
});
|
|
45
88
|
}
|
|
46
89
|
else {
|
|
47
|
-
throw
|
|
90
|
+
throw e;
|
|
48
91
|
}
|
|
49
92
|
}
|
|
93
|
+
if (!this.store) {
|
|
94
|
+
if (write) {
|
|
95
|
+
throw new Error('Tried to open in write without opening in read first.');
|
|
96
|
+
}
|
|
97
|
+
this.store = new InternalStore(link);
|
|
98
|
+
}
|
|
99
|
+
else if (write) {
|
|
100
|
+
this.store.linkWrite = link;
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
this.store.link = link;
|
|
104
|
+
}
|
|
105
|
+
this.store.connected = true;
|
|
106
|
+
setOptions(link);
|
|
107
|
+
}
|
|
108
|
+
catch (e) {
|
|
109
|
+
if (this.store) {
|
|
110
|
+
this.store.connected = false;
|
|
111
|
+
}
|
|
112
|
+
if (filename === ':memory:') {
|
|
113
|
+
throw new nymph_1.NotConfiguredError("It seems the config hasn't been set up correctly. Could not connect: " +
|
|
114
|
+
e?.message);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
throw new nymph_1.UnableToConnectError('Could not connect: ' + e?.message);
|
|
118
|
+
}
|
|
50
119
|
}
|
|
51
|
-
return this.connected;
|
|
52
120
|
}
|
|
53
121
|
async disconnect() {
|
|
54
|
-
if (this.connected) {
|
|
55
|
-
this.
|
|
56
|
-
|
|
57
|
-
|
|
122
|
+
if (this.store.connected) {
|
|
123
|
+
if (this.store.linkWrite && !this.config.explicitWrite) {
|
|
124
|
+
this.store.linkWrite.exec('PRAGMA optimize;');
|
|
125
|
+
this.store.linkWrite.close();
|
|
126
|
+
this.store.linkWrite = undefined;
|
|
127
|
+
}
|
|
128
|
+
if (this.config.explicitWrite) {
|
|
129
|
+
this.store.link.exec('PRAGMA optimize;');
|
|
130
|
+
}
|
|
131
|
+
this.store.link.close();
|
|
132
|
+
this.store.transactionsStarted = 0;
|
|
133
|
+
this.store.connected = false;
|
|
58
134
|
}
|
|
59
|
-
return this.connected;
|
|
135
|
+
return this.store.connected;
|
|
60
136
|
}
|
|
61
137
|
async inTransaction() {
|
|
62
|
-
return this.transactionsStarted > 0;
|
|
138
|
+
return this.store.transactionsStarted > 0;
|
|
63
139
|
}
|
|
64
140
|
isConnected() {
|
|
65
|
-
return this.connected;
|
|
66
|
-
}
|
|
67
|
-
checkReadOnlyMode() {
|
|
68
|
-
if (this.config.readonly) {
|
|
69
|
-
throw new nymph_1.InvalidParametersError('Attempt to write to SQLite3 DB in read only mode.');
|
|
70
|
-
}
|
|
141
|
+
return this.store.connected;
|
|
71
142
|
}
|
|
72
143
|
createTables(etype = null) {
|
|
73
|
-
this.checkReadOnlyMode();
|
|
74
144
|
this.startTransaction('nymph-tablecreation');
|
|
75
145
|
try {
|
|
76
146
|
if (etype != null) {
|
|
@@ -96,13 +166,13 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
96
166
|
else {
|
|
97
167
|
this.queryRun(`CREATE TABLE IF NOT EXISTS ${SQLite3Driver.escape(`${this.prefix}uids`)} ("name" TEXT PRIMARY KEY NOT NULL, "cur_uid" INTEGER NOT NULL);`);
|
|
98
168
|
}
|
|
99
|
-
this.commit('nymph-tablecreation');
|
|
100
|
-
return true;
|
|
101
169
|
}
|
|
102
170
|
catch (e) {
|
|
103
171
|
this.rollback('nymph-tablecreation');
|
|
104
172
|
throw e;
|
|
105
173
|
}
|
|
174
|
+
this.commit('nymph-tablecreation');
|
|
175
|
+
return true;
|
|
106
176
|
}
|
|
107
177
|
query(runQuery, query, etypes = []) {
|
|
108
178
|
try {
|
|
@@ -130,23 +200,32 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
130
200
|
}
|
|
131
201
|
}
|
|
132
202
|
queryIter(query, { etypes = [], params = {}, } = {}) {
|
|
133
|
-
return this.query(() => this.
|
|
203
|
+
return this.query(() => (this.store.linkWrite || this.store.link)
|
|
204
|
+
.prepare(query)
|
|
205
|
+
.iterate(params), `${query} -- ${JSON.stringify(params)}`, etypes);
|
|
134
206
|
}
|
|
135
207
|
queryGet(query, { etypes = [], params = {}, } = {}) {
|
|
136
|
-
return this.query(() => this.link.prepare(query).get(params), `${query} -- ${JSON.stringify(params)}`, etypes);
|
|
208
|
+
return this.query(() => (this.store.linkWrite || this.store.link).prepare(query).get(params), `${query} -- ${JSON.stringify(params)}`, etypes);
|
|
137
209
|
}
|
|
138
210
|
queryRun(query, { etypes = [], params = {}, } = {}) {
|
|
139
|
-
return this.query(() => this.link.prepare(query).run(params), `${query} -- ${JSON.stringify(params)}`, etypes);
|
|
211
|
+
return this.query(() => (this.store.linkWrite || this.store.link).prepare(query).run(params), `${query} -- ${JSON.stringify(params)}`, etypes);
|
|
140
212
|
}
|
|
141
213
|
async commit(name) {
|
|
142
214
|
if (name == null || typeof name !== 'string' || name.length === 0) {
|
|
143
215
|
throw new nymph_1.InvalidParametersError('Transaction commit attempted without a name.');
|
|
144
216
|
}
|
|
145
|
-
if (this.transactionsStarted === 0) {
|
|
217
|
+
if (this.store.transactionsStarted === 0) {
|
|
146
218
|
return true;
|
|
147
219
|
}
|
|
148
220
|
this.queryRun(`RELEASE SAVEPOINT ${SQLite3Driver.escape(name)};`);
|
|
149
|
-
this.transactionsStarted--;
|
|
221
|
+
this.store.transactionsStarted--;
|
|
222
|
+
if (this.store.transactionsStarted === 0 &&
|
|
223
|
+
this.store.linkWrite &&
|
|
224
|
+
!this.config.explicitWrite) {
|
|
225
|
+
this.store.linkWrite.exec('PRAGMA optimize;');
|
|
226
|
+
this.store.linkWrite.close();
|
|
227
|
+
this.store.linkWrite = undefined;
|
|
228
|
+
}
|
|
150
229
|
return true;
|
|
151
230
|
}
|
|
152
231
|
async deleteEntityByID(guid, className) {
|
|
@@ -159,7 +238,6 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
159
238
|
EntityClass = className;
|
|
160
239
|
}
|
|
161
240
|
const etype = EntityClass.ETYPE;
|
|
162
|
-
this.checkReadOnlyMode();
|
|
163
241
|
await this.startTransaction('nymph-delete');
|
|
164
242
|
try {
|
|
165
243
|
this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} WHERE "guid"=@guid;`, {
|
|
@@ -186,27 +264,28 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
186
264
|
guid,
|
|
187
265
|
},
|
|
188
266
|
});
|
|
189
|
-
await this.commit('nymph-delete');
|
|
190
|
-
if (this.nymph.config.cache) {
|
|
191
|
-
this.cleanCache(guid);
|
|
192
|
-
}
|
|
193
|
-
return true;
|
|
194
267
|
}
|
|
195
268
|
catch (e) {
|
|
196
269
|
await this.rollback('nymph-delete');
|
|
197
270
|
throw e;
|
|
198
271
|
}
|
|
272
|
+
await this.commit('nymph-delete');
|
|
273
|
+
if (this.nymph.config.cache) {
|
|
274
|
+
this.cleanCache(guid);
|
|
275
|
+
}
|
|
276
|
+
return true;
|
|
199
277
|
}
|
|
200
278
|
async deleteUID(name) {
|
|
201
279
|
if (!name) {
|
|
202
280
|
throw new nymph_1.InvalidParametersError('Name not given for UID');
|
|
203
281
|
}
|
|
204
|
-
this.
|
|
282
|
+
await this.startTransaction('nymph-delete-uid');
|
|
205
283
|
this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}uids`)} WHERE "name"=@name;`, {
|
|
206
284
|
params: {
|
|
207
285
|
name,
|
|
208
286
|
},
|
|
209
287
|
});
|
|
288
|
+
await this.commit('nymph-delete-uid');
|
|
210
289
|
return true;
|
|
211
290
|
}
|
|
212
291
|
async exportEntities(writeLine) {
|
|
@@ -274,6 +353,7 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
274
353
|
const cTable = `c${tableSuffix}`;
|
|
275
354
|
const fTable = `f${tableSuffix}`;
|
|
276
355
|
const ieTable = `ie${tableSuffix}`;
|
|
356
|
+
const sTable = `s${tableSuffix}`;
|
|
277
357
|
const sort = options.sort ?? 'cdate';
|
|
278
358
|
const queryParts = this.iterateSelectorsForQuery(formattedSelectors, (key, value, typeIsOr, typeIsNot) => {
|
|
279
359
|
const clauseNot = key.startsWith('!');
|
|
@@ -1019,18 +1099,31 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
1019
1099
|
return curQuery;
|
|
1020
1100
|
});
|
|
1021
1101
|
let sortBy;
|
|
1102
|
+
let sortByInner;
|
|
1103
|
+
let sortJoin = '';
|
|
1104
|
+
const order = options.reverse ? ' DESC' : '';
|
|
1022
1105
|
switch (sort) {
|
|
1023
1106
|
case 'mdate':
|
|
1024
|
-
sortBy =
|
|
1107
|
+
sortBy = `${eTable}."mdate"${order}`;
|
|
1108
|
+
sortByInner = `${ieTable}."mdate"${order}`;
|
|
1025
1109
|
break;
|
|
1026
1110
|
case 'cdate':
|
|
1111
|
+
sortBy = `${eTable}."cdate"${order}`;
|
|
1112
|
+
sortByInner = `${ieTable}."cdate"${order}`;
|
|
1113
|
+
break;
|
|
1027
1114
|
default:
|
|
1028
|
-
|
|
1115
|
+
const name = `param${++count.i}`;
|
|
1116
|
+
sortJoin = `LEFT JOIN (
|
|
1117
|
+
SELECT "guid", "string", "number"
|
|
1118
|
+
FROM ${SQLite3Driver.escape(this.prefix + 'comparisons_' + etype)}
|
|
1119
|
+
WHERE "name"=@${name}
|
|
1120
|
+
ORDER BY "number"${order}, "string"${order}
|
|
1121
|
+
) ${sTable} USING ("guid")`;
|
|
1122
|
+
sortBy = `${sTable}."number"${order}, ${sTable}."string"${order}`;
|
|
1123
|
+
sortByInner = sortBy;
|
|
1124
|
+
params[name] = sort;
|
|
1029
1125
|
break;
|
|
1030
1126
|
}
|
|
1031
|
-
if (options.reverse) {
|
|
1032
|
-
sortBy += ' DESC';
|
|
1033
|
-
}
|
|
1034
1127
|
let query;
|
|
1035
1128
|
if (queryParts.length) {
|
|
1036
1129
|
if (subquery) {
|
|
@@ -1063,8 +1156,9 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
1063
1156
|
else if (options.return === 'guid') {
|
|
1064
1157
|
query = `SELECT "guid"
|
|
1065
1158
|
FROM ${SQLite3Driver.escape(this.prefix + 'entities_' + etype)} ${ieTable}
|
|
1159
|
+
${sortJoin}
|
|
1066
1160
|
WHERE (${whereClause})
|
|
1067
|
-
ORDER BY ${
|
|
1161
|
+
ORDER BY ${sortByInner}, "guid"${limit}${offset}`;
|
|
1068
1162
|
}
|
|
1069
1163
|
else {
|
|
1070
1164
|
query = `SELECT
|
|
@@ -1079,13 +1173,15 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
1079
1173
|
FROM ${SQLite3Driver.escape(this.prefix + 'entities_' + etype)} ${eTable}
|
|
1080
1174
|
LEFT JOIN ${SQLite3Driver.escape(this.prefix + 'data_' + etype)} ${dTable} USING ("guid")
|
|
1081
1175
|
INNER JOIN ${SQLite3Driver.escape(this.prefix + 'comparisons_' + etype)} ${cTable} USING ("guid", "name")
|
|
1176
|
+
${sortJoin}
|
|
1082
1177
|
INNER JOIN (
|
|
1083
1178
|
SELECT "guid"
|
|
1084
1179
|
FROM ${SQLite3Driver.escape(this.prefix + 'entities_' + etype)} ${ieTable}
|
|
1180
|
+
${sortJoin}
|
|
1085
1181
|
WHERE (${whereClause})
|
|
1086
|
-
ORDER BY ${
|
|
1182
|
+
ORDER BY ${sortByInner}${limit}${offset}
|
|
1087
1183
|
) ${fTable} USING ("guid")
|
|
1088
|
-
ORDER BY ${
|
|
1184
|
+
ORDER BY ${sortBy}, ${eTable}."guid"`;
|
|
1089
1185
|
}
|
|
1090
1186
|
}
|
|
1091
1187
|
}
|
|
@@ -1117,7 +1213,8 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
1117
1213
|
else if (options.return === 'guid') {
|
|
1118
1214
|
query = `SELECT "guid"
|
|
1119
1215
|
FROM ${SQLite3Driver.escape(this.prefix + 'entities_' + etype)} ${ieTable}
|
|
1120
|
-
|
|
1216
|
+
${sortJoin}
|
|
1217
|
+
ORDER BY ${sortByInner}, "guid"${limit}${offset}`;
|
|
1121
1218
|
}
|
|
1122
1219
|
else {
|
|
1123
1220
|
if (limit || offset) {
|
|
@@ -1133,12 +1230,14 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
1133
1230
|
FROM ${SQLite3Driver.escape(this.prefix + 'entities_' + etype)} ${eTable}
|
|
1134
1231
|
LEFT JOIN ${SQLite3Driver.escape(this.prefix + 'data_' + etype)} ${dTable} USING ("guid")
|
|
1135
1232
|
INNER JOIN ${SQLite3Driver.escape(this.prefix + 'comparisons_' + etype)} c USING ("guid", "name")
|
|
1233
|
+
${sortJoin}
|
|
1136
1234
|
INNER JOIN (
|
|
1137
1235
|
SELECT "guid"
|
|
1138
1236
|
FROM ${SQLite3Driver.escape(this.prefix + 'entities_' + etype)} ${ieTable}
|
|
1139
|
-
|
|
1237
|
+
${sortJoin}
|
|
1238
|
+
ORDER BY ${sortByInner}${limit}${offset}
|
|
1140
1239
|
) ${fTable} USING ("guid")
|
|
1141
|
-
ORDER BY ${
|
|
1240
|
+
ORDER BY ${sortBy}, ${eTable}."guid"`;
|
|
1142
1241
|
}
|
|
1143
1242
|
else {
|
|
1144
1243
|
query = `SELECT
|
|
@@ -1153,7 +1252,8 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
1153
1252
|
FROM ${SQLite3Driver.escape(this.prefix + 'entities_' + etype)} ${eTable}
|
|
1154
1253
|
LEFT JOIN ${SQLite3Driver.escape(this.prefix + 'data_' + etype)} ${dTable} USING ("guid")
|
|
1155
1254
|
INNER JOIN ${SQLite3Driver.escape(this.prefix + 'comparisons_' + etype)} ${cTable} USING ("guid", "name")
|
|
1156
|
-
|
|
1255
|
+
${sortJoin}
|
|
1256
|
+
ORDER BY ${sortBy}, ${eTable}."guid"`;
|
|
1157
1257
|
}
|
|
1158
1258
|
}
|
|
1159
1259
|
}
|
|
@@ -1175,9 +1275,6 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
1175
1275
|
};
|
|
1176
1276
|
}
|
|
1177
1277
|
async getEntities(options = {}, ...selectors) {
|
|
1178
|
-
return this.getEntitiesSync(options, ...selectors);
|
|
1179
|
-
}
|
|
1180
|
-
getEntitiesSync(options = {}, ...selectors) {
|
|
1181
1278
|
const { result, process } = this.getEntitesRowLike(options, selectors, (options, formattedSelectors, etype) => this.performQuery(options, formattedSelectors, etype), () => {
|
|
1182
1279
|
const next = result.next();
|
|
1183
1280
|
return next.done ? null : next.value;
|
|
@@ -1211,7 +1308,6 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
1211
1308
|
return result?.cur_uid ?? null;
|
|
1212
1309
|
}
|
|
1213
1310
|
async import(filename) {
|
|
1214
|
-
this.checkReadOnlyMode();
|
|
1215
1311
|
try {
|
|
1216
1312
|
return this.importFromFile(filename, async (guid, tags, sdata, etype) => {
|
|
1217
1313
|
this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} WHERE "guid"=@guid;`, {
|
|
@@ -1317,14 +1413,15 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
1317
1413
|
if (name == null) {
|
|
1318
1414
|
throw new nymph_1.InvalidParametersError('Name not given for UID.');
|
|
1319
1415
|
}
|
|
1320
|
-
this.checkReadOnlyMode();
|
|
1321
1416
|
await this.startTransaction('nymph-newuid');
|
|
1417
|
+
let curUid = undefined;
|
|
1322
1418
|
try {
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1419
|
+
curUid =
|
|
1420
|
+
this.queryGet(`SELECT "cur_uid" FROM ${SQLite3Driver.escape(`${this.prefix}uids`)} WHERE "name"=@name;`, {
|
|
1421
|
+
params: {
|
|
1422
|
+
name,
|
|
1423
|
+
},
|
|
1424
|
+
})?.cur_uid ?? null;
|
|
1328
1425
|
if (curUid == null) {
|
|
1329
1426
|
curUid = 1;
|
|
1330
1427
|
this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}uids`)} ("name", "cur_uid") VALUES (@name, @curUid);`, {
|
|
@@ -1343,40 +1440,47 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
1343
1440
|
},
|
|
1344
1441
|
});
|
|
1345
1442
|
}
|
|
1346
|
-
await this.commit('nymph-newuid');
|
|
1347
|
-
return curUid;
|
|
1348
1443
|
}
|
|
1349
1444
|
catch (e) {
|
|
1350
1445
|
await this.rollback('nymph-newuid');
|
|
1351
1446
|
throw e;
|
|
1352
1447
|
}
|
|
1448
|
+
await this.commit('nymph-newuid');
|
|
1449
|
+
return curUid;
|
|
1353
1450
|
}
|
|
1354
1451
|
async renameUID(oldName, newName) {
|
|
1355
1452
|
if (oldName == null || newName == null) {
|
|
1356
1453
|
throw new nymph_1.InvalidParametersError('Name not given for UID.');
|
|
1357
1454
|
}
|
|
1358
|
-
this.
|
|
1455
|
+
await this.startTransaction('nymph-rename-uid');
|
|
1359
1456
|
this.queryRun(`UPDATE ${SQLite3Driver.escape(`${this.prefix}uids`)} SET "name"=@newName WHERE "name"=@oldName;`, {
|
|
1360
1457
|
params: {
|
|
1361
1458
|
newName,
|
|
1362
1459
|
oldName,
|
|
1363
1460
|
},
|
|
1364
1461
|
});
|
|
1462
|
+
await this.commit('nymph-rename-uid');
|
|
1365
1463
|
return true;
|
|
1366
1464
|
}
|
|
1367
1465
|
async rollback(name) {
|
|
1368
1466
|
if (name == null || typeof name !== 'string' || name.length === 0) {
|
|
1369
1467
|
throw new nymph_1.InvalidParametersError('Transaction rollback attempted without a name.');
|
|
1370
1468
|
}
|
|
1371
|
-
if (this.transactionsStarted === 0) {
|
|
1469
|
+
if (this.store.transactionsStarted === 0) {
|
|
1372
1470
|
return true;
|
|
1373
1471
|
}
|
|
1374
1472
|
this.queryRun(`ROLLBACK TO SAVEPOINT ${SQLite3Driver.escape(name)};`);
|
|
1375
|
-
this.transactionsStarted--;
|
|
1473
|
+
this.store.transactionsStarted--;
|
|
1474
|
+
if (this.store.transactionsStarted === 0 &&
|
|
1475
|
+
this.store.linkWrite &&
|
|
1476
|
+
!this.config.explicitWrite) {
|
|
1477
|
+
this.store.linkWrite.exec('PRAGMA optimize;');
|
|
1478
|
+
this.store.linkWrite.close();
|
|
1479
|
+
this.store.linkWrite = undefined;
|
|
1480
|
+
}
|
|
1376
1481
|
return true;
|
|
1377
1482
|
}
|
|
1378
1483
|
async saveEntity(entity) {
|
|
1379
|
-
this.checkReadOnlyMode();
|
|
1380
1484
|
const insertData = (guid, data, sdata, etype) => {
|
|
1381
1485
|
const runInsertQuery = (name, value, svalue) => {
|
|
1382
1486
|
if (value === undefined) {
|
|
@@ -1424,8 +1528,13 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
1424
1528
|
runInsertQuery(name, JSON.parse(sdata[name]), sdata[name]);
|
|
1425
1529
|
}
|
|
1426
1530
|
};
|
|
1531
|
+
let inTransaction = false;
|
|
1427
1532
|
try {
|
|
1428
1533
|
return this.saveEntityRowLike(entity, async (_entity, guid, tags, data, sdata, cdate, etype) => {
|
|
1534
|
+
if (Object.keys(data).length === 0 &&
|
|
1535
|
+
Object.keys(sdata).length === 0) {
|
|
1536
|
+
return false;
|
|
1537
|
+
}
|
|
1429
1538
|
this.queryRun(`INSERT INTO ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} ("guid", "tags", "cdate", "mdate") VALUES (@guid, @tags, @cdate, @cdate);`, {
|
|
1430
1539
|
etypes: [etype],
|
|
1431
1540
|
params: {
|
|
@@ -1437,6 +1546,10 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
1437
1546
|
insertData(guid, data, sdata, etype);
|
|
1438
1547
|
return true;
|
|
1439
1548
|
}, async (entity, guid, tags, data, sdata, mdate, etype) => {
|
|
1549
|
+
if (Object.keys(data).length === 0 &&
|
|
1550
|
+
Object.keys(sdata).length === 0) {
|
|
1551
|
+
return false;
|
|
1552
|
+
}
|
|
1440
1553
|
const info = this.queryRun(`UPDATE ${SQLite3Driver.escape(`${this.prefix}entities_${etype}`)} SET "tags"=@tags, "mdate"=@mdate WHERE "guid"=@guid AND "mdate" <= @emdate;`, {
|
|
1441
1554
|
etypes: [etype],
|
|
1442
1555
|
params: {
|
|
@@ -1472,18 +1585,24 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
1472
1585
|
return success;
|
|
1473
1586
|
}, async () => {
|
|
1474
1587
|
await this.startTransaction('nymph-save');
|
|
1588
|
+
inTransaction = true;
|
|
1475
1589
|
}, async (success) => {
|
|
1476
|
-
if (
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1590
|
+
if (inTransaction) {
|
|
1591
|
+
inTransaction = false;
|
|
1592
|
+
if (success) {
|
|
1593
|
+
await this.commit('nymph-save');
|
|
1594
|
+
}
|
|
1595
|
+
else {
|
|
1596
|
+
await this.rollback('nymph-save');
|
|
1597
|
+
}
|
|
1481
1598
|
}
|
|
1482
1599
|
return success;
|
|
1483
1600
|
});
|
|
1484
1601
|
}
|
|
1485
1602
|
catch (e) {
|
|
1486
|
-
|
|
1603
|
+
if (inTransaction) {
|
|
1604
|
+
await this.rollback('nymph-save');
|
|
1605
|
+
}
|
|
1487
1606
|
throw e;
|
|
1488
1607
|
}
|
|
1489
1608
|
}
|
|
@@ -1491,7 +1610,7 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
1491
1610
|
if (name == null) {
|
|
1492
1611
|
throw new nymph_1.InvalidParametersError('Name not given for UID.');
|
|
1493
1612
|
}
|
|
1494
|
-
this.
|
|
1613
|
+
await this.startTransaction('nymph-set-uid');
|
|
1495
1614
|
this.queryRun(`DELETE FROM ${SQLite3Driver.escape(`${this.prefix}uids`)} WHERE "name"=@name;`, {
|
|
1496
1615
|
params: {
|
|
1497
1616
|
name,
|
|
@@ -1503,14 +1622,18 @@ class SQLite3Driver extends nymph_1.NymphDriver {
|
|
|
1503
1622
|
curUid,
|
|
1504
1623
|
},
|
|
1505
1624
|
});
|
|
1625
|
+
await this.commit('nymph-set-uid');
|
|
1506
1626
|
return true;
|
|
1507
1627
|
}
|
|
1508
1628
|
async startTransaction(name) {
|
|
1509
1629
|
if (name == null || typeof name !== 'string' || name.length === 0) {
|
|
1510
1630
|
throw new nymph_1.InvalidParametersError('Transaction start attempted without a name.');
|
|
1511
1631
|
}
|
|
1632
|
+
if (!this.config.explicitWrite && !this.store.linkWrite) {
|
|
1633
|
+
this._connect(true);
|
|
1634
|
+
}
|
|
1512
1635
|
this.queryRun(`SAVEPOINT ${SQLite3Driver.escape(name)};`);
|
|
1513
|
-
this.transactionsStarted++;
|
|
1636
|
+
this.store.transactionsStarted++;
|
|
1514
1637
|
return this.nymph;
|
|
1515
1638
|
}
|
|
1516
1639
|
}
|