@tomsd/mongodbclient 2.8.1 → 3.0.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/dist/cjs/mongodbclient.d.ts +14 -14
- package/dist/cjs/mongodbclient.js +7 -7
- package/dist/esm/mongodbclient.d.ts +14 -14
- package/dist/esm/mongodbclient.js +5 -5
- package/package.json +3 -4
- package/test/test.ts +10 -12
|
@@ -4,15 +4,15 @@
|
|
|
4
4
|
* License: MIT
|
|
5
5
|
*/
|
|
6
6
|
declare const MongoClient: any;
|
|
7
|
-
|
|
7
|
+
import { Db, Collection, CollStats, WithId, Document, InsertManyResult, UpdateResult, DeleteResult } from "mongodb";
|
|
8
8
|
export declare class MongoConnection {
|
|
9
9
|
private _client;
|
|
10
10
|
private _db;
|
|
11
11
|
private _collection;
|
|
12
|
-
constructor(client: typeof MongoClient, db:
|
|
12
|
+
constructor(client: typeof MongoClient, db: Db, collection: Collection);
|
|
13
13
|
get client(): typeof MongoClient;
|
|
14
|
-
get db():
|
|
15
|
-
get collection():
|
|
14
|
+
get db(): Db;
|
|
15
|
+
get collection(): Collection;
|
|
16
16
|
}
|
|
17
17
|
export declare class MClient {
|
|
18
18
|
private m_uri;
|
|
@@ -22,16 +22,16 @@ export declare class MClient {
|
|
|
22
22
|
get uri(): string;
|
|
23
23
|
get db(): string;
|
|
24
24
|
get collection(): string;
|
|
25
|
-
connect(): Promise<
|
|
26
|
-
protected getConnected(): Promise<
|
|
27
|
-
upsert(pobj: any): Promise<
|
|
28
|
-
read
|
|
29
|
-
distinct(key: string, condition?: any): Promise<any>;
|
|
30
|
-
remove(condition: any): Promise<
|
|
31
|
-
stats(): Promise<
|
|
25
|
+
connect(): Promise<MongoConnection>;
|
|
26
|
+
protected getConnected(): Promise<MongoConnection>;
|
|
27
|
+
upsert(pobj: any): Promise<UpdateResult>;
|
|
28
|
+
read(condition?: any, opt?: any): Promise<WithId<Document>[]>;
|
|
29
|
+
distinct(key: string, condition?: any): Promise<any[]>;
|
|
30
|
+
remove(condition: any): Promise<DeleteResult>;
|
|
31
|
+
stats(): Promise<CollStats>;
|
|
32
32
|
count(condition?: any): Promise<number>;
|
|
33
|
-
insertMany(items: any[]): Promise<
|
|
34
|
-
dbStats(): Promise<
|
|
35
|
-
getCollections(): Promise<
|
|
33
|
+
insertMany(items: any[]): Promise<InsertManyResult<Document>>;
|
|
34
|
+
dbStats(): Promise<Document>;
|
|
35
|
+
getCollections(): Promise<Collection[]>;
|
|
36
36
|
}
|
|
37
37
|
export default MClient;
|
|
@@ -16,8 +16,6 @@ exports.MClient = exports.MongoConnection = void 0;
|
|
|
16
16
|
* License: MIT
|
|
17
17
|
*/
|
|
18
18
|
const MongoClient = require("mongodb").MongoClient;
|
|
19
|
-
const Db = require("mongodb").Db;
|
|
20
|
-
const { Collection, UpdateWriteOpResult, deleteWriteOpResult, insertWriteOpResult } = require("mongodb");
|
|
21
19
|
const uuid_1 = require("uuid");
|
|
22
20
|
class MongoConnection {
|
|
23
21
|
constructor(client, db, collection) {
|
|
@@ -52,7 +50,9 @@ class MClient {
|
|
|
52
50
|
return this.m_collection;
|
|
53
51
|
}
|
|
54
52
|
connect() {
|
|
55
|
-
return this
|
|
53
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
return this.getConnected();
|
|
55
|
+
});
|
|
56
56
|
}
|
|
57
57
|
getConnected() {
|
|
58
58
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -68,7 +68,7 @@ class MClient {
|
|
|
68
68
|
}
|
|
69
69
|
upsert(pobj) {
|
|
70
70
|
return __awaiter(this, void 0, void 0, function* () {
|
|
71
|
-
const savingObj = Object.assign({ _id: uuid_1.v4() }, pobj);
|
|
71
|
+
const savingObj = Object.assign({ _id: (0, uuid_1.v4)() }, pobj);
|
|
72
72
|
const connection = yield this.getConnected();
|
|
73
73
|
try {
|
|
74
74
|
return yield connection.collection.updateOne({ _id: savingObj._id }, { $set: savingObj }, { upsert: true, writeConcern: { w: 1 } });
|
|
@@ -102,7 +102,7 @@ class MClient {
|
|
|
102
102
|
return __awaiter(this, void 0, void 0, function* () {
|
|
103
103
|
const connection = yield this.getConnected();
|
|
104
104
|
try {
|
|
105
|
-
return yield connection.collection.distinct(key, condition);
|
|
105
|
+
return (yield connection.collection.distinct(key, condition));
|
|
106
106
|
}
|
|
107
107
|
catch (e) {
|
|
108
108
|
throw e;
|
|
@@ -144,7 +144,7 @@ class MClient {
|
|
|
144
144
|
return __awaiter(this, void 0, void 0, function* () {
|
|
145
145
|
const connection = yield this.getConnected();
|
|
146
146
|
try {
|
|
147
|
-
return yield connection.collection.countDocuments(condition);
|
|
147
|
+
return (yield connection.collection.countDocuments(condition));
|
|
148
148
|
}
|
|
149
149
|
catch (e) {
|
|
150
150
|
throw e;
|
|
@@ -157,7 +157,7 @@ class MClient {
|
|
|
157
157
|
insertMany(items) {
|
|
158
158
|
return __awaiter(this, void 0, void 0, function* () {
|
|
159
159
|
const connection = yield this.getConnected();
|
|
160
|
-
const savingItems = items.map(item => (Object.assign({ _id: uuid_1.v4() }, item)));
|
|
160
|
+
const savingItems = items.map(item => (Object.assign({ _id: (0, uuid_1.v4)() }, item)));
|
|
161
161
|
try {
|
|
162
162
|
return yield connection.collection
|
|
163
163
|
.insertMany(savingItems, { writeConcern: { w: 1 } });
|
|
@@ -4,15 +4,15 @@
|
|
|
4
4
|
* License: MIT
|
|
5
5
|
*/
|
|
6
6
|
declare const MongoClient: any;
|
|
7
|
-
|
|
7
|
+
import { Db, Collection, CollStats, WithId, Document, InsertManyResult, UpdateResult, DeleteResult } from "mongodb";
|
|
8
8
|
export declare class MongoConnection {
|
|
9
9
|
private _client;
|
|
10
10
|
private _db;
|
|
11
11
|
private _collection;
|
|
12
|
-
constructor(client: typeof MongoClient, db:
|
|
12
|
+
constructor(client: typeof MongoClient, db: Db, collection: Collection);
|
|
13
13
|
get client(): typeof MongoClient;
|
|
14
|
-
get db():
|
|
15
|
-
get collection():
|
|
14
|
+
get db(): Db;
|
|
15
|
+
get collection(): Collection;
|
|
16
16
|
}
|
|
17
17
|
export declare class MClient {
|
|
18
18
|
private m_uri;
|
|
@@ -22,16 +22,16 @@ export declare class MClient {
|
|
|
22
22
|
get uri(): string;
|
|
23
23
|
get db(): string;
|
|
24
24
|
get collection(): string;
|
|
25
|
-
connect(): Promise<
|
|
26
|
-
protected getConnected(): Promise<
|
|
27
|
-
upsert(pobj: any): Promise<
|
|
28
|
-
read
|
|
29
|
-
distinct(key: string, condition?: any): Promise<any>;
|
|
30
|
-
remove(condition: any): Promise<
|
|
31
|
-
stats(): Promise<
|
|
25
|
+
connect(): Promise<MongoConnection>;
|
|
26
|
+
protected getConnected(): Promise<MongoConnection>;
|
|
27
|
+
upsert(pobj: any): Promise<UpdateResult>;
|
|
28
|
+
read(condition?: any, opt?: any): Promise<WithId<Document>[]>;
|
|
29
|
+
distinct(key: string, condition?: any): Promise<any[]>;
|
|
30
|
+
remove(condition: any): Promise<DeleteResult>;
|
|
31
|
+
stats(): Promise<CollStats>;
|
|
32
32
|
count(condition?: any): Promise<number>;
|
|
33
|
-
insertMany(items: any[]): Promise<
|
|
34
|
-
dbStats(): Promise<
|
|
35
|
-
getCollections(): Promise<
|
|
33
|
+
insertMany(items: any[]): Promise<InsertManyResult<Document>>;
|
|
34
|
+
dbStats(): Promise<Document>;
|
|
35
|
+
getCollections(): Promise<Collection[]>;
|
|
36
36
|
}
|
|
37
37
|
export default MClient;
|
|
@@ -13,8 +13,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
13
13
|
* License: MIT
|
|
14
14
|
*/
|
|
15
15
|
const MongoClient = require("mongodb").MongoClient;
|
|
16
|
-
const Db = require("mongodb").Db;
|
|
17
|
-
const { Collection, UpdateWriteOpResult, deleteWriteOpResult, insertWriteOpResult } = require("mongodb");
|
|
18
16
|
import { v4 as uuidv4 } from "uuid";
|
|
19
17
|
export class MongoConnection {
|
|
20
18
|
constructor(client, db, collection) {
|
|
@@ -48,7 +46,9 @@ export class MClient {
|
|
|
48
46
|
return this.m_collection;
|
|
49
47
|
}
|
|
50
48
|
connect() {
|
|
51
|
-
return this
|
|
49
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
return this.getConnected();
|
|
51
|
+
});
|
|
52
52
|
}
|
|
53
53
|
getConnected() {
|
|
54
54
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -98,7 +98,7 @@ export class MClient {
|
|
|
98
98
|
return __awaiter(this, void 0, void 0, function* () {
|
|
99
99
|
const connection = yield this.getConnected();
|
|
100
100
|
try {
|
|
101
|
-
return yield connection.collection.distinct(key, condition);
|
|
101
|
+
return (yield connection.collection.distinct(key, condition));
|
|
102
102
|
}
|
|
103
103
|
catch (e) {
|
|
104
104
|
throw e;
|
|
@@ -140,7 +140,7 @@ export class MClient {
|
|
|
140
140
|
return __awaiter(this, void 0, void 0, function* () {
|
|
141
141
|
const connection = yield this.getConnected();
|
|
142
142
|
try {
|
|
143
|
-
return yield connection.collection.countDocuments(condition);
|
|
143
|
+
return (yield connection.collection.countDocuments(condition));
|
|
144
144
|
}
|
|
145
145
|
catch (e) {
|
|
146
146
|
throw e;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tomsd/mongodbclient",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/cjs/mongodbclient.js",
|
|
6
6
|
"module": "dist/esm/mongodbclient.js",
|
|
@@ -16,17 +16,16 @@
|
|
|
16
16
|
"author": "tom",
|
|
17
17
|
"license": "MIT",
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"mongodb": "^
|
|
19
|
+
"mongodb": "^4.7.0",
|
|
20
20
|
"uuid": "^8.3.2"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@types/mocha": "^9.1.1",
|
|
24
|
-
"@types/mongodb": "^3.5.31",
|
|
25
24
|
"@types/node": "^14.14.5",
|
|
26
25
|
"@types/uuid": "^8.3.4",
|
|
27
26
|
"dotenv": "^16.0.1",
|
|
28
27
|
"mocha": "^7.0.1",
|
|
29
28
|
"ts-node": "^10.8.1",
|
|
30
|
-
"typescript": "^4.
|
|
29
|
+
"typescript": "^4.7.4"
|
|
31
30
|
}
|
|
32
31
|
}
|
package/test/test.ts
CHANGED
|
@@ -13,9 +13,7 @@ const collName = uuidv4();
|
|
|
13
13
|
|
|
14
14
|
const mdbc = new MClient(mongouri, dbName, collName);
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const items: Seed[] = [
|
|
16
|
+
const items = [
|
|
19
17
|
{name:"alice"},
|
|
20
18
|
{name:"bob"},
|
|
21
19
|
{name:"charlie"},
|
|
@@ -24,7 +22,7 @@ const items: Seed[] = [
|
|
|
24
22
|
|
|
25
23
|
describe("MClient", () => {
|
|
26
24
|
it("insertMany()", async () => {
|
|
27
|
-
const { insertedCount } =
|
|
25
|
+
const { insertedCount } = await mdbc.insertMany(items);
|
|
28
26
|
assert.equal(insertedCount, items.length);
|
|
29
27
|
});
|
|
30
28
|
|
|
@@ -34,30 +32,30 @@ describe("MClient", () => {
|
|
|
34
32
|
});
|
|
35
33
|
|
|
36
34
|
it("read()", async () => {
|
|
37
|
-
const docs = await mdbc.read
|
|
38
|
-
const getNames = (items:
|
|
35
|
+
const docs = await mdbc.read();
|
|
36
|
+
const getNames = (items: any[]) =>
|
|
39
37
|
Array.from(new Set(items.map(({ name }) => name))).sort().join("\n");
|
|
40
38
|
assert.equal(getNames(docs), getNames(docs));
|
|
41
39
|
});
|
|
42
40
|
|
|
43
41
|
it("upsert()", async () => {
|
|
44
|
-
const docs =
|
|
45
|
-
const { modifiedCount } =
|
|
42
|
+
const docs = await mdbc.read();
|
|
43
|
+
const { modifiedCount } = await mdbc.upsert({ _id: docs[0]._id, name: "david" });
|
|
46
44
|
assert.equal(modifiedCount, 1);
|
|
47
45
|
});
|
|
48
46
|
|
|
49
47
|
it("distinct()", async () => {
|
|
50
|
-
const names =
|
|
48
|
+
const names = await mdbc.distinct("name");
|
|
51
49
|
assert.equal(names.length, 4);
|
|
52
50
|
});
|
|
53
51
|
|
|
54
52
|
it("dbStats()", async () => {
|
|
55
|
-
const { storageSize } =
|
|
53
|
+
const { storageSize } = await mdbc.dbStats();
|
|
56
54
|
assert(storageSize > 0);
|
|
57
55
|
});
|
|
58
56
|
|
|
59
57
|
it("stats()", async () => {
|
|
60
|
-
const { storageSize } =
|
|
58
|
+
const { storageSize } = await mdbc.stats();
|
|
61
59
|
assert(storageSize > 0);
|
|
62
60
|
});
|
|
63
61
|
|
|
@@ -67,7 +65,7 @@ describe("MClient", () => {
|
|
|
67
65
|
});
|
|
68
66
|
|
|
69
67
|
it("remove()", async () => {
|
|
70
|
-
const { deletedCount } =
|
|
68
|
+
const { deletedCount } = await mdbc.remove({});
|
|
71
69
|
assert.equal(deletedCount, items.length);
|
|
72
70
|
});
|
|
73
71
|
});
|