From faffacc73e5ce3276b35c74c809a78540ed10a9d Mon Sep 17 00:00:00 2001 From: Ulad Kasach Date: Sun, 9 Jun 2024 23:12:10 -0400 Subject: [PATCH] feat(alias): support dobj alias for reference variable name --- package-lock.json | 14 +-- package.json | 2 +- .../createExampleDomainObject.ts | 21 +++- .../readConfig.integration.test.ts.snap | 22 ++++ ...sertMethodCodeForDomainObject.test.ts.snap | 37 ++++++ .../defineDaoCodeFilesForDomainObject.test.ts | 4 + ...DaoFindByMethodCodeForDomainObject.test.ts | 114 ++++++++++++++++-- ...efineDaoFindByMethodCodeForDomainObject.ts | 5 + ...DaoUpsertMethodCodeForDomainObject.test.ts | 93 +++++++++++++- ...efineDaoUpsertMethodCodeForDomainObject.ts | 15 ++- ...oUtilCastMethodCodeForDomainObject.test.ts | 8 ++ ...tExpressionForDomainObjectProperty.test.ts | 12 ++ ...nInputExpressionForDomainObjectProperty.ts | 13 +- ...qlSchemaControlCodeForDomainObject.test.ts | 8 ++ ...SchemaGeneratorCodeForDomainObject.test.ts | 15 ++- ...tDomainObjectIsSafeToHaveSqlSchema.test.ts | 10 ++ ...emaPropertyForDomainObjectProperty.test.ts | 2 + ...lSchemaRelationshipForDomainObject.test.ts | 8 ++ 18 files changed, 369 insertions(+), 34 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8f41b37..044a7df 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,7 @@ "chalk": "2.4.2", "change-case": "4.1.1", "domain-objects": "0.20.0", - "domain-objects-metadata": "0.7.1", + "domain-objects-metadata": "0.7.3", "fast-glob": "3.2.2", "joi": "17.4.0", "lodash.omit": "4.5.0", @@ -9293,9 +9293,9 @@ } }, "node_modules/domain-objects-metadata": { - "version": "0.7.1", - "resolved": "https://registry.npmjs.org/domain-objects-metadata/-/domain-objects-metadata-0.7.1.tgz", - "integrity": "sha512-zFpHkixEWYylz1PGjIeXNx3Pu6yAJVSawcLQ1VTd5NIa8xmHgrxnrDm8VwXlhhO52wp6T+HW1KHTICNebPkEZw==", + "version": "0.7.3", + "resolved": "https://registry.npmjs.org/domain-objects-metadata/-/domain-objects-metadata-0.7.3.tgz", + "integrity": "sha512-9Azv2lhdvLRxrRGQ8j7ZBfxv9Y04Vo3zzaXyUM532JyOsVXIpWth+dO4wVDvQe7auM6RHFxifx+n7Tca4MUNXQ==", "hasInstallScript": true, "dependencies": { "@ehmpathy/error-fns": "1.0.2", @@ -30933,9 +30933,9 @@ } }, "domain-objects-metadata": { - "version": "0.7.1", - "resolved": "https://registry.npmjs.org/domain-objects-metadata/-/domain-objects-metadata-0.7.1.tgz", - "integrity": "sha512-zFpHkixEWYylz1PGjIeXNx3Pu6yAJVSawcLQ1VTd5NIa8xmHgrxnrDm8VwXlhhO52wp6T+HW1KHTICNebPkEZw==", + "version": "0.7.3", + "resolved": "https://registry.npmjs.org/domain-objects-metadata/-/domain-objects-metadata-0.7.3.tgz", + "integrity": "sha512-9Azv2lhdvLRxrRGQ8j7ZBfxv9Y04Vo3zzaXyUM532JyOsVXIpWth+dO4wVDvQe7auM6RHFxifx+n7Tca4MUNXQ==", "requires": { "@ehmpathy/error-fns": "1.0.2", "domain-objects": "^0.20.0", diff --git a/package.json b/package.json index 45e2820..edb017d 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,7 @@ "chalk": "2.4.2", "change-case": "4.1.1", "domain-objects": "0.20.0", - "domain-objects-metadata": "0.7.1", + "domain-objects-metadata": "0.7.3", "fast-glob": "3.2.2", "joi": "17.4.0", "lodash.omit": "4.5.0", diff --git a/src/logic/__test_assets__/createExampleDomainObject.ts b/src/logic/__test_assets__/createExampleDomainObject.ts index 2819634..0b4892c 100644 --- a/src/logic/__test_assets__/createExampleDomainObject.ts +++ b/src/logic/__test_assets__/createExampleDomainObject.ts @@ -13,7 +13,12 @@ export const createExampleDomainObjectMetadata = ({ name: 'ExampleDomainLiteral', extends: DomainObjectVariant.DOMAIN_LITERAL, properties: {}, - decorations: { unique: null, updatable: null }, + decorations: { + alias: null, + primary: null, + unique: null, + updatable: null, + }, }); if (extend === DomainObjectVariant.DOMAIN_ENTITY) @@ -21,14 +26,24 @@ export const createExampleDomainObjectMetadata = ({ name: 'ExampleDomainEntity', extends: DomainObjectVariant.DOMAIN_LITERAL, properties: {}, - decorations: { unique: ['uuid'], updatable: [] }, + decorations: { + alias: null, + primary: null, + unique: ['uuid'], + updatable: [], + }, }); if (extend === DomainObjectVariant.DOMAIN_EVENT) return new DomainObjectMetadata({ name: 'ExampleDomainEvent', extends: extend, properties: {}, - decorations: { unique: ['uuid'], updatable: null }, + decorations: { + alias: null, + primary: null, + unique: ['uuid'], + updatable: null, + }, }); throw new Error( 'unsupported domain object variant to create example object for', diff --git a/src/logic/config/getConfig/__snapshots__/readConfig.integration.test.ts.snap b/src/logic/config/getConfig/__snapshots__/readConfig.integration.test.ts.snap index 47476b4..2cdf4e8 100644 --- a/src/logic/config/getConfig/__snapshots__/readConfig.integration.test.ts.snap +++ b/src/logic/config/getConfig/__snapshots__/readConfig.integration.test.ts.snap @@ -7,6 +7,8 @@ exports[`readConfig should be able to read the example config provisioned in __t "objects": [ DomainObjectMetadata { "decorations": { + "alias": null, + "primary": null, "unique": [ "stationUuid", "trainLocatedEventUuid", @@ -73,6 +75,8 @@ exports[`readConfig should be able to read the example config provisioned in __t }, DomainObjectMetadata { "decorations": { + "alias": null, + "primary": null, "unique": [ "uuid", ], @@ -119,6 +123,8 @@ exports[`readConfig should be able to read the example config provisioned in __t }, DomainObjectMetadata { "decorations": { + "alias": null, + "primary": null, "unique": null, "updatable": null, }, @@ -152,6 +158,8 @@ exports[`readConfig should be able to read the example config provisioned in __t }, DomainObjectMetadata { "decorations": { + "alias": null, + "primary": null, "unique": null, "updatable": null, }, @@ -180,6 +188,8 @@ exports[`readConfig should be able to read the example config provisioned in __t }, DomainObjectMetadata { "decorations": { + "alias": null, + "primary": null, "unique": [ "externalId", ], @@ -253,6 +263,8 @@ exports[`readConfig should be able to read the example config provisioned in __t }, DomainObjectMetadata { "decorations": { + "alias": null, + "primary": null, "unique": null, "updatable": null, }, @@ -291,6 +303,8 @@ exports[`readConfig should be able to read the example config provisioned in __t }, DomainObjectMetadata { "decorations": { + "alias": null, + "primary": null, "unique": [ "ein", ], @@ -366,6 +380,8 @@ exports[`readConfig should be able to read the example config provisioned in __t }, DomainObjectMetadata { "decorations": { + "alias": null, + "primary": null, "unique": null, "updatable": null, }, @@ -397,6 +413,8 @@ exports[`readConfig should be able to read the example config provisioned in __t }, DomainObjectMetadata { "decorations": { + "alias": null, + "primary": null, "unique": [ "combinationId", ], @@ -489,6 +507,8 @@ exports[`readConfig should be able to read the example config provisioned in __t }, DomainObjectMetadata { "decorations": { + "alias": null, + "primary": null, "unique": [ "socialSecurityNumberHash", ], @@ -555,6 +575,8 @@ exports[`readConfig should be able to read the example config provisioned in __t }, DomainObjectMetadata { "decorations": { + "alias": null, + "primary": null, "unique": [ "geocode", ], diff --git a/src/logic/define/databaseAccessObjects/__snapshots__/defineDaoUpsertMethodCodeForDomainObject.test.ts.snap b/src/logic/define/databaseAccessObjects/__snapshots__/defineDaoUpsertMethodCodeForDomainObject.test.ts.snap index 8a813ba..a7fe2de 100644 --- a/src/logic/define/databaseAccessObjects/__snapshots__/defineDaoUpsertMethodCodeForDomainObject.test.ts.snap +++ b/src/logic/define/databaseAccessObjects/__snapshots__/defineDaoUpsertMethodCodeForDomainObject.test.ts.snap @@ -42,6 +42,43 @@ export const upsert = async ( };" `; +exports[`defineDaoUpsertMethodCodeForDomainObject should look correct for a simple domain entity with a custom alias 1`] = ` +"import { HasMetadata } from 'type-fns'; + +import { DatabaseConnection } from '$PATH_TO_DATABASE_CONNECTION'; +import { AsyncTaskGrantWish } from '$PATH_TO_DOMAIN_OBJECT'; +import { log } from '$PATH_TO_LOG_OBJECT'; +import { sqlQueryUpsertAsyncTaskGrantWish } from '$PATH_TO_GENERATED_SQL_QUERY_FUNCTIONS'; + +export const sql = \` + -- query_name = upsert_async_task_grant_wish + SELECT + dgv.id, dgv.uuid + FROM upsert_async_task_grant_wish( + :exid + ) as dgv; +\`; + +export const upsert = async ( + { + task, + }: { + task: AsyncTaskGrantWish; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> => { + const results = await sqlQueryUpsertAsyncTaskGrantWish({ + dbExecute: context.dbConnection.query, + logDebug: log.debug, + input: { + exid: task.exid, + }, + }); + const { id, uuid } = results[0]!; // grab the db generated values + return new AsyncTaskGrantWish({ ...task, id, uuid }) as HasMetadata; +};" +`; + exports[`defineDaoUpsertMethodCodeForDomainObject should look correct for domain entity with references, array and solo, implicit and direct 1`] = ` "import { HasMetadata } from 'type-fns'; diff --git a/src/logic/define/databaseAccessObjects/defineDaoCodeFilesForDomainObject.test.ts b/src/logic/define/databaseAccessObjects/defineDaoCodeFilesForDomainObject.test.ts index df7cf5c..a67c252 100644 --- a/src/logic/define/databaseAccessObjects/defineDaoCodeFilesForDomainObject.test.ts +++ b/src/logic/define/databaseAccessObjects/defineDaoCodeFilesForDomainObject.test.ts @@ -19,6 +19,8 @@ describe('defineDaoCodeFilesForDomainObject', () => { longitude: { name: 'longitude', type: DomainObjectPropertyType.NUMBER }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -47,6 +49,8 @@ describe('defineDaoCodeFilesForDomainObject', () => { longitude: { name: 'longitude', type: DomainObjectPropertyType.NUMBER }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, diff --git a/src/logic/define/databaseAccessObjects/defineDaoFindByMethodCodeForDomainObject.test.ts b/src/logic/define/databaseAccessObjects/defineDaoFindByMethodCodeForDomainObject.test.ts index b74829a..fec3f72 100644 --- a/src/logic/define/databaseAccessObjects/defineDaoFindByMethodCodeForDomainObject.test.ts +++ b/src/logic/define/databaseAccessObjects/defineDaoFindByMethodCodeForDomainObject.test.ts @@ -27,6 +27,8 @@ describe('defineDaoFindByMethodCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -89,6 +91,8 @@ describe('defineDaoFindByMethodCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: ['cin'], updatable: ['capacity'], }, @@ -153,6 +157,8 @@ describe('defineDaoFindByMethodCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: ['trainUuid', 'occurredAt'], updatable: [], }, @@ -184,6 +190,8 @@ describe('defineDaoFindByMethodCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -268,6 +276,8 @@ describe('defineDaoFindByMethodCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: ['tin'], updatable: [ 'homeStation', @@ -305,6 +315,8 @@ describe('defineDaoFindByMethodCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -332,6 +344,8 @@ describe('defineDaoFindByMethodCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -346,7 +360,12 @@ describe('defineDaoFindByMethodCodeForDomainObject', () => { properties: { uuid: { name: 'uuid', type: DomainObjectPropertyType.STRING }, }, // domain entity reference, so we dont need to look at properties - decorations: { unique: ['uuid'], updatable: [] }, + decorations: { + alias: null, + primary: null, + unique: ['uuid'], + updatable: [], + }, }), allDomainObjects: [domainObject], }); @@ -358,7 +377,12 @@ describe('defineDaoFindByMethodCodeForDomainObject', () => { properties: { uuid: { name: 'uuid', type: DomainObjectPropertyType.STRING }, }, // domain entity reference, so we dont need to look at properties - decorations: { unique: ['uuid'], updatable: [] }, + decorations: { + alias: null, + primary: null, + unique: ['uuid'], + updatable: [], + }, }), allDomainObjects: [domainObject], }); @@ -407,6 +431,8 @@ describe('defineDaoFindByMethodCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -468,6 +494,8 @@ describe('defineDaoFindByMethodCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: ['cin'], updatable: ['capacity'], }, @@ -532,6 +560,8 @@ describe('defineDaoFindByMethodCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: ['trainUuid', 'occurredAt'], updatable: [], }, @@ -558,6 +588,8 @@ describe('defineDaoFindByMethodCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -644,6 +676,8 @@ describe('defineDaoFindByMethodCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: ['tin'], updatable: [ 'homeStation', @@ -679,6 +713,8 @@ describe('defineDaoFindByMethodCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -706,6 +742,8 @@ describe('defineDaoFindByMethodCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -720,7 +758,12 @@ describe('defineDaoFindByMethodCodeForDomainObject', () => { properties: { uuid: { name: 'uuid', type: DomainObjectPropertyType.STRING }, }, // domain entity reference, so we dont need to look at properties - decorations: { unique: ['uuid'], updatable: [] }, + decorations: { + alias: null, + primary: null, + unique: ['uuid'], + updatable: [], + }, }), allDomainObjects: [domainObject], }); @@ -732,7 +775,12 @@ describe('defineDaoFindByMethodCodeForDomainObject', () => { properties: { uuid: { name: 'uuid', type: DomainObjectPropertyType.STRING }, }, // domain entity reference, so we dont need to look at properties - decorations: { unique: ['uuid'], updatable: [] }, + decorations: { + alias: null, + primary: null, + unique: ['uuid'], + updatable: [], + }, }), allDomainObjects: [domainObject], }); @@ -781,6 +829,8 @@ describe('defineDaoFindByMethodCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -842,6 +892,8 @@ async ( }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -870,6 +922,8 @@ async ( }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -928,6 +982,8 @@ async ( }, }, decorations: { + alias: null, + primary: null, unique: ['cin'], updatable: ['capacity'], }, @@ -998,6 +1054,8 @@ async ( }, }, decorations: { + alias: null, + primary: null, unique: ['uuid'], updatable: ['capacity'], }, @@ -1069,6 +1127,8 @@ async ( }, }, decorations: { + alias: null, + primary: null, unique: ['geocode'], updatable: ['name'], }, @@ -1095,6 +1155,8 @@ async ( }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -1171,6 +1233,8 @@ async ( }, }, decorations: { + alias: null, + primary: null, unique: ['trainUuid', 'occurredAt'], updatable: [], }, @@ -1197,6 +1261,8 @@ async ( }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -1303,6 +1369,8 @@ async ( }, }, decorations: { + alias: null, + primary: null, unique: ['tin'], updatable: [ 'homeStation', @@ -1338,6 +1406,8 @@ async ( }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -1365,6 +1435,8 @@ async ( }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -1379,7 +1451,12 @@ async ( properties: { uuid: { name: 'uuid', type: DomainObjectPropertyType.STRING }, }, // domain entity reference, so we dont need to look at properties - decorations: { unique: ['uuid'], updatable: [] }, + decorations: { + alias: null, + primary: null, + unique: ['uuid'], + updatable: [], + }, }), allDomainObjects: [domainObject], }); @@ -1391,7 +1468,12 @@ async ( properties: { uuid: { name: 'uuid', type: DomainObjectPropertyType.STRING }, }, // domain entity reference, so we dont need to look at properties - decorations: { unique: ['uuid'], updatable: [] }, + decorations: { + alias: null, + primary: null, + unique: ['uuid'], + updatable: [], + }, }), allDomainObjects: [domainObject], }); @@ -1482,6 +1564,8 @@ async ( }, }, decorations: { + alias: null, + primary: null, unique: [ 'homeStationGeocode', 'badges', @@ -1518,6 +1602,8 @@ async ( }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -1544,6 +1630,8 @@ async ( }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -1558,7 +1646,12 @@ async ( properties: { uuid: { name: 'uuid', type: DomainObjectPropertyType.STRING }, }, // domain entity reference, so we dont need to look at properties - decorations: { unique: ['uuid'], updatable: [] }, + decorations: { + alias: null, + primary: null, + unique: ['uuid'], + updatable: [], + }, }), allDomainObjects: [domainObject], }); @@ -1570,7 +1663,12 @@ async ( properties: { uuid: { name: 'uuid', type: DomainObjectPropertyType.STRING }, }, // domain entity reference, so we dont need to look at properties - decorations: { unique: ['uuid'], updatable: [] }, + decorations: { + alias: null, + primary: null, + unique: ['uuid'], + updatable: [], + }, }), allDomainObjects: [domainObject], }); diff --git a/src/logic/define/databaseAccessObjects/defineDaoFindByMethodCodeForDomainObject.ts b/src/logic/define/databaseAccessObjects/defineDaoFindByMethodCodeForDomainObject.ts index e4216f0..e239fd1 100644 --- a/src/logic/define/databaseAccessObjects/defineDaoFindByMethodCodeForDomainObject.ts +++ b/src/logic/define/databaseAccessObjects/defineDaoFindByMethodCodeForDomainObject.ts @@ -85,6 +85,10 @@ export const defineDaoFindByMethodCodeForDomainObject = ({ sqlSchemaProperty.isUpdatable || sqlSchemaProperty.isArray, ); + // define the dobj name to use in the input + const dobjInputVarName = + domainObject.decorations.alias ?? camelCase(domainObject.name); + // define which domain objects are referenced in this method const referencedDomainObjectNames = [ ...new Set([ @@ -297,6 +301,7 @@ export const defineDaoFindByMethodCodeForDomainObject = ({ } = propertyRelationship; return defineQueryFunctionInputExpressionForDomainObjectProperty({ domainObjectName: domainObject.name, + dobjInputVarName, sqlSchemaProperty, domainObjectProperty, allSqlSchemaRelationships, diff --git a/src/logic/define/databaseAccessObjects/defineDaoUpsertMethodCodeForDomainObject.test.ts b/src/logic/define/databaseAccessObjects/defineDaoUpsertMethodCodeForDomainObject.test.ts index 7a04005..21483ba 100644 --- a/src/logic/define/databaseAccessObjects/defineDaoUpsertMethodCodeForDomainObject.test.ts +++ b/src/logic/define/databaseAccessObjects/defineDaoUpsertMethodCodeForDomainObject.test.ts @@ -24,6 +24,8 @@ describe('defineDaoUpsertMethodCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -107,6 +109,8 @@ export const upsert = async ( }, }, decorations: { + alias: null, + primary: null, unique: ['cin'], updatable: ['capacity'], }, @@ -158,6 +162,69 @@ async ( ); // defines inputs correctly expect(code).toMatchSnapshot(); }); + it('should look correct for a simple domain entity with a custom alias', () => { + // define what we're testing on + const domainObject = new DomainObjectMetadata({ + name: 'AsyncTaskGrantWish', + extends: DomainObjectVariant.DOMAIN_ENTITY, + properties: { + id: { + name: 'id', + type: DomainObjectPropertyType.NUMBER, + required: false, + }, + uuid: { + name: 'uuid', + type: DomainObjectPropertyType.STRING, + required: false, + }, + exid: { + name: 'exid', + type: DomainObjectPropertyType.STRING, + required: true, + }, + }, + decorations: { + alias: 'task', + primary: null, + unique: ['exid'], + updatable: [], + }, + }); + const sqlSchemaRelationship = defineSqlSchemaRelationshipForDomainObject({ + domainObject, + allDomainObjects: [domainObject], + }); + + // run it + const code = defineDaoUpsertMethodCodeForDomainObject({ + domainObject, + sqlSchemaRelationship, + allSqlSchemaRelationships: [sqlSchemaRelationship], + }); + + // log an example + expect(code).toContain('-- query_name = upsert_async_task_grant_wish'); // name of query + expect(code).toContain( + ` + FROM upsert_async_task_grant_wish( + `.trim(), + ); // calls upsert correctly + expect(code).toContain( + ` +async ( + { + task, + }: { + task: AsyncTaskGrantWish; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> + `.trim(), + ); // defines fn correctly + expect(code).toContain('await sqlQueryUpsertAsyncTaskGrantWish'); + expect(code).toMatchSnapshot(); + }); it('should return the exact autogenerated values that were defined on the domain entity', () => { // define what we're testing on const domainObject = new DomainObjectMetadata({ @@ -197,6 +264,8 @@ async ( }, }, decorations: { + alias: null, + primary: null, unique: ['cin'], updatable: ['capacity'], }, @@ -288,6 +357,8 @@ async ( }, }, decorations: { + alias: null, + primary: null, unique: ['trainUuid', 'occurredAt'], updatable: [], }, @@ -314,6 +385,8 @@ async ( }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -419,6 +492,8 @@ async ( }, }, decorations: { + alias: null, + primary: null, unique: ['tin'], updatable: [ 'homeStation', @@ -454,6 +529,8 @@ async ( }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -481,6 +558,8 @@ async ( }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -495,7 +574,12 @@ async ( properties: { uuid: { name: 'uuid', type: DomainObjectPropertyType.STRING }, }, - decorations: { unique: ['uuid'], updatable: [] }, + decorations: { + alias: null, + primary: null, + unique: ['uuid'], + updatable: [], + }, }), allDomainObjects: [domainObject], }); @@ -507,7 +591,12 @@ async ( properties: { uuid: { name: 'uuid', type: DomainObjectPropertyType.STRING }, }, // domain entity reference, so we dont need to look at properties - decorations: { unique: ['uuid'], updatable: [] }, + decorations: { + alias: null, + primary: null, + unique: ['uuid'], + updatable: [], + }, }), allDomainObjects: [domainObject], }); diff --git a/src/logic/define/databaseAccessObjects/defineDaoUpsertMethodCodeForDomainObject.ts b/src/logic/define/databaseAccessObjects/defineDaoUpsertMethodCodeForDomainObject.ts index 84fb55b..77a5802 100644 --- a/src/logic/define/databaseAccessObjects/defineDaoUpsertMethodCodeForDomainObject.ts +++ b/src/logic/define/databaseAccessObjects/defineDaoUpsertMethodCodeForDomainObject.ts @@ -25,6 +25,10 @@ export const defineDaoUpsertMethodCodeForDomainObject = ({ const sqlSchemaName = sqlSchemaRelationship.name.sqlSchema; const isUniqueOnUuid = !!domainObject.decorations.unique?.includes('uuid'); + // define the dobj name to use in the input + const dobjInputVarName = + domainObject.decorations.alias ?? camelCase(domainObject.name); + // define the imports const imports = [ ...new Set([ @@ -77,6 +81,7 @@ export const defineDaoUpsertMethodCodeForDomainObject = ({ ({ sqlSchema: sqlSchemaProperty, domainObject: domainObjectProperty }) => defineQueryFunctionInputExpressionForDomainObjectProperty({ domainObjectName: domainObject.name, + dobjInputVarName, sqlSchemaProperty, domainObjectProperty, allSqlSchemaRelationships, @@ -114,9 +119,9 @@ export const sql = \` export const upsert = async ( { - ${camelCase(domainObject.name)}, + ${dobjInputVarName}, }: { - ${camelCase(domainObject.name)}: ${ + ${dobjInputVarName}: ${ isUniqueOnUuid ? `HasUuid<${domainObject.name}>` : domainObject.name }; }, @@ -137,9 +142,9 @@ export const upsert = async ( return `${sqlSchemaPropertyName}: ${domainObjectPropertyName}`; }) .join(', ')} } = results[0]!; // grab the db generated values - return new ${domainObject.name}({ ...${camelCase( - domainObject.name, - )}, ${dbGeneratedPropertiesOnDomainObject + return new ${ + domainObject.name + }({ ...${dobjInputVarName}, ${dbGeneratedPropertiesOnDomainObject .map((sqlSchemaPropertyName) => camelCase(sqlSchemaPropertyName)) .join(', ')} }) as ${outputType}; }; diff --git a/src/logic/define/databaseAccessObjects/defineDaoUtilCastMethodCodeForDomainObject.test.ts b/src/logic/define/databaseAccessObjects/defineDaoUtilCastMethodCodeForDomainObject.test.ts index f74c3b7..8366982 100644 --- a/src/logic/define/databaseAccessObjects/defineDaoUtilCastMethodCodeForDomainObject.test.ts +++ b/src/logic/define/databaseAccessObjects/defineDaoUtilCastMethodCodeForDomainObject.test.ts @@ -19,6 +19,8 @@ describe('defineDaoUtilCastMethodCodeForDomainObject', () => { longitude: { name: 'longitude', type: DomainObjectPropertyType.NUMBER }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -77,6 +79,8 @@ describe('defineDaoUtilCastMethodCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: ['cin'], updatable: ['capacity'], }, @@ -136,6 +140,8 @@ describe('defineDaoUtilCastMethodCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: ['trainUuid', 'occurredAt'], updatable: [], }, @@ -221,6 +227,8 @@ describe('defineDaoUtilCastMethodCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: ['tin'], updatable: [ 'homeStationGeocode', diff --git a/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.test.ts b/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.test.ts index c9f90b5..099034a 100644 --- a/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.test.ts +++ b/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.test.ts @@ -16,6 +16,7 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { const expression = defineQueryFunctionInputExpressionForDomainObjectProperty({ domainObjectName: 'TrainEngineer', + dobjInputVarName: 'trainEngineer', sqlSchemaProperty: { name: 'social_security_number_hash', isArray: false, @@ -39,6 +40,7 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { const expression = defineQueryFunctionInputExpressionForDomainObjectProperty({ domainObjectName: 'Train', + dobjInputVarName: 'train', sqlSchemaProperty: { name: 'lead_engineer_id', isArray: false, @@ -75,6 +77,7 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { const expression = defineQueryFunctionInputExpressionForDomainObjectProperty({ domainObjectName: 'TrainLocatedEvent', + dobjInputVarName: 'trainLocatedEvent', sqlSchemaProperty: { name: 'geocode_id', isArray: false, @@ -143,6 +146,7 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { const expression = defineQueryFunctionInputExpressionForDomainObjectProperty({ domainObjectName: 'TrainLocatedEvent', + dobjInputVarName: 'trainLocatedEvent', sqlSchemaProperty: { name: 'geocode_id', isArray: false, @@ -211,6 +215,7 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { const expression = defineQueryFunctionInputExpressionForDomainObjectProperty({ domainObjectName: 'Train', + dobjInputVarName: 'train', sqlSchemaProperty: { name: 'assigned_engineer_ids', isArray: true, @@ -250,6 +255,7 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { const expression = defineQueryFunctionInputExpressionForDomainObjectProperty({ domainObjectName: 'TrainLocatedEvent', + dobjInputVarName: 'trainLocatedEvent', sqlSchemaProperty: { name: 'geocode_ids', isArray: true, @@ -323,6 +329,7 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { const expression = defineQueryFunctionInputExpressionForDomainObjectProperty({ domainObjectName: 'TrainEngineer', + dobjInputVarName: 'trainEngineer', sqlSchemaProperty: { name: 'social_security_number_hash', isArray: false, @@ -344,6 +351,7 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { const expression = defineQueryFunctionInputExpressionForDomainObjectProperty({ domainObjectName: 'Train', + dobjInputVarName: 'train', sqlSchemaProperty: { name: 'lead_engineer_id', isArray: false, @@ -380,6 +388,7 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { const expression = defineQueryFunctionInputExpressionForDomainObjectProperty({ domainObjectName: 'TrainLocatedEvent', + dobjInputVarName: 'trainLocatedEvent', sqlSchemaProperty: { name: 'geocode_id', isArray: false, @@ -446,6 +455,7 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { const expression = defineQueryFunctionInputExpressionForDomainObjectProperty({ domainObjectName: 'TrainLocatedEvent', + dobjInputVarName: 'trainLocatedEvent', sqlSchemaProperty: { name: 'geocode_id', isArray: false, @@ -514,6 +524,7 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { const expression = defineQueryFunctionInputExpressionForDomainObjectProperty({ domainObjectName: 'Train', + dobjInputVarName: 'train', sqlSchemaProperty: { name: 'assigned_engineer_ids', isArray: true, @@ -553,6 +564,7 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { const expression = defineQueryFunctionInputExpressionForDomainObjectProperty({ domainObjectName: 'TrainLocatedEvent', + dobjInputVarName: 'trailLocatedEvent', sqlSchemaProperty: { name: 'geocode_ids', isArray: true, diff --git a/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.ts b/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.ts index dff9d94..5658b37 100644 --- a/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.ts +++ b/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.ts @@ -14,24 +14,23 @@ export enum GetTypescriptCodeForPropertyContext { export const defineQueryFunctionInputExpressionForDomainObjectProperty = ({ domainObjectName, + dobjInputVarName, sqlSchemaProperty, domainObjectProperty, allSqlSchemaRelationships, context, }: { domainObjectName: string; + dobjInputVarName: string; sqlSchemaProperty: SqlSchemaPropertyMetadata; domainObjectProperty: DomainObjectPropertyMetadata; allSqlSchemaRelationships: SqlSchemaToDomainObjectRelationship[]; context: GetTypescriptCodeForPropertyContext; }): string => { - // define constant used for upsert context - const domainObjectUpsertVarName = camelCase(domainObjectName); - // if its a non-reference, then just return the property directly if (!sqlSchemaProperty.reference) { if (context === GetTypescriptCodeForPropertyContext.FOR_UPSERT_QUERY) - return `${domainObjectProperty.name}: ${domainObjectUpsertVarName}.${domainObjectProperty.name}`; + return `${domainObjectProperty.name}: ${dobjInputVarName}.${domainObjectProperty.name}`; return domainObjectProperty.name; } @@ -57,7 +56,7 @@ export const defineQueryFunctionInputExpressionForDomainObjectProperty = ({ SqlSchemaReferenceMethod.IMPLICIT_BY_UUID ) { if (context === GetTypescriptCodeForPropertyContext.FOR_UPSERT_QUERY) - return `${domainObjectProperty.name}: ${domainObjectUpsertVarName}.${domainObjectProperty.name}`; + return `${domainObjectProperty.name}: ${dobjInputVarName}.${domainObjectProperty.name}`; return `${domainObjectProperty.name}`; } @@ -70,7 +69,7 @@ export const defineQueryFunctionInputExpressionForDomainObjectProperty = ({ if (!sqlSchemaProperty.isArray) { const domainObjectPropertyVariableName = context === GetTypescriptCodeForPropertyContext.FOR_UPSERT_QUERY - ? `${domainObjectUpsertVarName}.${domainObjectProperty.name}` + ? `${dobjInputVarName}.${domainObjectProperty.name}` : domainObjectProperty.name; const nullabilityPrefix = sqlSchemaProperty.isNullable @@ -99,7 +98,7 @@ export const defineQueryFunctionInputExpressionForDomainObjectProperty = ({ // e.g.,: `geocodeIds: await Promise.all(location.geocodes.map(async (geocode) => geocode.id ? geocode.id : (await geocodeDao.upsert({ geocode: location.geocode }, context)).id)` return `${camelCase( sqlSchemaProperty.name, - )}: await Promise.all(${domainObjectUpsertVarName}.${ + )}: await Promise.all(${dobjInputVarName}.${ domainObjectProperty.name }.map(async (${camelCase(referencedSqlSchemaName)}) => ${camelCase( referencedSqlSchemaName, diff --git a/src/logic/define/sqlSchemaControl/defineSqlSchemaControlCodeForDomainObject.test.ts b/src/logic/define/sqlSchemaControl/defineSqlSchemaControlCodeForDomainObject.test.ts index f70c67c..0bfc171 100644 --- a/src/logic/define/sqlSchemaControl/defineSqlSchemaControlCodeForDomainObject.test.ts +++ b/src/logic/define/sqlSchemaControl/defineSqlSchemaControlCodeForDomainObject.test.ts @@ -24,6 +24,8 @@ describe('defineSqlSchemaControlCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -78,6 +80,8 @@ describe('defineSqlSchemaControlCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: ['cin'], updatable: ['capacity'], }, @@ -141,6 +145,8 @@ describe('defineSqlSchemaControlCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: ['trainUuid', 'occurredAt'], updatable: [], }, @@ -216,6 +222,8 @@ describe('defineSqlSchemaControlCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: ['uuid'], updatable: ['locomotiveUuids', 'leadEngineerUuid', 'sensorUuids'], }, diff --git a/src/logic/define/sqlSchemaGenerator/defineSqlSchemaGeneratorCodeForDomainObject.test.ts b/src/logic/define/sqlSchemaGenerator/defineSqlSchemaGeneratorCodeForDomainObject.test.ts index 4b236b6..a35af8f 100644 --- a/src/logic/define/sqlSchemaGenerator/defineSqlSchemaGeneratorCodeForDomainObject.test.ts +++ b/src/logic/define/sqlSchemaGenerator/defineSqlSchemaGeneratorCodeForDomainObject.test.ts @@ -25,6 +25,8 @@ describe('defineSqlSchemaGeneratorCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -104,6 +106,8 @@ describe('defineSqlSchemaGeneratorCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: ['uuid'], updatable: [ 'homeStation', @@ -177,7 +181,12 @@ describe('defineSqlSchemaGeneratorCodeForDomainObject', () => { type: DomainObjectPropertyType.NUMBER, }, }, - decorations: { unique: null, updatable: null }, + decorations: { + alias: null, + primary: null, + unique: null, + updatable: null, + }, }); const sqlSchemaRelationship = defineSqlSchemaRelationshipForDomainObject({ domainObject, @@ -236,6 +245,8 @@ describe('defineSqlSchemaGeneratorCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: ['cin'], updatable: [], }, @@ -297,6 +308,8 @@ describe('defineSqlSchemaGeneratorCodeForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: ['trainUuid', 'occurredAt'], updatable: [], }, diff --git a/src/logic/define/sqlSchemaRelationship/assertDomainObjectIsSafeToHaveSqlSchema.test.ts b/src/logic/define/sqlSchemaRelationship/assertDomainObjectIsSafeToHaveSqlSchema.test.ts index a19487c..1736fd4 100644 --- a/src/logic/define/sqlSchemaRelationship/assertDomainObjectIsSafeToHaveSqlSchema.test.ts +++ b/src/logic/define/sqlSchemaRelationship/assertDomainObjectIsSafeToHaveSqlSchema.test.ts @@ -15,6 +15,8 @@ describe('assertDomainObjectIsSafeToHaveSqlSchema', () => { extends: DomainObjectVariant.DOMAIN_LITERAL, properties: {}, decorations: { + alias: null, + primary: null, unique: ['latitude'], updatable: null, }, @@ -35,6 +37,8 @@ describe('assertDomainObjectIsSafeToHaveSqlSchema', () => { extends: DomainObjectVariant.DOMAIN_LITERAL, properties: {}, decorations: { + alias: null, + primary: null, unique: null, updatable: ['latitude'], }, @@ -57,6 +61,8 @@ describe('assertDomainObjectIsSafeToHaveSqlSchema', () => { extends: DomainObjectVariant.DOMAIN_ENTITY, properties: {}, decorations: { + alias: null, + primary: null, unique: null, updatable: [], }, @@ -77,6 +83,8 @@ describe('assertDomainObjectIsSafeToHaveSqlSchema', () => { extends: DomainObjectVariant.DOMAIN_ENTITY, properties: {}, decorations: { + alias: null, + primary: null, unique: ['cin'], updatable: null, }, @@ -99,6 +107,8 @@ describe('assertDomainObjectIsSafeToHaveSqlSchema', () => { extends: DomainObjectVariant.DOMAIN_EVENT, properties: {}, decorations: { + alias: null, + primary: null, unique: null, updatable: [], }, diff --git a/src/logic/define/sqlSchemaRelationship/defineSqlSchemaPropertyForDomainObjectProperty.test.ts b/src/logic/define/sqlSchemaRelationship/defineSqlSchemaPropertyForDomainObjectProperty.test.ts index 799acdf..72c08a4 100644 --- a/src/logic/define/sqlSchemaRelationship/defineSqlSchemaPropertyForDomainObjectProperty.test.ts +++ b/src/logic/define/sqlSchemaRelationship/defineSqlSchemaPropertyForDomainObjectProperty.test.ts @@ -175,6 +175,8 @@ describe('defineSqlSchemaPropertyForDomainObjectProperty', () => { domainObject: { ...createExampleDomainObjectMetadata(), decorations: { + alias: null, + primary: null, updatable: ['socialSecurityNumberHash'], unique: null, }, diff --git a/src/logic/define/sqlSchemaRelationship/defineSqlSchemaRelationshipForDomainObject.test.ts b/src/logic/define/sqlSchemaRelationship/defineSqlSchemaRelationshipForDomainObject.test.ts index 727dbc6..9712c9d 100644 --- a/src/logic/define/sqlSchemaRelationship/defineSqlSchemaRelationshipForDomainObject.test.ts +++ b/src/logic/define/sqlSchemaRelationship/defineSqlSchemaRelationshipForDomainObject.test.ts @@ -23,6 +23,8 @@ describe('defineSqlSchemarelationshipForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -53,6 +55,8 @@ describe('defineSqlSchemarelationshipForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: null, updatable: null, }, @@ -110,6 +114,8 @@ describe('defineSqlSchemarelationshipForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: ['cin'], updatable: ['capacity'], }, @@ -153,6 +159,8 @@ describe('defineSqlSchemarelationshipForDomainObject', () => { }, }, decorations: { + alias: null, + primary: null, unique: ['trainUuid', 'occurredAt'], updatable: [], },