insite-db 1.0.30

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) 2024 Eugene Nesvetaev
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,13 @@
1
+ # MongoDB for inSite
2
+
3
+ ## Installation
4
+
5
+ ```sh
6
+ $ npm install insite-db
7
+ ```
8
+
9
+ Or
10
+
11
+ ```sh
12
+ $ yarn add insite-db
13
+ ```
@@ -0,0 +1,55 @@
1
+ export class Collections extends Map {
2
+ constructor(db) {
3
+ super();
4
+
5
+ this.db = db;
6
+
7
+ }
8
+
9
+ async ensure(name, options = {}) {
10
+
11
+ const { db } = this;
12
+
13
+ let collection = this.get(name);
14
+
15
+ if (!collection) {
16
+ if ((await db.listCollections({ name }, { nameOnly: true }).toArray()).length)
17
+ collection = db.collection(name);
18
+ else {
19
+ collection = await db.createCollection(name, {
20
+ ...options.jsonSchema && { validator: { $jsonSchema: options.jsonSchema } },
21
+ ...options.blockCompressor && { storageEngine: { wiredTiger: { configString: `block_compressor=${options.blockCompressor}` } } }
22
+ });
23
+ delete options.jsonSchema;
24
+ delete options.blockCompressor;
25
+ }
26
+
27
+ this.set(name, collection);
28
+ this[name] = collection;
29
+
30
+ if (options.watch !== false) {
31
+ collection.changeListeners = new Set();
32
+
33
+ collection.changeStream =
34
+ collection.watch({ fullDocument: options.fullDocument === false ? undefined : "updateLookup" })
35
+ .on("change", collection.handleChangeStreamChange)
36
+ .on("error", Collections.handleChangeStreamError);
37
+ }
38
+ }
39
+
40
+ if (options.jsonSchema)
41
+ await db.command({
42
+ collMod: name,
43
+ ...options.jsonSchema && { validator: { $jsonSchema: options.jsonSchema } }
44
+ });
45
+
46
+ return collection;
47
+ }
48
+
49
+
50
+ static handleChangeStreamError(error) {
51
+ console.error(`🌿❗️ Mongo ${this.parent.collectionName} Change Stream:`, error);
52
+
53
+ }
54
+
55
+ }
package/lib/connect.js ADDED
@@ -0,0 +1,37 @@
1
+ import { MongoClient } from "mongodb";
2
+ import { Collections } from "./Collections";
3
+
4
+
5
+ export async function connect(url, name) {
6
+
7
+ if (/localhost/.test(url))
8
+ url = url.replace(/localhost/, "127.0.0.1");
9
+
10
+ const client = new MongoClient(url, { maxPoolSize: 100 });
11
+
12
+ try {
13
+ await client.connect();
14
+ console.info("🌿 Mongo Client connected to", url.replace(/(?<=\/\/)[^:]+?:[^@]+@/, "").replace(/(?:\/|\?)[^/]*$/, ""));
15
+ } catch (error) {
16
+ console.error("🌿❗️ Mongo Client connection: ", error);
17
+ }
18
+
19
+ client
20
+ .on("close", () => console.error("🌿❗️ Mongo DB socket closed"))
21
+ .on("error", error => console.error("🌿❗️ Mongo DB:", error))
22
+ .on("parseError", error => console.error("🌿❗️ Mongo DB parse:", error))
23
+ .on("reconnect", () => console.info("🌿 Mongo DB reconnected"))
24
+ .on("timeout", () => console.error("🌿❗️ Mongo DB timeout"));
25
+
26
+ const db = client.db(name);
27
+
28
+ const collections = new Collections(db);
29
+
30
+ db.insiteCollections = collections;
31
+
32
+ return {
33
+ client,
34
+ db,
35
+ collections
36
+ };
37
+ }
@@ -0,0 +1,25 @@
1
+ import { Collection } from "mongodb";
2
+
3
+
4
+ Collection.prototype.ensureIndexes = async function (indexesToEnsure) {
5
+
6
+ // const existingIndexes = await this.indexes();
7
+
8
+ // TODO:
9
+
10
+ try {
11
+ for (const indexToEnsure of indexesToEnsure)
12
+ await this.createIndex(...indexToEnsure);
13
+ } catch {}
14
+
15
+ };
16
+
17
+ Collection.prototype.handleChangeStreamChange = function (next) {
18
+
19
+ if (process.env.NODE_ENV === "development")
20
+ console.log("Change stream change", this.parent.collectionName, next);
21
+
22
+ for (const listener of this.parent.changeListeners)
23
+ listener(next);
24
+
25
+ };
@@ -0,0 +1 @@
1
+ import "./Collection";
package/lib/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export { Binary, GridFSBucket, ObjectId } from "mongodb";
2
+ import "./extensions";
3
+
4
+
5
+ export { connect } from "./connect";
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "insite-db",
3
+ "version": "1.0.30",
4
+ "description": "MongoDB for inSite",
5
+ "type": "module",
6
+ "main": "./lib/index.js",
7
+ "module": "./lib/index.js",
8
+ "packageManager": "yarn@1.22.22",
9
+ "scripts": {
10
+ "test": "echo \"Error: no test specified\" && exit 1"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/nesvet/insite-db.git"
15
+ },
16
+ "keywords": [
17
+ "insite",
18
+ "db",
19
+ "mongodb"
20
+ ],
21
+ "author": "Eugene Nesvetaev <dev@nesvet.dev> (https://nesvet.dev/)",
22
+ "license": "MIT",
23
+ "dependencies": {
24
+ "mongodb": "^5"
25
+ },
26
+ "devDependencies": {
27
+ "@nesvet/eslint-config": "^1"
28
+ }
29
+ }