@tomsd/mongodbclient 2.2.2 → 2.5.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.
|
@@ -25,8 +25,8 @@ export declare class MClient {
|
|
|
25
25
|
connect(): Promise<MongoConnection>;
|
|
26
26
|
protected getConnected(): Promise<MongoConnection>;
|
|
27
27
|
upsert(pobj: any): Promise<unknown>;
|
|
28
|
-
read(condition
|
|
29
|
-
distinct(key: string, condition
|
|
28
|
+
read(condition?: any, opt?: any): Promise<unknown>;
|
|
29
|
+
distinct(key: string, condition?: any): Promise<unknown>;
|
|
30
30
|
remove(condition: any): Promise<unknown>;
|
|
31
31
|
stats(): Promise<unknown>;
|
|
32
32
|
count(condition: any): Promise<number>;
|
|
@@ -9,7 +9,7 @@ exports.MClient = exports.MongoConnection = void 0;
|
|
|
9
9
|
const MongoClient = require("mongodb").MongoClient;
|
|
10
10
|
const Db = require("mongodb").Db;
|
|
11
11
|
const { Collection, UpdateWriteOpResult, deleteWriteOpResult, insertWriteOpResult } = require("mongodb");
|
|
12
|
-
const
|
|
12
|
+
const uuid_1 = require("uuid");
|
|
13
13
|
class MongoConnection {
|
|
14
14
|
constructor(client, db, collection) {
|
|
15
15
|
this._client = client;
|
|
@@ -62,7 +62,7 @@ class MClient {
|
|
|
62
62
|
}
|
|
63
63
|
upsert(pobj) {
|
|
64
64
|
const that = this;
|
|
65
|
-
pobj._id = "_id" in pobj ? pobj._id :
|
|
65
|
+
pobj._id = "_id" in pobj ? pobj._id : uuid_1.v4();
|
|
66
66
|
return new Promise(function (resolve, reject) {
|
|
67
67
|
that.getConnected()
|
|
68
68
|
.then(function (conn) {
|
|
@@ -78,7 +78,7 @@ class MClient {
|
|
|
78
78
|
}, reject);
|
|
79
79
|
});
|
|
80
80
|
}
|
|
81
|
-
read(condition, opt) {
|
|
81
|
+
read(condition = {}, opt) {
|
|
82
82
|
const that = this;
|
|
83
83
|
return new Promise(function (resolve, reject) {
|
|
84
84
|
that.getConnected()
|
|
@@ -96,7 +96,7 @@ class MClient {
|
|
|
96
96
|
});
|
|
97
97
|
});
|
|
98
98
|
}
|
|
99
|
-
distinct(key, condition) {
|
|
99
|
+
distinct(key, condition = {}) {
|
|
100
100
|
const that = this;
|
|
101
101
|
return new Promise(function (resolve, reject) {
|
|
102
102
|
that.getConnected()
|
|
@@ -167,7 +167,7 @@ class MClient {
|
|
|
167
167
|
insertMany(items) {
|
|
168
168
|
const that = this;
|
|
169
169
|
items.forEach(function (item) {
|
|
170
|
-
item._id = item._id ? item._id :
|
|
170
|
+
item._id = item._id ? item._id : uuid_1.v4();
|
|
171
171
|
});
|
|
172
172
|
return new Promise(function (resolve, reject) {
|
|
173
173
|
that.getConnected()
|
|
@@ -25,8 +25,8 @@ export declare class MClient {
|
|
|
25
25
|
connect(): Promise<MongoConnection>;
|
|
26
26
|
protected getConnected(): Promise<MongoConnection>;
|
|
27
27
|
upsert(pobj: any): Promise<unknown>;
|
|
28
|
-
read(condition
|
|
29
|
-
distinct(key: string, condition
|
|
28
|
+
read(condition?: any, opt?: any): Promise<unknown>;
|
|
29
|
+
distinct(key: string, condition?: any): Promise<unknown>;
|
|
30
30
|
remove(condition: any): Promise<unknown>;
|
|
31
31
|
stats(): Promise<unknown>;
|
|
32
32
|
count(condition: any): Promise<number>;
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
const MongoClient = require("mongodb").MongoClient;
|
|
7
7
|
const Db = require("mongodb").Db;
|
|
8
8
|
const { Collection, UpdateWriteOpResult, deleteWriteOpResult, insertWriteOpResult } = require("mongodb");
|
|
9
|
-
|
|
9
|
+
import { v4 as uuidv4 } from "uuid";
|
|
10
10
|
export class MongoConnection {
|
|
11
11
|
constructor(client, db, collection) {
|
|
12
12
|
this._client = client;
|
|
@@ -58,7 +58,7 @@ export class MClient {
|
|
|
58
58
|
}
|
|
59
59
|
upsert(pobj) {
|
|
60
60
|
const that = this;
|
|
61
|
-
pobj._id = "_id" in pobj ? pobj._id :
|
|
61
|
+
pobj._id = "_id" in pobj ? pobj._id : uuidv4();
|
|
62
62
|
return new Promise(function (resolve, reject) {
|
|
63
63
|
that.getConnected()
|
|
64
64
|
.then(function (conn) {
|
|
@@ -74,7 +74,7 @@ export class MClient {
|
|
|
74
74
|
}, reject);
|
|
75
75
|
});
|
|
76
76
|
}
|
|
77
|
-
read(condition, opt) {
|
|
77
|
+
read(condition = {}, opt) {
|
|
78
78
|
const that = this;
|
|
79
79
|
return new Promise(function (resolve, reject) {
|
|
80
80
|
that.getConnected()
|
|
@@ -92,7 +92,7 @@ export class MClient {
|
|
|
92
92
|
});
|
|
93
93
|
});
|
|
94
94
|
}
|
|
95
|
-
distinct(key, condition) {
|
|
95
|
+
distinct(key, condition = {}) {
|
|
96
96
|
const that = this;
|
|
97
97
|
return new Promise(function (resolve, reject) {
|
|
98
98
|
that.getConnected()
|
|
@@ -163,7 +163,7 @@ export class MClient {
|
|
|
163
163
|
insertMany(items) {
|
|
164
164
|
const that = this;
|
|
165
165
|
items.forEach(function (item) {
|
|
166
|
-
item._id = item._id ? item._id :
|
|
166
|
+
item._id = item._id ? item._id : uuidv4();
|
|
167
167
|
});
|
|
168
168
|
return new Promise(function (resolve, reject) {
|
|
169
169
|
that.getConnected()
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tomsd/mongodbclient",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/cjs/mongodbclient.js",
|
|
6
6
|
"module": "dist/esm/mongodbclient.js",
|
|
@@ -16,7 +16,6 @@
|
|
|
16
16
|
"author": "tom",
|
|
17
17
|
"license": "MIT",
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@tomsd/rand": "^3.0.0",
|
|
20
19
|
"mongodb": "^3.6.6",
|
|
21
20
|
"uuid": "^8.3.2"
|
|
22
21
|
},
|
|
@@ -25,6 +24,7 @@
|
|
|
25
24
|
"@types/mongodb": "^3.5.31",
|
|
26
25
|
"@types/node": "^14.14.5",
|
|
27
26
|
"@types/uuid": "^8.3.4",
|
|
27
|
+
"dotenv": "^16.0.1",
|
|
28
28
|
"mocha": "^7.0.1",
|
|
29
29
|
"ts-node": "^10.8.1",
|
|
30
30
|
"typescript": "^4.0.5"
|
package/test/test.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
import dotenv from "dotenv";
|
|
3
|
+
dotenv.config();
|
|
4
|
+
|
|
1
5
|
import {describe, it } from "mocha";
|
|
2
6
|
import { MClient } from "../src/mongodbclient";
|
|
3
7
|
import { strict as assert } from "assert";
|
|
4
8
|
import { v4 as uuidv4 } from "uuid";
|
|
5
9
|
|
|
6
|
-
const mongouri =
|
|
10
|
+
const mongouri = process.env.MONGODB_URI as string;
|
|
7
11
|
const dbName = uuidv4();
|
|
8
12
|
const collName = uuidv4();
|
|
9
13
|
|
|
@@ -28,18 +32,18 @@ describe("MClient", () => {
|
|
|
28
32
|
});
|
|
29
33
|
|
|
30
34
|
it("read()", async () => {
|
|
31
|
-
const docs = (await mdbc.read(
|
|
35
|
+
const docs = (await mdbc.read()) as any[];
|
|
32
36
|
assert.equal(docs.length, items.length);
|
|
33
37
|
});
|
|
34
38
|
|
|
35
39
|
it("upsert()", async () => {
|
|
36
|
-
const docs = (await mdbc.read(
|
|
40
|
+
const docs = (await mdbc.read()) as any[];
|
|
37
41
|
const { modifiedCount } = (await mdbc.upsert({ _id: docs[0]._id, name: "david" })) as { modifiedCount: number };
|
|
38
42
|
assert.equal(modifiedCount, 1);
|
|
39
43
|
});
|
|
40
44
|
|
|
41
45
|
it("distinct()", async () => {
|
|
42
|
-
const names = (await mdbc.distinct("name"
|
|
46
|
+
const names = (await mdbc.distinct("name")) as string[];
|
|
43
47
|
assert.equal(names.length, 4);
|
|
44
48
|
});
|
|
45
49
|
|