@yunsoft/yuncms-core 0.1.17 → 0.1.18

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yunsoft/yuncms-core",
3
- "version": "0.1.17",
3
+ "version": "0.1.18",
4
4
  "description": "Core services, schema, authentication, permissions and storage primitives for YunCMS.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -95,6 +95,38 @@ function mergeSystemEntries(entries, schema, accountability, operation, now = ne
95
95
  return [...entries, ...systemMutationEntries(schema, accountability, operation, now)];
96
96
  }
97
97
 
98
+ function databaseMutationValue(field, value, schema) {
99
+ if (value == null) return value;
100
+ const type = schema.fields[field]?.type;
101
+
102
+ if (type === 'json') {
103
+ if (typeof value === 'string') {
104
+ try {
105
+ JSON.parse(value);
106
+ return value;
107
+ } catch {
108
+ return JSON.stringify(value);
109
+ }
110
+ }
111
+ return JSON.stringify(value);
112
+ }
113
+
114
+ if (
115
+ (type === 'datetime' || type === 'timestamp')
116
+ && typeof value === 'string'
117
+ && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/u.test(value)
118
+ ) {
119
+ const date = new Date(value);
120
+ if (!Number.isNaN(date.getTime())) return date;
121
+ }
122
+
123
+ return value;
124
+ }
125
+
126
+ function databaseMutationParams(entries, schema) {
127
+ return entries.map(([field, value]) => databaseMutationValue(field, value, schema));
128
+ }
129
+
98
130
  export class ItemsService extends BaseService {
99
131
  constructor(collection, options = {}) {
100
132
  super(options);
@@ -372,7 +404,7 @@ export class ItemsService extends BaseService {
372
404
  const table = quoteIdentifier(this.collection, 'collection name');
373
405
  await this.database.query(
374
406
  `INSERT INTO ${table} (${fields.map((field) => quoteIdentifier(field, 'field name')).join(', ')})\n VALUES (${fields.map(() => '?').join(', ')})`,
375
- fields.map((field) => values[field]),
407
+ databaseMutationParams(fields.map((field) => [field, values[field]]), schema),
376
408
  );
377
409
 
378
410
  const record = await this.returnCreatedOrUpdated(id, schema);
@@ -408,7 +440,10 @@ export class ItemsService extends BaseService {
408
440
  const fields = Object.keys(entry.values);
409
441
  await connection.query(
410
442
  `INSERT INTO ${table} (${fields.map((field) => quoteIdentifier(field, 'field name')).join(', ')})\n VALUES (${fields.map(() => '?').join(', ')})`,
411
- fields.map((field) => entry.values[field]),
443
+ databaseMutationParams(
444
+ fields.map((field) => [field, entry.values[field]]),
445
+ schema,
446
+ ),
412
447
  );
413
448
  }
414
449
  });
@@ -448,7 +483,7 @@ export class ItemsService extends BaseService {
448
483
  const setSql = entries.map(([field]) => `${quoteIdentifier(field, 'field name')} = ?`).join(', ');
449
484
  const [result] = await this.database.query(
450
485
  `UPDATE ${table} SET ${setSql}${filter.sql}`,
451
- [...entries.map(([, value]) => value), ...filter.params],
486
+ [...databaseMutationParams(entries, schema), ...filter.params],
452
487
  );
453
488
  if (result.affectedRows === 0) return null;
454
489
  const record = await this.returnCreatedOrUpdated(id, schema);
@@ -490,7 +525,7 @@ export class ItemsService extends BaseService {
490
525
  const setSql = entries.map(([field]) => `${quoteIdentifier(field, 'field name')} = ?`).join(', ');
491
526
  const [result] = await this.database.query(
492
527
  `UPDATE ${table} SET ${setSql}${filter.sql}`,
493
- [...entries.map(([, value]) => value), ...filter.params],
528
+ [...databaseMutationParams(entries, schema), ...filter.params],
494
529
  );
495
530
  await this.actionMutation('items.update', { filter: filterInput, changes: effectiveChanges, affected: result.affectedRows }, { operation: 'update', bulk: true });
496
531
  return result.affectedRows;