db-crud-api 0.3.16 → 0.3.23

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 CHANGED
@@ -11,24 +11,49 @@ const pools = {};
11
11
 
12
12
  // Common
13
13
  function stringifyValue(fieldName, value, tSchema) {
14
- if (value == undefined) { return 'null' }
15
- if (typeof value !== 'string' || value.trimStart().charAt(0) !== '[') {
16
- if (tSchema.table.fields && tSchema.table.fields[fieldName] && tSchema.table.fields[fieldName].type) {
17
- if (tSchema.table.fields[fieldName].type == 'datetime') {
18
- if (typeof value == 'datetime' || typeof value == 'object') return `\'${value.toISOString()}\'`;
19
- if (typeof value == 'string' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"') return `\'${value}\'`;
20
- return value;
21
- }
22
- if (tSchema.table.fields[fieldName].type == 'boolean' && typeof value == 'boolean') return `\'${value}\'`;
23
- if (tSchema.table.fields[fieldName].type == 'string' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"') return `\'${value}\'`;
24
- if (tSchema.table.fields[fieldName].type == 'uuid' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"') return `\'${value}\'`;
14
+
15
+ // null or undefined
16
+ if (value == undefined)
17
+ return 'null';
18
+
19
+ // detect field type from schema
20
+ let _fieldType = undefined;
21
+ if (tSchema.table.fields && tSchema.table.fields[fieldName] && tSchema.table.fields[fieldName].type) {
22
+ _fieldType = tSchema.table.fields[fieldName].type;
23
+ }
24
+
25
+ // if datetime
26
+ if (_fieldType == 'datetime') {
27
+ // my-sql not accepts 'Z' at end of ISO string
28
+ if (value instanceof Date) return `\'${value.toISOString()}\'`;
29
+ if (typeof value == 'string') {
30
+ const valueDate = new Date(Date.parse(value));
31
+ return `\'${valueDate.toISOString()}\'`;
25
32
  }
26
- else {
27
- // if (typeof value == 'datetime') return `\'${value.toISOString()}\'`;
28
- if (typeof value == 'boolean') return `\'${value}\'`;
29
- if (typeof value == 'string' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"') return `\'${value}\'`;
33
+ return value;
34
+ }
35
+
36
+ // if boolean
37
+ if (_fieldType == 'boolean' && typeof value == 'boolean')
38
+ return `\'${value}\'`;
39
+
40
+ // if string or uuid
41
+ if (_fieldType == 'string' || _fieldType == 'uuid') {
42
+ if (value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"') {
43
+ return `\'${value}\'`;
30
44
  }
31
45
  }
46
+
47
+ // field not in schema
48
+ if (_fieldType == undefined) {
49
+ if (value instanceof Date)
50
+ return `\'${value.toISOString()}\'`;
51
+ if (typeof value == 'boolean')
52
+ return `\'${value}\'`;
53
+ if (typeof value == 'string' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"')
54
+ return `\'${value}\'`;
55
+ }
56
+
32
57
  return value;
33
58
  }
34
59
 
@@ -129,14 +154,36 @@ export async function query(connection, dbOpes) {
129
154
  return sqlresult.recordset;
130
155
  }
131
156
 
132
- // Normalize SpecialName
133
- function normalizeSpecialName(name) {
134
- let _odd = false; // interpreted as 0
135
- return name.replaceAll('\`', (_m) => {
136
- _odd = !_odd;
137
- if (_odd) return '[';
138
- else return ']';
139
- });
157
+ // Normalize SQL to replace backticks with square brackets, considering quoted strings
158
+ function normalizeSpecialName(sqlString) {
159
+ let result = "";
160
+ let inSingleQuote = false;
161
+ let inDoubleQuote = false;
162
+ let msSqlOpening = true; // Serve per alternare [ e ] in modalità mssql
163
+
164
+ for (let i = 0; i < sqlString.length; i++) {
165
+ const char = sqlString[i];
166
+
167
+ // Gestione delle stringhe (costanti)
168
+ if (char === "'" && !inDoubleQuote) {
169
+ inSingleQuote = !inSingleQuote;
170
+ } else if (char === '"' && !inSingleQuote) {
171
+ inDoubleQuote = !inDoubleQuote;
172
+ }
173
+
174
+ // Se siamo fuori dalle citazioni, applichiamo la logica di conversione
175
+ if (!inSingleQuote && !inDoubleQuote) {
176
+ if (char === '`') {
177
+ result += msSqlOpening ? '[' : ']';
178
+ msSqlOpening = !msSqlOpening; // Alterna per il prossimo backtick
179
+ continue;
180
+ }
181
+ }
182
+
183
+ result += char;
184
+ }
185
+
186
+ return result;
140
187
  }
141
188
 
142
189
  // Compose fully qualified table name
package/lib/mysql.js CHANGED
@@ -12,27 +12,55 @@ const pools = {};
12
12
 
13
13
  // Common
14
14
  function stringifyValue(fieldName, value, tSchema) {
15
- if (value == undefined) { return 'null' }
16
- if (typeof value !== 'string' || value.trimStart().charAt(0) !== '\`') {
17
- if (tSchema.table.fields && tSchema.table.fields[fieldName] && tSchema.table.fields[fieldName].type) {
18
- if (tSchema.table.fields[fieldName].type == 'datetime') {
19
- if (typeof value == 'datetime' || typeof value == 'object') return `\'${value.toISOString().slice(0, 19).replace('T', ' ')}\'`;
20
- if (typeof value == 'string' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"') return `\'${value}\'`;
21
- return value;
22
- }
23
- if (tSchema.table.fields[fieldName].type == 'boolean' && typeof value == 'boolean') return `\'${value}\'`;
24
- if (tSchema.table.fields[fieldName].type == 'string' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"') return `\'${value}\'`;
25
- if (tSchema.table.fields[fieldName].type == 'uuid' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"') return `\'${value}\'`;
15
+
16
+ // null or undefined
17
+ if (value == undefined)
18
+ return 'null';
19
+
20
+ // detect field type
21
+ let _fieldType = undefined;
22
+ if (tSchema.table.fields && tSchema.table.fields[fieldName] && tSchema.table.fields[fieldName].type) {
23
+ _fieldType = tSchema.table.fields[fieldName].type;
24
+ }
25
+
26
+ // if datetime
27
+ if (_fieldType == 'datetime') {
28
+ // my-sql not accepts 'Z' at end of ISO string
29
+ if (value instanceof Date)
30
+ return `\'${value.toISOString().slice(0, -1)}\'`;
31
+ if (typeof value == 'string') {
32
+ const valueDate = new Date(Date.parse(value));
33
+ return `\'${valueDate.toISOString().slice(0, -1)}\'`;
26
34
  }
27
- else {
28
- // if (typeof value == 'datetime') return `\'${value.toISOString().slice(0, 19).replace('T', ' ')}\'`;
29
- if (typeof value == 'boolean') return `\'${value}\'`;
30
- if (typeof value == 'string' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"') return `\'${value}\'`;
35
+ return value;
36
+ }
37
+
38
+ // if boolean
39
+ if (_fieldType == 'boolean' && typeof value == 'boolean')
40
+ return `\'${value}\'`;
41
+
42
+ // if string or uuid
43
+ if (_fieldType == 'string' || _fieldType == 'uuid') {
44
+ if (value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"') {
45
+ return `\'${value}\'`;
31
46
  }
32
47
  }
48
+
49
+ // field not in schema
50
+ if (_fieldType == undefined) {
51
+ if (value instanceof Date)
52
+ return `\'${value.toISOString().slice(0, -1)}\'`;
53
+ if (typeof value == 'boolean')
54
+ return `\'${value}\'`;
55
+ if (typeof value == 'string' && value.trimStart().charAt(0) !== '\'' && value.trimStart().charAt(0) !== '\"')
56
+ return `\'${value}\'`;
57
+ }
58
+
33
59
  return value;
34
60
  }
35
61
 
62
+
63
+
36
64
  // Create config object for pool
37
65
  export function prepareConnection(tSchema) {
38
66
  return {
@@ -128,9 +156,36 @@ export async function query(connection, dbOpes) {
128
156
  return sqlresult[0];
129
157
  }
130
158
 
131
- // Normalize SpecialName
132
- function normalizeSpecialName(name) {
133
- return name.replaceAll('[', '\`').replaceAll(']', '\`');
159
+ // Normalize special name to replace square brackets with backticks, considering quoted strings
160
+ function normalizeSpecialName(sql) {
161
+ let result = "";
162
+ let inSingleQuote = false;
163
+ let inDoubleQuote = false;
164
+
165
+ for (let i = 0; i < sql.length; i++) {
166
+ const char = sql[i];
167
+
168
+ // Gestione delle virgolette singole '
169
+ if (char === "'" && !inDoubleQuote) {
170
+ inSingleQuote = !inSingleQuote;
171
+ }
172
+ // Gestione delle virgolette doppie "
173
+ else if (char === '"' && !inSingleQuote) {
174
+ inDoubleQuote = !inDoubleQuote;
175
+ }
176
+
177
+ // Se non siamo all'interno di una stringa, sostituiamo le quadre
178
+ if (!inSingleQuote && !inDoubleQuote) {
179
+ if (char === '[' || char === ']') {
180
+ result += '`';
181
+ continue;
182
+ }
183
+ }
184
+
185
+ result += char;
186
+ }
187
+
188
+ return result;
134
189
  }
135
190
 
136
191
  // Compose fully qualified table name
@@ -138,7 +193,6 @@ function fullyQualifiedTableName(tSchema) {
138
193
  return (tSchema.database.realName + '.' + tSchema.table.realName);
139
194
  }
140
195
 
141
-
142
196
  function containLIMIT(s) {
143
197
  const _return = s?.match(_match_LIMIT_n);
144
198
  if (_return) return _return[0];
package/lib/schema.js CHANGED
@@ -1,11 +1,14 @@
1
- export default {
2
- servers: {
3
- },
4
- config: {
5
- log: {
6
- level: 0,
7
- callback: undefined,
8
- maxAsyncInstance: 50
9
- }
10
- }
1
+ export default {
2
+ servers: {
3
+ },
4
+ config: {
5
+ log: {
6
+ level: 0,
7
+ callback: undefined,
8
+ maxAsyncInstance: 50
9
+ },
10
+ session: {
11
+ tablePath: 'sessions'
12
+ }
13
+ }
11
14
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "db-crud-api",
3
- "version": "0.3.16",
3
+ "version": "0.3.23",
4
4
  "type": "module",
5
5
  "description": "CRUD api for database tables",
6
6
  "main": "index.js",
@@ -12,11 +12,19 @@
12
12
  "dependencies": {
13
13
  "mssql": "^12.0.0",
14
14
  "mysql2": "^3.15.3",
15
- "uuid": "^11.1.0"
15
+ "uuid": "^11.1.0",
16
+ "express-session": "^1.18.2"
16
17
  },
17
18
  "keywords": [
18
19
  "db",
19
20
  "crud",
20
21
  "api"
22
+ ],
23
+ "files": [
24
+ "lib/",
25
+ "CHANGELOG.md",
26
+ "README.md",
27
+ "index.js",
28
+ "package.json"
21
29
  ]
22
30
  }