@tomsd/mongodbclient 2.1.0 → 2.2.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.
- package/README.md +47 -38
- package/package.json +7 -3
- package/test/test.ts +60 -0
- package/test/test.js +0 -84
package/README.md
CHANGED
|
@@ -7,13 +7,13 @@ npm install @tomsd/mongodbclient
|
|
|
7
7
|
|
|
8
8
|
# Usage
|
|
9
9
|
|
|
10
|
-
```
|
|
10
|
+
``` typescript
|
|
11
|
+
import { MClient } from "@tomsd/mongodbclient";
|
|
12
|
+
|
|
11
13
|
const uri = "mongodb+srv://...";
|
|
12
14
|
const dbname = "mydb";
|
|
13
15
|
const collectionname = "mycollection";
|
|
14
16
|
|
|
15
|
-
const MClient = require("@tomsd/mongodbclient").MClient;
|
|
16
|
-
|
|
17
17
|
const mdbc = new MClient(uri, dbname, collectionname);
|
|
18
18
|
|
|
19
19
|
const items = [
|
|
@@ -24,40 +24,49 @@ const items = [
|
|
|
24
24
|
];
|
|
25
25
|
|
|
26
26
|
mdbc.insertMany(items)
|
|
27
|
-
.then((r) => {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
})
|
|
31
|
-
.then((collections) => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
})
|
|
39
|
-
.then((
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
.
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
.
|
|
59
|
-
|
|
60
|
-
})
|
|
61
|
-
|
|
27
|
+
.then((r: any) => {
|
|
28
|
+
console.log(r.insertedCount); // 4
|
|
29
|
+
return mdbc.getCollections();
|
|
30
|
+
})
|
|
31
|
+
.then((collections) => {
|
|
32
|
+
console.log(
|
|
33
|
+
collections.find(
|
|
34
|
+
collection => collection.s.namespace.db === dbname
|
|
35
|
+
)
|
|
36
|
+
);
|
|
37
|
+
return mdbc.read({});
|
|
38
|
+
})
|
|
39
|
+
.then((docs: any) => {
|
|
40
|
+
console.log({
|
|
41
|
+
docs,
|
|
42
|
+
state: docs.length >= 4
|
|
43
|
+
});
|
|
44
|
+
return mdbc.upsert({
|
|
45
|
+
_id:docs[0]._id,
|
|
46
|
+
name:"david"
|
|
47
|
+
});
|
|
48
|
+
})
|
|
49
|
+
.then((r: any) => {
|
|
50
|
+
console.log(r.upsertedCount + r.modifiedCount);
|
|
51
|
+
return mdbc.distinct("name", {});
|
|
52
|
+
})
|
|
53
|
+
.then((names) => {
|
|
54
|
+
console.log(names);
|
|
55
|
+
return mdbc.stats();
|
|
56
|
+
})
|
|
57
|
+
.then((r: any) => {
|
|
58
|
+
console.log(r.storageSize);
|
|
59
|
+
return mdbc.count({});
|
|
60
|
+
})
|
|
61
|
+
.then((n) => {
|
|
62
|
+
console.log(n);
|
|
63
|
+
return mdbc.remove({});
|
|
64
|
+
})
|
|
65
|
+
.then((r: any) => {
|
|
66
|
+
console.log(r.deletedCount);
|
|
67
|
+
})
|
|
68
|
+
.catch((e) => {
|
|
69
|
+
console.error(e);
|
|
70
|
+
});
|
|
62
71
|
|
|
63
72
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tomsd/mongodbclient",
|
|
3
|
-
"version": "2.1
|
|
3
|
+
"version": "2.2.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/cjs/mongodbclient.js",
|
|
6
6
|
"module": "dist/esm/mongodbclient.js",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"scripts": {
|
|
9
9
|
"build": "tsc --project tsconfig.cjs.json && tsc --project tsconfig.esm.json",
|
|
10
10
|
"test": "exit 0",
|
|
11
|
-
"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"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [
|
|
14
14
|
"mongodb"
|
|
@@ -20,9 +20,13 @@
|
|
|
20
20
|
"mongodb": "^3.6.6"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
+
"@types/mocha": "^9.1.1",
|
|
23
24
|
"@types/mongodb": "^3.5.31",
|
|
24
25
|
"@types/node": "^14.14.5",
|
|
26
|
+
"@types/uuid": "^8.3.4",
|
|
25
27
|
"mocha": "^7.0.1",
|
|
26
|
-
"
|
|
28
|
+
"ts-node": "^10.8.1",
|
|
29
|
+
"typescript": "^4.0.5",
|
|
30
|
+
"uuid": "^8.3.2"
|
|
27
31
|
}
|
|
28
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/cjs/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
|
-
});
|