@tomsd/mongodbclient 2.0.1 → 2.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.
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @license mongodbclient
|
|
3
|
+
* (c) 2020 tom
|
|
4
|
+
* License: MIT
|
|
5
|
+
*/
|
|
6
|
+
declare const MongoClient: any;
|
|
7
|
+
declare const Db: any;
|
|
8
|
+
export declare class MongoConnection {
|
|
9
|
+
private _client;
|
|
10
|
+
private _db;
|
|
11
|
+
private _collection;
|
|
12
|
+
constructor(client: typeof MongoClient, db: typeof Db, collection: any);
|
|
13
|
+
get client(): typeof MongoClient;
|
|
14
|
+
get db(): typeof Db;
|
|
15
|
+
get collection(): any;
|
|
16
|
+
}
|
|
17
|
+
export declare class MClient {
|
|
18
|
+
private m_uri;
|
|
19
|
+
private m_db;
|
|
20
|
+
private m_collection;
|
|
21
|
+
constructor(uri: string, db: string, collection: string);
|
|
22
|
+
get uri(): string;
|
|
23
|
+
get db(): string;
|
|
24
|
+
get collection(): string;
|
|
25
|
+
connect(): Promise<MongoConnection>;
|
|
26
|
+
protected getConnected(): Promise<MongoConnection>;
|
|
27
|
+
upsert(pobj: any): Promise<unknown>;
|
|
28
|
+
read(condition: any, opt?: any): Promise<unknown>;
|
|
29
|
+
distinct(key: string, condition: any): Promise<unknown>;
|
|
30
|
+
remove(condition: any): Promise<unknown>;
|
|
31
|
+
stats(): Promise<unknown>;
|
|
32
|
+
count(condition: any): Promise<number>;
|
|
33
|
+
insertMany(items: any[]): Promise<unknown>;
|
|
34
|
+
dbStats(): Promise<unknown>;
|
|
35
|
+
getCollections(): Promise<any[]>;
|
|
36
|
+
}
|
|
37
|
+
export default MClient;
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @license mongodbclient
|
|
3
|
+
* (c) 2020 tom
|
|
4
|
+
* License: MIT
|
|
5
|
+
*/
|
|
6
|
+
const MongoClient = require("mongodb").MongoClient;
|
|
7
|
+
const Db = require("mongodb").Db;
|
|
8
|
+
const { Collection, UpdateWriteOpResult, deleteWriteOpResult, insertWriteOpResult } = require("mongodb");
|
|
9
|
+
const rand = require("@tomsd/rand").default;
|
|
10
|
+
export class MongoConnection {
|
|
11
|
+
constructor(client, db, collection) {
|
|
12
|
+
this._client = client;
|
|
13
|
+
this._db = db;
|
|
14
|
+
this._collection = collection;
|
|
15
|
+
}
|
|
16
|
+
get client() {
|
|
17
|
+
return this._client;
|
|
18
|
+
}
|
|
19
|
+
get db() {
|
|
20
|
+
return this._db;
|
|
21
|
+
}
|
|
22
|
+
get collection() {
|
|
23
|
+
return this._collection;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export class MClient {
|
|
27
|
+
constructor(uri, db, collection) {
|
|
28
|
+
this.m_uri = uri;
|
|
29
|
+
this.m_db = db;
|
|
30
|
+
this.m_collection = collection;
|
|
31
|
+
}
|
|
32
|
+
get uri() {
|
|
33
|
+
return this.m_uri;
|
|
34
|
+
}
|
|
35
|
+
get db() {
|
|
36
|
+
return this.m_db;
|
|
37
|
+
}
|
|
38
|
+
get collection() {
|
|
39
|
+
return this.m_collection;
|
|
40
|
+
}
|
|
41
|
+
connect() {
|
|
42
|
+
return this.getConnected();
|
|
43
|
+
}
|
|
44
|
+
getConnected() {
|
|
45
|
+
const that = this;
|
|
46
|
+
const client = new MongoClient(that.m_uri, { useUnifiedTopology: true });
|
|
47
|
+
return new Promise(function (resolve, reject) {
|
|
48
|
+
client.connect()
|
|
49
|
+
.then(function (client) {
|
|
50
|
+
const db = client.db(that.m_db);
|
|
51
|
+
const collection = db.collection(that.m_collection);
|
|
52
|
+
resolve(new MongoConnection(client, db, collection));
|
|
53
|
+
})
|
|
54
|
+
.catch(function (e) {
|
|
55
|
+
reject(e);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
upsert(pobj) {
|
|
60
|
+
const that = this;
|
|
61
|
+
pobj._id = "_id" in pobj ? pobj._id : rand.id(rand.char());
|
|
62
|
+
return new Promise(function (resolve, reject) {
|
|
63
|
+
that.getConnected()
|
|
64
|
+
.then(function (conn) {
|
|
65
|
+
conn.collection.updateOne({ _id: pobj._id }, { $set: pobj }, { upsert: true, writeConcern: { w: 1 } })
|
|
66
|
+
.then(function (r) {
|
|
67
|
+
conn.client.close();
|
|
68
|
+
resolve(r);
|
|
69
|
+
})
|
|
70
|
+
.catch(function (e) {
|
|
71
|
+
conn.client.close();
|
|
72
|
+
reject(e);
|
|
73
|
+
});
|
|
74
|
+
}, reject);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
read(condition, opt) {
|
|
78
|
+
const that = this;
|
|
79
|
+
return new Promise(function (resolve, reject) {
|
|
80
|
+
that.getConnected()
|
|
81
|
+
.then(function (conn) {
|
|
82
|
+
conn.collection.find(condition, opt)
|
|
83
|
+
.toArray()
|
|
84
|
+
.then(function (docs) {
|
|
85
|
+
conn.client.close();
|
|
86
|
+
resolve(docs);
|
|
87
|
+
})
|
|
88
|
+
.catch(function (e) {
|
|
89
|
+
conn.client.close();
|
|
90
|
+
reject(e);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
distinct(key, condition) {
|
|
96
|
+
const that = this;
|
|
97
|
+
return new Promise(function (resolve, reject) {
|
|
98
|
+
that.getConnected()
|
|
99
|
+
.then(function (conn) {
|
|
100
|
+
conn.collection.distinct(key, condition)
|
|
101
|
+
.then(function (values) {
|
|
102
|
+
conn.client.close();
|
|
103
|
+
resolve(values);
|
|
104
|
+
})
|
|
105
|
+
.catch(function (e) {
|
|
106
|
+
conn.client.close();
|
|
107
|
+
reject(e);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
remove(condition) {
|
|
113
|
+
const that = this;
|
|
114
|
+
return new Promise(function (resolve, reject) {
|
|
115
|
+
that.getConnected()
|
|
116
|
+
.then(function (conn) {
|
|
117
|
+
conn.collection.deleteMany(condition, { writeConcern: { w: 1 } })
|
|
118
|
+
.then(function (r) {
|
|
119
|
+
conn.client.close();
|
|
120
|
+
resolve(r);
|
|
121
|
+
})
|
|
122
|
+
.catch(function (e) {
|
|
123
|
+
conn.client.close();
|
|
124
|
+
reject(e);
|
|
125
|
+
});
|
|
126
|
+
}, reject);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
stats() {
|
|
130
|
+
const that = this;
|
|
131
|
+
return new Promise(function (resolve, reject) {
|
|
132
|
+
that.getConnected()
|
|
133
|
+
.then(function (conn) {
|
|
134
|
+
conn.collection.stats()
|
|
135
|
+
.then(function (r) {
|
|
136
|
+
conn.client.close();
|
|
137
|
+
resolve(r);
|
|
138
|
+
})
|
|
139
|
+
.catch(function (e) {
|
|
140
|
+
conn.client.close();
|
|
141
|
+
reject(e);
|
|
142
|
+
});
|
|
143
|
+
}, reject);
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
count(condition) {
|
|
147
|
+
const that = this;
|
|
148
|
+
return new Promise(function (resolve, reject) {
|
|
149
|
+
that.getConnected()
|
|
150
|
+
.then(function (conn) {
|
|
151
|
+
conn.collection.countDocuments(condition)
|
|
152
|
+
.then(function (r) {
|
|
153
|
+
conn.client.close();
|
|
154
|
+
resolve(r);
|
|
155
|
+
})
|
|
156
|
+
.catch(function (e) {
|
|
157
|
+
conn.client.close();
|
|
158
|
+
reject(e);
|
|
159
|
+
});
|
|
160
|
+
}, reject);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
insertMany(items) {
|
|
164
|
+
const that = this;
|
|
165
|
+
items.forEach(function (item) {
|
|
166
|
+
item._id = item._id ? item._id : rand.id(rand.char());
|
|
167
|
+
});
|
|
168
|
+
return new Promise(function (resolve, reject) {
|
|
169
|
+
that.getConnected()
|
|
170
|
+
.then(function (conn) {
|
|
171
|
+
conn.collection.insertMany(items, { writeConcern: { w: 1 } })
|
|
172
|
+
.then(function (r) {
|
|
173
|
+
conn.client.close();
|
|
174
|
+
resolve(r);
|
|
175
|
+
})
|
|
176
|
+
.catch(function (e) {
|
|
177
|
+
conn.client.close();
|
|
178
|
+
reject(e);
|
|
179
|
+
});
|
|
180
|
+
}, reject);
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
dbStats() {
|
|
184
|
+
const that = this;
|
|
185
|
+
return new Promise(function (resolve, reject) {
|
|
186
|
+
that.getConnected()
|
|
187
|
+
.then(function (conn) {
|
|
188
|
+
conn.db.stats()
|
|
189
|
+
.then(function (r) {
|
|
190
|
+
conn.client.close();
|
|
191
|
+
resolve(r);
|
|
192
|
+
})
|
|
193
|
+
.catch(function (e) {
|
|
194
|
+
conn.client.close();
|
|
195
|
+
reject(e);
|
|
196
|
+
});
|
|
197
|
+
}, reject);
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
getCollections() {
|
|
201
|
+
const that = this;
|
|
202
|
+
return new Promise(function (resolve, reject) {
|
|
203
|
+
that.getConnected()
|
|
204
|
+
.then(function (conn) {
|
|
205
|
+
conn.db.collections()
|
|
206
|
+
.then(function (r) {
|
|
207
|
+
conn.client.close();
|
|
208
|
+
resolve(r);
|
|
209
|
+
})
|
|
210
|
+
.catch(function (e) {
|
|
211
|
+
conn.client.close();
|
|
212
|
+
reject(e);
|
|
213
|
+
});
|
|
214
|
+
}, reject);
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
export default MClient;
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tomsd/mongodbclient",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "",
|
|
5
|
-
"main": "dist/mongodbclient.js",
|
|
6
|
-
"
|
|
5
|
+
"main": "dist/cjs/mongodbclient.js",
|
|
6
|
+
"module": "dist/esm/mongodbclient.js",
|
|
7
|
+
"types": "dist/esm/mongodbclient.d.ts",
|
|
7
8
|
"scripts": {
|
|
8
|
-
"build": "tsc --
|
|
9
|
+
"build": "tsc --project tsconfig.cjs.json && tsc --project tsconfig.esm.json",
|
|
9
10
|
"test": "exit 0",
|
|
10
|
-
"test_with_dburi": "mocha test/test.
|
|
11
|
+
"test_with_dburi": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' mocha -r ts-node/register \"test/test.ts\" --timeout 30000"
|
|
11
12
|
},
|
|
12
13
|
"keywords": [
|
|
13
14
|
"mongodb"
|
|
@@ -19,9 +20,13 @@
|
|
|
19
20
|
"mongodb": "^3.6.6"
|
|
20
21
|
},
|
|
21
22
|
"devDependencies": {
|
|
23
|
+
"@types/mocha": "^9.1.1",
|
|
22
24
|
"@types/mongodb": "^3.5.31",
|
|
23
25
|
"@types/node": "^14.14.5",
|
|
26
|
+
"@types/uuid": "^8.3.4",
|
|
24
27
|
"mocha": "^7.0.1",
|
|
25
|
-
"
|
|
28
|
+
"ts-node": "^10.8.1",
|
|
29
|
+
"typescript": "^4.0.5",
|
|
30
|
+
"uuid": "^8.3.2"
|
|
26
31
|
}
|
|
27
32
|
}
|
package/test/test.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import {describe, it } from "mocha";
|
|
2
|
+
import { MClient } from "../src/mongodbclient";
|
|
3
|
+
import { strict as assert } from "assert";
|
|
4
|
+
import { v4 as uuidv4 } from "uuid";
|
|
5
|
+
|
|
6
|
+
const mongouri = "mongodb+srv://...";
|
|
7
|
+
const dbName = uuidv4();
|
|
8
|
+
const collName = uuidv4();
|
|
9
|
+
|
|
10
|
+
const mdbc = new MClient(mongouri, dbName, collName);
|
|
11
|
+
|
|
12
|
+
const items = [
|
|
13
|
+
{name:"alice"},
|
|
14
|
+
{name:"bob"},
|
|
15
|
+
{name:"charlie"},
|
|
16
|
+
{name:"alice"}
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
describe("MClient", () => {
|
|
20
|
+
it("insertMany()", async () => {
|
|
21
|
+
const { insertedCount } = (await mdbc.insertMany(items)) as { insertedCount: number };
|
|
22
|
+
assert.equal(insertedCount, items.length);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("getCollections()", async () => {
|
|
26
|
+
const collections = await mdbc.getCollections();
|
|
27
|
+
assert(collections.length > 0);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("read()", async () => {
|
|
31
|
+
const docs = (await mdbc.read({})) as any[];
|
|
32
|
+
assert.equal(docs.length, items.length);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("upsert()", async () => {
|
|
36
|
+
const docs = (await mdbc.read({})) as any[];
|
|
37
|
+
const { modifiedCount } = (await mdbc.upsert({ _id: docs[0]._id, name: "david" })) as { modifiedCount: number };
|
|
38
|
+
assert.equal(modifiedCount, 1);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("distinct()", async () => {
|
|
42
|
+
const names = (await mdbc.distinct("name", {})) as string[];
|
|
43
|
+
assert.equal(names.length, 4);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("stats()", async () => {
|
|
47
|
+
const { storageSize } = (await mdbc.stats()) as { storageSize: number };
|
|
48
|
+
assert(storageSize > 0);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("count()", async () => {
|
|
52
|
+
const n = await mdbc.count({name:"alice"});
|
|
53
|
+
assert.equal(n,1);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("remove()", async () => {
|
|
57
|
+
const { deletedCount } = (await mdbc.remove({})) as { deletedCount: number };
|
|
58
|
+
assert.equal(deletedCount, items.length);
|
|
59
|
+
});
|
|
60
|
+
});
|
package/test/test.js
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
const mongouri = "mongodb+srv://...";
|
|
2
|
-
const dbName = `db${(new Date()).getTime().toString()}`;
|
|
3
|
-
const collName = `col${(new Date()).getTime().toString()}`;
|
|
4
|
-
|
|
5
|
-
const assert = require("assert");
|
|
6
|
-
const MClient = require("../dist/mongodbclient.js").MClient;
|
|
7
|
-
|
|
8
|
-
const mdbc = new MClient(mongouri, dbName, collName);
|
|
9
|
-
|
|
10
|
-
const items = [
|
|
11
|
-
{name:"alice"},
|
|
12
|
-
{name:"bob"},
|
|
13
|
-
{name:"charlie"},
|
|
14
|
-
{name:"alice"}
|
|
15
|
-
];
|
|
16
|
-
|
|
17
|
-
describe("MClient", function(){
|
|
18
|
-
it("insertMany()",function(done){
|
|
19
|
-
mdbc.insertMany(items)
|
|
20
|
-
.then(function(r){
|
|
21
|
-
assert.equal(r.insertedCount, items.length);
|
|
22
|
-
done();
|
|
23
|
-
});
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it("getCollections()", function(done){
|
|
27
|
-
mdbc.getCollections()
|
|
28
|
-
.then(function(collections){
|
|
29
|
-
assert(collections.length > 0);
|
|
30
|
-
done();
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it("read()", function(done){
|
|
35
|
-
mdbc.read()
|
|
36
|
-
.then(function(docs){
|
|
37
|
-
assert.equal(docs.length, items.length);
|
|
38
|
-
done();
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it("upsert()", function(done){
|
|
43
|
-
mdbc.read()
|
|
44
|
-
.then(function(docs){
|
|
45
|
-
return mdbc.upsert({_id:docs[0]._id, name:"david"});
|
|
46
|
-
})
|
|
47
|
-
.then(function(r){
|
|
48
|
-
assert.equal(r.modifiedCount, 1);
|
|
49
|
-
done();
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it("distinct()", function(done){
|
|
54
|
-
mdbc.distinct("name")
|
|
55
|
-
.then(function(names){
|
|
56
|
-
assert.equal(names.length, 4);
|
|
57
|
-
done();
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it("stats()", function(done){
|
|
62
|
-
mdbc.stats()
|
|
63
|
-
.then(function(r){
|
|
64
|
-
assert(r.storageSize > 0);
|
|
65
|
-
done();
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it("count()", function(done){
|
|
70
|
-
mdbc.count({name:"alice"})
|
|
71
|
-
.then(function(n){
|
|
72
|
-
assert.equal(n,1);
|
|
73
|
-
done();
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it("remove()", function(done){
|
|
78
|
-
mdbc.remove()
|
|
79
|
-
.then(function(r){
|
|
80
|
-
assert.equal(r.deletedCount, items.length);
|
|
81
|
-
done();
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
});
|