@tomsd/mongodbclient 2.8.0 → 3.0.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 CHANGED
@@ -11,62 +11,51 @@ npm install @tomsd/mongodbclient
11
11
  import { MClient } from "@tomsd/mongodbclient";
12
12
 
13
13
  const uri = "mongodb+srv://...";
14
- const dbname = "mydb";
15
- const collectionname = "mycollection";
14
+ const dbName = "mydb";
15
+ const collectionName = "mycollection";
16
16
 
17
- const mdbc = new MClient(uri, dbname, collectionname);
17
+ const mdbc = new MClient(uri, dbName, collectionName);
18
18
 
19
19
  const items = [
20
- {name:"alice"},
21
- {name:"bob"},
22
- {name:"charlie"},
23
- {name:"alice"}
20
+ { name: "alice" },
21
+ { name: "bob" },
22
+ { name: "charlie" },
23
+ { name: "alice" }
24
24
  ];
25
25
 
26
- mdbc.insertMany(items)
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);
26
+ (async () => {
27
+ const { insertedCount } = await mdbc.insertMany(items);
28
+ console.log(insertedCount); // 4
29
+
30
+ const collections = await mdbc.getCollections();
31
+ console.log(
32
+ collections.some(
33
+ // @ts-ignore
34
+ collection => collection.s.namespace.db === dbName
35
+ )
36
+ ); // true
37
+
38
+ const docs = await mdbc.read();
39
+ console.log(docs);
40
+
41
+ const { upsertedCount, modifiedCount } = await mdbc.upsert({
42
+ ...docs[0],
43
+ name: "david"
70
44
  });
45
+ console.log(`upsertedCount: ${upsertedCount}, modifiedCount: ${modifiedCount}`);
46
+
47
+ const names = await mdbc.distinct("name");
48
+ console.log(`distinct names: ${names.length}`); // 4
49
+
50
+ const { storageSize } = await mdbc.stats();
51
+ console.log(`storageSize: ${storageSize}`);
52
+
53
+ const itemLength = await mdbc.count();
54
+ console.log(`count: ${itemLength}`); // 4
55
+
56
+ const { deletedCount } = await mdbc.remove({});
57
+ console.log(`deletedCount: ${deletedCount}`); // 4
58
+
59
+ })();
71
60
 
72
61
  ```
@@ -4,15 +4,15 @@
4
4
  * License: MIT
5
5
  */
6
6
  declare const MongoClient: any;
7
- declare const Db: any;
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: typeof Db, collection: any);
12
+ constructor(client: typeof MongoClient, db: Db, collection: Collection);
13
13
  get client(): typeof MongoClient;
14
- get db(): typeof Db;
15
- get collection(): any;
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<any>;
26
- protected getConnected(): Promise<any>;
27
- upsert(pobj: any): Promise<any>;
28
- read<T = any>(condition?: any, opt?: any): Promise<T[]>;
29
- distinct(key: string, condition?: any): Promise<any>;
30
- remove(condition: any): Promise<any>;
31
- stats(): Promise<any>;
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<any>;
34
- dbStats(): Promise<any>;
35
- getCollections(): Promise<any>;
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.getConnected();
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
- declare const Db: any;
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: typeof Db, collection: any);
12
+ constructor(client: typeof MongoClient, db: Db, collection: Collection);
13
13
  get client(): typeof MongoClient;
14
- get db(): typeof Db;
15
- get collection(): any;
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<any>;
26
- protected getConnected(): Promise<any>;
27
- upsert(pobj: any): Promise<any>;
28
- read<T = any>(condition?: any, opt?: any): Promise<T[]>;
29
- distinct(key: string, condition?: any): Promise<any>;
30
- remove(condition: any): Promise<any>;
31
- stats(): Promise<any>;
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<any>;
34
- dbStats(): Promise<any>;
35
- getCollections(): Promise<any>;
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.getConnected();
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": "2.8.0",
3
+ "version": "3.0.1",
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": "^3.6.6",
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.0.5"
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
- type Seed = { name: string; };
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 } = (await mdbc.insertMany(items)) as { insertedCount: number };
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<Seed & { _id: string; }>();
38
- const getNames = (items: Seed[]) =>
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 = (await mdbc.read()) as any[];
45
- const { modifiedCount } = (await mdbc.upsert({ _id: docs[0]._id, name: "david" })) as { modifiedCount: number };
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 = (await mdbc.distinct("name")) as string[];
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 } = (await mdbc.dbStats()) as { storageSize: number };
53
+ const { storageSize } = await mdbc.dbStats();
56
54
  assert(storageSize > 0);
57
55
  });
58
56
 
59
57
  it("stats()", async () => {
60
- const { storageSize } = (await mdbc.stats()) as { storageSize: number };
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 } = (await mdbc.remove({})) as { deletedCount: number };
68
+ const { deletedCount } = await mdbc.remove({});
71
69
  assert.equal(deletedCount, items.length);
72
70
  });
73
71
  });