@tomsd/mongodbclient 3.0.6 → 3.0.8

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/.eslintrc.js ADDED
@@ -0,0 +1,19 @@
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
+ }
@@ -0,0 +1,26 @@
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
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env sh
2
+ . "$(dirname -- "$0")/_/husky.sh"
3
+
4
+ npx lint-staged
@@ -10,13 +10,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.MClient = exports.MongoConnection = void 0;
13
- /*!
14
- * @license mongodbclient
15
- * (c) 2020 tom
16
- * License: MIT
17
- */
18
- const MongoClient = require("mongodb").MongoClient;
19
13
  const uuid_1 = require("uuid");
14
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
15
+ const MongoClient = require("mongodb").MongoClient;
20
16
  class MongoConnection {
21
17
  constructor(client, db, collection) {
22
18
  this._client = client;
@@ -51,15 +47,13 @@ class MClient {
51
47
  }
52
48
  connect() {
53
49
  return __awaiter(this, void 0, void 0, function* () {
54
- return this.getConnected();
50
+ return yield this.getConnected();
55
51
  });
56
52
  }
57
53
  getConnected() {
58
54
  return __awaiter(this, void 0, void 0, function* () {
59
55
  const client = new MongoClient(this.m_uri, { useUnifiedTopology: true });
60
- return client
61
- .connect()
62
- .then((client) => {
56
+ return client.connect().then((client) => {
63
57
  const db = client.db(this.m_db);
64
58
  const collection = db.collection(this.m_collection);
65
59
  return new MongoConnection(client, db, collection);
@@ -73,9 +67,6 @@ class MClient {
73
67
  try {
74
68
  return yield connection.collection.updateOne({ _id: savingObj._id }, { $set: savingObj }, { upsert: true, writeConcern: { w: 1 } });
75
69
  }
76
- catch (e) {
77
- throw e;
78
- }
79
70
  finally {
80
71
  connection.client.close();
81
72
  }
@@ -85,13 +76,7 @@ class MClient {
85
76
  return __awaiter(this, void 0, void 0, function* () {
86
77
  const connection = yield this.getConnected();
87
78
  try {
88
- return yield connection
89
- .collection
90
- .find(condition, opt)
91
- .toArray();
92
- }
93
- catch (e) {
94
- throw e;
79
+ return yield connection.collection.find(condition, opt).toArray();
95
80
  }
96
81
  finally {
97
82
  connection.client.close();
@@ -104,9 +89,6 @@ class MClient {
104
89
  try {
105
90
  return (yield connection.collection.distinct(key, condition));
106
91
  }
107
- catch (e) {
108
- throw e;
109
- }
110
92
  finally {
111
93
  connection.client.close();
112
94
  }
@@ -116,10 +98,9 @@ class MClient {
116
98
  return __awaiter(this, void 0, void 0, function* () {
117
99
  const connection = yield this.getConnected();
118
100
  try {
119
- return yield connection.collection.deleteMany(condition, { writeConcern: { w: 1 } });
120
- }
121
- catch (e) {
122
- throw e;
101
+ return yield connection.collection.deleteMany(condition, {
102
+ writeConcern: { w: 1 },
103
+ });
123
104
  }
124
105
  finally {
125
106
  connection.client.close();
@@ -132,9 +113,6 @@ class MClient {
132
113
  try {
133
114
  return yield connection.collection.stats();
134
115
  }
135
- catch (e) {
136
- throw e;
137
- }
138
116
  finally {
139
117
  connection.client.close();
140
118
  }
@@ -146,9 +124,6 @@ class MClient {
146
124
  try {
147
125
  return (yield connection.collection.countDocuments(condition));
148
126
  }
149
- catch (e) {
150
- throw e;
151
- }
152
127
  finally {
153
128
  connection.client.close();
154
129
  }
@@ -157,13 +132,11 @@ class MClient {
157
132
  insertMany(items) {
158
133
  return __awaiter(this, void 0, void 0, function* () {
159
134
  const connection = yield this.getConnected();
160
- const savingItems = items.map(item => (Object.assign({ _id: (0, uuid_1.v4)() }, item)));
135
+ const savingItems = items.map((item) => (Object.assign({ _id: (0, uuid_1.v4)() }, item)));
161
136
  try {
162
- return yield connection.collection
163
- .insertMany(savingItems, { writeConcern: { w: 1 } });
164
- }
165
- catch (e) {
166
- throw e;
137
+ return yield connection.collection.insertMany(savingItems, {
138
+ writeConcern: { w: 1 },
139
+ });
167
140
  }
168
141
  finally {
169
142
  connection.client.close();
@@ -176,9 +149,6 @@ class MClient {
176
149
  try {
177
150
  return yield connection.db.stats();
178
151
  }
179
- catch (e) {
180
- throw e;
181
- }
182
152
  finally {
183
153
  connection.client.close();
184
154
  }
@@ -190,9 +160,6 @@ class MClient {
190
160
  try {
191
161
  return yield connection.db.collections();
192
162
  }
193
- catch (e) {
194
- throw e;
195
- }
196
163
  finally {
197
164
  connection.client.close();
198
165
  }
@@ -2,22 +2,22 @@
2
2
  * @license mongodbclient
3
3
  * (c) 2020 tom
4
4
  * License: MIT
5
- */
5
+ */
6
+ import { Db, Collection, CollStats, DeleteResult, Document, InsertManyResult, UpdateResult, WithId } from "mongodb";
6
7
  declare const MongoClient: any;
7
- import { Db, Collection, CollStats, WithId, Document, InsertManyResult, UpdateResult, DeleteResult } from "mongodb";
8
8
  export declare class MongoConnection {
9
- private _client;
10
- private _db;
11
- private _collection;
9
+ private readonly _client;
10
+ private readonly _db;
11
+ private readonly _collection;
12
12
  constructor(client: typeof MongoClient, db: Db, collection: Collection);
13
13
  get client(): typeof MongoClient;
14
14
  get db(): Db;
15
15
  get collection(): Collection;
16
16
  }
17
17
  export declare class MClient {
18
- private m_uri;
19
- private m_db;
20
- private m_collection;
18
+ private readonly m_uri;
19
+ private readonly m_db;
20
+ private readonly m_collection;
21
21
  constructor(uri: string, db: string, collection: string);
22
22
  get uri(): string;
23
23
  get db(): string;
@@ -25,7 +25,7 @@ export declare class MClient {
25
25
  connect(): Promise<MongoConnection>;
26
26
  protected getConnected(): Promise<MongoConnection>;
27
27
  upsert(pobj: any): Promise<UpdateResult>;
28
- read(condition?: any, opt?: any): Promise<WithId<Document>[]>;
28
+ read(condition?: any, opt?: any): Promise<Array<WithId<Document>>>;
29
29
  distinct(key: string, condition?: any): Promise<any[]>;
30
30
  remove(condition: any): Promise<DeleteResult>;
31
31
  stats(): Promise<CollStats>;
@@ -7,13 +7,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- /*!
11
- * @license mongodbclient
12
- * (c) 2020 tom
13
- * License: MIT
14
- */
15
- const MongoClient = require("mongodb").MongoClient;
16
10
  import { v4 as uuidv4 } from "uuid";
11
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
12
+ const MongoClient = require("mongodb").MongoClient;
17
13
  export class MongoConnection {
18
14
  constructor(client, db, collection) {
19
15
  this._client = client;
@@ -47,15 +43,13 @@ export class MClient {
47
43
  }
48
44
  connect() {
49
45
  return __awaiter(this, void 0, void 0, function* () {
50
- return this.getConnected();
46
+ return yield this.getConnected();
51
47
  });
52
48
  }
53
49
  getConnected() {
54
50
  return __awaiter(this, void 0, void 0, function* () {
55
51
  const client = new MongoClient(this.m_uri, { useUnifiedTopology: true });
56
- return client
57
- .connect()
58
- .then((client) => {
52
+ return client.connect().then((client) => {
59
53
  const db = client.db(this.m_db);
60
54
  const collection = db.collection(this.m_collection);
61
55
  return new MongoConnection(client, db, collection);
@@ -69,9 +63,6 @@ export class MClient {
69
63
  try {
70
64
  return yield connection.collection.updateOne({ _id: savingObj._id }, { $set: savingObj }, { upsert: true, writeConcern: { w: 1 } });
71
65
  }
72
- catch (e) {
73
- throw e;
74
- }
75
66
  finally {
76
67
  connection.client.close();
77
68
  }
@@ -81,13 +72,7 @@ export class MClient {
81
72
  return __awaiter(this, void 0, void 0, function* () {
82
73
  const connection = yield this.getConnected();
83
74
  try {
84
- return yield connection
85
- .collection
86
- .find(condition, opt)
87
- .toArray();
88
- }
89
- catch (e) {
90
- throw e;
75
+ return yield connection.collection.find(condition, opt).toArray();
91
76
  }
92
77
  finally {
93
78
  connection.client.close();
@@ -100,9 +85,6 @@ export class MClient {
100
85
  try {
101
86
  return (yield connection.collection.distinct(key, condition));
102
87
  }
103
- catch (e) {
104
- throw e;
105
- }
106
88
  finally {
107
89
  connection.client.close();
108
90
  }
@@ -112,10 +94,9 @@ export class MClient {
112
94
  return __awaiter(this, void 0, void 0, function* () {
113
95
  const connection = yield this.getConnected();
114
96
  try {
115
- return yield connection.collection.deleteMany(condition, { writeConcern: { w: 1 } });
116
- }
117
- catch (e) {
118
- throw e;
97
+ return yield connection.collection.deleteMany(condition, {
98
+ writeConcern: { w: 1 },
99
+ });
119
100
  }
120
101
  finally {
121
102
  connection.client.close();
@@ -128,9 +109,6 @@ export class MClient {
128
109
  try {
129
110
  return yield connection.collection.stats();
130
111
  }
131
- catch (e) {
132
- throw e;
133
- }
134
112
  finally {
135
113
  connection.client.close();
136
114
  }
@@ -142,9 +120,6 @@ export class MClient {
142
120
  try {
143
121
  return (yield connection.collection.countDocuments(condition));
144
122
  }
145
- catch (e) {
146
- throw e;
147
- }
148
123
  finally {
149
124
  connection.client.close();
150
125
  }
@@ -153,13 +128,11 @@ export class MClient {
153
128
  insertMany(items) {
154
129
  return __awaiter(this, void 0, void 0, function* () {
155
130
  const connection = yield this.getConnected();
156
- const savingItems = items.map(item => (Object.assign({ _id: uuidv4() }, item)));
131
+ const savingItems = items.map((item) => (Object.assign({ _id: uuidv4() }, item)));
157
132
  try {
158
- return yield connection.collection
159
- .insertMany(savingItems, { writeConcern: { w: 1 } });
160
- }
161
- catch (e) {
162
- throw e;
133
+ return yield connection.collection.insertMany(savingItems, {
134
+ writeConcern: { w: 1 },
135
+ });
163
136
  }
164
137
  finally {
165
138
  connection.client.close();
@@ -172,9 +145,6 @@ export class MClient {
172
145
  try {
173
146
  return yield connection.db.stats();
174
147
  }
175
- catch (e) {
176
- throw e;
177
- }
178
148
  finally {
179
149
  connection.client.close();
180
150
  }
@@ -186,9 +156,6 @@ export class MClient {
186
156
  try {
187
157
  return yield connection.db.collections();
188
158
  }
189
- catch (e) {
190
- throw e;
191
- }
192
159
  finally {
193
160
  connection.client.close();
194
161
  }
package/package.json CHANGED
@@ -1,20 +1,40 @@
1
1
  {
2
2
  "name": "@tomsd/mongodbclient",
3
- "version": "3.0.6",
3
+ "version": "3.0.8",
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",
7
7
  "types": "dist/esm/mongodbclient.d.ts",
8
8
  "scripts": {
9
9
  "build": "tsc --project tsconfig.cjs.json && tsc --project tsconfig.esm.json",
10
+ "format": "npm run format:src && npm run format:test",
11
+ "format:src": "prettier --write src/**/*.ts",
12
+ "format:test": "prettier --write test/**/*.ts",
13
+ "lint:src": "eslint src/**/*.ts",
14
+ "lint:test": "eslint test/**/*.ts",
15
+ "prepare": "husky install",
10
16
  "test": "exit 0",
11
17
  "test_with_dburi": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' mocha -r ts-node/register \"test/test.ts\" --timeout 30000"
12
18
  },
19
+ "lint-staged": {
20
+ "src/**/*.ts": [
21
+ "npm run lint:src",
22
+ "npm run format:src"
23
+ ],
24
+ "test/**/*.ts": [
25
+ "npm run lint:test",
26
+ "npm run format:test"
27
+ ]
28
+ },
13
29
  "keywords": [
14
30
  "mongodb"
15
31
  ],
16
32
  "author": "tom",
17
33
  "license": "MIT",
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "https://github.com/tomsdoo/mongodbclient"
37
+ },
18
38
  "dependencies": {
19
39
  "mongodb": "^4.7.0",
20
40
  "uuid": "^8.3.2"
@@ -23,9 +43,19 @@
23
43
  "@types/mocha": "^9.1.1",
24
44
  "@types/node": "^14.14.5",
25
45
  "@types/uuid": "^8.3.4",
46
+ "@typescript-eslint/eslint-plugin": "^5.38.0",
26
47
  "dotenv": "^16.0.1",
48
+ "eslint": "^8.24.0",
49
+ "eslint-config-prettier": "^8.5.0",
50
+ "eslint-config-standard-with-typescript": "^23.0.0",
51
+ "eslint-plugin-import": "^2.26.0",
52
+ "eslint-plugin-n": "^15.3.0",
53
+ "eslint-plugin-promise": "^6.0.1",
54
+ "husky": "^8.0.1",
55
+ "lint-staged": "^13.0.3",
27
56
  "mocha": "^7.0.1",
57
+ "prettier": "^2.7.1",
28
58
  "ts-node": "^10.8.1",
29
- "typescript": "^4.7.4"
59
+ "typescript": "^4.8.3"
30
60
  }
31
61
  }
package/test/test.ts CHANGED
@@ -1,11 +1,9 @@
1
- // @ts-ignore
2
- import dotenv from "dotenv";
3
- dotenv.config();
4
-
5
- import {describe, it } from "mocha";
1
+ import { describe, it } from "mocha";
6
2
  import { MClient } from "../src/mongodbclient";
7
3
  import { strict as assert } from "assert";
8
4
  import { v4 as uuidv4 } from "uuid";
5
+ import dotenv from "dotenv";
6
+ dotenv.config();
9
7
 
10
8
  const mongouri = process.env.MONGODB_URI as string;
11
9
  const dbName = uuidv4();
@@ -14,10 +12,10 @@ const collName = uuidv4();
14
12
  const mdbc = new MClient(mongouri, dbName, collName);
15
13
 
16
14
  const items = [
17
- {name:"alice"},
18
- {name:"bob"},
19
- {name:"charlie"},
20
- {name:"alice"}
15
+ { name: "alice" },
16
+ { name: "bob" },
17
+ { name: "charlie" },
18
+ { name: "alice" },
21
19
  ];
22
20
 
23
21
  describe("MClient", () => {
@@ -33,14 +31,19 @@ describe("MClient", () => {
33
31
 
34
32
  it("read()", async () => {
35
33
  const docs = await mdbc.read();
36
- const getNames = (items: any[]) =>
37
- Array.from(new Set(items.map(({ name }) => name))).sort().join("\n");
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
38
  assert.equal(getNames(docs), getNames(docs));
39
39
  });
40
40
 
41
41
  it("upsert()", async () => {
42
42
  const docs = await mdbc.read();
43
- const { modifiedCount } = await mdbc.upsert({ _id: docs[0]._id, name: "david" });
43
+ const { modifiedCount } = await mdbc.upsert({
44
+ _id: docs[0]._id,
45
+ name: "david",
46
+ });
44
47
  assert.equal(modifiedCount, 1);
45
48
  });
46
49
 
@@ -60,8 +63,8 @@ describe("MClient", () => {
60
63
  });
61
64
 
62
65
  it("count()", async () => {
63
- const n = await mdbc.count({name:"alice"});
64
- assert.equal(n,1);
66
+ const n = await mdbc.count({ name: "alice" });
67
+ assert.equal(n, 1);
65
68
  });
66
69
 
67
70
  it("remove()", async () => {
@@ -1,37 +0,0 @@
1
- /*!
2
- * @license mongodbclient
3
- * (c) 2020 tom
4
- * License: MIT
5
- */
6
- declare const MongoClient: any;
7
- import { Db, Collection, CollStats, WithId, Document, InsertManyResult, UpdateResult, DeleteResult } from "mongodb";
8
- export declare class MongoConnection {
9
- private _client;
10
- private _db;
11
- private _collection;
12
- constructor(client: typeof MongoClient, db: Db, collection: Collection);
13
- get client(): typeof MongoClient;
14
- get db(): Db;
15
- get collection(): Collection;
16
- }
17
- export declare class MClient {
18
- private m_uri;
19
- private m_db;
20
- private m_collection;
21
- constructor(uri: string, db: string, collection: string);
22
- get uri(): string;
23
- get db(): string;
24
- get collection(): string;
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
- count(condition?: any): Promise<number>;
33
- insertMany(items: any[]): Promise<InsertManyResult<Document>>;
34
- dbStats(): Promise<Document>;
35
- getCollections(): Promise<Collection[]>;
36
- }
37
- export default MClient;