@tomsd/mongodbclient 2.0.0 → 2.1.1

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.
@@ -66,7 +66,7 @@ class MClient {
66
66
  return new Promise(function (resolve, reject) {
67
67
  that.getConnected()
68
68
  .then(function (conn) {
69
- conn.collection.updateOne({ _id: pobj._id }, { $set: pobj }, { upsert: true, w: 1 })
69
+ conn.collection.updateOne({ _id: pobj._id }, { $set: pobj }, { upsert: true, writeConcern: { w: 1 } })
70
70
  .then(function (r) {
71
71
  conn.client.close();
72
72
  resolve(r);
@@ -118,7 +118,7 @@ class MClient {
118
118
  return new Promise(function (resolve, reject) {
119
119
  that.getConnected()
120
120
  .then(function (conn) {
121
- conn.collection.deleteMany(condition, { w: 1 })
121
+ conn.collection.deleteMany(condition, { writeConcern: { w: 1 } })
122
122
  .then(function (r) {
123
123
  conn.client.close();
124
124
  resolve(r);
@@ -172,7 +172,7 @@ class MClient {
172
172
  return new Promise(function (resolve, reject) {
173
173
  that.getConnected()
174
174
  .then(function (conn) {
175
- conn.collection.insertMany(items, { w: 1 })
175
+ conn.collection.insertMany(items, { writeConcern: { w: 1 } })
176
176
  .then(function (r) {
177
177
  conn.client.close();
178
178
  resolve(r);
@@ -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,11 +1,12 @@
1
1
  {
2
2
  "name": "@tomsd/mongodbclient",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
4
4
  "description": "",
5
- "main": "dist/mongodbclient.js",
6
- "types": "dist/mongodbclient.d.ts",
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 --build tsconfig.json",
9
+ "build": "tsc --project tsconfig.cjs.json && tsc --project tsconfig.esm.json",
9
10
  "test": "exit 0",
10
11
  "test_with_dburi": "mocha test/test.js --timeout 20000"
11
12
  },
@@ -16,7 +17,7 @@
16
17
  "license": "MIT",
17
18
  "dependencies": {
18
19
  "@tomsd/rand": "^3.0.0",
19
- "mongodb": "^3.6.2"
20
+ "mongodb": "^3.6.6"
20
21
  },
21
22
  "devDependencies": {
22
23
  "@types/mongodb": "^3.5.31",
package/test/test.js CHANGED
@@ -3,7 +3,7 @@ const dbName = `db${(new Date()).getTime().toString()}`;
3
3
  const collName = `col${(new Date()).getTime().toString()}`;
4
4
 
5
5
  const assert = require("assert");
6
- const MClient = require("../dist/mongodbclient.js").MClient;
6
+ const MClient = require("../dist/cjs/mongodbclient.js").MClient;
7
7
 
8
8
  const mdbc = new MClient(mongouri, dbName, collName);
9
9
 
@@ -14,71 +14,45 @@ const items = [
14
14
  {name:"alice"}
15
15
  ];
16
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
- });
17
+ describe("MClient", () => {
18
+ it("insertMany()", async () => {
19
+ const { insertedCount } = await mdbc.insertMany(items)
20
+ assert.equal(insertedCount, items.length);
24
21
  });
25
22
 
26
- it("getCollections()", function(done){
27
- mdbc.getCollections()
28
- .then(function(collections){
29
- assert(collections.length > 0);
30
- done();
31
- });
23
+ it("getCollections()", async () => {
24
+ const collections = await mdbc.getCollections();
25
+ assert(collections.length > 0);
32
26
  });
33
27
 
34
- it("read()", function(done){
35
- mdbc.read()
36
- .then(function(docs){
37
- assert.equal(docs.length, items.length);
38
- done();
39
- });
28
+ it("read()", async () => {
29
+ const docs = await mdbc.read();
30
+ assert.equal(docs.length, items.length);
40
31
  });
41
32
 
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
- });
33
+ it("upsert()", async () => {
34
+ const docs = await mdbc.read();
35
+ const { modifiedCount } = await mdbc.upsert({ _id: docs[0]._id, name: "david" });
36
+ assert.equal(modifiedCount, 1);
51
37
  });
52
38
 
53
- it("distinct()", function(done){
54
- mdbc.distinct("name")
55
- .then(function(names){
56
- assert.equal(names.length, 4);
57
- done();
58
- });
39
+ it("distinct()", async () => {
40
+ const names = await mdbc.distinct("name");
41
+ assert.equal(names.length, 4);
59
42
  });
60
43
 
61
- it("stats()", function(done){
62
- mdbc.stats()
63
- .then(function(r){
64
- assert(r.storageSize > 0);
65
- done();
66
- });
44
+ it("stats()", async () => {
45
+ const { storageSize } = await mdbc.stats();
46
+ assert(storageSize > 0);
67
47
  });
68
48
 
69
- it("count()", function(done){
70
- mdbc.count({name:"alice"})
71
- .then(function(n){
72
- assert.equal(n,1);
73
- done();
74
- });
49
+ it("count()", async () => {
50
+ const n = await mdbc.count({name:"alice"});
51
+ assert.equal(n,1);
75
52
  });
76
53
 
77
- it("remove()", function(done){
78
- mdbc.remove()
79
- .then(function(r){
80
- assert.equal(r.deletedCount, items.length);
81
- done();
82
- });
54
+ it("remove()", async () => {
55
+ const { deletedCount } = await mdbc.remove();
56
+ assert.equal(deletedCount, items.length);
83
57
  });
84
58
  });