@primate/mongodb 0.6.0 → 0.8.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.
@@ -1,22 +1,24 @@
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
- readonly port: import("pema").DefaultType<import("pema").UintType<"u32">, 27017>;
6
+ readonly port: import("pema").DefaultType<import("pema").UintType<"u32", undefined>, 27017>;
6
7
  readonly database: import("pema").StringType;
7
8
  readonly username: import("pema").OptionalType<import("pema").StringType>;
8
9
  readonly password: import("pema").OptionalType<import("pema").StringType>;
9
- }>, {
10
+ }>, undefined, {
10
11
  host: string;
11
12
  port: number;
12
13
  database: string;
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>;
@@ -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 params = "directConnection=true&replicaSet=rs0";
147
- const client = new MongoClient(`mongodb://${host}:${port}?${params}`);
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
- await this.#client?.close();
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 docs = await collection
324
- .find(filter, options)
325
- .limit(limit)
326
- .toArray();
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.6.0",
3
+ "version": "0.8.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.8.1",
21
- "@rcompat/dict": "^0.5.1",
22
- "@rcompat/is": "^0.6.1",
23
- "mongodb": "^6.17.0",
24
- "@primate/core": "^0.6.0",
25
- "pema": "0.6.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.8.0",
25
+ "pema": "0.8.0"
26
26
  },
27
27
  "devDependencies": {
28
- "@rcompat/type": "^0.11.1"
28
+ "@rcompat/type": "^0.17.0"
29
29
  },
30
30
  "peerDependencies": {
31
- "primate": "^0.37.0"
31
+ "primate": "^0.39.0"
32
32
  },
33
33
  "imports": {
34
34
  "#*": {
@@ -43,10 +43,10 @@
43
43
  }
44
44
  },
45
45
  "scripts": {
46
- "build": "npm run clean && tsc",
46
+ "build": "npm run clean && tsgo",
47
47
  "clean": "rm -rf ./lib",
48
48
  "lint": "eslint .",
49
- "test": "npx proby",
50
- "start:db": "sudo mongod --dbpath /var/lib/mongodb"
49
+ "test": "npm run db && npx proby",
50
+ "db": "scripts/db.sh"
51
51
  }
52
52
  }