multi-db-orm 3.0.12 → 3.1.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.
@@ -1,78 +1,158 @@
1
- class Metrics {
2
-
3
- loglevel = 1
4
- dbstats;
5
-
6
- constructor(loglevel) {
7
- this.loglevel = loglevel;
8
- this.dbstats = {
9
- reads: 0,
10
- batchReads: 0,
11
- inserts: 0,
12
- updates: 0,
13
- deletes: 0
14
- }
15
- }
16
-
17
- printStatus() {
18
- console.log(dbstats)
19
- }
20
-
21
- reset() {
22
- this.dbstats = {
23
- reads: 0,
24
- batchReads: 0,
25
- inserts: 0,
26
- updates: 0,
27
- deletes: 0
28
- }
29
- }
30
-
31
- getStatus() {
32
- this.dbstats.readsTotal = this.dbstats.reads + this.dbstats.batchReads;
33
- return this.dbstats;
34
- }
35
-
36
- async get(modelname, filter) {
37
- this.dbstats.batchReads++;
38
- if (this.loglevel > 4)
39
- this.printStatus()
40
- }
41
-
42
- async getOne(modelname, filter) {
43
- this.dbstats.reads++;
44
- if (this.loglevel > 4)
45
- this.printStatus()
46
- }
47
-
48
- async create(modelname, object) {
49
- this.dbstats.inserts++;
50
- if (this.loglevel > 4)
51
- this.printStatus()
52
- }
53
-
54
- async insert(modelname, object) {
55
- this.dbstats.inserts++;
56
- if (this.loglevel > 4)
57
- printStatus()
58
- }
59
-
60
- async update(modelname, filter, object) {
61
- this.dbstats.updates++;
62
- if (this.loglevel > 4)
63
- this.printStatus()
64
- }
65
-
66
- async delete(modelname, filter) {
67
- this.dbstats.deletes++;
68
- if (this.loglevel > 4)
69
- this.printStatus()
70
- }
71
-
72
- }
73
-
74
-
75
-
76
- module.exports = {
77
- Metrics
1
+ class Metrics {
2
+
3
+ loglevel = 1
4
+ dbstats;
5
+
6
+ constructor(loglevel) {
7
+ this.loglevel = loglevel;
8
+ this.dbstats = {
9
+ reads: 0,
10
+ batchReads: 0,
11
+ inserts: 0,
12
+ updates: 0,
13
+ deletes: 0
14
+ }
15
+ }
16
+ setLogLevel(level) {
17
+ this.loglevel = level;
18
+ }
19
+
20
+ printStatus() {
21
+ console.log(this.dbstats)
22
+ }
23
+
24
+ reset() {
25
+ this.dbstats = {
26
+ reads: 0,
27
+ batchReads: 0,
28
+ inserts: 0,
29
+ updates: 0,
30
+ deletes: 0
31
+ }
32
+ }
33
+
34
+ getStatus() {
35
+ this.dbstats.readsTotal = this.dbstats.reads + this.dbstats.batchReads;
36
+ return this.dbstats;
37
+ }
38
+
39
+ async get(modelname, filter) {
40
+ const args = Array.from(arguments)
41
+ const last = args[args.length - 1]
42
+ const start = last && typeof last === 'object' && last.startTime ? last.startTime : process.hrtime.bigint()
43
+ this.dbstats.batchReads++;
44
+ if (this.loglevel > 4)
45
+ this.printStatus()
46
+ this._logOperation(modelname, 'get', start, { filter })
47
+ }
48
+
49
+ async getOne(modelname, filter) {
50
+ const args = Array.from(arguments)
51
+ const last = args[args.length - 1]
52
+ const start = last && typeof last === 'object' && last.startTime ? last.startTime : process.hrtime.bigint()
53
+ this.dbstats.reads++;
54
+ if (this.loglevel > 4)
55
+ this.printStatus()
56
+ this._logOperation(modelname, 'getOne', start, { filter })
57
+ }
58
+
59
+ async create(modelname, object) {
60
+ const args = Array.from(arguments)
61
+ const last = args[args.length - 1]
62
+ const start = last && typeof last === 'object' && last.startTime ? last.startTime : process.hrtime.bigint()
63
+ this.dbstats.inserts++;
64
+ if (this.loglevel > 4)
65
+ this.printStatus()
66
+ this._logOperation(modelname, 'create', start, { object })
67
+ }
68
+
69
+ async insert(modelname, object) {
70
+ const args = Array.from(arguments)
71
+ const last = args[args.length - 1]
72
+ const start = last && typeof last === 'object' && last.startTime ? last.startTime : process.hrtime.bigint()
73
+ this.dbstats.inserts++;
74
+ if (this.loglevel > 4)
75
+ this.printStatus()
76
+ this._logOperation(modelname, 'insert', start, { object })
77
+ }
78
+
79
+ async update(modelname, filter, object) {
80
+ const args = Array.from(arguments)
81
+ const last = args[args.length - 1]
82
+ const start = last && typeof last === 'object' && last.startTime ? last.startTime : process.hrtime.bigint()
83
+ this.dbstats.updates++;
84
+ if (this.loglevel > 4)
85
+ this.printStatus()
86
+ this._logOperation(modelname, 'update', start, { filter, object })
87
+ }
88
+
89
+ async delete(modelname, filter) {
90
+ const args = Array.from(arguments)
91
+ const last = args[args.length - 1]
92
+ const start = last && typeof last === 'object' && last.startTime ? last.startTime : process.hrtime.bigint()
93
+ this.dbstats.deletes++;
94
+ if (this.loglevel > 4)
95
+ this.printStatus()
96
+ this._logOperation(modelname, 'delete', start, { filter })
97
+ }
98
+
99
+ _safeStringify(v) {
100
+ try {
101
+ return JSON.stringify(v)
102
+ } catch (e) {
103
+ return String(v)
104
+ }
105
+ }
106
+
107
+ startSpan(operation) {
108
+ return { startTime: process.hrtime.bigint(), operation }
109
+ }
110
+
111
+ updateSpan() {
112
+ return this.startSpan('update')
113
+ }
114
+
115
+ insertSpan() {
116
+ return this.startSpan('insert')
117
+ }
118
+
119
+ getSpan() {
120
+ return this.startSpan('get')
121
+ }
122
+
123
+ getOneSpan() {
124
+ return this.startSpan('getOne')
125
+ }
126
+
127
+ createSpan() {
128
+ return this.startSpan('create')
129
+ }
130
+
131
+ deleteSpan() {
132
+ return this.startSpan('delete')
133
+ }
134
+
135
+ _logOperation(modelname, operation, startTime, details) {
136
+ if (!(this.loglevel > 3)) return
137
+ const elapsedMs = Number(process.hrtime.bigint() - startTime) / 1e6
138
+ if (this.loglevel > 4) {
139
+ const parts = []
140
+ if (details) {
141
+ if (details.filter !== undefined) parts.push(`filter=${this._safeStringify(details.filter)}`)
142
+ if (details.object !== undefined) parts.push(`object=${this._safeStringify(details.object)}`)
143
+ if (details.sort !== undefined) parts.push(`sort=${this._safeStringify(details.sort)}`)
144
+ }
145
+ const detailStr = parts.length ? parts.join(' ') : ''
146
+ console.log(`${operation} ${modelname} in ${elapsedMs.toFixed(3)}ms`, detailStr)
147
+ } else {
148
+ console.log(`${operation} ${modelname} in ${elapsedMs.toFixed(3)}ms`)
149
+ }
150
+ }
151
+
152
+ }
153
+
154
+
155
+
156
+ module.exports = {
157
+ Metrics
78
158
  }
@@ -1,18 +1,18 @@
1
- import MongoClient from 'mongodb/lib/mongo_client';
2
- import { MultiDbORM } from './multidb';
3
-
4
- export declare class MongoDB extends MultiDbORM {
5
- mongodbname: string;
6
- dbc: typeof import('mongodb');
7
- db: typeof import('mongodb');
8
- client: MongoClient;
9
- url: string;
10
-
11
- constructor(secureUrl: string, mongodbname: string);
12
-
13
- _close(): Promise<void>;
14
-
15
- _connect(): Promise<void>;
16
-
17
- run(query: string): Promise<any>;
18
- }
1
+ import MongoClient from 'mongodb/lib/mongo_client';
2
+ import { MultiDbORM } from './multidb';
3
+
4
+ export declare class MongoDB extends MultiDbORM {
5
+ mongodbname: string;
6
+ dbc: typeof import('mongodb');
7
+ db: typeof import('mongodb');
8
+ client: MongoClient;
9
+ url: string;
10
+
11
+ constructor(secureUrl: string, mongodbname: string);
12
+
13
+ _close(): Promise<void>;
14
+
15
+ _connect(): Promise<void>;
16
+
17
+ run(query: string): Promise<any>;
18
+ }
@@ -1,149 +1,156 @@
1
- const { MultiDbORM } = require("./multidb");
2
-
3
- class MongoDB extends MultiDbORM {
4
-
5
- mongodbname
6
- dbc
7
- client
8
- url
9
- constructor(secureUrl, mongodbname) {
10
- super()
11
-
12
- this.url = secureUrl;
13
- this.mongodbname = mongodbname
14
-
15
- const MongoClient = require('mongodb').MongoClient;
16
- this.client = new MongoClient(this.url, {
17
- useUnifiedTopology: true,
18
- useNewUrlParser: true
19
- });
20
- this._connect();
21
- }
22
-
23
- async _close() {
24
- this.dbc.close();
25
- }
26
-
27
- async _connect() {
28
- var dbc = await this.client.connect();
29
- this.dbc = dbc;
30
- this.db = dbc.db(this.mongodbname);
31
- console.log("MongoDB Initialized");
32
- this.dbType = 'mongodb'
33
- }
34
-
35
- async run(query) {
36
- if (this.loglevel > 3)
37
- console.log('RUN : Not Supported in DB Type', this.dbType)
38
- }
39
- _inq2mongop(symbol) {
40
- switch (symbol) {
41
- case "=":
42
- return "$eq";
43
- case ">":
44
- return "$gt";
45
- case ">=":
46
- return "$gte";
47
- case "<":
48
- return "$lt";
49
- case "<=":
50
- return "$lte";
51
- case "!=":
52
- return "$ne";
53
- }
54
- }
55
- async get(modelname, filter, options) {
56
- this.metrics.get(modelname, filter, options);
57
- if (options && options.apply && options.apply.ineq) {
58
- filter[`${options.apply.field}`] = {};
59
- filter[`${options.apply.field}`][`${this._inq2mongop(options.apply.ineq.op)}`] = options.apply.ineq.value
60
- }
61
- var crs = this.getdb().collection(modelname).find(filter);
62
- if (options) {
63
- if (options.apply) {
64
- if (options.apply.sort) {
65
- var order = 1;
66
- if (options.apply.sort == 'desc') {
67
- order = -1;
68
- }
69
- var sortOption = {};
70
- sortOption[`${options.apply.field}`] = order;
71
- crs = crs.sort(sortOption);
72
- }
73
-
74
- } else if (options.sort) {
75
-
76
- var sortOption = {};
77
- options.sort.forEach(srt => {
78
- var order = 1;
79
- if (srt.order == 'desc') {
80
- order = -1;
81
- }
82
- sortOption[`${srt.field}`] = order;
83
- });
84
- crs = crs.sort(sortOption);
85
- }
86
-
87
- if (options.limit) {
88
- crs.limit(options.limit)
89
- }
90
-
91
- if (options.offset) {
92
- crs.skip(options.offset)
93
- }
94
- }
95
-
96
- var snapshot = await crs.toArray()
97
- return snapshot;
98
-
99
- }
100
-
101
- async getOne(modelname, filter, options) {
102
- this.metrics.getOne(modelname, filter, options);
103
- var snapshot = await this.getdb().collection(modelname).findOne(filter)
104
- return snapshot;
105
- }
106
-
107
- async create(modelname, sampleObject) {
108
- this.sync.create(modelname, sampleObject)
109
- this.metrics.create(modelname, sampleObject);
110
-
111
- if (this.loglevel > 3)
112
- console.log('CREATE : Not required in DB Type', this.dbType)
113
- }
114
-
115
- async insert(modelname, object) {
116
- this.sync.insert(modelname, object)
117
- this.metrics.insert(modelname, object)
118
-
119
- const collref = this.getdb().collection(modelname)
120
- try {
121
- return await collref.insertOne(object);
122
- } catch (e) {
123
-
124
- throw e;
125
-
126
- }
127
-
128
- }
129
-
130
- async update(modelname, filter, object) {
131
- this.sync.update(modelname, filter, object)
132
- this.metrics.update(modelname, filter, object)
133
- var resp = await this.getdb().collection(modelname).updateMany(filter, { $set: object })
134
- return resp;
135
- }
136
-
137
- async delete(modelname, filter) {
138
- this.sync.delete(modelname, filter)
139
- this.metrics.delete(modelname, filter)
140
- var resp = await this.getdb().collection(modelname).deleteMany(filter)
141
- return resp;
142
- }
143
- }
144
-
145
-
146
-
147
- module.exports = {
148
- MongoDB
1
+ const { MultiDbORM } = require("./multidb");
2
+
3
+ class MongoDB extends MultiDbORM {
4
+
5
+ mongodbname
6
+ dbc
7
+ client
8
+ url
9
+ constructor(secureUrl, mongodbname) {
10
+ super()
11
+
12
+ this.url = secureUrl;
13
+ this.mongodbname = mongodbname
14
+
15
+ const MongoClient = require('mongodb').MongoClient;
16
+ this.client = new MongoClient(this.url, {
17
+ useUnifiedTopology: true,
18
+ useNewUrlParser: true
19
+ });
20
+ this._connect();
21
+ }
22
+
23
+ async _close() {
24
+ this.dbc.close();
25
+ }
26
+
27
+ async _connect() {
28
+ var dbc = await this.client.connect();
29
+ this.dbc = dbc;
30
+ this.db = dbc.db(this.mongodbname);
31
+ console.log("MongoDB Initialized");
32
+ this.dbType = 'mongodb'
33
+ }
34
+
35
+ async run(query) {
36
+ if (this.loglevel > 3)
37
+ console.log('RUN : Not Supported in DB Type', this.dbType)
38
+ }
39
+ _inq2mongop(symbol) {
40
+ switch (symbol) {
41
+ case "=":
42
+ return "$eq";
43
+ case ">":
44
+ return "$gt";
45
+ case ">=":
46
+ return "$gte";
47
+ case "<":
48
+ return "$lt";
49
+ case "<=":
50
+ return "$lte";
51
+ case "!=":
52
+ return "$ne";
53
+ }
54
+ }
55
+ async get(modelname, filter, options) {
56
+ const span = this.metrics.getSpan();
57
+ if (options && options.apply && options.apply.ineq) {
58
+ filter[`${options.apply.field}`] = {};
59
+ filter[`${options.apply.field}`][`${this._inq2mongop(options.apply.ineq.op)}`] = options.apply.ineq.value
60
+ }
61
+ var crs = this.getdb().collection(modelname).find(filter);
62
+ if (options) {
63
+ if (options.apply) {
64
+ if (options.apply.sort) {
65
+ var order = 1;
66
+ if (options.apply.sort == 'desc') {
67
+ order = -1;
68
+ }
69
+ var sortOption = {};
70
+ sortOption[`${options.apply.field}`] = order;
71
+ crs = crs.sort(sortOption);
72
+ }
73
+
74
+ } else if (options.sort) {
75
+
76
+ var sortOption = {};
77
+ options.sort.forEach(srt => {
78
+ var order = 1;
79
+ if (srt.order == 'desc') {
80
+ order = -1;
81
+ }
82
+ sortOption[`${srt.field}`] = order;
83
+ });
84
+ crs = crs.sort(sortOption);
85
+ }
86
+
87
+ if (options.limit) {
88
+ crs.limit(options.limit)
89
+ }
90
+
91
+ if (options.offset) {
92
+ crs.skip(options.offset)
93
+ }
94
+ }
95
+
96
+ var snapshot = await crs.toArray()
97
+ this.metrics.get(modelname, filter, options, span);
98
+ return snapshot;
99
+
100
+ }
101
+
102
+ async getOne(modelname, filter, options) {
103
+ const span = this.metrics.getOneSpan();
104
+ var snapshot = await this.getdb().collection(modelname).findOne(filter)
105
+ this.metrics.getOne(modelname, filter, options, span);
106
+ return snapshot;
107
+ }
108
+
109
+ async create(modelname, sampleObject) {
110
+ this.sync.create(modelname, sampleObject)
111
+ const span = this.metrics.createSpan();
112
+ this.metrics.create(modelname, sampleObject, span);
113
+
114
+ if (this.loglevel > 3)
115
+ console.log('CREATE : Not required in DB Type', this.dbType)
116
+ }
117
+
118
+ async insert(modelname, object) {
119
+ this.sync.insert(modelname, object)
120
+ const span = this.metrics.insertSpan();
121
+
122
+ const collref = this.getdb().collection(modelname)
123
+ try {
124
+ const res = await collref.insertOne(object);
125
+ this.metrics.insert(modelname, object, span);
126
+ return res;
127
+ } catch (e) {
128
+
129
+ throw e;
130
+
131
+ }
132
+
133
+ }
134
+
135
+ async update(modelname, filter, object) {
136
+ this.sync.update(modelname, filter, object)
137
+ const span = this.metrics.updateSpan();
138
+ var resp = await this.getdb().collection(modelname).updateMany(filter, { $set: object })
139
+ this.metrics.update(modelname, filter, object, span)
140
+ return resp;
141
+ }
142
+
143
+ async delete(modelname, filter) {
144
+ this.sync.delete(modelname, filter)
145
+ const span = this.metrics.deleteSpan();
146
+ var resp = await this.getdb().collection(modelname).deleteMany(filter)
147
+ this.metrics.delete(modelname, filter, span)
148
+ return resp;
149
+ }
150
+ }
151
+
152
+
153
+
154
+ module.exports = {
155
+ MongoDB
149
156
  }
@@ -1,41 +1,41 @@
1
- export class MultiDbORM {
2
- db: any;
3
- dbType: string;
4
- reqMade: number;
5
- lastQLatency: any;
6
- loglevel: number;
7
- sync: import('../sync').Sync;
8
- metrics: import('./metrics').Metrics;
9
-
10
- constructor(db: any);
11
-
12
- connect(): Promise<void>;
13
-
14
- setdb(db: any): void;
15
-
16
- getdb(): any;
17
-
18
- get(modelname: string, filter?: Record<string, any>, options?: {
19
- apply?: {
20
- field: string,
21
- sort: string,
22
- ineq: {
23
- op: '>=' | '<=' | '=' | '>' | '<',
24
- value: string | number | boolean
25
- }
26
- },
27
- sort?: { field: string, order: 'asc' | 'desc' }[]
28
- limit?: number,
29
- offset?: number
30
- }): Promise<any[]>;
31
-
32
- getOne(modelname: string, filter: Record<string, any>): Promise<any>;
33
-
34
- create(modelname: string, object: Record<string, any>): Promise<any>;
35
-
36
- insert(modelname: string, object: Record<string, any>): Promise<any>;
37
-
38
- update(modelname: string, filter: Record<string, any>, object: Record<string, any>): Promise<any>;
39
-
40
- delete(modelname: string, filter?: Record<string, any>): Promise<any>;
41
- }
1
+ export class MultiDbORM {
2
+ db: any;
3
+ dbType: string;
4
+ reqMade: number;
5
+ lastQLatency: any;
6
+ loglevel: number;
7
+ sync: import('../sync').Sync;
8
+ metrics: import('./metrics').Metrics;
9
+
10
+ constructor(db: any);
11
+
12
+ connect(): Promise<void>;
13
+
14
+ setdb(db: any): void;
15
+
16
+ getdb(): any;
17
+
18
+ get<T = any>(modelname: string, filter?: Record<string, any>, options?: {
19
+ apply?: {
20
+ field: string,
21
+ sort: string,
22
+ ineq: {
23
+ op: '>=' | '<=' | '=' | '>' | '<',
24
+ value: string | number | boolean
25
+ }
26
+ },
27
+ sort?: { field: string, order: 'asc' | 'desc' }[]
28
+ limit?: number,
29
+ offset?: number
30
+ }): Promise<T[]>;
31
+
32
+ getOne<T = any>(modelname: string, filter: Record<string, any>): Promise<T>;
33
+
34
+ create<T = any>(modelname: string, object: Record<string, any>): Promise<T>;
35
+
36
+ insert<T = any>(modelname: string, object: Record<string, any>): Promise<T>;
37
+
38
+ update<T = any>(modelname: string, filter: Record<string, any>, object: Record<string, any>): Promise<T>;
39
+
40
+ delete<T = any>(modelname: string, filter?: Record<string, any>): Promise<T>;
41
+ }