From 86ff15de78df8068ca0ba14b53ce34809ae57c1d Mon Sep 17 00:00:00 2001 From: Ulad Kasach Date: Sat, 15 Jun 2024 09:32:22 -0400 Subject: [PATCH] fix(alias): correctly identify upsert varname based on alias in upsert derefs --- .../SqlSchemaToDomainObjectRelationship.ts | 6 + ...tExpressionForDomainObjectProperty.test.ts | 122 ++++++++++++++++-- ...nInputExpressionForDomainObjectProperty.ts | 31 ++--- ...nputExpressionForSqlSchemaProperty.test.ts | 32 ++++- ...lectExpressionForSqlSchemaProperty.test.ts | 77 +++++++++-- ...ntReferenceAvailableProvisionOrder.test.ts | 2 +- ...maRelationshipForDomainObject.test.ts.snap | 102 +++++++++++++++ ...psForDomainObject.integration.test.ts.snap | 36 ++++++ ...lSchemaRelationshipForDomainObject.test.ts | 33 +++++ ...ineSqlSchemaRelationshipForDomainObject.ts | 3 + 10 files changed, 402 insertions(+), 42 deletions(-) diff --git a/src/domain/objects/SqlSchemaToDomainObjectRelationship.ts b/src/domain/objects/SqlSchemaToDomainObjectRelationship.ts index 183cad3..90c4395 100644 --- a/src/domain/objects/SqlSchemaToDomainObjectRelationship.ts +++ b/src/domain/objects/SqlSchemaToDomainObjectRelationship.ts @@ -18,6 +18,9 @@ const schema = Joi.object().keys({ ) .required(), decorations: Joi.object().keys({ + alias: Joi.object().keys({ + domainObject: Joi.string().required().allow(null), + }), unique: Joi.object().keys({ domainObject: Joi.array().items(Joi.string()).required().allow(null), sqlSchema: Joi.array().items(Joi.string()).required().allow(null), @@ -35,6 +38,9 @@ export interface SqlSchemaToDomainObjectRelationship { domainObject: DomainObjectPropertyMetadata | null; // may be null, if the sql-schema-property is a db-generated property that was not defined by the user in the domain-object }[]; decorations: { + alias: { + domainObject: string | null; + }; unique: { sqlSchema: string[] | null; domainObject: string[] | null; diff --git a/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.test.ts b/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.test.ts index 8ee27b4..579fa2e 100644 --- a/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.test.ts +++ b/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.test.ts @@ -66,7 +66,10 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { sqlSchema: 'train_engineer', }, properties: [], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { sqlSchema: null, domainObject: null }, + }, }), ], // not needed for this one context: GetTypescriptCodeForPropertyContext.FOR_UPSERT_QUERY, @@ -133,7 +136,10 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { }, }, ], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { sqlSchema: null, domainObject: null }, + }, }), ], context: GetTypescriptCodeForPropertyContext.FOR_UPSERT_QUERY, @@ -142,6 +148,78 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { 'geocodeId: trainLocatedEvent.geocode.id ? trainLocatedEvent.geocode.id : (await geocodeDao.upsert({ geocode: trainLocatedEvent.geocode }, context)).id', ); }); + it('should define the input expression correctly for a solo, aliased, DIRECT_BY_NESTING reference', () => { + const expression = + defineQueryFunctionInputExpressionForDomainObjectProperty({ + domainObjectName: 'TrainLocatedEvent', + dobjInputVarName: 'trainLocatedEvent', + sqlSchemaProperty: { + name: 'geocode_id', + isArray: false, + isNullable: false, + isUpdatable: false, + isDatabaseGenerated: false, + reference: { + method: SqlSchemaReferenceMethod.DIRECT_BY_NESTING, + of: { + name: 'Geocode', + extends: DomainObjectVariant.DOMAIN_LITERAL, + }, + }, + }, + domainObjectProperty: { + name: 'geocode', + type: DomainObjectPropertyType.REFERENCE, + of: { + name: 'Geocode', + extends: DomainObjectVariant.DOMAIN_LITERAL, + }, + }, + allSqlSchemaRelationships: [ + new SqlSchemaToDomainObjectRelationship({ + name: { domainObject: 'Geocode', sqlSchema: 'geocode' }, + properties: [ + { + domainObject: { + name: 'latitude', + type: DomainObjectPropertyType.NUMBER, + }, + sqlSchema: { + name: 'latitude', + isArray: false, + isNullable: false, + isUpdatable: false, + isDatabaseGenerated: false, + reference: null, + }, + }, + { + domainObject: { + name: 'longitude', + type: DomainObjectPropertyType.NUMBER, + }, + sqlSchema: { + name: 'longitude', + isArray: false, + isNullable: false, + isUpdatable: false, + isDatabaseGenerated: false, + reference: null, + }, + }, + ], + decorations: { + alias: { domainObject: 'geo' }, + unique: { sqlSchema: null, domainObject: null }, + }, + }), + ], + context: GetTypescriptCodeForPropertyContext.FOR_UPSERT_QUERY, + }); + expect(expression).toEqual( + 'geocodeId: trainLocatedEvent.geocode.id ? trainLocatedEvent.geocode.id : (await geocodeDao.upsert({ geo: trainLocatedEvent.geocode }, context)).id', + ); + }); it('should define the input expression correctly for a solo, nullable, DIRECT_BY_NESTING reference', () => { const expression = defineQueryFunctionInputExpressionForDomainObjectProperty({ @@ -202,7 +280,10 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { }, }, ], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { sqlSchema: null, domainObject: null }, + }, }), ], context: GetTypescriptCodeForPropertyContext.FOR_UPSERT_QUERY, @@ -244,7 +325,10 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { sqlSchema: 'train_engineer', }, properties: [], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { sqlSchema: null, domainObject: null }, + }, }), ], // not needed for this one context: GetTypescriptCodeForPropertyContext.FOR_FIND_BY_QUERY, @@ -314,7 +398,10 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { }, }, ], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { sqlSchema: null, domainObject: null }, + }, }), ], context: GetTypescriptCodeForPropertyContext.FOR_UPSERT_QUERY, @@ -377,7 +464,10 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { sqlSchema: 'train_engineer', }, properties: [], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { sqlSchema: null, domainObject: null }, + }, }), ], // not needed for this one context: GetTypescriptCodeForPropertyContext.FOR_FIND_BY_QUERY, @@ -444,7 +534,10 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { }, }, ], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { sqlSchema: null, domainObject: null }, + }, }), ], context: GetTypescriptCodeForPropertyContext.FOR_FIND_BY_QUERY, @@ -513,7 +606,10 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { }, }, ], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { sqlSchema: null, domainObject: null }, + }, }), ], context: GetTypescriptCodeForPropertyContext.FOR_FIND_BY_QUERY, @@ -555,7 +651,10 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { sqlSchema: 'train_engineer', }, properties: [], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { sqlSchema: null, domainObject: null }, + }, }), ], // not needed for this one context: GetTypescriptCodeForPropertyContext.FOR_FIND_BY_QUERY, @@ -625,7 +724,10 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { }, }, ], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { sqlSchema: null, domainObject: null }, + }, }), ], context: GetTypescriptCodeForPropertyContext.FOR_FIND_BY_QUERY, diff --git a/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.ts b/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.ts index f52b955..f2f45fb 100644 --- a/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.ts +++ b/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.ts @@ -49,6 +49,15 @@ export const defineQueryFunctionInputExpressionForDomainObjectProperty = ({ referencedSqlSchemaRelationship.name.sqlSchema; const referencedDomainObjectName = referencedSqlSchemaRelationship.name.domainObject; + const referencedDomainObjectAttributeName = camelCase( + referencedSqlSchemaName, + ); + const referencedDomainObjectUpsertInputVariableName = + referencedSqlSchemaRelationship.decorations.alias.domainObject + ? camelCase( + referencedSqlSchemaRelationship.decorations.alias.domainObject, + ) + : referencedDomainObjectAttributeName; // if its an implicit uuid reference, then all the legwork is done in the sql. simple case here if ( @@ -82,9 +91,7 @@ export const defineQueryFunctionInputExpressionForDomainObjectProperty = ({ sqlSchemaProperty.name, )}: ${nullabilityPrefix}${domainObjectPropertyVariableName}.id ? ${domainObjectPropertyVariableName}.id : (await ${castDomainObjectNameToDaoName( referencedDomainObjectName, - )}.upsert({ ${camelCase( - referencedSqlSchemaName, - )}: ${domainObjectPropertyVariableName} }, context)).id`; + )}.upsert({ ${referencedDomainObjectUpsertInputVariableName}: ${domainObjectPropertyVariableName} }, context)).id`; // e.g., `geocodeId: geocode.id` return `${camelCase(sqlSchemaProperty.name)}: ${nullabilityPrefix}${ @@ -102,26 +109,16 @@ export const defineQueryFunctionInputExpressionForDomainObjectProperty = ({ sqlSchemaProperty.name, )}: await Promise.all(${dobjInputVarName}.${ domainObjectProperty.name - }.map(async (${camelCase(referencedSqlSchemaName)}) => ${camelCase( - referencedSqlSchemaName, - )}.id ? ${camelCase( - referencedSqlSchemaName, - )}.id : (await ${castDomainObjectNameToDaoName( + }.map(async (${referencedDomainObjectAttributeName}) => ${referencedDomainObjectAttributeName}.id ? ${referencedDomainObjectAttributeName}.id : (await ${castDomainObjectNameToDaoName( referencedDomainObjectName, - )}.upsert({ ${camelCase(referencedSqlSchemaName)} }, context)).id))`; + )}.upsert({ ${referencedDomainObjectUpsertInputVariableName} }, context)).id))`; // e.g., `geocodeIds: geocodes.map(geocode => geocode.id)` return `${camelCase(sqlSchemaProperty.name)}: await Promise.all(${ domainObjectProperty.name - }.map(async (${camelCase(referencedSqlSchemaName)}) => ${camelCase( - referencedSqlSchemaName, - )}.id ? ${camelCase( - referencedSqlSchemaName, - )}.id : ((await ${castDomainObjectNameToDaoName( + }.map(async (${referencedDomainObjectAttributeName}) => ${referencedDomainObjectAttributeName}.id ? ${referencedDomainObjectAttributeName}.id : ((await ${castDomainObjectNameToDaoName( referencedDomainObjectName, - )}.findByUnique(${camelCase( - referencedSqlSchemaName, - )}, context))?.id ?? -1) ))`; + )}.findByUnique(${referencedDomainObjectAttributeName}, context))?.id ?? -1) ))`; } } diff --git a/src/logic/define/databaseAccessObjects/defineQueryInputExpressionForSqlSchemaProperty.test.ts b/src/logic/define/databaseAccessObjects/defineQueryInputExpressionForSqlSchemaProperty.test.ts index 1cd78cb..299092a 100644 --- a/src/logic/define/databaseAccessObjects/defineQueryInputExpressionForSqlSchemaProperty.test.ts +++ b/src/logic/define/databaseAccessObjects/defineQueryInputExpressionForSqlSchemaProperty.test.ts @@ -52,7 +52,13 @@ describe('defineQueryInputExpressionForSqlSchemaProperty', () => { new SqlSchemaToDomainObjectRelationship({ name: { domainObject: 'TrainEngineer', sqlSchema: 'train_engineer' }, properties: [], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { + sqlSchema: null, + domainObject: null, + }, + }, }), ], // not needed for this one }); @@ -118,7 +124,13 @@ describe('defineQueryInputExpressionForSqlSchemaProperty', () => { }, }, ], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { + sqlSchema: null, + domainObject: null, + }, + }, }), ], }); @@ -152,7 +164,13 @@ describe('defineQueryInputExpressionForSqlSchemaProperty', () => { new SqlSchemaToDomainObjectRelationship({ name: { domainObject: 'TrainEngineer', sqlSchema: 'train_engineer' }, properties: [], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { + sqlSchema: null, + domainObject: null, + }, + }, }), ], // not needed for this one }); @@ -233,7 +251,13 @@ describe('defineQueryInputExpressionForSqlSchemaProperty', () => { }, }, ], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { + sqlSchema: null, + domainObject: null, + }, + }, }), ], }); diff --git a/src/logic/define/databaseAccessObjects/defineQuerySelectExpressionForSqlSchemaProperty.test.ts b/src/logic/define/databaseAccessObjects/defineQuerySelectExpressionForSqlSchemaProperty.test.ts index 8312f75..916acf9 100644 --- a/src/logic/define/databaseAccessObjects/defineQuerySelectExpressionForSqlSchemaProperty.test.ts +++ b/src/logic/define/databaseAccessObjects/defineQuerySelectExpressionForSqlSchemaProperty.test.ts @@ -56,7 +56,13 @@ describe('defineQuerySelectExpressionForSqlSchemaProperty', () => { sqlSchema: 'train_engineer', }, properties: [], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { + sqlSchema: null, + domainObject: null, + }, + }, }), ], // not needed for this one }); @@ -99,7 +105,13 @@ describe('defineQuerySelectExpressionForSqlSchemaProperty', () => { sqlSchema: 'train_engineer', }, properties: [], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { + sqlSchema: null, + domainObject: null, + }, + }, }), ], // not needed for this one }); @@ -182,7 +194,13 @@ describe('defineQuerySelectExpressionForSqlSchemaProperty', () => { }, }, ], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { + sqlSchema: null, + domainObject: null, + }, + }, }), ], }); @@ -255,7 +273,13 @@ describe('defineQuerySelectExpressionForSqlSchemaProperty', () => { }, }, ], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { + sqlSchema: null, + domainObject: null, + }, + }, }), ], }); @@ -355,7 +379,13 @@ describe('defineQuerySelectExpressionForSqlSchemaProperty', () => { }, }, ], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { + sqlSchema: null, + domainObject: null, + }, + }, }), new SqlSchemaToDomainObjectRelationship({ name: { domainObject: 'Price', sqlSchema: 'price' }, @@ -390,7 +420,13 @@ describe('defineQuerySelectExpressionForSqlSchemaProperty', () => { }, }, ], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { + sqlSchema: null, + domainObject: null, + }, + }, }), ], }); @@ -492,7 +528,13 @@ describe('defineQuerySelectExpressionForSqlSchemaProperty', () => { }, }, ], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { + sqlSchema: null, + domainObject: null, + }, + }, }), new SqlSchemaToDomainObjectRelationship({ name: { domainObject: 'Price', sqlSchema: 'price' }, @@ -527,7 +569,13 @@ describe('defineQuerySelectExpressionForSqlSchemaProperty', () => { }, }, ], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { + sqlSchema: null, + domainObject: null, + }, + }, }), ], }); @@ -628,7 +676,13 @@ describe('defineQuerySelectExpressionForSqlSchemaProperty', () => { }, }, ], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { + sqlSchema: null, + domainObject: null, + }, + }, }), new SqlSchemaToDomainObjectRelationship({ name: { domainObject: 'Price', sqlSchema: 'price' }, @@ -663,7 +717,10 @@ describe('defineQuerySelectExpressionForSqlSchemaProperty', () => { }, }, ], - decorations: { unique: { sqlSchema: null, domainObject: null } }, + decorations: { + alias: { domainObject: null }, + unique: { sqlSchema: null, domainObject: null }, + }, }), ], }); diff --git a/src/logic/define/sqlSchemaControl/defineDependentReferenceAvailableProvisionOrder.test.ts b/src/logic/define/sqlSchemaControl/defineDependentReferenceAvailableProvisionOrder.test.ts index 98fcaf5..908ae40 100644 --- a/src/logic/define/sqlSchemaControl/defineDependentReferenceAvailableProvisionOrder.test.ts +++ b/src/logic/define/sqlSchemaControl/defineDependentReferenceAvailableProvisionOrder.test.ts @@ -17,7 +17,7 @@ describe('defineDependentReferenceAvailableProvisionOrder', () => { defineDependentReferenceAvailableProvisionOrder({ sqlSchemaRelationships, }); - console.log({ order, reason, depth }); + // console.log({ order, reason, depth }); expect({ order, reason, depth }).toMatchSnapshot(); }); diff --git a/src/logic/define/sqlSchemaRelationship/__snapshots__/defineSqlSchemaRelationshipForDomainObject.test.ts.snap b/src/logic/define/sqlSchemaRelationship/__snapshots__/defineSqlSchemaRelationshipForDomainObject.test.ts.snap index e686c4c..e493694 100644 --- a/src/logic/define/sqlSchemaRelationship/__snapshots__/defineSqlSchemaRelationshipForDomainObject.test.ts.snap +++ b/src/logic/define/sqlSchemaRelationship/__snapshots__/defineSqlSchemaRelationshipForDomainObject.test.ts.snap @@ -3,6 +3,9 @@ exports[`defineSqlSchemarelationshipForDomainObject should look right for a domain-entity 1`] = ` SqlSchemaToDomainObjectRelationship { "decorations": { + "alias": { + "domainObject": null, + }, "unique": { "domainObject": [ "cin", @@ -160,6 +163,9 @@ SqlSchemaToDomainObjectRelationship { exports[`defineSqlSchemarelationshipForDomainObject should look right for a domain-event 1`] = ` SqlSchemaToDomainObjectRelationship { "decorations": { + "alias": { + "domainObject": null, + }, "unique": { "domainObject": [ "trainUuid", @@ -281,6 +287,99 @@ SqlSchemaToDomainObjectRelationship { exports[`defineSqlSchemarelationshipForDomainObject should look right for a domain-literal 1`] = ` SqlSchemaToDomainObjectRelationship { "decorations": { + "alias": { + "domainObject": null, + }, + "unique": { + "domainObject": null, + "sqlSchema": [ + "latitude", + "longitude", + ], + }, + }, + "name": { + "domainObject": "PreciseGeocode", + "sqlSchema": "precise_geocode", + }, + "properties": [ + { + "domainObject": { + "name": "id", + "type": "NUMBER", + }, + "sqlSchema": SqlSchemaPropertyMetadata { + "isArray": false, + "isDatabaseGenerated": true, + "isNullable": false, + "isUpdatable": false, + "name": "id", + "reference": null, + }, + }, + { + "domainObject": { + "name": "uuid", + "type": "STRING", + }, + "sqlSchema": SqlSchemaPropertyMetadata { + "isArray": false, + "isDatabaseGenerated": true, + "isNullable": false, + "isUpdatable": false, + "name": "uuid", + "reference": null, + }, + }, + { + "domainObject": null, + "sqlSchema": SqlSchemaPropertyMetadata { + "isArray": false, + "isDatabaseGenerated": true, + "isNullable": false, + "isUpdatable": false, + "name": "created_at", + "reference": null, + }, + }, + { + "domainObject": { + "name": "latitude", + "type": "NUMBER", + }, + "sqlSchema": SqlSchemaPropertyMetadata { + "isArray": false, + "isDatabaseGenerated": false, + "isNullable": false, + "isUpdatable": false, + "name": "latitude", + "reference": null, + }, + }, + { + "domainObject": { + "name": "longitude", + "type": "NUMBER", + }, + "sqlSchema": SqlSchemaPropertyMetadata { + "isArray": false, + "isDatabaseGenerated": false, + "isNullable": false, + "isUpdatable": false, + "name": "longitude", + "reference": null, + }, + }, + ], +} +`; + +exports[`defineSqlSchemarelationshipForDomainObject should look right for a domain-literal with an alias 1`] = ` +SqlSchemaToDomainObjectRelationship { + "decorations": { + "alias": { + "domainObject": "geocode", + }, "unique": { "domainObject": null, "sqlSchema": [ @@ -368,6 +467,9 @@ SqlSchemaToDomainObjectRelationship { exports[`defineSqlSchemarelationshipForDomainObject should look right for another domain-literal, one with multi word property names 1`] = ` SqlSchemaToDomainObjectRelationship { "decorations": { + "alias": { + "domainObject": null, + }, "unique": { "domainObject": null, "sqlSchema": [ diff --git a/src/logic/define/sqlSchemaRelationship/__snapshots__/defineSqlSchemaRelationshipsForDomainObject.integration.test.ts.snap b/src/logic/define/sqlSchemaRelationship/__snapshots__/defineSqlSchemaRelationshipsForDomainObject.integration.test.ts.snap index 0bff2d5..e85f71c 100644 --- a/src/logic/define/sqlSchemaRelationship/__snapshots__/defineSqlSchemaRelationshipsForDomainObject.integration.test.ts.snap +++ b/src/logic/define/sqlSchemaRelationship/__snapshots__/defineSqlSchemaRelationshipsForDomainObject.integration.test.ts.snap @@ -4,6 +4,9 @@ exports[`defineSqlSchemaRelationshipsForDomainObjects should work on the example [ SqlSchemaToDomainObjectRelationship { "decorations": { + "alias": { + "domainObject": null, + }, "unique": { "domainObject": [ "stationUuid", @@ -168,6 +171,9 @@ exports[`defineSqlSchemaRelationshipsForDomainObjects should work on the example }, SqlSchemaToDomainObjectRelationship { "decorations": { + "alias": { + "domainObject": null, + }, "unique": { "domainObject": [ "uuid", @@ -281,6 +287,9 @@ exports[`defineSqlSchemaRelationshipsForDomainObjects should work on the example }, SqlSchemaToDomainObjectRelationship { "decorations": { + "alias": { + "domainObject": null, + }, "unique": { "domainObject": null, "sqlSchema": [ @@ -373,6 +382,9 @@ exports[`defineSqlSchemaRelationshipsForDomainObjects should work on the example }, SqlSchemaToDomainObjectRelationship { "decorations": { + "alias": { + "domainObject": null, + }, "unique": { "domainObject": [ "socialSecurityNumberHash", @@ -540,6 +552,9 @@ exports[`defineSqlSchemaRelationshipsForDomainObjects should work on the example }, SqlSchemaToDomainObjectRelationship { "decorations": { + "alias": { + "domainObject": null, + }, "unique": { "domainObject": null, "sqlSchema": [ @@ -627,6 +642,9 @@ exports[`defineSqlSchemaRelationshipsForDomainObjects should work on the example }, SqlSchemaToDomainObjectRelationship { "decorations": { + "alias": { + "domainObject": null, + }, "unique": { "domainObject": [ "combinationId", @@ -875,6 +893,9 @@ exports[`defineSqlSchemaRelationshipsForDomainObjects should work on the example }, SqlSchemaToDomainObjectRelationship { "decorations": { + "alias": { + "domainObject": null, + }, "unique": { "domainObject": [ "ein", @@ -1044,6 +1065,9 @@ exports[`defineSqlSchemaRelationshipsForDomainObjects should work on the example }, SqlSchemaToDomainObjectRelationship { "decorations": { + "alias": { + "domainObject": null, + }, "unique": { "domainObject": [ "trainUuid", @@ -1166,6 +1190,9 @@ exports[`defineSqlSchemaRelationshipsForDomainObjects should work on the example }, SqlSchemaToDomainObjectRelationship { "decorations": { + "alias": { + "domainObject": null, + }, "unique": { "domainObject": [ "geocode", @@ -1291,6 +1318,9 @@ exports[`defineSqlSchemaRelationshipsForDomainObjects should work on the example }, SqlSchemaToDomainObjectRelationship { "decorations": { + "alias": { + "domainObject": null, + }, "unique": { "domainObject": null, "sqlSchema": [ @@ -1381,6 +1411,9 @@ exports[`defineSqlSchemaRelationshipsForDomainObjects should work on the example }, SqlSchemaToDomainObjectRelationship { "decorations": { + "alias": { + "domainObject": null, + }, "unique": { "domainObject": null, "sqlSchema": [ @@ -1495,6 +1528,9 @@ exports[`defineSqlSchemaRelationshipsForDomainObjects should work on the example }, SqlSchemaToDomainObjectRelationship { "decorations": { + "alias": { + "domainObject": null, + }, "unique": { "domainObject": [ "externalId", diff --git a/src/logic/define/sqlSchemaRelationship/defineSqlSchemaRelationshipForDomainObject.test.ts b/src/logic/define/sqlSchemaRelationship/defineSqlSchemaRelationshipForDomainObject.test.ts index 9712c9d..ea9d08f 100644 --- a/src/logic/define/sqlSchemaRelationship/defineSqlSchemaRelationshipForDomainObject.test.ts +++ b/src/logic/define/sqlSchemaRelationship/defineSqlSchemaRelationshipForDomainObject.test.ts @@ -40,6 +40,39 @@ describe('defineSqlSchemarelationshipForDomainObject', () => { ]); // should be all of the properties, since domain literal expect(relationship).toMatchSnapshot(); }); + it('should look right for a domain-literal with an alias', () => { + const relationship = defineSqlSchemaRelationshipForDomainObject({ + domainObject: new DomainObjectMetadata({ + name: 'PreciseGeocode', + extends: DomainObjectVariant.DOMAIN_LITERAL, + properties: { + id: { name: 'id', type: DomainObjectPropertyType.NUMBER }, + uuid: { name: 'uuid', type: DomainObjectPropertyType.STRING }, + latitude: { name: 'latitude', type: DomainObjectPropertyType.NUMBER }, + longitude: { + name: 'longitude', + type: DomainObjectPropertyType.NUMBER, + }, + }, + decorations: { + alias: 'geocode', + primary: null, + unique: null, + updatable: null, + }, + }), + allDomainObjects: [], + }); + expect(relationship.name.sqlSchema).toEqual('precise_geocode'); // should be snake case + expect(relationship.properties.length).toEqual(5); // sanity check + expect(relationship.decorations.alias.domainObject).toEqual('geocode'); + expect(relationship.decorations.unique.domainObject).toEqual(null); // it wasn't defined, since domain literal + expect(relationship.decorations.unique.sqlSchema).toEqual([ + 'latitude', + 'longitude', + ]); // should be all of the properties, since domain literal + expect(relationship).toMatchSnapshot(); + }); it('should look right for another domain-literal, one with multi word property names', () => { const relationship = defineSqlSchemaRelationshipForDomainObject({ domainObject: new DomainObjectMetadata({ diff --git a/src/logic/define/sqlSchemaRelationship/defineSqlSchemaRelationshipForDomainObject.ts b/src/logic/define/sqlSchemaRelationship/defineSqlSchemaRelationshipForDomainObject.ts index 25de35e..fa4ea07 100644 --- a/src/logic/define/sqlSchemaRelationship/defineSqlSchemaRelationshipForDomainObject.ts +++ b/src/logic/define/sqlSchemaRelationship/defineSqlSchemaRelationshipForDomainObject.ts @@ -139,6 +139,9 @@ export const defineSqlSchemaRelationshipForDomainObject = ({ }, properties: propertiesRelationship, decorations: { + alias: { + domainObject: domainObject.decorations.alias, + }, unique: { sqlSchema: sqlSchemaUniqueKeys, domainObject: domainObject.decorations.unique,