schema-seed-adapter-mongodb 0.1.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 schema-seed contributors
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,5 @@
1
+ # @schema-seed/adapter-mongodb
2
+
3
+ Part of the [schema-seed](https://github.com/AliNazar-111/schema-seed) project.
4
+
5
+ For full documentation and usage, please visit the [main repository](https://github.com/AliNazar-111/schema-seed).
package/dist/index.cjs ADDED
@@ -0,0 +1,84 @@
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
+ MongodbAdapter: () => MongodbAdapter,
24
+ createMongoAdapter: () => createMongoAdapter
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var import_mongodb = require("mongodb");
28
+ var MongodbAdapter = class {
29
+ client;
30
+ db = null;
31
+ dbName;
32
+ constructor(uri, dbName) {
33
+ this.client = new import_mongodb.MongoClient(uri);
34
+ this.dbName = dbName;
35
+ }
36
+ async connect() {
37
+ await this.client.connect();
38
+ this.db = this.client.db(this.dbName);
39
+ }
40
+ async disconnect() {
41
+ await this.client.close();
42
+ this.db = null;
43
+ }
44
+ async begin() {
45
+ }
46
+ async commit() {
47
+ }
48
+ async rollback() {
49
+ }
50
+ async introspectCollections() {
51
+ if (!this.db) throw new Error("Not connected");
52
+ const collections = await this.db.listCollections().toArray();
53
+ return collections.map((c) => c.name);
54
+ }
55
+ async getValidatorSchema(collectionName) {
56
+ if (!this.db) throw new Error("Not connected");
57
+ const collections = await this.db.listCollections({ name: collectionName }).toArray();
58
+ const coll = collections[0];
59
+ if (coll && coll.options?.validator) {
60
+ return coll.options.validator;
61
+ }
62
+ return null;
63
+ }
64
+ async insertMany(collection, documents) {
65
+ if (!this.db) throw new Error("Not connected");
66
+ if (documents.length === 0) return;
67
+ await this.db.collection(collection).insertMany(documents);
68
+ }
69
+ async truncateCollections(collections) {
70
+ if (!this.db) throw new Error("Not connected");
71
+ for (const name of collections) {
72
+ await this.db.collection(name).deleteMany({});
73
+ }
74
+ }
75
+ };
76
+ function createMongoAdapter(uri, dbName) {
77
+ return new MongodbAdapter(uri, dbName);
78
+ }
79
+ // Annotate the CommonJS export names for ESM import in node:
80
+ 0 && (module.exports = {
81
+ MongodbAdapter,
82
+ createMongoAdapter
83
+ });
84
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { MongoClient, Db, Document } from 'mongodb'\nimport {\n MongoAdapter,\n DbAdapter\n} from 'schema-seed-core'\n\nexport class MongodbAdapter implements MongoAdapter {\n private client: MongoClient\n private db: Db | null = null\n private dbName: string | undefined\n\n constructor(uri: string, dbName?: string) {\n this.client = new MongoClient(uri)\n this.dbName = dbName\n }\n\n async connect(): Promise<void> {\n await this.client.connect()\n this.db = this.client.db(this.dbName)\n }\n\n async disconnect(): Promise<void> {\n await this.client.close()\n this.db = null\n }\n\n async begin(): Promise<void> {\n // MongoDB supports sessions/transactions but for seeding we usually don't need them\n // or we'd need to handle replica sets. Skipping for now.\n }\n\n async commit(): Promise<void> {\n }\n\n async rollback(): Promise<void> {\n }\n\n async introspectCollections(): Promise<string[]> {\n if (!this.db) throw new Error('Not connected')\n const collections = await this.db.listCollections().toArray()\n return collections.map((c: any) => c.name)\n }\n\n async getValidatorSchema(collectionName: string): Promise<any> {\n if (!this.db) throw new Error('Not connected')\n const collections = await this.db.listCollections({ name: collectionName }).toArray()\n const coll = collections[0] as any\n if (coll && coll.options?.validator) {\n return coll.options.validator\n }\n return null\n }\n\n async insertMany(collection: string, documents: any[]): Promise<void> {\n if (!this.db) throw new Error('Not connected')\n if (documents.length === 0) return\n await this.db.collection(collection).insertMany(documents)\n }\n\n async truncateCollections(collections: string[]): Promise<void> {\n if (!this.db) throw new Error('Not connected')\n for (const name of collections) {\n await this.db.collection(name).deleteMany({})\n }\n }\n}\n\nexport function createMongoAdapter(uri: string, dbName?: string): MongodbAdapter {\n return new Mongod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,KAAK,GAAG,WAAW,UAAU,EAAE,WAAW,SAAS;AAAA,EAC3D;AAAA,EAEA,MAAM,oBAAoB,aAAsC;AAC9D,QAAI,CAAC,KAAK,GAAI,OAAM,IAAI,MAAM,eAAe;AAC7C,eAAW,QAAQ,aAAa;AAC9B,YAAM,KAAK,GAAG,WAAW,IAAI,EAAE,WAAW,CAAC,CAAC;AAAA,IAC9C;AAAA,EACF;AACF;AAEO,SAAS,mBAAmB,KAAa,QAAiC;AAC/E,SAAO,IAAI,eAAe,KAAK,MAAM;AACvC;","names":[]}
@@ -0,0 +1,20 @@
1
+ import { MongoAdapter } from 'schema-seed-core';
2
+
3
+ declare class MongodbAdapter implements MongoAdapter {
4
+ private client;
5
+ private db;
6
+ private dbName;
7
+ constructor(uri: string, dbName?: string);
8
+ connect(): Promise<void>;
9
+ disconnect(): Promise<void>;
10
+ begin(): Promise<void>;
11
+ commit(): Promise<void>;
12
+ rollback(): Promise<void>;
13
+ introspectCollections(): Promise<string[]>;
14
+ getValidatorSchema(collectionName: string): Promise<any>;
15
+ insertMany(collection: string, documents: any[]): Promise<void>;
16
+ truncateCollections(collections: string[]): Promise<void>;
17
+ }
18
+ declare function createMongoAdapter(uri: string, dbName?: string): MongodbAdapter;
19
+
20
+ export { MongodbAdapter, createMongoAdapter };
@@ -0,0 +1,20 @@
1
+ import { MongoAdapter } from 'schema-seed-core';
2
+
3
+ declare class MongodbAdapter implements MongoAdapter {
4
+ private client;
5
+ private db;
6
+ private dbName;
7
+ constructor(uri: string, dbName?: string);
8
+ connect(): Promise<void>;
9
+ disconnect(): Promise<void>;
10
+ begin(): Promise<void>;
11
+ commit(): Promise<void>;
12
+ rollback(): Promise<void>;
13
+ introspectCollections(): Promise<string[]>;
14
+ getValidatorSchema(collectionName: string): Promise<any>;
15
+ insertMany(collection: string, documents: any[]): Promise<void>;
16
+ truncateCollections(collections: string[]): Promise<void>;
17
+ }
18
+ declare function createMongoAdapter(uri: string, dbName?: string): MongodbAdapter;
19
+
20
+ export { MongodbAdapter, createMongoAdapter };
package/dist/index.js ADDED
@@ -0,0 +1,58 @@
1
+ // src/index.ts
2
+ import { MongoClient } from "mongodb";
3
+ var MongodbAdapter = class {
4
+ client;
5
+ db = null;
6
+ dbName;
7
+ constructor(uri, dbName) {
8
+ this.client = new MongoClient(uri);
9
+ this.dbName = dbName;
10
+ }
11
+ async connect() {
12
+ await this.client.connect();
13
+ this.db = this.client.db(this.dbName);
14
+ }
15
+ async disconnect() {
16
+ await this.client.close();
17
+ this.db = null;
18
+ }
19
+ async begin() {
20
+ }
21
+ async commit() {
22
+ }
23
+ async rollback() {
24
+ }
25
+ async introspectCollections() {
26
+ if (!this.db) throw new Error("Not connected");
27
+ const collections = await this.db.listCollections().toArray();
28
+ return collections.map((c) => c.name);
29
+ }
30
+ async getValidatorSchema(collectionName) {
31
+ if (!this.db) throw new Error("Not connected");
32
+ const collections = await this.db.listCollections({ name: collectionName }).toArray();
33
+ const coll = collections[0];
34
+ if (coll && coll.options?.validator) {
35
+ return coll.options.validator;
36
+ }
37
+ return null;
38
+ }
39
+ async insertMany(collection, documents) {
40
+ if (!this.db) throw new Error("Not connected");
41
+ if (documents.length === 0) return;
42
+ await this.db.collection(collection).insertMany(documents);
43
+ }
44
+ async truncateCollections(collections) {
45
+ if (!this.db) throw new Error("Not connected");
46
+ for (const name of collections) {
47
+ await this.db.collection(name).deleteMany({});
48
+ }
49
+ }
50
+ };
51
+ function createMongoAdapter(uri, dbName) {
52
+ return new MongodbAdapter(uri, dbName);
53
+ }
54
+ export {
55
+ MongodbAdapter,
56
+ createMongoAdapter
57
+ };
58
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { MongoClient, Db, Document } from 'mongodb'\nimport {\n MongoAdapter,\n DbAdapter\n} from 'schema-seed-core'\n\nexport class MongodbAdapter implements MongoAdapter {\n private client: MongoClient\n private db: Db | null = null\n private dbName: string | undefined\n\n constructor(uri: string, dbName?: string) {\n this.client = new MongoClient(uri)\n this.dbName = dbName\n }\n\n async connect(): Promise<void> {\n await this.client.connect()\n this.db = this.client.db(this.dbName)\n }\n\n async disconnect(): Promise<void> {\n await this.client.close()\n this.db = null\n }\n\n async begin(): Promise<void> {\n // MongoDB supports sessions/transactions but for seeding we usually don't need them\n // or we'd need to handle replica sets. Skipping for now.\n }\n\n async commit(): Promise<void> {\n }\n\n async rollback(): Promise<void> {\n }\n\n async introspectCollections(): Promise<string[]> {\n if (!this.db) throw new Error('Not connected')\n const collections = await this.db.listCollections().toArray()\n return collections.map((c: any) => c.name)\n }\n\n async getValidatorSchema(collectionName: string): Promise<any> {\n if (!this.db) throw new Error('Not connected')\n const collections = await this.db.listCollections({ name: collectionName }).toArray()\n const coll = collections[0] as any\n if (coll && coll.options?.validator) {\n return coll.options.validator\n }\n return null\n }\n\n async insertMany(collection: string, documents: any[]): Promise<void> {\n if (!this.db) throw new Error('Not connected')\n if (documents.length === 0) return\n await this.db.collection(collection).insertMany(documents)\n }\n\n async truncateCollections(collections: string[]): Promise<void> {\n if (!this.db) throw new Error('Not connected')\n for (const name of collections) {\n await this.db.collection(name).deleteMany({})\n }\n }\n}\n\nexport function createMongoAdapter(uri: string, dbName?: string): MongodbAdapter {\n return new MongodbAdapter(uri, dbName)\n}\n"],"mappings":";AAAA,SAAS,mBAAiC;AAMnC,IAAM,iBAAN,MAA6C;AAAA,EAC1C;AAAA,EACA,KAAgB;AAAA,EAChB;AAAA,EAER,YAAY,KAAa,QAAiB;AACxC,SAAK,SAAS,IAAI,YAAY,GAAG;AACjC,SAAK,SAAS;AAAA,EAChB;AAAA,EAEA,MAAM,UAAyB;AAC7B,UAAM,KAAK,OAAO,QAAQ;AAC1B,SAAK,KAAK,KAAK,OAAO,GAAG,KAAK,MAAM;AAAA,EACtC;AAAA,EAEA,MAAM,aAA4B;AAChC,UAAM,KAAK,OAAO,MAAM;AACxB,SAAK,KAAK;AAAA,EACZ;AAAA,EAEA,MAAM,QAAuB;AAAA,EAG7B;AAAA,EAEA,MAAM,SAAwB;AAAA,EAC9B;AAAA,EAEA,MAAM,WAA0B;AAAA,EAChC;AAAA,EAEA,MAAM,wBAA2C;AAC/C,QAAI,CAAC,KAAK,GAAI,OAAM,IAAI,MAAM,eAAe;AAC7C,UAAM,cAAc,MAAM,KAAK,GAAG,gBAAgB,EAAE,QAAQ;AAC5D,WAAO,YAAY,IAAI,CAAC,MAAW,EAAE,IAAI;AAAA,EAC3C;AAAA,EAEA,MAAM,mBAAmB,gBAAsC;AAC7D,QAAI,CAAC,KAAK,GAAI,OAAM,IAAI,MAAM,eAAe;AAC7C,UAAM,cAAc,MAAM,KAAK,GAAG,gBAAgB,EAAE,MAAM,eAAe,CAAC,EAAE,QAAQ;AACpF,UAAM,OAAO,YAAY,CAAC;AAC1B,QAAI,QAAQ,KAAK,SAAS,WAAW;AACnC,aAAO,KAAK,QAAQ;AAAA,IACtB;AACA,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,WAAW,YAAoB,WAAiC;AACpE,QAAI,CAAC,KAAK,GAAI,OAAM,IAAI,MAAM,eAAe;AAC7C,QAAI,UAAU,WAAW,EAAG;AAC5B,UAAM,KAAK,GAAG,WAAW,UAAU,EAAE,WAAW,SAAS;AAAA,EAC3D;AAAA,EAEA,MAAM,oBAAoB,aAAsC;AAC9D,QAAI,CAAC,KAAK,GAAI,OAAM,IAAI,MAAM,eAAe;AAC7C,eAAW,QAAQ,aAAa;AAC9B,YAAM,KAAK,GAAG,WAAW,IAAI,EAAE,WAAW,CAAC,CAAC;AAAA,IAC9C;AAAA,EACF;AACF;AAEO,SAAS,mBAAmB,KAAa,QAAiC;AAC/E,SAAO,IAAI,eAAe,KAAK,MAAM;AACvC;","names":[]}
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "schema-seed-adapter-mongodb",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "MongoDB adapter for schema-seed",
6
+ "author": "Ali Nazar",
7
+ "license": "MIT",
8
+ "private": false,
9
+ "sideEffects": false,
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/alinazar-111/schema-seed.git"
13
+ },
14
+ "homepage": "https://github.com/alinazar-111/schema-seed#readme",
15
+ "bugs": {
16
+ "url": "https://github.com/alinazar-111/schema-seed/issues"
17
+ },
18
+ "keywords": [
19
+ "database",
20
+ "seeding",
21
+ "test-data",
22
+ "adapter",
23
+ "schema-seed"
24
+ ],
25
+ "main": "./dist/index.cjs",
26
+ "module": "./dist/index.js",
27
+ "types": "./dist/index.d.ts",
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "import": "./dist/index.js",
32
+ "require": "./dist/index.cjs"
33
+ }
34
+ },
35
+ "files": [
36
+ "dist",
37
+ "README.md",
38
+ "LICENSE"
39
+ ],
40
+ "dependencies": {
41
+ "mongodb": "^6.10.0",
42
+ "schema-seed-core": "0.1.0"
43
+ },
44
+ "devDependencies": {
45
+ "tsup": "^8.3.5",
46
+ "typescript": "^5.7.2",
47
+ "vitest": "^2.1.8"
48
+ },
49
+ "scripts": {
50
+ "build": "tsup",
51
+ "clean": "rm -rf dist",
52
+ "dev": "tsup --watch",
53
+ "lint": "eslint .",
54
+ "test": "vitest run --passWithNoTests",
55
+ "typecheck": "tsc --noEmit"
56
+ }
57
+ }