@tomsd/mongodbclient 3.0.10 → 3.0.12
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 +1 -1
- package/package.json +2 -2
- package/.env.default +0 -1
- package/.eslintrc.js +0 -19
- package/.github/workflows/ci.yml +0 -26
- package/.husky/pre-commit +0 -4
- package/test/mclient.test.ts +0 -239
- package/test/with_dburi.test.ts +0 -74
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ See [mongodbclient.netlify.app](https://mongodbclient.netlify.app/) for details.
|
|
|
7
7
|

|
|
8
8
|

|
|
9
9
|

|
|
10
|
-

|
|
11
11
|

|
|
12
12
|
|
|
13
13
|
## Installation
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tomsd/mongodbclient",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.12",
|
|
4
4
|
"description": "It's a handy mongodb client for easy-use.",
|
|
5
5
|
"main": "dist/cjs/mongodbclient.js",
|
|
6
6
|
"module": "dist/esm/mongodbclient.js",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"eslint-plugin-promise": "^6.0.1",
|
|
61
61
|
"husky": "^8.0.1",
|
|
62
62
|
"lint-staged": "^13.0.3",
|
|
63
|
-
"mocha": "^7.0
|
|
63
|
+
"mocha": "^7.2.0",
|
|
64
64
|
"prettier": "^2.7.1",
|
|
65
65
|
"sinon": "^14.0.0",
|
|
66
66
|
"ts-node": "^10.8.1",
|
package/.env.default
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
MONGODB_URI=mongodb+srv://...
|
package/.eslintrc.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
env: {
|
|
3
|
-
es2021: true,
|
|
4
|
-
node: true
|
|
5
|
-
},
|
|
6
|
-
extends: [
|
|
7
|
-
'standard-with-typescript',
|
|
8
|
-
'prettier'
|
|
9
|
-
],
|
|
10
|
-
overrides: [
|
|
11
|
-
],
|
|
12
|
-
parserOptions: {
|
|
13
|
-
ecmaVersion: 'latest',
|
|
14
|
-
sourceType: 'module',
|
|
15
|
-
project: './tsconfig.base.json'
|
|
16
|
-
},
|
|
17
|
-
rules: {
|
|
18
|
-
}
|
|
19
|
-
}
|
package/.github/workflows/ci.yml
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
name: CI
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
branches:
|
|
6
|
-
- main
|
|
7
|
-
pull_request:
|
|
8
|
-
|
|
9
|
-
jobs:
|
|
10
|
-
build:
|
|
11
|
-
|
|
12
|
-
runs-on: ubuntu-latest
|
|
13
|
-
|
|
14
|
-
steps:
|
|
15
|
-
- uses: actions/checkout@v2
|
|
16
|
-
|
|
17
|
-
- name: Use Node.js
|
|
18
|
-
uses: actions/setup-node@v1
|
|
19
|
-
with:
|
|
20
|
-
node-version: '16.x'
|
|
21
|
-
|
|
22
|
-
- name: npm ci and build and test
|
|
23
|
-
run: |
|
|
24
|
-
npm install
|
|
25
|
-
npm run build
|
|
26
|
-
npm test
|
package/.husky/pre-commit
DELETED
package/test/mclient.test.ts
DELETED
|
@@ -1,239 +0,0 @@
|
|
|
1
|
-
import { describe, it } from "mocha";
|
|
2
|
-
import { MClient } from "../src/mongodbclient";
|
|
3
|
-
import { strict as assert } from "assert";
|
|
4
|
-
import { mock } from "sinon";
|
|
5
|
-
|
|
6
|
-
const testConfig = {
|
|
7
|
-
uri: "mongodb+srv://...",
|
|
8
|
-
db: "db",
|
|
9
|
-
collection: "collection",
|
|
10
|
-
};
|
|
11
|
-
let mdbc: MClient;
|
|
12
|
-
|
|
13
|
-
describe("MClient", () => {
|
|
14
|
-
before(() => {
|
|
15
|
-
mdbc = new MClient(testConfig.uri, testConfig.db, testConfig.collection);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it("property uri", () => {
|
|
19
|
-
assert.equal(mdbc.uri, testConfig.uri);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it("properti db", () => {
|
|
23
|
-
assert.equal(mdbc.db, testConfig.db);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it("property collection", () => {
|
|
27
|
-
assert.equal(mdbc.collection, testConfig.collection);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it("upsert()", async () => {
|
|
31
|
-
const connection = {
|
|
32
|
-
collection: {
|
|
33
|
-
updateOne: async (obj: any) =>
|
|
34
|
-
await Promise.resolve({
|
|
35
|
-
upsertedCount: 1,
|
|
36
|
-
modifiedCount: 0,
|
|
37
|
-
}),
|
|
38
|
-
},
|
|
39
|
-
client: {
|
|
40
|
-
close: () => undefined,
|
|
41
|
-
},
|
|
42
|
-
};
|
|
43
|
-
const mocked = mock(mdbc);
|
|
44
|
-
mocked
|
|
45
|
-
.expects("getConnected")
|
|
46
|
-
.once()
|
|
47
|
-
.withArgs()
|
|
48
|
-
.returns(Promise.resolve(connection));
|
|
49
|
-
|
|
50
|
-
assert.equal(
|
|
51
|
-
await mdbc
|
|
52
|
-
.upsert({ name: "test" })
|
|
53
|
-
.then(({ upsertedCount }) => upsertedCount),
|
|
54
|
-
1
|
|
55
|
-
);
|
|
56
|
-
|
|
57
|
-
mocked.verify();
|
|
58
|
-
mocked.restore();
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it("read()", async () => {
|
|
62
|
-
const returningDocuments = [{ name: "test" }];
|
|
63
|
-
const connection = {
|
|
64
|
-
collection: {
|
|
65
|
-
find: (condition: any) => ({
|
|
66
|
-
toArray: async () => await Promise.resolve(returningDocuments),
|
|
67
|
-
}),
|
|
68
|
-
},
|
|
69
|
-
client: {
|
|
70
|
-
close: () => undefined,
|
|
71
|
-
},
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
const mocked = mock(mdbc);
|
|
75
|
-
mocked
|
|
76
|
-
.expects("getConnected")
|
|
77
|
-
.once()
|
|
78
|
-
.withArgs()
|
|
79
|
-
.returns(Promise.resolve(connection));
|
|
80
|
-
|
|
81
|
-
assert.equal(
|
|
82
|
-
JSON.stringify(await mdbc.read({ name: "test" })),
|
|
83
|
-
JSON.stringify(returningDocuments)
|
|
84
|
-
);
|
|
85
|
-
|
|
86
|
-
mocked.verify();
|
|
87
|
-
mocked.restore();
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it("distinct()", async () => {
|
|
91
|
-
const returningValues = ["test1", "test2"];
|
|
92
|
-
const connection = {
|
|
93
|
-
collection: {
|
|
94
|
-
distinct: async (key: string) => await Promise.resolve(returningValues),
|
|
95
|
-
},
|
|
96
|
-
client: {
|
|
97
|
-
close: () => undefined,
|
|
98
|
-
},
|
|
99
|
-
};
|
|
100
|
-
const mocked = mock(mdbc);
|
|
101
|
-
mocked
|
|
102
|
-
.expects("getConnected")
|
|
103
|
-
.once()
|
|
104
|
-
.withArgs()
|
|
105
|
-
.returns(Promise.resolve(connection));
|
|
106
|
-
|
|
107
|
-
assert.equal(
|
|
108
|
-
JSON.stringify(await mdbc.distinct("name")),
|
|
109
|
-
JSON.stringify(returningValues)
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
mocked.verify();
|
|
113
|
-
mocked.restore();
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
it("remove()", async () => {
|
|
117
|
-
const returningValue = { deletedCount: 1 };
|
|
118
|
-
const connection = {
|
|
119
|
-
collection: {
|
|
120
|
-
deleteMany: async (condition: any) =>
|
|
121
|
-
await Promise.resolve(returningValue),
|
|
122
|
-
},
|
|
123
|
-
client: {
|
|
124
|
-
close: () => undefined,
|
|
125
|
-
},
|
|
126
|
-
};
|
|
127
|
-
const mocked = mock(mdbc);
|
|
128
|
-
mocked
|
|
129
|
-
.expects("getConnected")
|
|
130
|
-
.once()
|
|
131
|
-
.withArgs()
|
|
132
|
-
.returns(Promise.resolve(connection));
|
|
133
|
-
|
|
134
|
-
assert.equal(
|
|
135
|
-
JSON.stringify(await mdbc.remove({ name: "test" })),
|
|
136
|
-
JSON.stringify(returningValue)
|
|
137
|
-
);
|
|
138
|
-
|
|
139
|
-
mocked.verify();
|
|
140
|
-
mocked.restore();
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
it("stats()", async () => {
|
|
144
|
-
const returningValue = { storageSize: 100 };
|
|
145
|
-
const connection = {
|
|
146
|
-
collection: {
|
|
147
|
-
stats: async () => await Promise.resolve(returningValue),
|
|
148
|
-
},
|
|
149
|
-
client: {
|
|
150
|
-
close: () => undefined,
|
|
151
|
-
},
|
|
152
|
-
};
|
|
153
|
-
const mocked = mock(mdbc);
|
|
154
|
-
mocked.expects("getConnected").once().withArgs().returns(connection);
|
|
155
|
-
|
|
156
|
-
assert.equal(
|
|
157
|
-
await mdbc.stats().then(({ storageSize }) => storageSize),
|
|
158
|
-
100
|
|
159
|
-
);
|
|
160
|
-
|
|
161
|
-
mocked.verify();
|
|
162
|
-
mocked.restore();
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
it("count()", async () => {
|
|
166
|
-
const resultValue = 100;
|
|
167
|
-
const connection = {
|
|
168
|
-
collection: {
|
|
169
|
-
countDocuments: async (condition: any) =>
|
|
170
|
-
await Promise.resolve(resultValue),
|
|
171
|
-
},
|
|
172
|
-
client: {
|
|
173
|
-
close: () => undefined,
|
|
174
|
-
},
|
|
175
|
-
};
|
|
176
|
-
const mocked = mock(mdbc);
|
|
177
|
-
mocked.expects("getConnected").once().withArgs().returns(connection);
|
|
178
|
-
|
|
179
|
-
assert.equal(await mdbc.count({ name: "test" }), resultValue);
|
|
180
|
-
|
|
181
|
-
mocked.verify();
|
|
182
|
-
mocked.restore();
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
it("insertMany()", async () => {
|
|
186
|
-
const insertingItems = [
|
|
187
|
-
{ name: "test1" },
|
|
188
|
-
{ name: "test2" },
|
|
189
|
-
{ name: "test3" },
|
|
190
|
-
];
|
|
191
|
-
const connection = {
|
|
192
|
-
collection: {
|
|
193
|
-
insertMany: async (savingItems: any[]) =>
|
|
194
|
-
await Promise.resolve({
|
|
195
|
-
insertedCount: savingItems.length,
|
|
196
|
-
}),
|
|
197
|
-
},
|
|
198
|
-
client: {
|
|
199
|
-
close: () => undefined,
|
|
200
|
-
},
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
const mocked = mock(mdbc);
|
|
204
|
-
mocked.expects("getConnected").once().withArgs().returns(connection);
|
|
205
|
-
|
|
206
|
-
assert.equal(
|
|
207
|
-
await mdbc
|
|
208
|
-
.insertMany(insertingItems)
|
|
209
|
-
.then(({ insertedCount }) => insertedCount),
|
|
210
|
-
insertingItems.length
|
|
211
|
-
);
|
|
212
|
-
|
|
213
|
-
mocked.verify();
|
|
214
|
-
mocked.restore();
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
it("dbStats()", async () => {
|
|
218
|
-
const resultValue = { storageSize: 100 };
|
|
219
|
-
const connection = {
|
|
220
|
-
db: {
|
|
221
|
-
stats: async () => await Promise.resolve(resultValue),
|
|
222
|
-
},
|
|
223
|
-
client: {
|
|
224
|
-
close: () => undefined,
|
|
225
|
-
},
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
const mocked = mock(mdbc);
|
|
229
|
-
mocked.expects("getConnected").once().withArgs().returns(connection);
|
|
230
|
-
|
|
231
|
-
assert.equal(
|
|
232
|
-
await mdbc.dbStats().then(({ storageSize }) => storageSize),
|
|
233
|
-
100
|
|
234
|
-
);
|
|
235
|
-
|
|
236
|
-
mocked.verify();
|
|
237
|
-
mocked.restore();
|
|
238
|
-
});
|
|
239
|
-
});
|
package/test/with_dburi.test.ts
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
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
|
-
import dotenv from "dotenv";
|
|
6
|
-
dotenv.config();
|
|
7
|
-
|
|
8
|
-
const mongouri = process.env.MONGODB_URI as string;
|
|
9
|
-
const dbName = uuidv4();
|
|
10
|
-
const collName = uuidv4();
|
|
11
|
-
|
|
12
|
-
const mdbc = new MClient(mongouri, dbName, collName);
|
|
13
|
-
|
|
14
|
-
const items = [
|
|
15
|
-
{ name: "alice" },
|
|
16
|
-
{ name: "bob" },
|
|
17
|
-
{ name: "charlie" },
|
|
18
|
-
{ name: "alice" },
|
|
19
|
-
];
|
|
20
|
-
|
|
21
|
-
describe("MClient", () => {
|
|
22
|
-
it("insertMany()", async () => {
|
|
23
|
-
const { insertedCount } = await mdbc.insertMany(items);
|
|
24
|
-
assert.equal(insertedCount, items.length);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it("getCollections()", async () => {
|
|
28
|
-
const collections = await mdbc.getCollections();
|
|
29
|
-
assert(collections.length > 0);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it("read()", async () => {
|
|
33
|
-
const docs = await mdbc.read();
|
|
34
|
-
const getNames = (items: any[]): string =>
|
|
35
|
-
Array.from(new Set(items.map(({ name }) => name)))
|
|
36
|
-
.sort((a, b) => (a > b ? 1 : -1))
|
|
37
|
-
.join("\n");
|
|
38
|
-
assert.equal(getNames(docs), getNames(docs));
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it("upsert()", async () => {
|
|
42
|
-
const docs = await mdbc.read();
|
|
43
|
-
const { modifiedCount } = await mdbc.upsert({
|
|
44
|
-
_id: docs[0]._id,
|
|
45
|
-
name: "david",
|
|
46
|
-
});
|
|
47
|
-
assert.equal(modifiedCount, 1);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it("distinct()", async () => {
|
|
51
|
-
const names = await mdbc.distinct("name");
|
|
52
|
-
assert.equal(names.length, 4);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it("dbStats()", async () => {
|
|
56
|
-
const { storageSize } = await mdbc.dbStats();
|
|
57
|
-
assert(storageSize > 0);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it("stats()", async () => {
|
|
61
|
-
const { storageSize } = await mdbc.stats();
|
|
62
|
-
assert(storageSize > 0);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it("count()", async () => {
|
|
66
|
-
const n = await mdbc.count({ name: "alice" });
|
|
67
|
-
assert.equal(n, 1);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it("remove()", async () => {
|
|
71
|
-
const { deletedCount } = await mdbc.remove({});
|
|
72
|
-
assert.equal(deletedCount, items.length);
|
|
73
|
-
});
|
|
74
|
-
});
|