multi-db-orm 3.0.11 → 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.
@@ -1,251 +1,251 @@
1
- const path = require("path");
2
- const { MultiDbORM } = require("./multidb");
3
- var fs = require('fs')
4
- const os = require('os');
5
-
6
- class OracleDB extends MultiDbORM {
7
-
8
- connection_pool_name
9
- schema
10
- pool_creation
11
- dataMap = {
12
- "id": "VARCHAR(50) NOT NULL PRIMARY KEY",
13
- "string": "VARCHAR2(4000)",
14
- "number": "NUMBER",
15
- "boolean": "VARCHAR(5)",
16
- "array": "CLOB",
17
- "object": "CLOB",
18
- }
19
-
20
-
21
- static async initOracleLibraries() {
22
-
23
- }
24
-
25
- constructor({
26
- username,
27
- password,
28
- net_service_name,
29
- wallet_dir,
30
- connection_pool_name,
31
- wallet_password,
32
- lib_dir }) {
33
- super()
34
- const oracledb = require('oracledb')
35
- const oracleInstantClient = require("oracle-instantclient");
36
- if (!wallet_password)
37
- wallet_password = password
38
- process.env.LD_LIBRARY_PATH = lib_dir || oracleInstantClient.path
39
- process.env.TS_ADMIN = wallet_dir
40
- oracledb.initOracleClient({
41
- configDir: wallet_dir,
42
- libDir: lib_dir || oracleInstantClient.path
43
- })
44
- oracledb.autoCommit = true;
45
- oracledb.outFormat = oracledb.OUT_FORMAT_OBJECT
46
- oracledb.fetchAsString = [oracledb.CLOB];
47
-
48
- this.connection_pool_name = connection_pool_name || username
49
- this.db = oracledb
50
- this.schema = username
51
-
52
- let sqlnetora = path.join(wallet_dir, 'sqlnet.ora')
53
- let configString = fs.readFileSync(sqlnetora).toString()
54
- const regex = /DIRECTORY="([^"]*)"/;
55
- let walletDirPath = wallet_dir
56
- if (os.platform() == 'win32')
57
- walletDirPath = wallet_dir.split("\\").join("\\\\")
58
- configString = configString.replace(regex, `DIRECTORY="${walletDirPath}"`);
59
- fs.writeFileSync(sqlnetora, configString)
60
-
61
- this.pool_creation = oracledb.createPool({
62
- user: username,
63
- password: password,
64
- configDir: wallet_dir,
65
- walletLocation: wallet_dir,
66
- walletPassword: wallet_password,
67
- connectString: net_service_name,
68
- poolAlias: this.connection_pool_name
69
- }).then(async (pool) => {
70
- console.log(`OracleDB Initialized`);
71
- }).catch(e => {
72
- console.log("Error initializing oracle DB: " + e.message)
73
- throw e
74
- })
75
-
76
- this.dbType = 'oracle'
77
- this.reqMade = 0
78
- }
79
-
80
- async connect() {
81
- await this.pool_creation
82
- }
83
-
84
- async run(query) {
85
- let connection
86
- var that = this
87
- this.reqMade++
88
- return new Promise(async function (resolve, reject) {
89
- try {
90
- connection = await that.db.getConnection(that.connection_pool_name);
91
- let resp = await connection.execute(query)
92
- resolve(resp);
93
- if (that.loglevel > 3)
94
- console.log("Query ", query, ' -> ', resp)
95
-
96
- } catch (err) {
97
- reject(err)
98
- } finally {
99
- if (connection) {
100
- try {
101
- await connection.close();
102
- } catch (err) {
103
- throw (err);
104
- }
105
- }
106
- }
107
- })
108
-
109
-
110
- }
111
-
112
- async get(modelname, filter, options) {
113
- this.metrics.get(modelname, filter, options)
114
- var where = ''
115
- for (var key in filter) {
116
- where = where + `"${key}" = '${filter[key]}' AND `
117
- }
118
- where = where + " 1 = 1 ";
119
- var sort = "";
120
- if (options) {
121
- if (options.apply) {
122
- if (options.apply.ineq) {
123
- where = where + ` AND "${options.apply.field}" ${options.apply.ineq.op} '${options.apply.ineq.value}'`;
124
- }
125
- if (options.apply.sort) {
126
- sort = `ORDER BY "${options.apply.field}" ${options.apply.sort}`
127
- }
128
- }
129
- else if (options.sort) {
130
- sort = `ORDER BY`
131
- for (let i = 0; i < options.sort.length; i++) {
132
- sort = sort + ` "${options.sort[i].field}" ${options.sort[i].order}`;
133
- if (i < options.sort.length - 1) {
134
- sort = sort + ' , ';
135
- }
136
-
137
- }
138
- }
139
- }
140
- var query = `SELECT * FROM ${modelname} WHERE ${where} ${sort} `
141
- var row = await this.run(query)
142
-
143
- return row.rows
144
- }
145
-
146
- async getOne(modelname, filter) {
147
- this.metrics.getOne(modelname, filter)
148
- var where = ''
149
- for (var key in filter) {
150
- where = where + `"${key}" = '${filter[key]}' AND `
151
- }
152
- where = where + " 1 = 1 ";
153
- var query = `SELECT * FROM ${modelname} WHERE ${where} AND rownum < 2`
154
- var row = await this.run(query)
155
- return row.rows[0];
156
- }
157
-
158
- async create(modelname, sampleObject) {
159
- this.sync.create(modelname, sampleObject)
160
- this.metrics.create(modelname, sampleObject)
161
-
162
- var cols = ''
163
- for (var key in sampleObject) {
164
- var type = this.dataMap[typeof (sampleObject[key])] || 'VARCHAR(4000)'
165
- if (Array.isArray(sampleObject[key])) {
166
- type = this.dataMap['array']
167
- }
168
- if (key == 'id') {
169
- type = this.dataMap['id']
170
- }
171
- cols = cols + `"${key}" ${type}, `
172
- }
173
- cols = cols.substring(0, cols.length - 2)
174
- var query = `CREATE TABLE ${modelname} (${cols})`
175
- try {
176
- return await this.run(query)
177
- } catch (err) {
178
- if (!err.message.indexOf("name is already used")) {
179
- console.log(err)
180
- }
181
- return undefined;
182
- }
183
- }
184
-
185
- async insert(modelname, object) {
186
- this.sync.insert(modelname, object)
187
- this.metrics.insert(modelname, object)
188
- var cols = ''
189
- var vals = ''
190
- for (var key in object) {
191
- cols = cols + `"${key}",`
192
- let value = object[key]
193
- if (typeof value == 'object')
194
- value = JSON.stringify(value)
195
- vals = vals + `'${value}',`
196
- }
197
- cols = cols.substring(0, cols.length - 1)
198
- vals = vals.substring(0, vals.length - 1)
199
-
200
- var query = `INSERT INTO ${modelname} (${cols}) VALUES(${vals})`
201
-
202
- try {
203
- return await this.run(query)
204
- } catch (err) {
205
- if (err.message && err.message.indexOf('SQLITE_ERROR: no such table: ') > -1) {
206
- await this.create(modelname, object);
207
- return await this.run(query)
208
- }
209
- else
210
- throw err;
211
- }
212
- }
213
-
214
- async update(modelname, filter, object) {
215
- this.sync.update(modelname, filter, object)
216
- this.metrics.update(modelname, filter, object)
217
-
218
- var where = ''
219
- var vals = ''
220
- for (var key in filter) {
221
- where = where + `"${key}" = '${filter[key]}' AND `
222
- }
223
- for (var key in object) {
224
- vals = vals + ` "${key}" = '${object[key]}',`
225
- }
226
- where = where + " 1 = 1";
227
- vals = vals.substring(0, vals.length - 1)
228
-
229
- var query = `UPDATE ${modelname} SET ${vals} WHERE ${where}`
230
- return await this.run(query)
231
- }
232
-
233
- async delete(modelname, filter) {
234
- this.sync.delete(modelname, filter)
235
- this.metrics.delete(modelname, filter)
236
-
237
- var where = ''
238
- for (var key in filter) {
239
- where = where + `"${key}" = '${filter[key]}' AND `
240
- }
241
- where = where + " 1 = 1";
242
- var query = `DELETE FROM ${modelname} WHERE ${where}`
243
- return await this.run(query)
244
- }
245
- }
246
-
247
-
248
-
249
- module.exports = {
250
- OracleDB
1
+ const path = require("path");
2
+ const { MultiDbORM } = require("./multidb");
3
+ var fs = require('fs')
4
+ const os = require('os');
5
+
6
+ class OracleDB extends MultiDbORM {
7
+
8
+ connection_pool_name
9
+ schema
10
+ pool_creation
11
+ dataMap = {
12
+ "id": "VARCHAR(50) NOT NULL PRIMARY KEY",
13
+ "string": "VARCHAR2(4000)",
14
+ "number": "NUMBER",
15
+ "boolean": "VARCHAR(5)",
16
+ "array": "CLOB",
17
+ "object": "CLOB",
18
+ }
19
+
20
+
21
+ static async initOracleLibraries() {
22
+
23
+ }
24
+
25
+ constructor({
26
+ username,
27
+ password,
28
+ net_service_name,
29
+ wallet_dir,
30
+ connection_pool_name,
31
+ wallet_password,
32
+ lib_dir }) {
33
+ super()
34
+ const oracledb = require('oracledb')
35
+ const oracleInstantClient = require("oracle-instantclient");
36
+ if (!wallet_password)
37
+ wallet_password = password
38
+ process.env.LD_LIBRARY_PATH = lib_dir || oracleInstantClient.path
39
+ process.env.TS_ADMIN = wallet_dir
40
+ oracledb.initOracleClient({
41
+ configDir: wallet_dir,
42
+ libDir: lib_dir || oracleInstantClient.path
43
+ })
44
+ oracledb.autoCommit = true;
45
+ oracledb.outFormat = oracledb.OUT_FORMAT_OBJECT
46
+ oracledb.fetchAsString = [oracledb.CLOB];
47
+
48
+ this.connection_pool_name = connection_pool_name || username
49
+ this.db = oracledb
50
+ this.schema = username
51
+
52
+ let sqlnetora = path.join(wallet_dir, 'sqlnet.ora')
53
+ let configString = fs.readFileSync(sqlnetora).toString()
54
+ const regex = /DIRECTORY="([^"]*)"/;
55
+ let walletDirPath = wallet_dir
56
+ if (os.platform() == 'win32')
57
+ walletDirPath = wallet_dir.split("\\").join("\\\\")
58
+ configString = configString.replace(regex, `DIRECTORY="${walletDirPath}"`);
59
+ fs.writeFileSync(sqlnetora, configString)
60
+
61
+ this.pool_creation = oracledb.createPool({
62
+ user: username,
63
+ password: password,
64
+ configDir: wallet_dir,
65
+ walletLocation: wallet_dir,
66
+ walletPassword: wallet_password,
67
+ connectString: net_service_name,
68
+ poolAlias: this.connection_pool_name
69
+ }).then(async (pool) => {
70
+ console.log(`OracleDB Initialized`);
71
+ }).catch(e => {
72
+ console.log("Error initializing oracle DB: " + e.message)
73
+ throw e
74
+ })
75
+
76
+ this.dbType = 'oracle'
77
+ this.reqMade = 0
78
+ }
79
+
80
+ async connect() {
81
+ await this.pool_creation
82
+ }
83
+
84
+ async run(query) {
85
+ let connection
86
+ var that = this
87
+ this.reqMade++
88
+ return new Promise(async function (resolve, reject) {
89
+ try {
90
+ connection = await that.db.getConnection(that.connection_pool_name);
91
+ let resp = await connection.execute(query)
92
+ resolve(resp);
93
+ if (that.loglevel > 3)
94
+ console.log("Query ", query, ' -> ', resp)
95
+
96
+ } catch (err) {
97
+ reject(err)
98
+ } finally {
99
+ if (connection) {
100
+ try {
101
+ await connection.close();
102
+ } catch (err) {
103
+ throw (err);
104
+ }
105
+ }
106
+ }
107
+ })
108
+
109
+
110
+ }
111
+
112
+ async get(modelname, filter, options) {
113
+ this.metrics.get(modelname, filter, options)
114
+ var where = ''
115
+ for (var key in filter) {
116
+ where = where + `"${key}" = '${filter[key]}' AND `
117
+ }
118
+ where = where + " 1 = 1 ";
119
+ var sort = "";
120
+ if (options) {
121
+ if (options.apply) {
122
+ if (options.apply.ineq) {
123
+ where = where + ` AND "${options.apply.field}" ${options.apply.ineq.op} '${options.apply.ineq.value}'`;
124
+ }
125
+ if (options.apply.sort) {
126
+ sort = `ORDER BY "${options.apply.field}" ${options.apply.sort}`
127
+ }
128
+ }
129
+ else if (options.sort) {
130
+ sort = `ORDER BY`
131
+ for (let i = 0; i < options.sort.length; i++) {
132
+ sort = sort + ` "${options.sort[i].field}" ${options.sort[i].order}`;
133
+ if (i < options.sort.length - 1) {
134
+ sort = sort + ' , ';
135
+ }
136
+
137
+ }
138
+ }
139
+ }
140
+ var query = `SELECT * FROM ${modelname} WHERE ${where} ${sort} `
141
+ var row = await this.run(query)
142
+
143
+ return row.rows
144
+ }
145
+
146
+ async getOne(modelname, filter) {
147
+ this.metrics.getOne(modelname, filter)
148
+ var where = ''
149
+ for (var key in filter) {
150
+ where = where + `"${key}" = '${filter[key]}' AND `
151
+ }
152
+ where = where + " 1 = 1 ";
153
+ var query = `SELECT * FROM ${modelname} WHERE ${where} AND rownum < 2`
154
+ var row = await this.run(query)
155
+ return row.rows[0];
156
+ }
157
+
158
+ async create(modelname, sampleObject) {
159
+ this.sync.create(modelname, sampleObject)
160
+ this.metrics.create(modelname, sampleObject)
161
+
162
+ var cols = ''
163
+ for (var key in sampleObject) {
164
+ var type = this.dataMap[typeof (sampleObject[key])] || 'VARCHAR(4000)'
165
+ if (Array.isArray(sampleObject[key])) {
166
+ type = this.dataMap['array']
167
+ }
168
+ if (key == 'id') {
169
+ type = this.dataMap['id']
170
+ }
171
+ cols = cols + `"${key}" ${type}, `
172
+ }
173
+ cols = cols.substring(0, cols.length - 2)
174
+ var query = `CREATE TABLE ${modelname} (${cols})`
175
+ try {
176
+ return await this.run(query)
177
+ } catch (err) {
178
+ if (!err.message.indexOf("name is already used")) {
179
+ console.log(err)
180
+ }
181
+ return undefined;
182
+ }
183
+ }
184
+
185
+ async insert(modelname, object) {
186
+ this.sync.insert(modelname, object)
187
+ this.metrics.insert(modelname, object)
188
+ var cols = ''
189
+ var vals = ''
190
+ for (var key in object) {
191
+ cols = cols + `"${key}",`
192
+ let value = object[key]
193
+ if (typeof value == 'object')
194
+ value = JSON.stringify(value)
195
+ vals = vals + `'${value}',`
196
+ }
197
+ cols = cols.substring(0, cols.length - 1)
198
+ vals = vals.substring(0, vals.length - 1)
199
+
200
+ var query = `INSERT INTO ${modelname} (${cols}) VALUES(${vals})`
201
+
202
+ try {
203
+ return await this.run(query)
204
+ } catch (err) {
205
+ if (err.message && err.message.indexOf('SQLITE_ERROR: no such table: ') > -1) {
206
+ await this.create(modelname, object);
207
+ return await this.run(query)
208
+ }
209
+ else
210
+ throw err;
211
+ }
212
+ }
213
+
214
+ async update(modelname, filter, object) {
215
+ this.sync.update(modelname, filter, object)
216
+ this.metrics.update(modelname, filter, object)
217
+
218
+ var where = ''
219
+ var vals = ''
220
+ for (var key in filter) {
221
+ where = where + `"${key}" = '${filter[key]}' AND `
222
+ }
223
+ for (var key in object) {
224
+ vals = vals + ` "${key}" = '${object[key]}',`
225
+ }
226
+ where = where + " 1 = 1";
227
+ vals = vals.substring(0, vals.length - 1)
228
+
229
+ var query = `UPDATE ${modelname} SET ${vals} WHERE ${where}`
230
+ return await this.run(query)
231
+ }
232
+
233
+ async delete(modelname, filter) {
234
+ this.sync.delete(modelname, filter)
235
+ this.metrics.delete(modelname, filter)
236
+
237
+ var where = ''
238
+ for (var key in filter) {
239
+ where = where + `"${key}" = '${filter[key]}' AND `
240
+ }
241
+ where = where + " 1 = 1";
242
+ var query = `DELETE FROM ${modelname} WHERE ${where}`
243
+ return await this.run(query)
244
+ }
245
+ }
246
+
247
+
248
+
249
+ module.exports = {
250
+ OracleDB
251
251
  }
@@ -1,11 +1,11 @@
1
- import { MultiDbORM } from './multidb';
2
-
3
- export declare class SQLiteDB extends MultiDbORM {
4
- sqlite3: typeof import('sqlite3');
5
- db: typeof import('sqlite3')
6
-
7
- constructor(filepath?: string);
8
-
9
- run(query: string): Promise<any>;
10
-
11
- }
1
+ import { MultiDbORM } from './multidb';
2
+
3
+ export declare class SQLiteDB extends MultiDbORM {
4
+ sqlite3: typeof import('sqlite3');
5
+ db: typeof import('sqlite3')
6
+
7
+ constructor(filepath?: string);
8
+
9
+ run(query: string): Promise<any>;
10
+
11
+ }