db-crud-api 0.3.6 → 0.3.10
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/lib/mssql.js +35 -16
- package/lib/mysql.js +27 -19
- package/package.json +22 -22
package/lib/mssql.js
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
import sql from 'mssql';
|
|
4
4
|
|
|
5
5
|
// Regex
|
|
6
|
-
const
|
|
6
|
+
const _match_LIMIT_n = /\bLIMIT\b +\d+/ig
|
|
7
|
+
const _match_LIMIT = /\bLIMIT\b/ig
|
|
7
8
|
|
|
8
9
|
const pools = {};
|
|
9
10
|
|
|
@@ -88,6 +89,8 @@ export async function query(connection, dbOpes) {
|
|
|
88
89
|
if (Array.isArray(dbOpes)) dbOpes.forEach((dbOpe, index) => { sqlString += ToSql(dbOpe, sqlRequest); });
|
|
89
90
|
else sqlString += ToSql(dbOpes, sqlRequest);
|
|
90
91
|
|
|
92
|
+
sqlString = normalizeSpecialName(sqlString);
|
|
93
|
+
|
|
91
94
|
// Run query
|
|
92
95
|
const sqlresult = await sqlRequest.query(sqlString);
|
|
93
96
|
|
|
@@ -97,21 +100,35 @@ export async function query(connection, dbOpes) {
|
|
|
97
100
|
else return sqlresult.recordsets;
|
|
98
101
|
}
|
|
99
102
|
|
|
100
|
-
// Normalize
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
103
|
+
// Normalize SpecialName
|
|
104
|
+
function normalizeSpecialName(name) {
|
|
105
|
+
let _odd = false; // interpreted as 0
|
|
106
|
+
return name.replaceAll('\`', (_m) => {
|
|
107
|
+
_odd = !_odd;
|
|
108
|
+
if (_odd) return '[';
|
|
109
|
+
else return ']';
|
|
110
|
+
});
|
|
111
|
+
}
|
|
105
112
|
|
|
106
113
|
// Compose fully qualified table name
|
|
107
114
|
function fullyQualifiedTableName(tSchema) {
|
|
108
115
|
return (tSchema.database.realName + '.' + (tSchema.table.schema ?? '') + '.' + tSchema.table.realName);
|
|
109
116
|
}
|
|
110
117
|
|
|
118
|
+
function containLIMIT(s) {
|
|
119
|
+
const _return = s?.match(_match_LIMIT_n);
|
|
120
|
+
if (_return) return _return[0];
|
|
121
|
+
else return undefined;
|
|
122
|
+
};
|
|
123
|
+
|
|
111
124
|
function replaceLIMITToTOP(s) {
|
|
112
|
-
|
|
125
|
+
const _replace = containLIMIT(s);
|
|
126
|
+
if (_replace) { return s.replace(_match_LIMIT_n, _replace.replace(_match_LIMIT, 'TOP')); }
|
|
127
|
+
else return s;
|
|
113
128
|
}
|
|
114
129
|
|
|
130
|
+
|
|
131
|
+
|
|
115
132
|
// Parse oprations object to sql string
|
|
116
133
|
function ToSql(dbOpe, sqlRequest) {
|
|
117
134
|
if (dbOpe.get) return getToSql(dbOpe, sqlRequest);
|
|
@@ -145,12 +162,12 @@ function getToSql(dbOpe, sqlRequest) {
|
|
|
145
162
|
|
|
146
163
|
let result = 'select'
|
|
147
164
|
|
|
148
|
-
if (
|
|
165
|
+
if (dbOpe.get.options && dbOpe.get.options.length > 0) {
|
|
149
166
|
if (Array.isArray(dbOpe.get.options)) { for (const s of dbOpe.get.options) { result += ' ' + replaceLIMITToTOP(s); }}
|
|
150
167
|
else result += ' ' + replaceLIMITToTOP(dbOpe.get.options);
|
|
151
168
|
}
|
|
152
169
|
|
|
153
|
-
if (
|
|
170
|
+
if (dbOpe.get.fields && dbOpe.get.fields.length > 0) {
|
|
154
171
|
if (Array.isArray(dbOpe.get.fields)) result += ' ' + dbOpe.get.fields.join(',')
|
|
155
172
|
else result += ' ' + dbOpe.get.fields;
|
|
156
173
|
}
|
|
@@ -158,22 +175,22 @@ function getToSql(dbOpe, sqlRequest) {
|
|
|
158
175
|
|
|
159
176
|
result += ' from ' + fullyQualifiedTableName(dbOpe.get.schema);
|
|
160
177
|
|
|
161
|
-
if (
|
|
178
|
+
if (dbOpe.get.filters && dbOpe.get.filters.length > 0) {
|
|
162
179
|
if (Array.isArray(dbOpe.get.filters)) result += ' where ' + dbOpe.get.filters.join(' ');
|
|
163
180
|
else result += ' where ' + dbOpe.get.filters;
|
|
164
181
|
}
|
|
165
182
|
|
|
166
|
-
if (
|
|
183
|
+
if (dbOpe.get.groups && dbOpe.get.groups.length > 0) {
|
|
167
184
|
if (Array.isArray(dbOpe.get.groups)) result += ' group by ' + dbOpe.get.groups.join(', ');
|
|
168
185
|
else result += ' group by ' + dbOpe.get.groups;
|
|
169
186
|
}
|
|
170
187
|
|
|
171
|
-
if (
|
|
188
|
+
if (dbOpe.get.groupsFilters && dbOpe.get.groupsFilters.length > 0) {
|
|
172
189
|
if (Array.isArray(dbOpe.get.groupsFilters)) result += ' having ' + dbOpe.get.groupsFilters.join(' ');
|
|
173
190
|
else result += ' having ' + dbOpe.get.groupsFilters;
|
|
174
191
|
}
|
|
175
192
|
|
|
176
|
-
if (
|
|
193
|
+
if (dbOpe.get.orderBy && dbOpe.get.orderBy.length > 0) {
|
|
177
194
|
if (Array.isArray(dbOpe.get.orderBy)) result += ' order by ' + dbOpe.get.orderBy.join(', ');
|
|
178
195
|
else result += ' order by ' + dbOpe.get.orderBy;
|
|
179
196
|
}
|
|
@@ -189,10 +206,12 @@ function getToSql(dbOpe, sqlRequest) {
|
|
|
189
206
|
function patchToSql(dbOpe, sqlRequest) {
|
|
190
207
|
let result = 'update ' + fullyQualifiedTableName(dbOpe.patch.schema);
|
|
191
208
|
|
|
192
|
-
if (dbOpe.patch.sets) {
|
|
209
|
+
if (dbOpe.patch.sets) {
|
|
210
|
+
result += ' set ' + Object.entries(dbOpe.patch.sets).map(e => { return e[0] + '=' + stringifyValue(e[0], e[1], dbOpe.patch.schema) }).join(', ');
|
|
211
|
+
}
|
|
193
212
|
else { throw new Error('sets is missing.'); }
|
|
194
213
|
|
|
195
|
-
if (
|
|
214
|
+
if (dbOpe.patch.filters && dbOpe.patch.filters.length > 0) {
|
|
196
215
|
if (Array.isArray(dbOpe.patch.filters)) result += ' where ' + dbOpe.patch.filters.join(' ');
|
|
197
216
|
else result += ' where ' + dbOpe.patch.filters;
|
|
198
217
|
}
|
|
@@ -231,7 +250,7 @@ function putToSql(dbOpe, sqlRequest) {
|
|
|
231
250
|
function deleteToSql(dbOpe, sqlRequest) {
|
|
232
251
|
let result = 'delete from ' + fullyQualifiedTableName(dbOpe.delete.schema);
|
|
233
252
|
|
|
234
|
-
if (
|
|
253
|
+
if (dbOpe.delete.filters && dbOpe.delete.filters.length > 0) {
|
|
235
254
|
if (Array.isArray(dbOpe.delete.filters)) result += ' where ' + dbOpe.delete.filters.join(' ');
|
|
236
255
|
else result += ' where ' + dbOpe.delete.filters;
|
|
237
256
|
}
|
package/lib/mysql.js
CHANGED
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
import sql from 'mysql2/promise';
|
|
4
4
|
|
|
5
5
|
// Regex
|
|
6
|
-
const
|
|
7
|
-
const
|
|
6
|
+
const _match_LIMIT_n = /\bLIMIT\b +\d+/ig
|
|
7
|
+
const _match_TOP_n = /\bTOP\b +\d+/ig
|
|
8
|
+
const _match_TOP = /\bTOP\b/ig
|
|
8
9
|
|
|
9
10
|
const pools = {};
|
|
10
11
|
|
|
@@ -86,6 +87,8 @@ export async function query(connection, dbOpes) {
|
|
|
86
87
|
if (Array.isArray(dbOpes)) dbOpes.forEach((dbOpe, index) => { sqlString += ToSql(dbOpe, pool); });
|
|
87
88
|
else sqlString += ToSql(dbOpes, pool);
|
|
88
89
|
|
|
90
|
+
sqlString = normalizeSpecialName(sqlString);
|
|
91
|
+
|
|
89
92
|
// Run query
|
|
90
93
|
let sqlresult = undefined;
|
|
91
94
|
let sqlconn = undefined;
|
|
@@ -102,11 +105,10 @@ export async function query(connection, dbOpes) {
|
|
|
102
105
|
else return sqlresult[0];
|
|
103
106
|
}
|
|
104
107
|
|
|
105
|
-
// Normalize
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
//}
|
|
108
|
+
// Normalize SpecialName
|
|
109
|
+
function normalizeSpecialName(name) {
|
|
110
|
+
return name.replaceAll('[', '\`').replaceAll(']', '\`');
|
|
111
|
+
}
|
|
110
112
|
|
|
111
113
|
// Compose fully qualified table name
|
|
112
114
|
function fullyQualifiedTableName(tSchema) {
|
|
@@ -115,15 +117,21 @@ function fullyQualifiedTableName(tSchema) {
|
|
|
115
117
|
|
|
116
118
|
|
|
117
119
|
function containLIMIT(s) {
|
|
118
|
-
|
|
120
|
+
const _return = s?.match(_match_LIMIT_n);
|
|
121
|
+
if (_return) return _return[0];
|
|
122
|
+
else return undefined;
|
|
119
123
|
};
|
|
120
124
|
|
|
121
125
|
function containTOP(s) {
|
|
122
|
-
|
|
126
|
+
const _return = s?.match(_match_TOP_n);
|
|
127
|
+
if (_return) return _return[0];
|
|
128
|
+
else return undefined;
|
|
123
129
|
};
|
|
124
130
|
|
|
125
131
|
function replaceTOPToLIMIT(s) {
|
|
126
|
-
|
|
132
|
+
const _replace = containTOP(s);
|
|
133
|
+
if (_replace) { return s.replace(_match_TOP_n, _replace.replace(_match_TOP, 'LIMIT')); }
|
|
134
|
+
else return s;
|
|
127
135
|
}
|
|
128
136
|
|
|
129
137
|
// Parse oprations object to sql string
|
|
@@ -160,14 +168,14 @@ function getToSql(dbOpe, sqlRequest) {
|
|
|
160
168
|
let result = 'select'
|
|
161
169
|
|
|
162
170
|
// omit 'limit' or 'top'
|
|
163
|
-
if (
|
|
171
|
+
if (dbOpe.get.options && dbOpe.get.options.length > 0) {
|
|
164
172
|
if (Array.isArray(dbOpe.get.options)) {
|
|
165
173
|
for (const s of dbOpe.get.options) { if (!containLIMIT(s) && !containTOP(s)) result += ' ' + s; }
|
|
166
174
|
}
|
|
167
175
|
else if (!containLIMIT(dbOpe.get.options) && !containTOP(dbOpe.get.options)) result += ' ' + dbOpe.get.options;
|
|
168
176
|
}
|
|
169
177
|
|
|
170
|
-
if (
|
|
178
|
+
if (dbOpe.get.fields && dbOpe.get.fields.length > 0) {
|
|
171
179
|
if (Array.isArray(dbOpe.get.fields)) result += ' ' + dbOpe.get.fields.join(',')
|
|
172
180
|
else result += ' ' + dbOpe.get.fields;
|
|
173
181
|
}
|
|
@@ -175,28 +183,28 @@ function getToSql(dbOpe, sqlRequest) {
|
|
|
175
183
|
|
|
176
184
|
result += ' from ' + fullyQualifiedTableName(dbOpe.get.schema);
|
|
177
185
|
|
|
178
|
-
if (
|
|
186
|
+
if (dbOpe.get.filters && dbOpe.get.filters.length > 0) {
|
|
179
187
|
if (Array.isArray(dbOpe.get.filters)) result += ' where ' + dbOpe.get.filters.join(' ');
|
|
180
188
|
else result += ' where ' + dbOpe.get.filters;
|
|
181
189
|
}
|
|
182
190
|
|
|
183
|
-
if (
|
|
191
|
+
if (dbOpe.get.groups && dbOpe.get.groups.length > 0) {
|
|
184
192
|
if (Array.isArray(dbOpe.get.groups)) result += ' group by ' + dbOpe.get.groups.join(', ');
|
|
185
193
|
else result += ' group by ' + dbOpe.get.groups;
|
|
186
194
|
}
|
|
187
195
|
|
|
188
|
-
if (
|
|
196
|
+
if (dbOpe.get.groupsFilters && dbOpe.get.groupsFilters.length > 0) {
|
|
189
197
|
if (Array.isArray(dbOpe.get.groupsFilters)) result += ' having ' + dbOpe.get.groupsFilters.join(' ');
|
|
190
198
|
else result += ' having ' + dbOpe.get.groupsFilters;
|
|
191
199
|
}
|
|
192
200
|
|
|
193
|
-
if (
|
|
201
|
+
if (dbOpe.get.orderBy && dbOpe.get.orderBy.length > 0) {
|
|
194
202
|
if (Array.isArray(dbOpe.get.orderBy)) result += ' order by ' + dbOpe.get.orderBy.join(', ');
|
|
195
203
|
else result += ' order by ' + dbOpe.get.orderBy;
|
|
196
204
|
}
|
|
197
205
|
|
|
198
206
|
// search if 'limit' or 'top'
|
|
199
|
-
if (
|
|
207
|
+
if (dbOpe.get.options && dbOpe.get.options.length > 0) {
|
|
200
208
|
if (Array.isArray(dbOpe.get.options)) {
|
|
201
209
|
for (const s of dbOpe.get.options) { if (containLIMIT(s) || containTOP(s)) result += ' ' + replaceTOPToLIMIT(s); }
|
|
202
210
|
}
|
|
@@ -217,7 +225,7 @@ function patchToSql(dbOpe, sqlRequest) {
|
|
|
217
225
|
if (dbOpe.patch.sets) { result += ' set ' + Object.entries(dbOpe.patch.sets).map(e => { return e[0] + '=' + stringifyValue(e[0], e[1], dbOpe.patch.schema) }).join(', '); }
|
|
218
226
|
else { throw new Error('sets is missing.'); }
|
|
219
227
|
|
|
220
|
-
if (
|
|
228
|
+
if (dbOpe.patch.filters && dbOpe.patch.filters.length > 0) {
|
|
221
229
|
if (Array.isArray(dbOpe.patch.filters)) result += ' where ' + dbOpe.patch.filters.join(' ');
|
|
222
230
|
else result += ' where ' + dbOpe.patch.filters;
|
|
223
231
|
}
|
|
@@ -256,7 +264,7 @@ function putToSql(dbOpe, sqlRequest) {
|
|
|
256
264
|
function deleteToSql(dbOpe, sqlRequest) {
|
|
257
265
|
let result = 'delete from ' + fullyQualifiedTableName(dbOpe.delete.schema);
|
|
258
266
|
|
|
259
|
-
if (
|
|
267
|
+
if (dbOpe.delete.filters && dbOpe.delete.filters.length > 0) {
|
|
260
268
|
if (Array.isArray(dbOpe.delete.filters)) result += ' where ' + dbOpe.delete.filters.join(' ');
|
|
261
269
|
else result += ' where ' + dbOpe.delete.filters;
|
|
262
270
|
}
|
package/package.json
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "db-crud-api",
|
|
3
|
-
"version": "0.3.
|
|
4
|
-
"type": "module",
|
|
5
|
-
"description": "CRUD api for database tables",
|
|
6
|
-
"main": "index.js",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
|
-
},
|
|
10
|
-
"author": "FF",
|
|
11
|
-
"license": "MIT",
|
|
12
|
-
"dependencies": {
|
|
13
|
-
"mssql": "^11.0.1",
|
|
14
|
-
"mysql2": "^3.12.0",
|
|
15
|
-
"uuid": "^11.0.3"
|
|
16
|
-
},
|
|
17
|
-
"keywords": [
|
|
18
|
-
"db",
|
|
19
|
-
"crud",
|
|
20
|
-
"api"
|
|
21
|
-
]
|
|
22
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "db-crud-api",
|
|
3
|
+
"version": "0.3.10",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "CRUD api for database tables",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
|
+
},
|
|
10
|
+
"author": "FF",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"mssql": "^11.0.1",
|
|
14
|
+
"mysql2": "^3.12.0",
|
|
15
|
+
"uuid": "^11.0.3"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"db",
|
|
19
|
+
"crud",
|
|
20
|
+
"api"
|
|
21
|
+
]
|
|
22
|
+
}
|