@primate/mongodb 0.6.0 → 0.7.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/lib/private/MongoDB.d.ts +3 -1
- package/lib/private/MongoDB.js +27 -10
- package/package.json +11 -11
package/lib/private/MongoDB.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { As, DataDict, DB, Schema, Sort, With } from "@primate/core/db";
|
|
2
2
|
import type { Dict } from "@rcompat/type";
|
|
3
|
+
import { MongoClient } from "mongodb";
|
|
3
4
|
declare const schema: import("pema").ObjectType<import("pema").NormalizeSchemaObject<{
|
|
4
5
|
readonly host: import("pema").DefaultType<import("pema").StringType, "localhost">;
|
|
5
6
|
readonly port: import("pema").DefaultType<import("pema").UintType<"u32">, 27017>;
|
|
@@ -13,10 +14,11 @@ declare const schema: import("pema").ObjectType<import("pema").NormalizeSchemaOb
|
|
|
13
14
|
username: string | undefined;
|
|
14
15
|
password: string | undefined;
|
|
15
16
|
}>;
|
|
16
|
-
export default class MongoDB implements DB {
|
|
17
|
+
export default class MongoDB implements DB<MongoClient> {
|
|
17
18
|
#private;
|
|
18
19
|
static config: typeof schema.input;
|
|
19
20
|
constructor(config?: typeof schema.input);
|
|
21
|
+
get client(): MongoClient;
|
|
20
22
|
close(): Promise<void>;
|
|
21
23
|
get schema(): Schema;
|
|
22
24
|
create<O extends Dict>(as: As, record: Dict): Promise<O>;
|
package/lib/private/MongoDB.js
CHANGED
|
@@ -89,6 +89,12 @@ async function bind(as, object) {
|
|
|
89
89
|
case "$before":
|
|
90
90
|
next = { $lt: await bind_value(datatype, op_value) };
|
|
91
91
|
break;
|
|
92
|
+
case "$in": {
|
|
93
|
+
const array = op_value;
|
|
94
|
+
const bound = await Promise.all(array.map(v => bind_value(datatype, v)));
|
|
95
|
+
next = { $in: bound };
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
92
98
|
default:
|
|
93
99
|
throw E.operator_unknown(field, op);
|
|
94
100
|
}
|
|
@@ -142,9 +148,15 @@ export default class MongoDB {
|
|
|
142
148
|
#database;
|
|
143
149
|
#client;
|
|
144
150
|
constructor(config) {
|
|
145
|
-
const { host, port, database } = schema.parse(config);
|
|
146
|
-
const
|
|
147
|
-
|
|
151
|
+
const { host, port, database, username, password, } = schema.parse(config);
|
|
152
|
+
const uri = new URL(`mongodb://${host}:${port}/${database}`);
|
|
153
|
+
uri.searchParams.set("directConnection", "true");
|
|
154
|
+
if (username !== undefined) {
|
|
155
|
+
uri.username = username;
|
|
156
|
+
uri.password = password ?? "";
|
|
157
|
+
uri.searchParams.set("authSource", database);
|
|
158
|
+
}
|
|
159
|
+
const client = new MongoClient(uri.toString());
|
|
148
160
|
this.#database = database;
|
|
149
161
|
this.#factory = async () => {
|
|
150
162
|
await client.connect();
|
|
@@ -155,8 +167,13 @@ export default class MongoDB {
|
|
|
155
167
|
this.#client ??= await this.#factory();
|
|
156
168
|
return this.#client.db(this.#database).collection(name);
|
|
157
169
|
}
|
|
170
|
+
get client() {
|
|
171
|
+
return this.#client;
|
|
172
|
+
}
|
|
158
173
|
async close() {
|
|
159
|
-
|
|
174
|
+
const client = this.#client;
|
|
175
|
+
this.#client = undefined;
|
|
176
|
+
await client?.close();
|
|
160
177
|
}
|
|
161
178
|
get schema() {
|
|
162
179
|
return {
|
|
@@ -316,14 +333,14 @@ export default class MongoDB {
|
|
|
316
333
|
const sort = get_sort(args.sort);
|
|
317
334
|
const limit = get_limit(args.limit);
|
|
318
335
|
const options = { useBigInt64: true };
|
|
319
|
-
if (projection)
|
|
336
|
+
if (is.defined(projection))
|
|
320
337
|
options.projection = projection;
|
|
321
|
-
if (sort)
|
|
338
|
+
if (is.defined(sort))
|
|
322
339
|
options.sort = sort;
|
|
323
|
-
const
|
|
324
|
-
|
|
325
|
-
.
|
|
326
|
-
|
|
340
|
+
const cursor = collection.find(filter, options).limit(limit);
|
|
341
|
+
if (is.defined(args.offset))
|
|
342
|
+
cursor.skip(args.offset);
|
|
343
|
+
const docs = await cursor.toArray();
|
|
327
344
|
return docs.map(doc => this.#unbind(as, doc));
|
|
328
345
|
}
|
|
329
346
|
async #read_phased(as, args) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@primate/mongodb",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "MongoDB databases for Primate",
|
|
5
5
|
"homepage": "https://primate.run/docs/database/mongodb",
|
|
6
6
|
"bugs": "https://github.com/primate-run/primate/issues",
|
|
@@ -17,18 +17,18 @@
|
|
|
17
17
|
"!/**/*.spec.*"
|
|
18
18
|
],
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@rcompat/assert": "^0.
|
|
21
|
-
"@rcompat/dict": "^0.
|
|
22
|
-
"@rcompat/is": "^0.
|
|
23
|
-
"mongodb": "^
|
|
24
|
-
"@primate/core": "^0.
|
|
25
|
-
"pema": "0.
|
|
20
|
+
"@rcompat/assert": "^0.13.0",
|
|
21
|
+
"@rcompat/dict": "^0.10.0",
|
|
22
|
+
"@rcompat/is": "^0.11.0",
|
|
23
|
+
"mongodb": "^7.2.0",
|
|
24
|
+
"@primate/core": "^0.7.0",
|
|
25
|
+
"pema": "0.7.0"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@rcompat/type": "^0.
|
|
28
|
+
"@rcompat/type": "^0.17.0"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"primate": "^0.
|
|
31
|
+
"primate": "^0.38.0"
|
|
32
32
|
},
|
|
33
33
|
"imports": {
|
|
34
34
|
"#*": {
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"build": "npm run clean && tsc",
|
|
47
47
|
"clean": "rm -rf ./lib",
|
|
48
48
|
"lint": "eslint .",
|
|
49
|
-
"test": "npx proby",
|
|
50
|
-
"
|
|
49
|
+
"test": "npm run db && npx proby",
|
|
50
|
+
"db": "scripts/db.sh"
|
|
51
51
|
}
|
|
52
52
|
}
|