@tomsd/mongodbclient 2.7.8 → 2.8.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 +16 -8
- package/dist/cjs/mongodbclient.d.ts +3 -3
- package/dist/cjs/mongodbclient.js +24 -31
- package/dist/esm/mongodbclient.d.ts +3 -3
- package/dist/esm/mongodbclient.js +24 -31
- package/package.json +1 -1
- package/test/test.ts +7 -3
package/README.md
CHANGED
|
@@ -16,11 +16,19 @@ const collectionname = "mycollection";
|
|
|
16
16
|
|
|
17
17
|
const mdbc = new MClient(uri, dbname, collectionname);
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
type Seed = {
|
|
20
|
+
name: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type Entry = Seed & {
|
|
24
|
+
_id: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const items: Seed[] = [
|
|
28
|
+
{ name: "alice" },
|
|
29
|
+
{ name: "bob" },
|
|
30
|
+
{ name: "charlie" },
|
|
31
|
+
{ name: "alice" }
|
|
24
32
|
];
|
|
25
33
|
|
|
26
34
|
mdbc.insertMany(items)
|
|
@@ -31,12 +39,12 @@ mdbc.insertMany(items)
|
|
|
31
39
|
.then((collections) => {
|
|
32
40
|
console.log(
|
|
33
41
|
collections.find(
|
|
34
|
-
collection => collection.s.namespace.db === dbname
|
|
42
|
+
(collection: any) => collection.s.namespace.db === dbname
|
|
35
43
|
)
|
|
36
44
|
);
|
|
37
|
-
return mdbc.read(
|
|
45
|
+
return mdbc.read<Entry>();
|
|
38
46
|
})
|
|
39
|
-
.then((docs
|
|
47
|
+
.then((docs) => {
|
|
40
48
|
console.log({
|
|
41
49
|
docs,
|
|
42
50
|
state: docs.length >= 4
|
|
@@ -25,12 +25,12 @@ export declare class MClient {
|
|
|
25
25
|
connect(): Promise<any>;
|
|
26
26
|
protected getConnected(): Promise<any>;
|
|
27
27
|
upsert(pobj: any): Promise<any>;
|
|
28
|
-
read(condition?: any, opt?: any): Promise<
|
|
28
|
+
read<T = any>(condition?: any, opt?: any): Promise<T[]>;
|
|
29
29
|
distinct(key: string, condition?: any): Promise<any>;
|
|
30
|
-
remove(condition: any): Promise<
|
|
30
|
+
remove(condition: any): Promise<any>;
|
|
31
31
|
stats(): Promise<any>;
|
|
32
32
|
count(condition?: any): Promise<number>;
|
|
33
|
-
insertMany(items: any[]): Promise<
|
|
33
|
+
insertMany(items: any[]): Promise<any>;
|
|
34
34
|
dbStats(): Promise<any>;
|
|
35
35
|
getCollections(): Promise<any>;
|
|
36
36
|
}
|
|
@@ -113,20 +113,17 @@ class MClient {
|
|
|
113
113
|
});
|
|
114
114
|
}
|
|
115
115
|
remove(condition) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
.
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
reject(e);
|
|
128
|
-
});
|
|
129
|
-
}, reject);
|
|
116
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
117
|
+
const connection = yield this.getConnected();
|
|
118
|
+
try {
|
|
119
|
+
return yield connection.collection.deleteMany(condition, { writeConcern: { w: 1 } });
|
|
120
|
+
}
|
|
121
|
+
catch (e) {
|
|
122
|
+
throw e;
|
|
123
|
+
}
|
|
124
|
+
finally {
|
|
125
|
+
connection.client.close();
|
|
126
|
+
}
|
|
130
127
|
});
|
|
131
128
|
}
|
|
132
129
|
stats() {
|
|
@@ -158,23 +155,19 @@ class MClient {
|
|
|
158
155
|
});
|
|
159
156
|
}
|
|
160
157
|
insertMany(items) {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
conn.client.close();
|
|
175
|
-
reject(e);
|
|
176
|
-
});
|
|
177
|
-
}, reject);
|
|
158
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
159
|
+
const connection = yield this.getConnected();
|
|
160
|
+
const savingItems = items.map(item => (Object.assign({ _id: uuid_1.v4() }, item)));
|
|
161
|
+
try {
|
|
162
|
+
return yield connection.collection
|
|
163
|
+
.insertMany(savingItems, { writeConcern: { w: 1 } });
|
|
164
|
+
}
|
|
165
|
+
catch (e) {
|
|
166
|
+
throw e;
|
|
167
|
+
}
|
|
168
|
+
finally {
|
|
169
|
+
connection.client.close();
|
|
170
|
+
}
|
|
178
171
|
});
|
|
179
172
|
}
|
|
180
173
|
dbStats() {
|
|
@@ -25,12 +25,12 @@ export declare class MClient {
|
|
|
25
25
|
connect(): Promise<any>;
|
|
26
26
|
protected getConnected(): Promise<any>;
|
|
27
27
|
upsert(pobj: any): Promise<any>;
|
|
28
|
-
read(condition?: any, opt?: any): Promise<
|
|
28
|
+
read<T = any>(condition?: any, opt?: any): Promise<T[]>;
|
|
29
29
|
distinct(key: string, condition?: any): Promise<any>;
|
|
30
|
-
remove(condition: any): Promise<
|
|
30
|
+
remove(condition: any): Promise<any>;
|
|
31
31
|
stats(): Promise<any>;
|
|
32
32
|
count(condition?: any): Promise<number>;
|
|
33
|
-
insertMany(items: any[]): Promise<
|
|
33
|
+
insertMany(items: any[]): Promise<any>;
|
|
34
34
|
dbStats(): Promise<any>;
|
|
35
35
|
getCollections(): Promise<any>;
|
|
36
36
|
}
|
|
@@ -109,20 +109,17 @@ export class MClient {
|
|
|
109
109
|
});
|
|
110
110
|
}
|
|
111
111
|
remove(condition) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
.
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
reject(e);
|
|
124
|
-
});
|
|
125
|
-
}, reject);
|
|
112
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
113
|
+
const connection = yield this.getConnected();
|
|
114
|
+
try {
|
|
115
|
+
return yield connection.collection.deleteMany(condition, { writeConcern: { w: 1 } });
|
|
116
|
+
}
|
|
117
|
+
catch (e) {
|
|
118
|
+
throw e;
|
|
119
|
+
}
|
|
120
|
+
finally {
|
|
121
|
+
connection.client.close();
|
|
122
|
+
}
|
|
126
123
|
});
|
|
127
124
|
}
|
|
128
125
|
stats() {
|
|
@@ -154,23 +151,19 @@ export class MClient {
|
|
|
154
151
|
});
|
|
155
152
|
}
|
|
156
153
|
insertMany(items) {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
conn.client.close();
|
|
171
|
-
reject(e);
|
|
172
|
-
});
|
|
173
|
-
}, reject);
|
|
154
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
155
|
+
const connection = yield this.getConnected();
|
|
156
|
+
const savingItems = items.map(item => (Object.assign({ _id: uuidv4() }, item)));
|
|
157
|
+
try {
|
|
158
|
+
return yield connection.collection
|
|
159
|
+
.insertMany(savingItems, { writeConcern: { w: 1 } });
|
|
160
|
+
}
|
|
161
|
+
catch (e) {
|
|
162
|
+
throw e;
|
|
163
|
+
}
|
|
164
|
+
finally {
|
|
165
|
+
connection.client.close();
|
|
166
|
+
}
|
|
174
167
|
});
|
|
175
168
|
}
|
|
176
169
|
dbStats() {
|
package/package.json
CHANGED
package/test/test.ts
CHANGED
|
@@ -13,7 +13,9 @@ const collName = uuidv4();
|
|
|
13
13
|
|
|
14
14
|
const mdbc = new MClient(mongouri, dbName, collName);
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
type Seed = { name: string; };
|
|
17
|
+
|
|
18
|
+
const items: Seed[] = [
|
|
17
19
|
{name:"alice"},
|
|
18
20
|
{name:"bob"},
|
|
19
21
|
{name:"charlie"},
|
|
@@ -32,8 +34,10 @@ describe("MClient", () => {
|
|
|
32
34
|
});
|
|
33
35
|
|
|
34
36
|
it("read()", async () => {
|
|
35
|
-
const docs =
|
|
36
|
-
|
|
37
|
+
const docs = await mdbc.read<Seed & { _id: string; }>();
|
|
38
|
+
const getNames = (items: Seed[]) =>
|
|
39
|
+
Array.from(new Set(items.map(({ name }) => name))).sort().join("\n");
|
|
40
|
+
assert.equal(getNames(docs), getNames(docs));
|
|
37
41
|
});
|
|
38
42
|
|
|
39
43
|
it("upsert()", async () => {
|