vona-module-a-orm 5.1.30 → 5.1.31

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/dist/index.js CHANGED
@@ -2156,10 +2156,11 @@ ServiceCacheQuery = __decorate([Service(), BeanInfo({
2156
2156
  })], ServiceCacheQuery);
2157
2157
 
2158
2158
  const BOOLEAN_COLUMN_TYPES = ['bit', 'bool', 'boolean'];
2159
- const NUMBER_COLUMN_TYPES = ['int'];
2160
- const TIMESTAMP_COLUMN_TYPE_PREFIXES = ['timestamp'];
2161
- const FLOAT_COLUMN_TYPE_PREFIXES = ['float', 'double'];
2162
- const INTEGER_COLUMN_TYPE_PREFIXES = ['tinyint', 'smallint', 'mediumint', 'bigint', 'numeric', 'integer'];
2159
+ const INTEGER_COLUMN_TYPES = ['tinyint', 'smallint', 'mediumint', 'int', 'integer'];
2160
+ const FLOAT_COLUMN_TYPES = ['float', 'double'];
2161
+ const GENERATED_DEFAULT_PATTERN = /^(?:current_timestamp\b(?:\s*\(\s*\d*\s*\))?|current_date\b|current_time\b(?:\s*\(\s*\d*\s*\))?|now\s*\(\s*\)|nextval\s*\()/i;
2162
+ const NUMBER_LITERAL_PATTERN = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i;
2163
+ const QUOTED_LITERAL_PATTERN = /^'((?:''|[^'])*)'$/;
2163
2164
  let ServiceDatabaseDialectBase = class ServiceDatabaseDialectBase extends BeanBase {
2164
2165
  constructor(...args) {
2165
2166
  super(...args);
@@ -2204,9 +2205,9 @@ let ServiceDatabaseDialectBase = class ServiceDatabaseDialectBase extends BeanBa
2204
2205
  const result = {
2205
2206
  type: column.type
2206
2207
  };
2207
- result.default = this._coerceColumnValue(column.type, column.defaultValue);
2208
2208
  const columnType = column.columnType;
2209
2209
  if (columnType) result.columnType = columnType;
2210
+ result.default = this._coerceColumnValue(result, column.defaultValue);
2210
2211
  return result;
2211
2212
  }
2212
2213
  _isMysqlBooleanColumn(column) {
@@ -2274,30 +2275,101 @@ let ServiceDatabaseDialectBase = class ServiceDatabaseDialectBase extends BeanBa
2274
2275
  const items = await builder;
2275
2276
  return [items.map(item => item.id), builder];
2276
2277
  }
2277
- _coerceColumnValue(type, value) {
2278
+ _coerceColumnValue(column, value) {
2278
2279
  if (isNil(value)) return undefined;
2279
- if (BOOLEAN_COLUMN_TYPES.includes(type)) return safeBoolean(value);
2280
- if (NUMBER_COLUMN_TYPES.includes(type)) return this._safeNumber(value);
2281
- if (this._columnTypePrefixes(type, TIMESTAMP_COLUMN_TYPE_PREFIXES) && value === 'CURRENT_TIMESTAMP') {
2282
- return undefined; // new Date();
2283
- }
2284
- if (this._columnTypePrefixes(type, FLOAT_COLUMN_TYPE_PREFIXES)) {
2285
- return this._safeNumber(value);
2286
- }
2287
- if (this._columnTypePrefixes(type, INTEGER_COLUMN_TYPE_PREFIXES)) {
2288
- return this._safeNumber(value);
2280
+ if (typeof value !== 'string') {
2281
+ return this._isBooleanColumn(column) ? safeBoolean(value) : value;
2282
+ }
2283
+ const literal = this._coerceColumnLiteral(value);
2284
+ if (literal === undefined) return undefined;
2285
+ if (this._isBooleanColumn(column)) return safeBoolean(literal.value);
2286
+ if (literal.kind === 'boolean') return literal.value;
2287
+ const type = this._columnTypeName(column.type);
2288
+ if (literal.kind === 'string') return literal.value;
2289
+ if (INTEGER_COLUMN_TYPES.includes(type)) return this._safeInteger(literal.value);
2290
+ if (FLOAT_COLUMN_TYPES.includes(type)) return this._safeNumber(literal.value);
2291
+ return literal.value;
2292
+ }
2293
+ _isBooleanColumn(column) {
2294
+ return this._isMysqlBooleanColumn(column) || BOOLEAN_COLUMN_TYPES.includes(this._columnTypeName(column.type));
2295
+ }
2296
+ _columnTypeName(type) {
2297
+ return type.trim().toLowerCase().match(/^\w+/)?.[0] ?? '';
2298
+ }
2299
+ _coerceColumnLiteral(value) {
2300
+ const literal = this._unwrapColumnDefault(value);
2301
+ if (literal === '') return undefined;
2302
+ const literalLower = literal.toLowerCase();
2303
+ if (literalLower === 'null' || GENERATED_DEFAULT_PATTERN.test(literal)) return undefined;
2304
+ const quoted = literal.match(QUOTED_LITERAL_PATTERN);
2305
+ if (quoted) return {
2306
+ kind: 'string',
2307
+ value: quoted[1].replaceAll("''", "'")
2308
+ };
2309
+ if (literalLower === 'true' || literalLower === 't') return {
2310
+ kind: 'boolean',
2311
+ value: true
2312
+ };
2313
+ if (literalLower === 'false' || literalLower === 'f') return {
2314
+ kind: 'boolean',
2315
+ value: false
2316
+ };
2317
+ if (NUMBER_LITERAL_PATTERN.test(literal)) return {
2318
+ kind: 'number',
2319
+ value: literal
2320
+ };
2321
+ if (this._isExpressionColumnDefault(literal)) return undefined;
2322
+ return {
2323
+ kind: 'string',
2324
+ value: literal
2325
+ };
2326
+ }
2327
+ _unwrapColumnDefault(value) {
2328
+ let literal = value.trim();
2329
+ while (literal.startsWith('(') && literal.endsWith(')')) {
2330
+ const inner = literal.slice(1, -1).trim();
2331
+ if (!inner || !this._isColumnDefaultBalanced(inner)) break;
2332
+ literal = inner;
2333
+ }
2334
+ const castIndex = literal.indexOf('::');
2335
+ if (castIndex > 0 && this._isScalarColumnDefault(literal.slice(0, castIndex).trim())) {
2336
+ literal = literal.slice(0, castIndex).trim();
2337
+ }
2338
+ return literal;
2339
+ }
2340
+ _isScalarColumnDefault(value) {
2341
+ return value.toLowerCase() === 'null' || QUOTED_LITERAL_PATTERN.test(value) || value.toLowerCase() === 'true' || value.toLowerCase() === 'false' || NUMBER_LITERAL_PATTERN.test(value);
2342
+ }
2343
+ _isColumnDefaultBalanced(value) {
2344
+ let depth = 0;
2345
+ let quoted = false;
2346
+ for (let index = 0; index < value.length; index++) {
2347
+ const char = value[index];
2348
+ if (char === "'") {
2349
+ if (quoted && value[index + 1] === "'") {
2350
+ index++;
2351
+ } else {
2352
+ quoted = !quoted;
2353
+ }
2354
+ } else if (!quoted && char === '(') {
2355
+ depth++;
2356
+ } else if (!quoted && char === ')') {
2357
+ if (depth === 0) return false;
2358
+ depth--;
2359
+ }
2289
2360
  }
2290
- // pg: NULL::character varying
2291
- if (value.indexOf('NULL::') === 0) return undefined;
2292
- return value;
2361
+ return !quoted && depth === 0;
2293
2362
  }
2294
- // pg: nextval
2295
- _safeNumber(value) {
2363
+ _isExpressionColumnDefault(value) {
2364
+ return /[()*/+\-\s]/.test(value);
2365
+ }
2366
+ _safeInteger(value) {
2296
2367
  const num = Number(value);
2297
- return Number.isNaN(num) ? undefined : num;
2368
+ return Number.isSafeInteger(num) ? num : value;
2298
2369
  }
2299
- _columnTypePrefixes(type, prefixes) {
2300
- return prefixes.some(prefix => type.includes(prefix));
2370
+ _safeNumber(value) {
2371
+ const num = Number(value);
2372
+ return Number.isFinite(num) ? num : undefined;
2301
2373
  }
2302
2374
  };
2303
2375
  ServiceDatabaseDialectBase = __decorate([Service(), Virtual(), BeanInfo({