@quatrain/backend-sqlite 1.1.5 → 1.1.6
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/dist/SQLiteAdapter.js
CHANGED
|
@@ -182,63 +182,61 @@ class SQLiteAdapter extends backend_1.AbstractBackendAdapter {
|
|
|
182
182
|
*/
|
|
183
183
|
create(dataObject, desiredUid) {
|
|
184
184
|
return __awaiter(this, void 0, void 0, function* () {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
throw new backend_1.BackendError(`Data object already has an uid and can't be created`);
|
|
189
|
-
}
|
|
190
|
-
const uid = desiredUid || (0, node_crypto_1.randomUUID)();
|
|
191
|
-
// Make sure table exists
|
|
192
|
-
yield this._ensureTable(dataObject);
|
|
193
|
-
// Set uid before middlewares so they can use it
|
|
194
|
-
dataObject.uri.path = this._buildPath(dataObject, uid);
|
|
195
|
-
// execute middlewares
|
|
196
|
-
yield this.executeMiddlewares(dataObject, backend_1.BackendAction.CREATE, 'before', {
|
|
197
|
-
useDateFormat: true,
|
|
198
|
-
});
|
|
199
|
-
const data = dataObject.toJSON({
|
|
200
|
-
withoutURIData: true,
|
|
201
|
-
converters: {
|
|
202
|
-
datetime: (v) => (v ? new Date(v).getTime() : v), // Store as timestamp in SQLite
|
|
203
|
-
},
|
|
204
|
-
});
|
|
205
|
-
const db = yield this._connect();
|
|
206
|
-
const collection = this.getCollection(dataObject);
|
|
207
|
-
let columns = ['id'];
|
|
208
|
-
let placeholders = ['?'];
|
|
209
|
-
let values = [uid];
|
|
210
|
-
Object.entries(data).forEach(([key, value]) => {
|
|
211
|
-
columns.push(`"${key}"`);
|
|
212
|
-
placeholders.push('?');
|
|
213
|
-
// Convert arrays and objects to JSON strings
|
|
214
|
-
if (Array.isArray(value) ||
|
|
215
|
-
(typeof value === 'object' && value !== null)) {
|
|
216
|
-
values.push(JSON.stringify(value));
|
|
217
|
-
}
|
|
218
|
-
else {
|
|
219
|
-
values.push(value);
|
|
220
|
-
}
|
|
221
|
-
});
|
|
222
|
-
const query = `INSERT INTO ${collection === null || collection === void 0 ? void 0 : collection.toLowerCase()} (${columns.join(', ')})
|
|
223
|
-
VALUES (${placeholders.join(', ')})`;
|
|
224
|
-
backend_1.Backend.debug(`[SQLA] ${query}`);
|
|
225
|
-
backend_1.Backend.debug(`[SQLA] Values ${JSON.stringify(values)}`);
|
|
226
|
-
yield db.run(query, values);
|
|
227
|
-
// uri.path is already set before middlewares
|
|
228
|
-
dataObject.uri.label = data && Reflect.get(data, 'name');
|
|
229
|
-
dataObject.isPersisted(true);
|
|
230
|
-
backend_1.Backend.info(`[SQLA] Saved object "${data.name}" at path ${dataObject.path}`);
|
|
231
|
-
yield this.executeMiddlewares(dataObject, backend_1.BackendAction.CREATE, 'after', {
|
|
232
|
-
useDateFormat: true,
|
|
233
|
-
});
|
|
234
|
-
resolve(dataObject);
|
|
235
|
-
}
|
|
236
|
-
catch (err) {
|
|
237
|
-
console.error(err);
|
|
238
|
-
backend_1.Backend.error(err.message);
|
|
239
|
-
reject(new backend_1.BackendError(err.message));
|
|
185
|
+
try {
|
|
186
|
+
if (dataObject.uid) {
|
|
187
|
+
throw new backend_1.BackendError(`Data object already has an uid and can't be created`);
|
|
240
188
|
}
|
|
241
|
-
|
|
189
|
+
const uid = desiredUid || (0, node_crypto_1.randomUUID)();
|
|
190
|
+
// Make sure table exists
|
|
191
|
+
yield this._ensureTable(dataObject);
|
|
192
|
+
// Set uid before middlewares so they can use it
|
|
193
|
+
dataObject.uri.path = this._buildPath(dataObject, uid);
|
|
194
|
+
// execute middlewares
|
|
195
|
+
yield this.executeMiddlewares(dataObject, backend_1.BackendAction.CREATE, 'before', {
|
|
196
|
+
useDateFormat: true,
|
|
197
|
+
});
|
|
198
|
+
const data = dataObject.toJSON({
|
|
199
|
+
withoutURIData: true,
|
|
200
|
+
converters: {
|
|
201
|
+
datetime: (v) => (v ? new Date(v).getTime() : v), // Store as timestamp in SQLite
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
const db = yield this._connect();
|
|
205
|
+
const collection = this.getCollection(dataObject);
|
|
206
|
+
let columns = ['id'];
|
|
207
|
+
let placeholders = ['?'];
|
|
208
|
+
let values = [uid];
|
|
209
|
+
Object.entries(data).forEach(([key, value]) => {
|
|
210
|
+
columns.push(`"${key}"`);
|
|
211
|
+
placeholders.push('?');
|
|
212
|
+
// Convert arrays and objects to JSON strings
|
|
213
|
+
if (Array.isArray(value) ||
|
|
214
|
+
(typeof value === 'object' && value !== null)) {
|
|
215
|
+
values.push(JSON.stringify(value));
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
values.push(value);
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
const query = `INSERT INTO ${collection === null || collection === void 0 ? void 0 : collection.toLowerCase()} (${columns.join(', ')})
|
|
222
|
+
VALUES (${placeholders.join(', ')})`;
|
|
223
|
+
backend_1.Backend.debug(`[SQLA] ${query}`);
|
|
224
|
+
backend_1.Backend.debug(`[SQLA] Values ${JSON.stringify(values)}`);
|
|
225
|
+
yield db.run(query, values);
|
|
226
|
+
// uri.path is already set before middlewares
|
|
227
|
+
dataObject.uri.label = data && Reflect.get(data, 'name');
|
|
228
|
+
dataObject.isPersisted(true);
|
|
229
|
+
backend_1.Backend.info(`[SQLA] Saved object "${data.name}" at path ${dataObject.path}`);
|
|
230
|
+
yield this.executeMiddlewares(dataObject, backend_1.BackendAction.CREATE, 'after', {
|
|
231
|
+
useDateFormat: true,
|
|
232
|
+
});
|
|
233
|
+
return dataObject;
|
|
234
|
+
}
|
|
235
|
+
catch (err) {
|
|
236
|
+
console.error(err);
|
|
237
|
+
backend_1.Backend.error(err.message);
|
|
238
|
+
throw new backend_1.BackendError(err.message);
|
|
239
|
+
}
|
|
242
240
|
});
|
|
243
241
|
}
|
|
244
242
|
read(dataObject) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quatrain/backend-sqlite",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.6",
|
|
4
4
|
"license": "AGPL-3.0-only",
|
|
5
5
|
"description": "Backend adapter for SQLite",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
},
|
|
21
21
|
"author": "Quatrain Développement SAS <developers@quatrain.com>",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@quatrain/backend": "^1.2.
|
|
24
|
-
"@quatrain/core": "^1.2.
|
|
23
|
+
"@quatrain/backend": "^1.2.4",
|
|
24
|
+
"@quatrain/core": "^1.2.5",
|
|
25
25
|
"sqlite": "^5.1.1",
|
|
26
26
|
"sqlite3": "^5.1.7"
|
|
27
27
|
},
|
package/src/SQLiteAdapter.ts
CHANGED
|
@@ -214,87 +214,85 @@ export class SQLiteAdapter extends AbstractBackendAdapter {
|
|
|
214
214
|
dataObject: DataObjectClass<any>,
|
|
215
215
|
desiredUid: string | undefined
|
|
216
216
|
): Promise<DataObjectClass<any>> {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
217
|
+
try {
|
|
218
|
+
if (dataObject.uid) {
|
|
219
|
+
throw new BackendError(
|
|
220
|
+
`Data object already has an uid and can't be created`
|
|
221
|
+
)
|
|
222
|
+
}
|
|
224
223
|
|
|
225
|
-
|
|
224
|
+
const uid = desiredUid || randomUUID()
|
|
226
225
|
|
|
227
|
-
|
|
228
|
-
|
|
226
|
+
// Make sure table exists
|
|
227
|
+
await this._ensureTable(dataObject)
|
|
229
228
|
|
|
230
|
-
|
|
231
|
-
|
|
229
|
+
// Set uid before middlewares so they can use it
|
|
230
|
+
dataObject.uri.path = this._buildPath(dataObject, uid)
|
|
232
231
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
232
|
+
// execute middlewares
|
|
233
|
+
await this.executeMiddlewares(dataObject, BackendAction.CREATE, 'before', {
|
|
234
|
+
useDateFormat: true,
|
|
235
|
+
})
|
|
237
236
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
237
|
+
const data = dataObject.toJSON({
|
|
238
|
+
withoutURIData: true,
|
|
239
|
+
converters: {
|
|
240
|
+
datetime: (v: any) => (v ? new Date(v).getTime() : v), // Store as timestamp in SQLite
|
|
241
|
+
},
|
|
242
|
+
})
|
|
244
243
|
|
|
245
|
-
|
|
246
|
-
|
|
244
|
+
const db = await this._connect()
|
|
245
|
+
const collection = this.getCollection(dataObject)
|
|
247
246
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
247
|
+
let columns = ['id']
|
|
248
|
+
let placeholders = ['?']
|
|
249
|
+
let values = [uid]
|
|
251
250
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
251
|
+
Object.entries(data).forEach(
|
|
252
|
+
([key, value]: [key: string, value: any]) => {
|
|
253
|
+
columns.push(`"${key}"`)
|
|
254
|
+
placeholders.push('?')
|
|
256
255
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
}
|
|
256
|
+
// Convert arrays and objects to JSON strings
|
|
257
|
+
if (
|
|
258
|
+
Array.isArray(value) ||
|
|
259
|
+
(typeof value === 'object' && value !== null)
|
|
260
|
+
) {
|
|
261
|
+
values.push(JSON.stringify(value))
|
|
262
|
+
} else {
|
|
263
|
+
values.push(value)
|
|
266
264
|
}
|
|
267
|
-
|
|
265
|
+
}
|
|
266
|
+
)
|
|
268
267
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
268
|
+
const query = `INSERT INTO ${collection?.toLowerCase()} (${columns.join(
|
|
269
|
+
', '
|
|
270
|
+
)})
|
|
271
|
+
VALUES (${placeholders.join(', ')})`
|
|
273
272
|
|
|
274
|
-
|
|
275
|
-
|
|
273
|
+
Backend.debug(`[SQLA] ${query}`)
|
|
274
|
+
Backend.debug(`[SQLA] Values ${JSON.stringify(values)}`)
|
|
276
275
|
|
|
277
|
-
|
|
276
|
+
await db.run(query, values)
|
|
278
277
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
278
|
+
// uri.path is already set before middlewares
|
|
279
|
+
dataObject.uri.label = data && Reflect.get(data, 'name')
|
|
280
|
+
dataObject.isPersisted(true)
|
|
282
281
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
282
|
+
Backend.info(
|
|
283
|
+
`[SQLA] Saved object "${data.name}" at path ${dataObject.path}`
|
|
284
|
+
)
|
|
286
285
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
286
|
+
await this.executeMiddlewares(dataObject, BackendAction.CREATE, 'after', {
|
|
287
|
+
useDateFormat: true,
|
|
288
|
+
})
|
|
290
289
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
})
|
|
290
|
+
return dataObject
|
|
291
|
+
} catch (err) {
|
|
292
|
+
console.error(err)
|
|
293
|
+
Backend.error((err as Error).message)
|
|
294
|
+
throw new BackendError((err as Error).message)
|
|
295
|
+
}
|
|
298
296
|
}
|
|
299
297
|
|
|
300
298
|
async read(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>> {
|
|
@@ -208,7 +208,7 @@ describe('SQLiteAdapter Tests', () => {
|
|
|
208
208
|
testUser._.firstname = `SearchUser${i}`
|
|
209
209
|
testUser._.lastname = 'SearchTest'
|
|
210
210
|
testUser._.email = `searchuser${i}@test.com`
|
|
211
|
-
testUser._.password = 'password'
|
|
211
|
+
testUser._.password = 'password' // NOSONAR
|
|
212
212
|
await adapter.create(testUser.dataObject, undefined)
|
|
213
213
|
}
|
|
214
214
|
})
|
|
@@ -314,7 +314,7 @@ describe('SQLiteAdapter Tests', () => {
|
|
|
314
314
|
newUser._.firstname = 'Auto'
|
|
315
315
|
newUser._.lastname = 'Table'
|
|
316
316
|
newUser._.email = 'auto@table.com'
|
|
317
|
-
newUser._.password = 'password'
|
|
317
|
+
newUser._.password = 'password' // NOSONAR
|
|
318
318
|
|
|
319
319
|
// This should automatically create the table
|
|
320
320
|
const result = await adapter.create(newUser.dataObject, undefined)
|
|
@@ -327,7 +327,7 @@ describe('SQLiteAdapter Tests', () => {
|
|
|
327
327
|
user._.firstname = 'Table'
|
|
328
328
|
user._.lastname = 'Exists'
|
|
329
329
|
user._.email = 'table@exists.com'
|
|
330
|
-
user._.password = 'password'
|
|
330
|
+
user._.password = 'password' // NOSONAR
|
|
331
331
|
|
|
332
332
|
await adapter.create(user.dataObject, undefined)
|
|
333
333
|
|
|
@@ -404,7 +404,7 @@ describe('SQLiteAdapter Tests', () => {
|
|
|
404
404
|
testUser._.firstname = 'Ref'
|
|
405
405
|
testUser._.lastname = 'Test'
|
|
406
406
|
testUser._.email = 'ref@test.com'
|
|
407
|
-
testUser._.password = 'password'
|
|
407
|
+
testUser._.password = 'password' // NOSONAR
|
|
408
408
|
testUser._.entity = entity
|
|
409
409
|
|
|
410
410
|
const result = await adapter.create(testUser.dataObject, undefined)
|
|
@@ -21,7 +21,7 @@ describe('SQLite Search Operations', () => {
|
|
|
21
21
|
testUser._.firstname = `TestUser${i}`
|
|
22
22
|
testUser._.lastname = i % 2 === 0 ? 'Even' : 'Odd'
|
|
23
23
|
testUser._.email = `testuser${i}@example.com`
|
|
24
|
-
testUser._.password = 'password'
|
|
24
|
+
testUser._.password = 'password' // NOSONAR
|
|
25
25
|
|
|
26
26
|
// Some users belong to the entity
|
|
27
27
|
if (i < 5) {
|