@primate/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,19 @@
1
+ Copyright (c) Terrablue <terrablue@proton.me> and contributors.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@primate/mongodb",
3
+ "version": "0.1.0",
4
+ "description": "Primate MongoDB database",
5
+ "homepage": "https://primatejs.com/modules/mongodb",
6
+ "bugs": "https://github.com/primatejs/primate/issues",
7
+ "license": "MIT",
8
+ "files": [
9
+ "src/**/*.js",
10
+ "!src/**/*.spec.js"
11
+ ],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/primatejs/primate",
15
+ "directory": "packages/mongodb"
16
+ },
17
+ "dependencies": {
18
+ "@rcompat/invariant": "^0.5.0",
19
+ "@rcompat/object": "^0.5.0",
20
+ "@primate/core": "^0.1.0",
21
+ "@primate/store": "^0.25.0"
22
+ },
23
+ "devDependencies": {
24
+ "mongodb": "^6.8.0"
25
+ },
26
+ "peerDependencies": {
27
+ "mongodb": "6"
28
+ },
29
+ "type": "module",
30
+ "imports": {
31
+ "#*": {
32
+ "@primate/lt": "./src/private/*.js",
33
+ "default": "./src/private/*.js"
34
+ }
35
+ },
36
+ "exports": {
37
+ ".": "./src/default.js"
38
+ }
39
+ }
package/src/default.js ADDED
@@ -0,0 +1,10 @@
1
+ import defaults from "#defaults";
2
+ import serve from "#serve";
3
+
4
+ export default ({
5
+ host = defaults.host,
6
+ port = defaults.port,
7
+ database,
8
+ } = {}) => ({
9
+ serve: serve({ host, port, database }),
10
+ });
@@ -0,0 +1,89 @@
1
+ import maybe from "@rcompat/invariant/maybe";
2
+ import empty from "@rcompat/object/empty";
3
+ import filter from "@rcompat/object/filter";
4
+ import valmap from "@rcompat/object/valmap";
5
+
6
+ const toid = ({ _id, ...rest }) => ({ id: _id, ...rest });
7
+ const to_id = ({ id, ...rest }) => id === undefined
8
+ ? rest
9
+ : { _id: id, ...rest };
10
+ const cid = criteria => criteria.id === undefined ? criteria : to_id(criteria);
11
+ const null_to_set_unset = delta => {
12
+ const $set = filter(delta, ([, value]) => value !== null);
13
+ const $unset = filter(delta, ([, value]) => value === null);
14
+ return { $set, $unset };
15
+ };
16
+
17
+ const make_sort = ({ sort = {} } = {}) => {
18
+ maybe(sort).object();
19
+
20
+ return valmap(sort, value => value === "asc" ? 1 : -1);
21
+ };
22
+
23
+ export default class Facade {
24
+ schema = {
25
+ create: async _ => {
26
+ // noop
27
+ },
28
+ delete: async name => {
29
+ await this.#by(name).drop();
30
+ },
31
+ };
32
+
33
+ constructor(connection, session) {
34
+ this.connection = connection;
35
+ this.session = session;
36
+ }
37
+
38
+ #by(name) {
39
+ return this.connection.collection(name);
40
+ }
41
+
42
+ get #options() {
43
+ return { session: this.session };
44
+ }
45
+
46
+ async find(name, criteria = {}, projection = [], options = {}) {
47
+ const sort = make_sort(options);
48
+ const $options = {
49
+ ...this.#options,
50
+ ...projection.length === 0
51
+ ? {}
52
+ : {
53
+ projection: {
54
+ // erase _id unless explicit in projection
55
+ _id: 0,
56
+ ...Object.fromEntries(projection.map(field => [field, 1])),
57
+ },
58
+ },
59
+ ...empty(sort) ? {} : { sort },
60
+ };
61
+ return (await this.#by(name).find(cid(criteria), $options).toArray())
62
+ .map(document => toid(document));
63
+ }
64
+
65
+ async count(name, criteria = {}) {
66
+ return this.#by(name).countDocuments(criteria, this.#options);
67
+ }
68
+
69
+ async get(name, primary, value) {
70
+ const result = await this.#by(name).findOne({ _id: value }, this.#options);
71
+ return result === null ? undefined : toid(result);
72
+ }
73
+
74
+ async insert(name, primary, document) {
75
+ await this.#by(name).insertOne(document, this.#options);
76
+ return toid(document);
77
+ }
78
+
79
+ async update(name, criteria = {}, delta = {}) {
80
+ return (await this.#by(name)
81
+ .updateMany(cid(criteria), null_to_set_unset(delta), this.#options))
82
+ .modifiedCount;
83
+ }
84
+
85
+ async delete(name, criteria = {}) {
86
+ return (await this.#by(name).deleteMany(cid(criteria), this.#options))
87
+ .deletedCount;
88
+ }
89
+ }
@@ -0,0 +1,8 @@
1
+ import { MongoClient } from "mongodb";
2
+
3
+ export default async ({ host, port }) => {
4
+ const url = `mongodb://${host}:${port}?replicaSet=rs0&directConnection=true`;
5
+ const client = new MongoClient(url);
6
+ await client.connect();
7
+ return client;
8
+ };
@@ -0,0 +1,4 @@
1
+ export default {
2
+ host: "localhost",
3
+ port: 27017,
4
+ };
@@ -0,0 +1,62 @@
1
+ import Facade from "#Facade";
2
+ import connect from "#connect";
3
+ import ident from "@primate/store/core/ident";
4
+ import wrap from "@primate/store/core/wrap";
5
+ import { Decimal128, ObjectId } from "mongodb";
6
+
7
+ export default ({ host, port, database } = {}) => async _ => {
8
+ const client = await connect({ host, port });
9
+
10
+ const types = {
11
+ primary: {
12
+ validate(value) {
13
+ /* TODO: check that has valid objectid form */
14
+ if (typeof value === "string") {
15
+ return value;
16
+ }
17
+ throw new Error(`\`${value}\` is not a valid primary key value`);
18
+ },
19
+ in(value) {
20
+ return new ObjectId(value);
21
+ },
22
+ out(value) {
23
+ return value.toString();
24
+ },
25
+ },
26
+ object: ident,
27
+ number: ident,
28
+ bigint: {
29
+ in(value) {
30
+ return new Decimal128(value.toString());
31
+ },
32
+ out(value) {
33
+ return BigInt(value.toString());
34
+ },
35
+ },
36
+ boolean: ident,
37
+ date: ident,
38
+ string: ident,
39
+ };
40
+
41
+ return {
42
+ name: "@primate/mongodb",
43
+ types,
44
+ async transact(stores) {
45
+ return async (others, next) => {
46
+ const session = client.startSession();
47
+ const facade = new Facade(client.db(database), session);
48
+
49
+ try {
50
+ return await session.withTransaction(async () => {
51
+ const response = await next([...others, ...stores.map(([_, store]) =>
52
+ [_, wrap(store, facade, types)]),
53
+ ]);
54
+ return response;
55
+ });
56
+ } finally {
57
+ session.endSession();
58
+ }
59
+ };
60
+ },
61
+ };
62
+ };