@prisma-next/driver-mongo 0.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 +13 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +60 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +46 -0
- package/src/exports/index.ts +1 -0
- package/src/mongo-driver.ts +90 -0
package/README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @prisma-next/driver-mongo
|
|
2
|
+
|
|
3
|
+
MongoDB driver for Prisma Next. Executes wire-protocol documents against a MongoDB connection.
|
|
4
|
+
|
|
5
|
+
## Responsibilities
|
|
6
|
+
|
|
7
|
+
- **Command execution**: Sends lowered wire-protocol documents to MongoDB and returns raw results
|
|
8
|
+
- **Connection management**: Creates and manages the MongoDB client connection
|
|
9
|
+
|
|
10
|
+
## Dependencies
|
|
11
|
+
|
|
12
|
+
- **Depends on**:
|
|
13
|
+
- `@prisma-next/mongo-core` (driver interface types)
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/mongo-driver.ts"],"sourcesContent":[],"mappings":";;;iBAoFsB,iBAAA,+BAAgD,QAAQ"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { MongoClient } from "mongodb";
|
|
2
|
+
|
|
3
|
+
//#region src/mongo-driver.ts
|
|
4
|
+
var MongoDriverImpl = class {
|
|
5
|
+
#db;
|
|
6
|
+
#client;
|
|
7
|
+
constructor(db, client) {
|
|
8
|
+
this.#db = db;
|
|
9
|
+
this.#client = client;
|
|
10
|
+
}
|
|
11
|
+
execute(wireCommand) {
|
|
12
|
+
switch (wireCommand.kind) {
|
|
13
|
+
case "find": return this.#executeFindCommand(wireCommand);
|
|
14
|
+
case "insertOne": return this.#executeInsertOneCommand(wireCommand);
|
|
15
|
+
case "updateOne": return this.#executeUpdateOneCommand(wireCommand);
|
|
16
|
+
case "deleteOne": return this.#executeDeleteOneCommand(wireCommand);
|
|
17
|
+
case "aggregate": return this.#executeAggregateCommand(wireCommand);
|
|
18
|
+
default: {
|
|
19
|
+
const _exhaustive = wireCommand;
|
|
20
|
+
throw new Error(`Unknown wire command kind: ${_exhaustive.kind}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
async close() {
|
|
25
|
+
await this.#client.close();
|
|
26
|
+
}
|
|
27
|
+
async *#executeFindCommand(cmd) {
|
|
28
|
+
let cursor = this.#db.collection(cmd.collection).find(cmd.filter ?? {});
|
|
29
|
+
if (cmd.projection) cursor = cursor.project(cmd.projection);
|
|
30
|
+
if (cmd.sort) cursor = cursor.sort(cmd.sort);
|
|
31
|
+
if (cmd.limit !== void 0) cursor = cursor.limit(cmd.limit);
|
|
32
|
+
if (cmd.skip !== void 0) cursor = cursor.skip(cmd.skip);
|
|
33
|
+
yield* cursor;
|
|
34
|
+
}
|
|
35
|
+
async *#executeInsertOneCommand(cmd) {
|
|
36
|
+
yield { insertedId: (await this.#db.collection(cmd.collection).insertOne(cmd.document)).insertedId };
|
|
37
|
+
}
|
|
38
|
+
async *#executeUpdateOneCommand(cmd) {
|
|
39
|
+
const result = await this.#db.collection(cmd.collection).updateOne(cmd.filter, cmd.update);
|
|
40
|
+
yield {
|
|
41
|
+
matchedCount: result.matchedCount,
|
|
42
|
+
modifiedCount: result.modifiedCount
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
async *#executeDeleteOneCommand(cmd) {
|
|
46
|
+
yield { deletedCount: (await this.#db.collection(cmd.collection).deleteOne(cmd.filter)).deletedCount };
|
|
47
|
+
}
|
|
48
|
+
async *#executeAggregateCommand(cmd) {
|
|
49
|
+
yield* this.#db.collection(cmd.collection).aggregate(cmd.pipeline);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
async function createMongoDriver(uri, dbName) {
|
|
53
|
+
const client = new MongoClient(uri);
|
|
54
|
+
await client.connect();
|
|
55
|
+
return new MongoDriverImpl(client.db(dbName), client);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
//#endregion
|
|
59
|
+
export { createMongoDriver };
|
|
60
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["#db","#client","#executeFindCommand","#executeInsertOneCommand","#executeUpdateOneCommand","#executeDeleteOneCommand","#executeAggregateCommand","_exhaustive: never"],"sources":["../src/mongo-driver.ts"],"sourcesContent":["import type {\n AnyMongoWireCommand,\n DeleteOneResult,\n InsertOneResult,\n MongoDriver,\n UpdateOneResult,\n} from '@prisma-next/mongo-core';\nimport { type Db, MongoClient, type Sort } from 'mongodb';\n\nclass MongoDriverImpl implements MongoDriver {\n readonly #db: Db;\n readonly #client: MongoClient;\n\n constructor(db: Db, client: MongoClient) {\n this.#db = db;\n this.#client = client;\n }\n\n execute<Row = Record<string, unknown>>(wireCommand: AnyMongoWireCommand): AsyncIterable<Row> {\n switch (wireCommand.kind) {\n case 'find':\n return this.#executeFindCommand<Row>(wireCommand);\n case 'insertOne':\n return this.#executeInsertOneCommand(wireCommand) as AsyncIterable<Row>;\n case 'updateOne':\n return this.#executeUpdateOneCommand(wireCommand) as AsyncIterable<Row>;\n case 'deleteOne':\n return this.#executeDeleteOneCommand(wireCommand) as AsyncIterable<Row>;\n case 'aggregate':\n return this.#executeAggregateCommand<Row>(wireCommand);\n default: {\n const _exhaustive: never = wireCommand;\n throw new Error(`Unknown wire command kind: ${(_exhaustive as { kind: string }).kind}`);\n }\n }\n }\n\n async close(): Promise<void> {\n await this.#client.close();\n }\n\n async *#executeFindCommand<Row>(cmd: AnyMongoWireCommand & { kind: 'find' }): AsyncIterable<Row> {\n const collection = this.#db.collection(cmd.collection);\n let cursor = collection.find(cmd.filter ?? {});\n if (cmd.projection) cursor = cursor.project(cmd.projection);\n if (cmd.sort) cursor = cursor.sort(cmd.sort as Sort);\n if (cmd.limit !== undefined) cursor = cursor.limit(cmd.limit);\n if (cmd.skip !== undefined) cursor = cursor.skip(cmd.skip);\n yield* cursor as AsyncIterable<Row>;\n }\n\n async *#executeInsertOneCommand(\n cmd: AnyMongoWireCommand & { kind: 'insertOne' },\n ): AsyncIterable<InsertOneResult> {\n const collection = this.#db.collection(cmd.collection);\n const result = await collection.insertOne(cmd.document);\n yield { insertedId: result.insertedId };\n }\n\n async *#executeUpdateOneCommand(\n cmd: AnyMongoWireCommand & { kind: 'updateOne' },\n ): AsyncIterable<UpdateOneResult> {\n const collection = this.#db.collection(cmd.collection);\n const result = await collection.updateOne(cmd.filter, cmd.update);\n yield { matchedCount: result.matchedCount, modifiedCount: result.modifiedCount };\n }\n\n async *#executeDeleteOneCommand(\n cmd: AnyMongoWireCommand & { kind: 'deleteOne' },\n ): AsyncIterable<DeleteOneResult> {\n const collection = this.#db.collection(cmd.collection);\n const result = await collection.deleteOne(cmd.filter);\n yield { deletedCount: result.deletedCount };\n }\n\n async *#executeAggregateCommand<Row>(\n cmd: AnyMongoWireCommand & { kind: 'aggregate' },\n ): AsyncIterable<Row> {\n const collection = this.#db.collection(cmd.collection);\n const cursor = collection.aggregate(cmd.pipeline as Record<string, unknown>[]);\n yield* cursor as AsyncIterable<Row>;\n }\n}\n\nexport async function createMongoDriver(uri: string, dbName: string): Promise<MongoDriver> {\n const client = new MongoClient(uri);\n await client.connect();\n const db = client.db(dbName);\n return new MongoDriverImpl(db, client);\n}\n"],"mappings":";;;AASA,IAAM,kBAAN,MAA6C;CAC3C,CAASA;CACT,CAASC;CAET,YAAY,IAAQ,QAAqB;AACvC,QAAKD,KAAM;AACX,QAAKC,SAAU;;CAGjB,QAAuC,aAAsD;AAC3F,UAAQ,YAAY,MAApB;GACE,KAAK,OACH,QAAO,MAAKC,mBAAyB,YAAY;GACnD,KAAK,YACH,QAAO,MAAKC,wBAAyB,YAAY;GACnD,KAAK,YACH,QAAO,MAA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EtB,QAAO,IAAI,gBADA,OAAO,GAAG,OAAO,EACG,OAAO"}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@prisma-next/driver-mongo",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"description": "MongoDB driver for Prisma Next",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsdown",
|
|
9
|
+
"test": "vitest run --passWithNoTests",
|
|
10
|
+
"test:coverage": "vitest run --coverage --passWithNoTests",
|
|
11
|
+
"typecheck": "tsc --noEmit",
|
|
12
|
+
"lint": "biome check . --error-on-warnings",
|
|
13
|
+
"lint:fix": "biome check --write .",
|
|
14
|
+
"lint:fix:unsafe": "biome check --write --unsafe .",
|
|
15
|
+
"clean": "rm -rf dist dist-tsc dist-tsc-prod coverage .tmp-output"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@prisma-next/mongo-core": "workspace:*",
|
|
19
|
+
"mongodb": "^6.16.0"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@prisma-next/test-utils": "workspace:*",
|
|
23
|
+
"@prisma-next/tsconfig": "workspace:*",
|
|
24
|
+
"@prisma-next/tsdown": "workspace:*",
|
|
25
|
+
"mongodb-memory-server": "^10.4.0",
|
|
26
|
+
"tsdown": "catalog:",
|
|
27
|
+
"typescript": "catalog:",
|
|
28
|
+
"vitest": "catalog:"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"src"
|
|
33
|
+
],
|
|
34
|
+
"exports": {
|
|
35
|
+
".": "./dist/index.mjs",
|
|
36
|
+
"./package.json": "./package.json"
|
|
37
|
+
},
|
|
38
|
+
"main": "./dist/index.mjs",
|
|
39
|
+
"module": "./dist/index.mjs",
|
|
40
|
+
"types": "./dist/index.d.mts",
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "https://github.com/prisma/prisma-next.git",
|
|
44
|
+
"directory": "packages/3-mongo-target/3-mongo-driver"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createMongoDriver } from '../mongo-driver';
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AnyMongoWireCommand,
|
|
3
|
+
DeleteOneResult,
|
|
4
|
+
InsertOneResult,
|
|
5
|
+
MongoDriver,
|
|
6
|
+
UpdateOneResult,
|
|
7
|
+
} from '@prisma-next/mongo-core';
|
|
8
|
+
import { type Db, MongoClient, type Sort } from 'mongodb';
|
|
9
|
+
|
|
10
|
+
class MongoDriverImpl implements MongoDriver {
|
|
11
|
+
readonly #db: Db;
|
|
12
|
+
readonly #client: MongoClient;
|
|
13
|
+
|
|
14
|
+
constructor(db: Db, client: MongoClient) {
|
|
15
|
+
this.#db = db;
|
|
16
|
+
this.#client = client;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
execute<Row = Record<string, unknown>>(wireCommand: AnyMongoWireCommand): AsyncIterable<Row> {
|
|
20
|
+
switch (wireCommand.kind) {
|
|
21
|
+
case 'find':
|
|
22
|
+
return this.#executeFindCommand<Row>(wireCommand);
|
|
23
|
+
case 'insertOne':
|
|
24
|
+
return this.#executeInsertOneCommand(wireCommand) as AsyncIterable<Row>;
|
|
25
|
+
case 'updateOne':
|
|
26
|
+
return this.#executeUpdateOneCommand(wireCommand) as AsyncIterable<Row>;
|
|
27
|
+
case 'deleteOne':
|
|
28
|
+
return this.#executeDeleteOneCommand(wireCommand) as AsyncIterable<Row>;
|
|
29
|
+
case 'aggregate':
|
|
30
|
+
return this.#executeAggregateCommand<Row>(wireCommand);
|
|
31
|
+
default: {
|
|
32
|
+
const _exhaustive: never = wireCommand;
|
|
33
|
+
throw new Error(`Unknown wire command kind: ${(_exhaustive as { kind: string }).kind}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async close(): Promise<void> {
|
|
39
|
+
await this.#client.close();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async *#executeFindCommand<Row>(cmd: AnyMongoWireCommand & { kind: 'find' }): AsyncIterable<Row> {
|
|
43
|
+
const collection = this.#db.collection(cmd.collection);
|
|
44
|
+
let cursor = collection.find(cmd.filter ?? {});
|
|
45
|
+
if (cmd.projection) cursor = cursor.project(cmd.projection);
|
|
46
|
+
if (cmd.sort) cursor = cursor.sort(cmd.sort as Sort);
|
|
47
|
+
if (cmd.limit !== undefined) cursor = cursor.limit(cmd.limit);
|
|
48
|
+
if (cmd.skip !== undefined) cursor = cursor.skip(cmd.skip);
|
|
49
|
+
yield* cursor as AsyncIterable<Row>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async *#executeInsertOneCommand(
|
|
53
|
+
cmd: AnyMongoWireCommand & { kind: 'insertOne' },
|
|
54
|
+
): AsyncIterable<InsertOneResult> {
|
|
55
|
+
const collection = this.#db.collection(cmd.collection);
|
|
56
|
+
const result = await collection.insertOne(cmd.document);
|
|
57
|
+
yield { insertedId: result.insertedId };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async *#executeUpdateOneCommand(
|
|
61
|
+
cmd: AnyMongoWireCommand & { kind: 'updateOne' },
|
|
62
|
+
): AsyncIterable<UpdateOneResult> {
|
|
63
|
+
const collection = this.#db.collection(cmd.collection);
|
|
64
|
+
const result = await collection.updateOne(cmd.filter, cmd.update);
|
|
65
|
+
yield { matchedCount: result.matchedCount, modifiedCount: result.modifiedCount };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async *#executeDeleteOneCommand(
|
|
69
|
+
cmd: AnyMongoWireCommand & { kind: 'deleteOne' },
|
|
70
|
+
): AsyncIterable<DeleteOneResult> {
|
|
71
|
+
const collection = this.#db.collection(cmd.collection);
|
|
72
|
+
const result = await collection.deleteOne(cmd.filter);
|
|
73
|
+
yield { deletedCount: result.deletedCount };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async *#executeAggregateCommand<Row>(
|
|
77
|
+
cmd: AnyMongoWireCommand & { kind: 'aggregate' },
|
|
78
|
+
): AsyncIterable<Row> {
|
|
79
|
+
const collection = this.#db.collection(cmd.collection);
|
|
80
|
+
const cursor = collection.aggregate(cmd.pipeline as Record<string, unknown>[]);
|
|
81
|
+
yield* cursor as AsyncIterable<Row>;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function createMongoDriver(uri: string, dbName: string): Promise<MongoDriver> {
|
|
86
|
+
const client = new MongoClient(uri);
|
|
87
|
+
await client.connect();
|
|
88
|
+
const db = client.db(dbName);
|
|
89
|
+
return new MongoDriverImpl(db, client);
|
|
90
|
+
}
|