@unchainedshop/mongodb 1.1.4 → 1.2.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/jest.config.js ADDED
@@ -0,0 +1,5 @@
1
+ /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
2
+ export default {
3
+ preset: 'ts-jest',
4
+ testEnvironment: 'node',
5
+ };
@@ -1,3 +1,5 @@
1
1
  import { Db } from 'mongodb';
2
+ export declare const startDb: () => Promise<string>;
3
+ export declare const stopDb: () => Promise<void>;
2
4
  declare const initDb: () => Promise<Db>;
3
5
  export { initDb };
package/lib/db/initDb.js CHANGED
@@ -1,26 +1,51 @@
1
- import { MongoInternals } from 'meteor/mongo';
2
- import { NpmModuleMongodb } from 'meteor/npm-mongo';
3
- // const url = process.env.MONGO_URL || 'mongodb://localhost:4011';
4
- // const initDbNative = async (): Promise<Db> => {
5
- // const client = new MongoClient(url);
6
- // const dbName = 'meteor';
7
- // await client.connect();
8
- // const db = client.db(dbName);
9
- // return db;
10
- // };
11
- const originalFn = NpmModuleMongodb.Collection.prototype.updateOne;
12
- NpmModuleMongodb.Collection.prototype.updateOne = async function updateOne(...rest) {
13
- const result = await originalFn.bind(this)(...rest);
14
- if (!result)
15
- return result;
16
- return {
17
- result: { nModified: result.modifiedCount },
18
- ...result,
19
- };
1
+ import { mkdirSync } from 'fs';
2
+ import { MongoClient } from 'mongodb';
3
+ let mongod;
4
+ export const startDb = async () => {
5
+ const { MongoMemoryServer } = await import('mongodb-memory-server');
6
+ try {
7
+ mkdirSync(`${process.cwd()}/.db`);
8
+ }
9
+ catch (e) {
10
+ //
11
+ }
12
+ console.log(`Local MongoDB Server Data: ${`${process.cwd()}/.db`}`);
13
+ mongod = await MongoMemoryServer.create({
14
+ instance: {
15
+ dbPath: `${process.cwd()}/.db`,
16
+ storageEngine: 'wiredTiger',
17
+ },
18
+ });
19
+ return `${mongod.getUri()}unchained`;
20
+ };
21
+ export const stopDb = async () => {
22
+ await mongod.stop();
20
23
  };
24
+ const initDbNative = async () => {
25
+ const url = process.env.MONGO_URL || (await startDb());
26
+ const client = new MongoClient(url);
27
+ await client.connect();
28
+ const db = client.db();
29
+ return db;
30
+ };
31
+ const isMeteor = typeof Meteor === 'object';
32
+ if (isMeteor) {
33
+ const { NpmModuleMongodb } = await import('meteor/npm-mongo');
34
+ const originalFn = NpmModuleMongodb.Collection.prototype.updateOne;
35
+ NpmModuleMongodb.Collection.prototype.updateOne = async function updateOne(...rest) {
36
+ const result = await originalFn.bind(this)(...rest);
37
+ if (!result)
38
+ return result;
39
+ return {
40
+ result: { nModified: result.modifiedCount },
41
+ ...result,
42
+ };
43
+ };
44
+ }
21
45
  const initDbMeteor = async () => {
46
+ const { MongoInternals } = await import('meteor/mongo');
22
47
  return MongoInternals.defaultRemoteCollectionDriver().mongo.db;
23
48
  };
24
- const initDb = initDbMeteor;
49
+ const initDb = isMeteor ? initDbMeteor : initDbNative;
25
50
  export { initDb };
26
51
  //# sourceMappingURL=initDb.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"initDb.js","sourceRoot":"","sources":["../../src/db/initDb.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACpD,mEAAmE;AAEnE,kDAAkD;AAClD,yCAAyC;AAEzC,6BAA6B;AAC7B,4BAA4B;AAC5B,kCAAkC;AAClC,eAAe;AACf,KAAK;AACL,MAAM,UAAU,GAAG,gBAAgB,CAAC,UAAU,CAAC,SAAS,CAAC,SAAS,CAAC;AACnE,gBAAgB,CAAC,UAAU,CAAC,SAAS,CAAC,SAAS,GAAG,KAAK,UAAU,SAAS,CAAC,GAAG,IAAI;IAChF,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;IACpD,IAAI,CAAC,MAAM;QAAE,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CAAC;AAEtD,OAAO,EAAE,MAAM,EAAE,CAAC"}
@@ -1 +1 @@
1
- export { initDb } from './db/initDb';
1
+ export { initDb, startDb, stopDb } from './db/initDb';
@@ -1,2 +1,2 @@
1
- export { initDb } from './db/initDb';
1
+ export { initDb, startDb, stopDb } from './db/initDb';
2
2
  //# sourceMappingURL=mongodb-index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"mongodb-index.js","sourceRoot":"","sources":["../src/mongodb-index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC"}
1
+ {"version":3,"file":"mongodb-index.js","sourceRoot":"","sources":["../src/mongodb-index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC"}
package/package.json CHANGED
@@ -1,15 +1,20 @@
1
1
  {
2
2
  "name": "@unchainedshop/mongodb",
3
- "version": "1.1.4",
3
+ "version": "1.2.0",
4
4
  "description": "MongoDB provider for unchained platform",
5
5
  "main": "lib/mongodb-index.js",
6
+ "exports": {
7
+ ".": "./lib/mongodb-index.js",
8
+ "./*": "./lib/*"
9
+ },
6
10
  "types": "lib/mongodb-index.d.ts",
7
11
  "type": "module",
8
12
  "scripts": {
13
+ "prepublishOnly": "npm install && npm run build || :",
9
14
  "clean": "rm -rf lib",
10
- "build": "npm run clean && tsc -p tsconfig.build.json",
11
- "build:watch": "npm run build --watch",
12
- "test": "METEOR_PACKAGE_DIRS=../ TEST_CLIENT=0 TEST_WATCH=1 meteor test-packages ./ --driver-package meteortesting:mocha --port 4200"
15
+ "build": "npm run clean && tsc",
16
+ "watch": "tsc -w",
17
+ "test": "jest --watch"
13
18
  },
14
19
  "repository": {
15
20
  "type": "git",
@@ -26,12 +31,15 @@
26
31
  },
27
32
  "homepage": "https://github.com/unchainedshop/unchained#readme",
28
33
  "dependencies": {
29
- "mongodb": "^4.3.1",
30
- "@unchainedshop/utils": "1.1.3"
34
+ "@unchainedshop/utils": "^1.1.9",
35
+ "mongodb": "^4.3.1"
36
+ },
37
+ "optionalDependencies": {
38
+ "mongodb-memory-server": "^8.7.2"
31
39
  },
32
40
  "devDependencies": {
33
- "@types/node": "^16.11.41",
34
- "@unchainedshop/types": "^1.1.5",
41
+ "@types/node": "^16.11.44",
42
+ "@unchainedshop/types": "^1.1.9",
35
43
  "chai": "^4.3.6",
36
44
  "typescript": "^4.7.4"
37
45
  }
package/src/db/initDb.ts CHANGED
@@ -1,30 +1,58 @@
1
- import { MongoInternals } from 'meteor/mongo';
2
- import { Db /* MongoClient */ } from 'mongodb';
3
- import { NpmModuleMongodb } from 'meteor/npm-mongo';
4
- // const url = process.env.MONGO_URL || 'mongodb://localhost:4011';
5
-
6
- // const initDbNative = async (): Promise<Db> => {
7
- // const client = new MongoClient(url);
8
-
9
- // const dbName = 'meteor';
10
- // await client.connect();
11
- // const db = client.db(dbName);
12
- // return db;
13
- // };
14
- const originalFn = NpmModuleMongodb.Collection.prototype.updateOne;
15
- NpmModuleMongodb.Collection.prototype.updateOne = async function updateOne(...rest) {
16
- const result = await originalFn.bind(this)(...rest);
17
- if (!result) return result;
18
- return {
19
- result: { nModified: result.modifiedCount },
20
- ...result,
21
- };
1
+ import { mkdirSync } from 'fs';
2
+ import { Db, MongoClient } from 'mongodb';
3
+
4
+ let mongod;
5
+
6
+ export const startDb = async () => {
7
+ const { MongoMemoryServer } = await import('mongodb-memory-server');
8
+
9
+ try {
10
+ mkdirSync(`${process.cwd()}/.db`);
11
+ } catch (e) {
12
+ //
13
+ }
14
+ console.log(`Local MongoDB Server Data: ${`${process.cwd()}/.db`}`);
15
+ mongod = await MongoMemoryServer.create({
16
+ instance: {
17
+ dbPath: `${process.cwd()}/.db`,
18
+ storageEngine: 'wiredTiger',
19
+ },
20
+ });
21
+ return `${mongod.getUri()}unchained`;
22
+ };
23
+
24
+ export const stopDb = async () => {
25
+ await mongod.stop();
22
26
  };
23
27
 
28
+ const initDbNative = async (): Promise<Db> => {
29
+ const url = process.env.MONGO_URL || (await startDb());
30
+ const client = new MongoClient(url);
31
+ await client.connect();
32
+ const db = client.db();
33
+ return db;
34
+ };
35
+
36
+ const isMeteor = typeof Meteor === 'object';
37
+
38
+ if (isMeteor) {
39
+ const { NpmModuleMongodb } = await import('meteor/npm-mongo');
40
+ const originalFn = NpmModuleMongodb.Collection.prototype.updateOne;
41
+ NpmModuleMongodb.Collection.prototype.updateOne = async function updateOne(...rest) {
42
+ const result = await originalFn.bind(this)(...rest);
43
+ if (!result) return result;
44
+ return {
45
+ result: { nModified: result.modifiedCount },
46
+ ...result,
47
+ };
48
+ };
49
+ }
50
+
24
51
  const initDbMeteor = async (): Promise<Db> => {
52
+ const { MongoInternals } = await import('meteor/mongo');
25
53
  return MongoInternals.defaultRemoteCollectionDriver().mongo.db as Db;
26
54
  };
27
55
 
28
- const initDb = initDbMeteor;
56
+ const initDb = isMeteor ? initDbMeteor : initDbNative;
29
57
 
30
58
  export { initDb };
@@ -1 +1 @@
1
- export { initDb } from './db/initDb';
1
+ export { initDb, startDb, stopDb } from './db/initDb';
package/tsconfig.json CHANGED
@@ -1,8 +1,29 @@
1
1
  {
2
- "extends": "./tsconfig.build.json",
3
2
  "compilerOptions": {
4
- "noEmit": true,
5
- "types": ["node", "mocha"]
3
+ "allowJs": true,
4
+ "allowSyntheticDefaultImports": true,
5
+ "declaration": true,
6
+ "esModuleInterop": true,
7
+ "experimentalDecorators": true,
8
+ "forceConsistentCasingInFileNames": true,
9
+ "lib": [
10
+ "esnext"
11
+ ],
12
+ "module": "esnext",
13
+ "moduleResolution": "node",
14
+ "noImplicitReturns": true,
15
+ "noUnusedLocals": false,
16
+ "outDir": "lib",
17
+ "preserveWatchOutput": true,
18
+ "skipLibCheck": true,
19
+ "sourceMap": true,
20
+ "target": "esnext",
21
+ "types": [
22
+ "node",
23
+ "jest"
24
+ ]
6
25
  },
7
- "include": ["src", "tests"]
8
- }
26
+ "include": [
27
+ "src"
28
+ ]
29
+ }
@@ -1,26 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "allowJs": true,
4
- "allowSyntheticDefaultImports": true,
5
- "declaration": true,
6
- "esModuleInterop": true,
7
- "experimentalDecorators": true,
8
- "forceConsistentCasingInFileNames": true,
9
- "lib": ["esnext"],
10
- "module": "esnext",
11
- "moduleResolution": "node",
12
- "noImplicitReturns": true,
13
- "noUnusedLocals": false,
14
- "outDir": "lib",
15
- "preserveWatchOutput": true,
16
- "skipLibCheck": true,
17
- "sourceMap": true,
18
- "target": "esnext",
19
- "types": ["node"],
20
- "baseUrl": ".", // This must be specified if "paths" is.
21
- "paths": {
22
- "meteor/unchained:*": ["node_modules/@unchainedshop/types/index.d.ts"]
23
- }
24
- },
25
- "include": ["src"]
26
- }