WIP - update records fixes
This commit is contained in:
@@ -1,7 +1,38 @@
|
|||||||
import { Model, ModelOptions, QueryContext, snakeCaseMappers } from 'objection';
|
import { Model, ModelOptions, QueryContext } from 'objection';
|
||||||
|
|
||||||
export class BaseModel extends Model {
|
export class BaseModel extends Model {
|
||||||
static columnNameMappers = snakeCaseMappers();
|
/**
|
||||||
|
* Use a minimal column mapper: keep property names as-is, but handle
|
||||||
|
* timestamp fields that are stored as created_at/updated_at in the DB.
|
||||||
|
*/
|
||||||
|
static columnNameMappers = {
|
||||||
|
parse(dbRow: Record<string, any>) {
|
||||||
|
const mapped: Record<string, any> = {};
|
||||||
|
for (const [key, value] of Object.entries(dbRow || {})) {
|
||||||
|
if (key === 'created_at') {
|
||||||
|
mapped.createdAt = value;
|
||||||
|
} else if (key === 'updated_at') {
|
||||||
|
mapped.updatedAt = value;
|
||||||
|
} else {
|
||||||
|
mapped[key] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mapped;
|
||||||
|
},
|
||||||
|
format(model: Record<string, any>) {
|
||||||
|
const mapped: Record<string, any> = {};
|
||||||
|
for (const [key, value] of Object.entries(model || {})) {
|
||||||
|
if (key === 'createdAt') {
|
||||||
|
mapped.created_at = value;
|
||||||
|
} else if (key === 'updatedAt') {
|
||||||
|
mapped.updated_at = value;
|
||||||
|
} else {
|
||||||
|
mapped[key] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mapped;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
id: string;
|
id: string;
|
||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
|
|||||||
@@ -179,7 +179,8 @@ export class DynamicModelFactory {
|
|||||||
* Convert a field definition to JSON schema property
|
* Convert a field definition to JSON schema property
|
||||||
*/
|
*/
|
||||||
private static fieldToJsonSchema(field: FieldDefinition): Record<string, any> {
|
private static fieldToJsonSchema(field: FieldDefinition): Record<string, any> {
|
||||||
switch (field.type.toUpperCase()) {
|
const baseSchema = () => {
|
||||||
|
switch (field.type.toUpperCase()) {
|
||||||
case 'TEXT':
|
case 'TEXT':
|
||||||
case 'STRING':
|
case 'STRING':
|
||||||
case 'EMAIL':
|
case 'EMAIL':
|
||||||
@@ -187,45 +188,57 @@ export class DynamicModelFactory {
|
|||||||
case 'PHONE':
|
case 'PHONE':
|
||||||
case 'PICKLIST':
|
case 'PICKLIST':
|
||||||
case 'MULTI_PICKLIST':
|
case 'MULTI_PICKLIST':
|
||||||
return {
|
return {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
...(field.isUnique && { uniqueItems: true }),
|
...(field.isUnique && { uniqueItems: true }),
|
||||||
};
|
};
|
||||||
|
|
||||||
case 'LONG_TEXT':
|
case 'LONG_TEXT':
|
||||||
return { type: 'string' };
|
return { type: 'string' };
|
||||||
|
|
||||||
case 'NUMBER':
|
case 'NUMBER':
|
||||||
case 'DECIMAL':
|
case 'DECIMAL':
|
||||||
case 'CURRENCY':
|
case 'CURRENCY':
|
||||||
case 'PERCENT':
|
case 'PERCENT':
|
||||||
return {
|
return {
|
||||||
type: 'number',
|
type: 'number',
|
||||||
...(field.isUnique && { uniqueItems: true }),
|
...(field.isUnique && { uniqueItems: true }),
|
||||||
};
|
};
|
||||||
|
|
||||||
case 'INTEGER':
|
case 'INTEGER':
|
||||||
return {
|
return {
|
||||||
type: 'integer',
|
type: 'integer',
|
||||||
...(field.isUnique && { uniqueItems: true }),
|
...(field.isUnique && { uniqueItems: true }),
|
||||||
};
|
};
|
||||||
|
|
||||||
case 'BOOLEAN':
|
case 'BOOLEAN':
|
||||||
return { type: 'boolean', default: false };
|
return { type: 'boolean', default: false };
|
||||||
|
|
||||||
case 'DATE':
|
case 'DATE':
|
||||||
return { type: 'string', format: 'date' };
|
return { type: 'string', format: 'date' };
|
||||||
|
|
||||||
case 'DATE_TIME':
|
case 'DATE_TIME':
|
||||||
return { type: 'string', format: 'date-time' };
|
return { type: 'string', format: 'date-time' };
|
||||||
|
|
||||||
case 'LOOKUP':
|
case 'LOOKUP':
|
||||||
case 'BELONGS_TO':
|
case 'BELONGS_TO':
|
||||||
return { type: 'string' };
|
return { type: 'string' };
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return { type: 'string' };
|
return { type: 'string' };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const schema = baseSchema();
|
||||||
|
|
||||||
|
// Allow null for non-required fields so optional strings/numbers don't fail validation
|
||||||
|
if (!field.isRequired) {
|
||||||
|
return {
|
||||||
|
anyOf: [schema, { type: 'null' }],
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return schema;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1179,7 +1179,8 @@ export class ObjectService {
|
|||||||
objectApiName,
|
objectApiName,
|
||||||
editableData,
|
editableData,
|
||||||
);
|
);
|
||||||
await boundModel.query().where({ id: recordId }).update(normalizedEditableData);
|
// Use patch to avoid validating or overwriting fields that aren't present in the edit view
|
||||||
|
await boundModel.query().patch(normalizedEditableData).where({ id: recordId });
|
||||||
const record = await boundModel.query().where({ id: recordId }).first();
|
const record = await boundModel.query().where({ id: recordId }).first();
|
||||||
await this.indexRecord(resolvedTenantId, objectApiName, objectDefModel.fields, record);
|
await this.indexRecord(resolvedTenantId, objectApiName, objectDefModel.fields, record);
|
||||||
return record;
|
return record;
|
||||||
|
|||||||
Reference in New Issue
Block a user