@stonyx/orm 0.3.2-beta.3 → 0.3.2-beta.5
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/main.js +1 -1
- package/dist/postgres/postgres-db.d.ts +1 -0
- package/dist/postgres/postgres-db.js +16 -6
- package/package.json +1 -1
- package/src/main.ts +1 -1
- package/src/postgres/postgres-db.ts +15 -6
package/dist/main.js
CHANGED
|
@@ -180,7 +180,7 @@ export default class Orm {
|
|
|
180
180
|
const record = createRecord(modelName, data, { serialize: false });
|
|
181
181
|
if (Orm.instance.sqlDb) {
|
|
182
182
|
const response = { data: { id: record.id } };
|
|
183
|
-
await Orm.instance.sqlDb.persist('create', modelName, {}, response);
|
|
183
|
+
await Orm.instance.sqlDb.persist('create', modelName, { rawData: data }, response);
|
|
184
184
|
}
|
|
185
185
|
return record;
|
|
186
186
|
}
|
|
@@ -365,7 +365,7 @@ export default class PostgresDB {
|
|
|
365
365
|
return this._persistDelete(modelName, context);
|
|
366
366
|
}
|
|
367
367
|
}
|
|
368
|
-
async _persistCreate(modelName,
|
|
368
|
+
async _persistCreate(modelName, context, response) {
|
|
369
369
|
const schemas = this.deps.introspectModels();
|
|
370
370
|
const schema = schemas[modelName];
|
|
371
371
|
if (!schema)
|
|
@@ -375,7 +375,7 @@ export default class PostgresDB {
|
|
|
375
375
|
const record = recordId != null ? storeRef.get(modelName, isNaN(recordId) ? recordId : parseInt(recordId)) : null;
|
|
376
376
|
if (!record)
|
|
377
377
|
return;
|
|
378
|
-
const insertData = this._recordToRow(record, schema);
|
|
378
|
+
const insertData = this._recordToRow(record, schema, context.rawData);
|
|
379
379
|
// For auto-increment models, remove the pending ID
|
|
380
380
|
const isPendingId = record.__data.__pendingSqlId;
|
|
381
381
|
if (isPendingId) {
|
|
@@ -420,7 +420,10 @@ export default class PostgresDB {
|
|
|
420
420
|
// Check FK changes too
|
|
421
421
|
for (const fkCol of Object.keys(schema.foreignKeys)) {
|
|
422
422
|
const relName = fkCol.replace(/_id$/, '');
|
|
423
|
-
const
|
|
423
|
+
const relValue = record.__relationships[relName];
|
|
424
|
+
const currentFkValue = (relValue && typeof relValue === 'object' && relValue !== null)
|
|
425
|
+
? relValue.id ?? null
|
|
426
|
+
: relValue ?? record.__data[relName] ?? null;
|
|
424
427
|
const oldFkValue = oldState[relName] ?? null;
|
|
425
428
|
if (currentFkValue !== oldFkValue) {
|
|
426
429
|
changedData[fkCol] = currentFkValue;
|
|
@@ -444,7 +447,7 @@ export default class PostgresDB {
|
|
|
444
447
|
const { sql, values } = this.deps.buildDelete(schema.table, id);
|
|
445
448
|
await this.requirePool().query(sql, values);
|
|
446
449
|
}
|
|
447
|
-
_recordToRow(record, schema) {
|
|
450
|
+
_recordToRow(record, schema, rawData) {
|
|
448
451
|
const row = {};
|
|
449
452
|
const data = record.__data;
|
|
450
453
|
// ID
|
|
@@ -464,13 +467,20 @@ export default class PostgresDB {
|
|
|
464
467
|
for (const fkCol of Object.keys(schema.foreignKeys)) {
|
|
465
468
|
const relName = fkCol.replace(/_id$/, '');
|
|
466
469
|
const related = record.__relationships[relName];
|
|
467
|
-
if (related) {
|
|
470
|
+
if (related && typeof related === 'object' && related !== null) {
|
|
468
471
|
row[fkCol] = related.id;
|
|
469
472
|
}
|
|
473
|
+
else if (related != null) {
|
|
474
|
+
// Raw FK value (e.g., string ID stored directly in __relationships)
|
|
475
|
+
row[fkCol] = related;
|
|
476
|
+
}
|
|
470
477
|
else if (data[relName] !== undefined) {
|
|
471
|
-
// Raw FK value (e.g., from create payload)
|
|
472
478
|
row[fkCol] = data[relName];
|
|
473
479
|
}
|
|
480
|
+
else if (rawData?.[relName] !== undefined) {
|
|
481
|
+
// Fallback to original create payload for unresolved belongsTo FKs
|
|
482
|
+
row[fkCol] = rawData[relName];
|
|
483
|
+
}
|
|
474
484
|
}
|
|
475
485
|
return row;
|
|
476
486
|
}
|
package/package.json
CHANGED
package/src/main.ts
CHANGED
|
@@ -228,7 +228,7 @@ export default class Orm {
|
|
|
228
228
|
|
|
229
229
|
if (Orm.instance.sqlDb) {
|
|
230
230
|
const response: { data: { id: unknown } } = { data: { id: record.id } };
|
|
231
|
-
await Orm.instance.sqlDb.persist('create', modelName, {}, response);
|
|
231
|
+
await Orm.instance.sqlDb.persist('create', modelName, { rawData: data }, response);
|
|
232
232
|
}
|
|
233
233
|
|
|
234
234
|
return record;
|
|
@@ -19,6 +19,7 @@ interface PersistContext {
|
|
|
19
19
|
record?: OrmRecord;
|
|
20
20
|
recordId?: unknown;
|
|
21
21
|
oldState?: Record<string, unknown>;
|
|
22
|
+
rawData?: Record<string, unknown>;
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
interface PersistResponse {
|
|
@@ -477,7 +478,7 @@ export default class PostgresDB {
|
|
|
477
478
|
}
|
|
478
479
|
}
|
|
479
480
|
|
|
480
|
-
private async _persistCreate(modelName: string,
|
|
481
|
+
private async _persistCreate(modelName: string, context: PersistContext, response: PersistResponse): Promise<void> {
|
|
481
482
|
const schemas = this.deps.introspectModels();
|
|
482
483
|
const schema = schemas[modelName];
|
|
483
484
|
|
|
@@ -491,7 +492,7 @@ export default class PostgresDB {
|
|
|
491
492
|
|
|
492
493
|
if (!record) return;
|
|
493
494
|
|
|
494
|
-
const insertData = this._recordToRow(record, schema);
|
|
495
|
+
const insertData = this._recordToRow(record, schema, context.rawData);
|
|
495
496
|
|
|
496
497
|
// For auto-increment models, remove the pending ID
|
|
497
498
|
const isPendingId = record.__data.__pendingSqlId;
|
|
@@ -549,7 +550,10 @@ export default class PostgresDB {
|
|
|
549
550
|
// Check FK changes too
|
|
550
551
|
for (const fkCol of Object.keys(schema.foreignKeys)) {
|
|
551
552
|
const relName = fkCol.replace(/_id$/, '');
|
|
552
|
-
const
|
|
553
|
+
const relValue = record.__relationships[relName];
|
|
554
|
+
const currentFkValue = (relValue && typeof relValue === 'object' && relValue !== null)
|
|
555
|
+
? (relValue as { id: unknown }).id ?? null
|
|
556
|
+
: relValue ?? record.__data[relName] ?? null;
|
|
553
557
|
const oldFkValue = oldState[relName] ?? null;
|
|
554
558
|
|
|
555
559
|
if (currentFkValue !== oldFkValue) {
|
|
@@ -579,7 +583,7 @@ export default class PostgresDB {
|
|
|
579
583
|
await this.requirePool().query(sql, values);
|
|
580
584
|
}
|
|
581
585
|
|
|
582
|
-
private _recordToRow(record: OrmRecord, schema: ModelSchema): Record<string, unknown> {
|
|
586
|
+
private _recordToRow(record: OrmRecord, schema: ModelSchema, rawData?: Record<string, unknown>): Record<string, unknown> {
|
|
583
587
|
const row: Record<string, unknown> = {};
|
|
584
588
|
const data = record.__data;
|
|
585
589
|
|
|
@@ -603,11 +607,16 @@ export default class PostgresDB {
|
|
|
603
607
|
const relName = fkCol.replace(/_id$/, '');
|
|
604
608
|
const related = record.__relationships[relName];
|
|
605
609
|
|
|
606
|
-
if (related) {
|
|
610
|
+
if (related && typeof related === 'object' && related !== null) {
|
|
607
611
|
row[fkCol] = (related as { id: unknown }).id;
|
|
612
|
+
} else if (related != null) {
|
|
613
|
+
// Raw FK value (e.g., string ID stored directly in __relationships)
|
|
614
|
+
row[fkCol] = related;
|
|
608
615
|
} else if (data[relName] !== undefined) {
|
|
609
|
-
// Raw FK value (e.g., from create payload)
|
|
610
616
|
row[fkCol] = data[relName];
|
|
617
|
+
} else if (rawData?.[relName] !== undefined) {
|
|
618
|
+
// Fallback to original create payload for unresolved belongsTo FKs
|
|
619
|
+
row[fkCol] = rawData[relName];
|
|
611
620
|
}
|
|
612
621
|
}
|
|
613
622
|
|