@form-engine-ts/storage-mongodb 1.0.0
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/LICENSE +21 -0
- package/README.md +23 -0
- package/dist/index.cjs +127 -0
- package/dist/index.d.cts +11 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +102 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 nitta-a
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# @form-engine-ts/storage-mongodb
|
|
2
|
+
|
|
3
|
+
MongoDB Native Driver implementation of the complete form-engine-ts storage contract.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @form-engine-ts/core @form-engine-ts/storage-mongodb mongodb
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick start
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createMongoDbStorage } from "@form-engine-ts/storage-mongodb";
|
|
15
|
+
import { MongoClient } from "mongodb";
|
|
16
|
+
|
|
17
|
+
const client = await new MongoClient(process.env.MONGODB_URI!).connect();
|
|
18
|
+
const storage = createMongoDbStorage({ db: client.db("forms") });
|
|
19
|
+
|
|
20
|
+
await storage.saveSchema(schema);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
The caller owns the MongoDB connection lifecycle. Collection names can be customized in the factory options.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
createMongoDbStorage: () => createMongoDbStorage
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var import_core = require("@form-engine-ts/core");
|
|
27
|
+
function cloneJson(value) {
|
|
28
|
+
return JSON.parse(JSON.stringify(value));
|
|
29
|
+
}
|
|
30
|
+
function isRecord(value) {
|
|
31
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
32
|
+
}
|
|
33
|
+
function isFormValue(value) {
|
|
34
|
+
return value === void 0 || typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value) || Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
35
|
+
}
|
|
36
|
+
function parseSubmission(value, location) {
|
|
37
|
+
if (!isRecord(value) || typeof value.id !== "string" || typeof value.formId !== "string" || !Number.isInteger(value.formVersion) || typeof value.locale !== "string" || typeof value.submittedAt !== "string" || !isRecord(value.values) || !Object.values(value.values).every(isFormValue)) {
|
|
38
|
+
throw new Error(`MongoDB submission at ${location} is invalid.`);
|
|
39
|
+
}
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
function schemaDocumentId(formId, formVersion) {
|
|
43
|
+
return `schema:${encodeURIComponent(formId)}:${formVersion}`;
|
|
44
|
+
}
|
|
45
|
+
function parseSchemaDocument(document) {
|
|
46
|
+
try {
|
|
47
|
+
(0, import_core.assertValidFormSchema)(document.schema);
|
|
48
|
+
} catch (cause) {
|
|
49
|
+
throw new Error(`MongoDB schema document "${String(document._id)}" is invalid.`, { cause });
|
|
50
|
+
}
|
|
51
|
+
if (document._id !== schemaDocumentId(document.schema.id, document.schema.version) || document.formId !== document.schema.id || document.formVersion !== document.schema.version) {
|
|
52
|
+
throw new Error(`MongoDB schema document "${String(document._id)}" has inconsistent metadata.`);
|
|
53
|
+
}
|
|
54
|
+
return cloneJson(document.schema);
|
|
55
|
+
}
|
|
56
|
+
function parseSubmissionDocument(document) {
|
|
57
|
+
const submission = parseSubmission(document.submission, `document "${String(document._id)}"`);
|
|
58
|
+
if (document._id !== submission.id || document.formId !== submission.formId || document.formVersion !== submission.formVersion || document.submittedAt !== submission.submittedAt) {
|
|
59
|
+
throw new Error(`MongoDB submission document "${String(document._id)}" has inconsistent metadata.`);
|
|
60
|
+
}
|
|
61
|
+
return cloneJson(submission);
|
|
62
|
+
}
|
|
63
|
+
function collectionName(value, fallback, optionName) {
|
|
64
|
+
if (value === void 0) return fallback;
|
|
65
|
+
if (value.trim().length === 0) throw new TypeError(`${optionName} must be a non-empty string.`);
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
function createMongoDbStorage(options) {
|
|
69
|
+
if (options?.db === void 0) throw new TypeError("db is required.");
|
|
70
|
+
const schemasCollectionName = collectionName(options.schemasCollectionName, "form_schemas", "schemasCollectionName");
|
|
71
|
+
const responsesCollectionName = collectionName(
|
|
72
|
+
options.responsesCollectionName,
|
|
73
|
+
"form_responses",
|
|
74
|
+
"responsesCollectionName"
|
|
75
|
+
);
|
|
76
|
+
const schemas = options.db.collection(schemasCollectionName);
|
|
77
|
+
const submissions = options.db.collection(responsesCollectionName);
|
|
78
|
+
return {
|
|
79
|
+
async saveSchema(schema) {
|
|
80
|
+
(0, import_core.assertValidFormSchema)(schema);
|
|
81
|
+
const stored = cloneJson(schema);
|
|
82
|
+
await schemas.updateOne(
|
|
83
|
+
{ _id: schemaDocumentId(schema.id, schema.version) },
|
|
84
|
+
{ $set: { formId: schema.id, formVersion: schema.version, schema: stored } },
|
|
85
|
+
{ upsert: true }
|
|
86
|
+
);
|
|
87
|
+
},
|
|
88
|
+
async getSchema(formId, formVersion) {
|
|
89
|
+
const document = await schemas.findOne({ _id: schemaDocumentId(formId, formVersion) });
|
|
90
|
+
return document === null ? null : parseSchemaDocument(document);
|
|
91
|
+
},
|
|
92
|
+
async listSchemas() {
|
|
93
|
+
const documents = await schemas.find({}).toArray();
|
|
94
|
+
return documents.map(parseSchemaDocument).sort((left, right) => left.id.localeCompare(right.id) || left.version - right.version);
|
|
95
|
+
},
|
|
96
|
+
async deleteSchema(formId, formVersion) {
|
|
97
|
+
await schemas.deleteOne({ _id: schemaDocumentId(formId, formVersion) });
|
|
98
|
+
},
|
|
99
|
+
async saveSubmission(submission) {
|
|
100
|
+
const stored = cloneJson(parseSubmission(submission, `input "${String(submission?.id)}"`));
|
|
101
|
+
await submissions.insertOne({
|
|
102
|
+
_id: stored.id,
|
|
103
|
+
formId: stored.formId,
|
|
104
|
+
formVersion: stored.formVersion,
|
|
105
|
+
submittedAt: stored.submittedAt,
|
|
106
|
+
submission: stored
|
|
107
|
+
});
|
|
108
|
+
},
|
|
109
|
+
async listSubmissions(formId, formVersion) {
|
|
110
|
+
const documents = await submissions.find({ formId, ...formVersion === void 0 ? {} : { formVersion } }).toArray();
|
|
111
|
+
return documents.map(parseSubmissionDocument).sort((left, right) => left.submittedAt.localeCompare(right.submittedAt) || left.id.localeCompare(right.id));
|
|
112
|
+
},
|
|
113
|
+
async deleteSubmission(submissionId) {
|
|
114
|
+
await submissions.deleteOne({ _id: submissionId });
|
|
115
|
+
},
|
|
116
|
+
async clearResponses(formId) {
|
|
117
|
+
await submissions.deleteMany({ formId });
|
|
118
|
+
},
|
|
119
|
+
async clear() {
|
|
120
|
+
await Promise.all([schemas.deleteMany({}), submissions.deleteMany({})]);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
125
|
+
0 && (module.exports = {
|
|
126
|
+
createMongoDbStorage
|
|
127
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { FormStorageAdapter } from '@form-engine-ts/core';
|
|
2
|
+
import { Db } from 'mongodb';
|
|
3
|
+
|
|
4
|
+
interface MongoDbStorageOptions {
|
|
5
|
+
readonly db: Db;
|
|
6
|
+
readonly schemasCollectionName?: string;
|
|
7
|
+
readonly responsesCollectionName?: string;
|
|
8
|
+
}
|
|
9
|
+
declare function createMongoDbStorage(options: MongoDbStorageOptions): FormStorageAdapter;
|
|
10
|
+
|
|
11
|
+
export { type MongoDbStorageOptions, createMongoDbStorage };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { FormStorageAdapter } from '@form-engine-ts/core';
|
|
2
|
+
import { Db } from 'mongodb';
|
|
3
|
+
|
|
4
|
+
interface MongoDbStorageOptions {
|
|
5
|
+
readonly db: Db;
|
|
6
|
+
readonly schemasCollectionName?: string;
|
|
7
|
+
readonly responsesCollectionName?: string;
|
|
8
|
+
}
|
|
9
|
+
declare function createMongoDbStorage(options: MongoDbStorageOptions): FormStorageAdapter;
|
|
10
|
+
|
|
11
|
+
export { type MongoDbStorageOptions, createMongoDbStorage };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { assertValidFormSchema } from "@form-engine-ts/core";
|
|
3
|
+
function cloneJson(value) {
|
|
4
|
+
return JSON.parse(JSON.stringify(value));
|
|
5
|
+
}
|
|
6
|
+
function isRecord(value) {
|
|
7
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
8
|
+
}
|
|
9
|
+
function isFormValue(value) {
|
|
10
|
+
return value === void 0 || typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value) || Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
11
|
+
}
|
|
12
|
+
function parseSubmission(value, location) {
|
|
13
|
+
if (!isRecord(value) || typeof value.id !== "string" || typeof value.formId !== "string" || !Number.isInteger(value.formVersion) || typeof value.locale !== "string" || typeof value.submittedAt !== "string" || !isRecord(value.values) || !Object.values(value.values).every(isFormValue)) {
|
|
14
|
+
throw new Error(`MongoDB submission at ${location} is invalid.`);
|
|
15
|
+
}
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
function schemaDocumentId(formId, formVersion) {
|
|
19
|
+
return `schema:${encodeURIComponent(formId)}:${formVersion}`;
|
|
20
|
+
}
|
|
21
|
+
function parseSchemaDocument(document) {
|
|
22
|
+
try {
|
|
23
|
+
assertValidFormSchema(document.schema);
|
|
24
|
+
} catch (cause) {
|
|
25
|
+
throw new Error(`MongoDB schema document "${String(document._id)}" is invalid.`, { cause });
|
|
26
|
+
}
|
|
27
|
+
if (document._id !== schemaDocumentId(document.schema.id, document.schema.version) || document.formId !== document.schema.id || document.formVersion !== document.schema.version) {
|
|
28
|
+
throw new Error(`MongoDB schema document "${String(document._id)}" has inconsistent metadata.`);
|
|
29
|
+
}
|
|
30
|
+
return cloneJson(document.schema);
|
|
31
|
+
}
|
|
32
|
+
function parseSubmissionDocument(document) {
|
|
33
|
+
const submission = parseSubmission(document.submission, `document "${String(document._id)}"`);
|
|
34
|
+
if (document._id !== submission.id || document.formId !== submission.formId || document.formVersion !== submission.formVersion || document.submittedAt !== submission.submittedAt) {
|
|
35
|
+
throw new Error(`MongoDB submission document "${String(document._id)}" has inconsistent metadata.`);
|
|
36
|
+
}
|
|
37
|
+
return cloneJson(submission);
|
|
38
|
+
}
|
|
39
|
+
function collectionName(value, fallback, optionName) {
|
|
40
|
+
if (value === void 0) return fallback;
|
|
41
|
+
if (value.trim().length === 0) throw new TypeError(`${optionName} must be a non-empty string.`);
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
function createMongoDbStorage(options) {
|
|
45
|
+
if (options?.db === void 0) throw new TypeError("db is required.");
|
|
46
|
+
const schemasCollectionName = collectionName(options.schemasCollectionName, "form_schemas", "schemasCollectionName");
|
|
47
|
+
const responsesCollectionName = collectionName(
|
|
48
|
+
options.responsesCollectionName,
|
|
49
|
+
"form_responses",
|
|
50
|
+
"responsesCollectionName"
|
|
51
|
+
);
|
|
52
|
+
const schemas = options.db.collection(schemasCollectionName);
|
|
53
|
+
const submissions = options.db.collection(responsesCollectionName);
|
|
54
|
+
return {
|
|
55
|
+
async saveSchema(schema) {
|
|
56
|
+
assertValidFormSchema(schema);
|
|
57
|
+
const stored = cloneJson(schema);
|
|
58
|
+
await schemas.updateOne(
|
|
59
|
+
{ _id: schemaDocumentId(schema.id, schema.version) },
|
|
60
|
+
{ $set: { formId: schema.id, formVersion: schema.version, schema: stored } },
|
|
61
|
+
{ upsert: true }
|
|
62
|
+
);
|
|
63
|
+
},
|
|
64
|
+
async getSchema(formId, formVersion) {
|
|
65
|
+
const document = await schemas.findOne({ _id: schemaDocumentId(formId, formVersion) });
|
|
66
|
+
return document === null ? null : parseSchemaDocument(document);
|
|
67
|
+
},
|
|
68
|
+
async listSchemas() {
|
|
69
|
+
const documents = await schemas.find({}).toArray();
|
|
70
|
+
return documents.map(parseSchemaDocument).sort((left, right) => left.id.localeCompare(right.id) || left.version - right.version);
|
|
71
|
+
},
|
|
72
|
+
async deleteSchema(formId, formVersion) {
|
|
73
|
+
await schemas.deleteOne({ _id: schemaDocumentId(formId, formVersion) });
|
|
74
|
+
},
|
|
75
|
+
async saveSubmission(submission) {
|
|
76
|
+
const stored = cloneJson(parseSubmission(submission, `input "${String(submission?.id)}"`));
|
|
77
|
+
await submissions.insertOne({
|
|
78
|
+
_id: stored.id,
|
|
79
|
+
formId: stored.formId,
|
|
80
|
+
formVersion: stored.formVersion,
|
|
81
|
+
submittedAt: stored.submittedAt,
|
|
82
|
+
submission: stored
|
|
83
|
+
});
|
|
84
|
+
},
|
|
85
|
+
async listSubmissions(formId, formVersion) {
|
|
86
|
+
const documents = await submissions.find({ formId, ...formVersion === void 0 ? {} : { formVersion } }).toArray();
|
|
87
|
+
return documents.map(parseSubmissionDocument).sort((left, right) => left.submittedAt.localeCompare(right.submittedAt) || left.id.localeCompare(right.id));
|
|
88
|
+
},
|
|
89
|
+
async deleteSubmission(submissionId) {
|
|
90
|
+
await submissions.deleteOne({ _id: submissionId });
|
|
91
|
+
},
|
|
92
|
+
async clearResponses(formId) {
|
|
93
|
+
await submissions.deleteMany({ formId });
|
|
94
|
+
},
|
|
95
|
+
async clear() {
|
|
96
|
+
await Promise.all([schemas.deleteMany({}), submissions.deleteMany({})]);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
export {
|
|
101
|
+
createMongoDbStorage
|
|
102
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@form-engine-ts/storage-mongodb",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"type": "module",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"README.md",
|
|
12
|
+
"LICENSE"
|
|
13
|
+
],
|
|
14
|
+
"main": "./dist/index.cjs",
|
|
15
|
+
"module": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js",
|
|
21
|
+
"require": "./dist/index.cjs"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/nitta-a/form-engine-ts.git",
|
|
28
|
+
"directory": "packages/storage-mongodb"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/nitta-a/form-engine-ts/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/nitta-a/form-engine-ts#readme",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"form",
|
|
36
|
+
"survey",
|
|
37
|
+
"storage",
|
|
38
|
+
"mongodb",
|
|
39
|
+
"typescript"
|
|
40
|
+
],
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@form-engine-ts/core": "1.0.0"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"mongodb": "^6.0.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"mongodb": "^6.0.0"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --clean --external @form-engine-ts/core --external mongodb",
|
|
52
|
+
"check": "biome check . && tsc --noEmit",
|
|
53
|
+
"test": "vitest run --globals",
|
|
54
|
+
"typecheck": "tsc --noEmit"
|
|
55
|
+
}
|
|
56
|
+
}
|