@form-engine-ts/storage-sqlite 7.18.1 → 8.0.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.
- package/README.md +3 -0
- package/dist/index.cjs +59 -0
- package/dist/index.js +61 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -28,3 +28,6 @@ const storage = createSqliteStorage({ db: executor, autoMigrate: true });
|
|
|
28
28
|
`better-sqlite3` is only an example and is not a runtime dependency. Wrap `bun:sqlite`, libSQL, or another driver in the same `run/get/all` interface; return values may be synchronous or promises. The caller owns the database lifecycle and transaction policy.
|
|
29
29
|
|
|
30
30
|
`autoMigrate` defaults to `false`; when enabled, idempotent table/index DDL runs lazily once. Schemas use a `(form_id, form_version)` primary key. Complete schemas and submissions are stored as JSON text alongside searchable response metadata. `listSubmissions` accepts inclusive `since`/`until` ISO 8601 boundaries and orders by timestamp then ID. Schema deletion does not cascade; form-scoped and full clears retain the configured tables.
|
|
31
|
+
|
|
32
|
+
`saveSubmissionWithinLimit(submission, maxResponses, options?)` requires `SqliteExecutor.transaction` so the capacity
|
|
33
|
+
check and insert commit atomically. Without it, the method throws an error with `code: "transaction_unsupported"`.
|
package/dist/index.cjs
CHANGED
|
@@ -214,6 +214,43 @@ function createSqliteStorage(options) {
|
|
|
214
214
|
[stored.id, stored.formId, stored.formVersion, stored.locale, stored.submittedAt, JSON.stringify(stored)]
|
|
215
215
|
);
|
|
216
216
|
},
|
|
217
|
+
async saveSubmissionWithinLimit(submission, maxResponses, saveOptions = {}) {
|
|
218
|
+
if (!Number.isInteger(maxResponses) || maxResponses < 1)
|
|
219
|
+
throw new RangeError("maxResponses must be a positive integer.");
|
|
220
|
+
await ensureReady();
|
|
221
|
+
if (options.db.transaction === void 0)
|
|
222
|
+
throw Object.assign(new Error("SQLite transactions are required for atomic response limits."), {
|
|
223
|
+
code: "transaction_unsupported"
|
|
224
|
+
});
|
|
225
|
+
const stored = parseSubmission(submission, `input "${String(submission?.id)}"`);
|
|
226
|
+
const payloadHash = await (0, import_core.hashFormSubmissionPayload)(stored);
|
|
227
|
+
return options.db.transaction(async (db) => {
|
|
228
|
+
const existingRow = await db.get(
|
|
229
|
+
`SELECT response_id, form_id, form_version, locale, submitted_at, submission_json
|
|
230
|
+
FROM ${responsesTable} WHERE response_id = ?`,
|
|
231
|
+
[stored.id]
|
|
232
|
+
);
|
|
233
|
+
if (existingRow !== void 0) {
|
|
234
|
+
if (saveOptions.idempotent !== true) throw new Error(`A submission with ID "${stored.id}" already exists.`);
|
|
235
|
+
const existing = parseSubmissionRow(existingRow, 0);
|
|
236
|
+
const existingPayloadHash = await (0, import_core.hashFormSubmissionPayload)(existing);
|
|
237
|
+
if (existingPayloadHash === payloadHash) return { status: "duplicate", submission: existing, payloadHash };
|
|
238
|
+
return { status: "conflict", submissionId: stored.id, payloadHash, existingPayloadHash };
|
|
239
|
+
}
|
|
240
|
+
const row = await db.get(
|
|
241
|
+
`SELECT COUNT(*) AS count FROM ${responsesTable} WHERE form_id = ? AND form_version = ?`,
|
|
242
|
+
[stored.formId, stored.formVersion]
|
|
243
|
+
);
|
|
244
|
+
if (Number(row?.count ?? 0) >= maxResponses) return { status: "limit_reached" };
|
|
245
|
+
await db.run(
|
|
246
|
+
`INSERT INTO ${responsesTable}
|
|
247
|
+
(response_id, form_id, form_version, locale, submitted_at, submission_json)
|
|
248
|
+
VALUES (?, ?, ?, ?, ?, ?)`,
|
|
249
|
+
[stored.id, stored.formId, stored.formVersion, stored.locale, stored.submittedAt, JSON.stringify(stored)]
|
|
250
|
+
);
|
|
251
|
+
if (saveOptions.idempotent === true) return { status: "created", submission: stored, payloadHash };
|
|
252
|
+
});
|
|
253
|
+
},
|
|
217
254
|
async listSubmissions(formId, formVersion, queryOptions) {
|
|
218
255
|
await ensureReady();
|
|
219
256
|
const conditions = ["form_id = ?"];
|
|
@@ -237,6 +274,28 @@ function createSqliteStorage(options) {
|
|
|
237
274
|
);
|
|
238
275
|
return rows.map(parseSubmissionRow);
|
|
239
276
|
},
|
|
277
|
+
async countSubmissions(formId, formVersion, queryOptions) {
|
|
278
|
+
await ensureReady();
|
|
279
|
+
const conditions = ["form_id = ?"];
|
|
280
|
+
const params = [formId];
|
|
281
|
+
if (formVersion !== void 0) {
|
|
282
|
+
conditions.push("form_version = ?");
|
|
283
|
+
params.push(formVersion);
|
|
284
|
+
}
|
|
285
|
+
if (queryOptions?.since !== void 0) {
|
|
286
|
+
conditions.push("submitted_at >= ?");
|
|
287
|
+
params.push(queryOptions.since);
|
|
288
|
+
}
|
|
289
|
+
if (queryOptions?.until !== void 0) {
|
|
290
|
+
conditions.push("submitted_at <= ?");
|
|
291
|
+
params.push(queryOptions.until);
|
|
292
|
+
}
|
|
293
|
+
const row = await options.db.get(
|
|
294
|
+
`SELECT COUNT(*) AS count FROM ${responsesTable} WHERE ${conditions.join(" AND ")}`,
|
|
295
|
+
params
|
|
296
|
+
);
|
|
297
|
+
return Number(row?.count ?? 0);
|
|
298
|
+
},
|
|
240
299
|
async deleteSubmission(submissionId) {
|
|
241
300
|
await ensureReady();
|
|
242
301
|
await options.db.run(`DELETE FROM ${responsesTable} WHERE response_id = ?`, [submissionId]);
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import {
|
|
3
3
|
assertValidFormSchema,
|
|
4
|
-
createFormLifecycleAdapter
|
|
4
|
+
createFormLifecycleAdapter,
|
|
5
|
+
hashFormSubmissionPayload
|
|
5
6
|
} from "@form-engine-ts/core";
|
|
6
7
|
function isRecord(value) {
|
|
7
8
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
@@ -193,6 +194,43 @@ function createSqliteStorage(options) {
|
|
|
193
194
|
[stored.id, stored.formId, stored.formVersion, stored.locale, stored.submittedAt, JSON.stringify(stored)]
|
|
194
195
|
);
|
|
195
196
|
},
|
|
197
|
+
async saveSubmissionWithinLimit(submission, maxResponses, saveOptions = {}) {
|
|
198
|
+
if (!Number.isInteger(maxResponses) || maxResponses < 1)
|
|
199
|
+
throw new RangeError("maxResponses must be a positive integer.");
|
|
200
|
+
await ensureReady();
|
|
201
|
+
if (options.db.transaction === void 0)
|
|
202
|
+
throw Object.assign(new Error("SQLite transactions are required for atomic response limits."), {
|
|
203
|
+
code: "transaction_unsupported"
|
|
204
|
+
});
|
|
205
|
+
const stored = parseSubmission(submission, `input "${String(submission?.id)}"`);
|
|
206
|
+
const payloadHash = await hashFormSubmissionPayload(stored);
|
|
207
|
+
return options.db.transaction(async (db) => {
|
|
208
|
+
const existingRow = await db.get(
|
|
209
|
+
`SELECT response_id, form_id, form_version, locale, submitted_at, submission_json
|
|
210
|
+
FROM ${responsesTable} WHERE response_id = ?`,
|
|
211
|
+
[stored.id]
|
|
212
|
+
);
|
|
213
|
+
if (existingRow !== void 0) {
|
|
214
|
+
if (saveOptions.idempotent !== true) throw new Error(`A submission with ID "${stored.id}" already exists.`);
|
|
215
|
+
const existing = parseSubmissionRow(existingRow, 0);
|
|
216
|
+
const existingPayloadHash = await hashFormSubmissionPayload(existing);
|
|
217
|
+
if (existingPayloadHash === payloadHash) return { status: "duplicate", submission: existing, payloadHash };
|
|
218
|
+
return { status: "conflict", submissionId: stored.id, payloadHash, existingPayloadHash };
|
|
219
|
+
}
|
|
220
|
+
const row = await db.get(
|
|
221
|
+
`SELECT COUNT(*) AS count FROM ${responsesTable} WHERE form_id = ? AND form_version = ?`,
|
|
222
|
+
[stored.formId, stored.formVersion]
|
|
223
|
+
);
|
|
224
|
+
if (Number(row?.count ?? 0) >= maxResponses) return { status: "limit_reached" };
|
|
225
|
+
await db.run(
|
|
226
|
+
`INSERT INTO ${responsesTable}
|
|
227
|
+
(response_id, form_id, form_version, locale, submitted_at, submission_json)
|
|
228
|
+
VALUES (?, ?, ?, ?, ?, ?)`,
|
|
229
|
+
[stored.id, stored.formId, stored.formVersion, stored.locale, stored.submittedAt, JSON.stringify(stored)]
|
|
230
|
+
);
|
|
231
|
+
if (saveOptions.idempotent === true) return { status: "created", submission: stored, payloadHash };
|
|
232
|
+
});
|
|
233
|
+
},
|
|
196
234
|
async listSubmissions(formId, formVersion, queryOptions) {
|
|
197
235
|
await ensureReady();
|
|
198
236
|
const conditions = ["form_id = ?"];
|
|
@@ -216,6 +254,28 @@ function createSqliteStorage(options) {
|
|
|
216
254
|
);
|
|
217
255
|
return rows.map(parseSubmissionRow);
|
|
218
256
|
},
|
|
257
|
+
async countSubmissions(formId, formVersion, queryOptions) {
|
|
258
|
+
await ensureReady();
|
|
259
|
+
const conditions = ["form_id = ?"];
|
|
260
|
+
const params = [formId];
|
|
261
|
+
if (formVersion !== void 0) {
|
|
262
|
+
conditions.push("form_version = ?");
|
|
263
|
+
params.push(formVersion);
|
|
264
|
+
}
|
|
265
|
+
if (queryOptions?.since !== void 0) {
|
|
266
|
+
conditions.push("submitted_at >= ?");
|
|
267
|
+
params.push(queryOptions.since);
|
|
268
|
+
}
|
|
269
|
+
if (queryOptions?.until !== void 0) {
|
|
270
|
+
conditions.push("submitted_at <= ?");
|
|
271
|
+
params.push(queryOptions.until);
|
|
272
|
+
}
|
|
273
|
+
const row = await options.db.get(
|
|
274
|
+
`SELECT COUNT(*) AS count FROM ${responsesTable} WHERE ${conditions.join(" AND ")}`,
|
|
275
|
+
params
|
|
276
|
+
);
|
|
277
|
+
return Number(row?.count ?? 0);
|
|
278
|
+
},
|
|
219
279
|
async deleteSubmission(submissionId) {
|
|
220
280
|
await ensureReady();
|
|
221
281
|
await options.db.run(`DELETE FROM ${responsesTable} WHERE response_id = ?`, [submissionId]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@form-engine-ts/storage-sqlite",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.1",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -40,10 +40,10 @@
|
|
|
40
40
|
"typescript"
|
|
41
41
|
],
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@form-engine-ts/core": "
|
|
43
|
+
"@form-engine-ts/core": "8.0.1"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@form-engine-ts/storage": "
|
|
46
|
+
"@form-engine-ts/storage": "8.0.1"
|
|
47
47
|
},
|
|
48
48
|
"scripts": {
|
|
49
49
|
"build": "tsup src/index.ts --format esm,cjs --dts --clean --external @form-engine-ts/core",
|