From e1d97c62f417d9a3fa777b8daccb1d0fe08d12c8 Mon Sep 17 00:00:00 2001 From: Ulad Kasach Date: Sun, 9 Jun 2024 19:27:48 -0400 Subject: [PATCH] feat(context): generate dao with separate context parameter per procedure pattern --- package.json | 3 +- .../commands/generate.integration.test.ts | 785 ++++++++------- .../readConfig.integration.test.ts.snap | 66 ++ ...atasFromConfigCriteria.integration.test.ts | 6 +- .../getAllPathsMatchingGlobs.test.ts | 2 +- .../getConfig/readConfig.integration.test.ts | 2 +- ...neDaoCodeFilesForDomainObject.test.ts.snap | 127 +-- ...sForDomainObjects.integration.test.ts.snap | 937 +++++++++++------- ...ndByMethodCodeForDomainObject.test.ts.snap | 292 +++--- ...sertMethodCodeForDomainObject.test.ts.snap | 91 +- ...DaoFindByMethodCodeForDomainObject.test.ts | 107 +- ...efineDaoFindByMethodCodeForDomainObject.ts | 21 +- ...DaoUpsertMethodCodeForDomainObject.test.ts | 81 +- ...efineDaoUpsertMethodCodeForDomainObject.ts | 17 +- ...tExpressionForDomainObjectProperty.test.ts | 6 +- ...nInputExpressionForDomainObjectProperty.ts | 12 +- ...sForDomainObjects.integration.test.ts.snap | 4 +- ...sForDomainObjects.integration.test.ts.snap | 24 +- ...psForDomainObject.integration.test.ts.snap | 164 +++ 19 files changed, 1699 insertions(+), 1048 deletions(-) diff --git a/package.json b/package.json index 1f19d16..27d258a 100644 --- a/package.json +++ b/package.json @@ -49,10 +49,11 @@ "build:clean": "rm dist/ -rf", "build:compile": "tsc -p ./tsconfig.build.json", "build": "npm run build:clean && npm run build:compile", + "provision:docker:clear": "docker rm -f $(docker ps -a -f 'publish=7821' -q) 2>/dev/null || true && echo 'ensured port is available 👍'", "provision:docker:up": "docker-compose -f ./provision/docker/integration_test_db/docker-compose.yml up -d --force-recreate --build --renew-anon-volumes", "provision:docker:await": "docker-compose -f ./provision/docker/integration_test_db/docker-compose.yml exec -T postgres /root/wait-for-postgres.sh", "provision:docker:down": "docker-compose -f ./provision/docker/integration_test_db/docker-compose.yml down", - "provision:integration-test-db": "npm run provision:docker:up && npm run provision:docker:await && npm run provision:docker:extensions", + "provision:integration-test-db": "npm run provision:docker:clear && npm run provision:docker:up && npm run provision:docker:await && npm run provision:docker:extensions", "test:commits": "LAST_TAG=$(git describe --tags --abbrev=0 @^ 2> /dev/null || git rev-list --max-parents=0 HEAD) && npx commitlint --from $LAST_TAG --to HEAD --verbose", "test:types": "tsc -p ./tsconfig.build.json --noEmit", "test:format:prettier": "prettier --parser typescript --check 'src/**/*.ts' --config ./prettier.config.js", diff --git a/src/logic/commands/generate.integration.test.ts b/src/logic/commands/generate.integration.test.ts index d2b6200..4e74ac7 100644 --- a/src/logic/commands/generate.integration.test.ts +++ b/src/logic/commands/generate.integration.test.ts @@ -75,58 +75,72 @@ describe('generate', () => { latitude: 8, longitude: 21, }); - const upsertedGeocode = await geocodeDao.upsert({ - dbConnection, - geocode, - }); + const upsertedGeocode = await geocodeDao.upsert( + { + geocode, + }, + { dbConnection }, + ); // console.log(upsertedGeocode); expect(upsertedGeocode).toMatchObject(geocode); expect(upsertedGeocode).toHaveProperty('id', expect.any(Number)); }); it('should return the same new literal if the properties are equivalent', async () => { - const geocodeA = await geocodeDao.upsert({ - dbConnection, - geocode: new Geocode({ - latitude: 8, - longitude: 21, - }), - }); - const geocodeB = await geocodeDao.upsert({ - dbConnection, - geocode: new Geocode({ - latitude: 8, - longitude: 21, - }), - }); + const geocodeA = await geocodeDao.upsert( + { + geocode: new Geocode({ + latitude: 8, + longitude: 21, + }), + }, + { dbConnection }, + ); + const geocodeB = await geocodeDao.upsert( + { + geocode: new Geocode({ + latitude: 8, + longitude: 21, + }), + }, + { dbConnection }, + ); expect(geocodeB).toEqual(geocodeA); // should be the same exact one }); it('should be able to find by id', async () => { - const geocode = await geocodeDao.upsert({ - dbConnection, - geocode: new Geocode({ - latitude: 8, - longitude: 21, - }), - }); - const foundGeocode = await geocodeDao.findById({ - dbConnection, - id: geocode.id, - }); + const geocode = await geocodeDao.upsert( + { + geocode: new Geocode({ + latitude: 8, + longitude: 21, + }), + }, + { dbConnection }, + ); + const foundGeocode = await geocodeDao.findById( + { + id: geocode.id, + }, + { dbConnection }, + ); expect(foundGeocode).toEqual(geocode); // should find the same one }); it('should be able to find by unique', async () => { - const geocode = await geocodeDao.upsert({ - dbConnection, - geocode: new Geocode({ - latitude: 8, - longitude: 21, - }), - }); - const foundGeocode = await geocodeDao.findByUnique({ - dbConnection, - latitude: geocode.latitude, - longitude: geocode.longitude, - }); + const geocode = await geocodeDao.upsert( + { + geocode: new Geocode({ + latitude: 8, + longitude: 21, + }), + }, + { dbConnection }, + ); + const foundGeocode = await geocodeDao.findByUnique( + { + latitude: geocode.latitude, + longitude: geocode.longitude, + }, + { dbConnection }, + ); expect(foundGeocode).toEqual(geocode); // should find the same one }); }); @@ -138,10 +152,12 @@ describe('generate', () => { capacity: 9001, milage: 1_700_000, }); - const upsertedLocomotive = await locomotiveDao.upsert({ - dbConnection, - locomotive, - }); + const upsertedLocomotive = await locomotiveDao.upsert( + { + locomotive, + }, + { dbConnection }, + ); // console.log(upsertedGeocode); expect(upsertedLocomotive).toMatchObject(locomotive); expect(upsertedLocomotive).toHaveProperty('id', expect.any(Number)); @@ -160,72 +176,88 @@ describe('generate', () => { ); }); it('should return the same entity if the unique property is the same', async () => { - const locomotive = await locomotiveDao.upsert({ - dbConnection, - locomotive: new Locomotive({ - ein: 'l821', - fuel: LocomotiveFuel.FISSION, - capacity: 9001, - milage: 1_700_000, - }), - }); - const locomotiveNow = await locomotiveDao.upsert({ - dbConnection, - locomotive: new Locomotive({ - ...locomotive, - milage: 1_900_000, - }), - }); + const locomotive = await locomotiveDao.upsert( + { + locomotive: new Locomotive({ + ein: 'l821', + fuel: LocomotiveFuel.FISSION, + capacity: 9001, + milage: 1_700_000, + }), + }, + { dbConnection }, + ); + const locomotiveNow = await locomotiveDao.upsert( + { + locomotive: new Locomotive({ + ...locomotive, + milage: 1_900_000, + }), + }, + { dbConnection }, + ); // check that its the same locomotive expect(locomotiveNow.id).toEqual(locomotive.id); }); it('should be able to find by id', async () => { - const locomotive = await locomotiveDao.upsert({ - dbConnection, - locomotive: new Locomotive({ - ein: 'l821', - fuel: LocomotiveFuel.FISSION, - capacity: 9001, - milage: 1_700_000, - }), - }); - const foundLocomotive = await locomotiveDao.findById({ - dbConnection, - id: locomotive.id, - }); + const locomotive = await locomotiveDao.upsert( + { + locomotive: new Locomotive({ + ein: 'l821', + fuel: LocomotiveFuel.FISSION, + capacity: 9001, + milage: 1_700_000, + }), + }, + { dbConnection }, + ); + const foundLocomotive = await locomotiveDao.findById( + { + id: locomotive.id, + }, + { dbConnection }, + ); expect(foundLocomotive).toMatchObject(locomotive); }); it('should be able to find by uuid', async () => { - const locomotive = await locomotiveDao.upsert({ - dbConnection, - locomotive: new Locomotive({ - ein: 'l821', - fuel: LocomotiveFuel.FISSION, - capacity: 9001, - milage: 1_700_000, - }), - }); - const foundLocomotive = await locomotiveDao.findByUuid({ - dbConnection, - uuid: locomotive.uuid, - }); + const locomotive = await locomotiveDao.upsert( + { + locomotive: new Locomotive({ + ein: 'l821', + fuel: LocomotiveFuel.FISSION, + capacity: 9001, + milage: 1_700_000, + }), + }, + { dbConnection }, + ); + const foundLocomotive = await locomotiveDao.findByUuid( + { + uuid: locomotive.uuid, + }, + { dbConnection }, + ); expect(foundLocomotive).toMatchObject(locomotive); }); it('should be able to find by unique', async () => { - const locomotive = await locomotiveDao.upsert({ - dbConnection, - locomotive: new Locomotive({ - ein: 'l821', - fuel: LocomotiveFuel.FISSION, - capacity: 9001, - milage: 1_700_000, - }), - }); - const foundLocomotive = await locomotiveDao.findByUnique({ - dbConnection, - ein: locomotive.ein, - }); + const locomotive = await locomotiveDao.upsert( + { + locomotive: new Locomotive({ + ein: 'l821', + fuel: LocomotiveFuel.FISSION, + capacity: 9001, + milage: 1_700_000, + }), + }, + { dbConnection }, + ); + const foundLocomotive = await locomotiveDao.findByUnique( + { + ein: locomotive.ein, + }, + { dbConnection }, + ); expect(foundLocomotive).toMatchObject(locomotive); }); }); @@ -237,82 +269,100 @@ describe('generate', () => { carries: CarriagePurpose.FREIGHT, capacity: 821, }) as HasMetadata; - const upsertedCarriage = await carriageDao.upsert({ - dbConnection, - carriage, - }); + const upsertedCarriage = await carriageDao.upsert( + { + carriage, + }, + { dbConnection }, + ); // console.log(upsertedGeocode); expect(upsertedCarriage).toMatchObject(carriage); expect(upsertedCarriage).toHaveProperty('id', expect.any(Number)); expect(upsertedCarriage).toHaveProperty('uuid', expect.any(String)); }); it('should return the same entity if the unique property is the same', async () => { - const carriage = await carriageDao.upsert({ - dbConnection, - carriage: new Carriage({ - uuid: uuid(), - cin: 'carriage-to-test', - carries: CarriagePurpose.FREIGHT, - capacity: 821, - }) as HasMetadata, - }); - const carriageNow = await carriageDao.upsert({ - dbConnection, - carriage: new Carriage({ - ...carriage, - capacity: 721, - }) as HasMetadata, - }); + const carriage = await carriageDao.upsert( + { + carriage: new Carriage({ + uuid: uuid(), + cin: 'carriage-to-test', + carries: CarriagePurpose.FREIGHT, + capacity: 821, + }) as HasMetadata, + }, + { dbConnection }, + ); + const carriageNow = await carriageDao.upsert( + { + carriage: new Carriage({ + ...carriage, + capacity: 721, + }) as HasMetadata, + }, + { dbConnection }, + ); // check that its the same locomotive expect(carriageNow.id).toEqual(carriage.id); }); it('should be able to find by id', async () => { - const carriage = await carriageDao.upsert({ - dbConnection, - carriage: new Carriage({ - uuid: uuid(), - cin: 'carriage-to-test', - carries: CarriagePurpose.FREIGHT, - capacity: 821, - }) as HasMetadata, - }); - const foundCarriage = await carriageDao.findById({ - dbConnection, - id: carriage.id, - }); + const carriage = await carriageDao.upsert( + { + carriage: new Carriage({ + uuid: uuid(), + cin: 'carriage-to-test', + carries: CarriagePurpose.FREIGHT, + capacity: 821, + }) as HasMetadata, + }, + { dbConnection }, + ); + const foundCarriage = await carriageDao.findById( + { + id: carriage.id, + }, + { dbConnection }, + ); expect(foundCarriage).toEqual(carriage); }); it('should be able to find by uuid', async () => { - const carriage = await carriageDao.upsert({ - dbConnection, - carriage: new Carriage({ - uuid: uuid(), - cin: 'carriage-to-test', - carries: CarriagePurpose.FREIGHT, - capacity: 821, - }) as HasMetadata, - }); - const foundCarriage = await carriageDao.findByUuid({ - dbConnection, - uuid: carriage.uuid, - }); + const carriage = await carriageDao.upsert( + { + carriage: new Carriage({ + uuid: uuid(), + cin: 'carriage-to-test', + carries: CarriagePurpose.FREIGHT, + capacity: 821, + }) as HasMetadata, + }, + { dbConnection }, + ); + const foundCarriage = await carriageDao.findByUuid( + { + uuid: carriage.uuid, + }, + { dbConnection }, + ); expect(foundCarriage).toEqual(carriage); }); it('should be able to find by unique', async () => { - const carriage = await carriageDao.upsert({ - dbConnection, - carriage: new Carriage({ - uuid: uuid(), - cin: 'carriage-to-test', - carries: CarriagePurpose.FREIGHT, - capacity: 821, - }) as HasMetadata, - }); - const foundCarriage = await carriageDao.findByUnique({ - dbConnection, - uuid: carriage.uuid, - }); + const carriage = await carriageDao.upsert( + { + carriage: new Carriage({ + uuid: uuid(), + cin: 'carriage-to-test', + carries: CarriagePurpose.FREIGHT, + capacity: 821, + }) as HasMetadata, + }, + { dbConnection }, + ); + const foundCarriage = await carriageDao.findByUnique( + { + uuid: carriage.uuid, + }, + { dbConnection }, + ); expect(foundCarriage).toEqual(carriage); }); }); @@ -323,47 +373,55 @@ describe('generate', () => { let leadEngineer: HasMetadata; beforeAll(async () => { // upsert the composed entities - locomotive = await locomotiveDao.upsert({ - dbConnection, - locomotive: new Locomotive({ - ein: 'l821', - fuel: LocomotiveFuel.FISSION, - capacity: 9001, - milage: 1_700_000, - }), - }); - boosterCarriage = await carriageDao.upsert({ - dbConnection, - carriage: new Carriage({ - uuid: uuid(), - cin: 'booster-transport-1', - carries: CarriagePurpose.FREIGHT, - capacity: 9001, - }) as HasMetadata, - }); - crewCarriage = await carriageDao.upsert({ - dbConnection, - carriage: new Carriage({ - uuid: uuid(), - cin: 'crew-transport-1', - carries: CarriagePurpose.PASSENGER, - capacity: 19, - }) as HasMetadata, - }); - leadEngineer = await trainEngineerDao.upsert({ - dbConnection, - trainEngineer: new TrainEngineer({ - socialSecurityNumberHash: 'x13!y^a821kx(*12', - certificates: [ - new Certificate({ - type: CertificateType.LOCOMOTIVE_DRIVING, - industryId: 'super-train-driving-deluxe-experience', - }), - ], - licenseUuids: [uuid()], - name: 'Burt', - }), - }); + locomotive = await locomotiveDao.upsert( + { + locomotive: new Locomotive({ + ein: 'l821', + fuel: LocomotiveFuel.FISSION, + capacity: 9001, + milage: 1_700_000, + }), + }, + { dbConnection }, + ); + boosterCarriage = await carriageDao.upsert( + { + carriage: new Carriage({ + uuid: uuid(), + cin: 'booster-transport-1', + carries: CarriagePurpose.FREIGHT, + capacity: 9001, + }) as HasMetadata, + }, + { dbConnection }, + ); + crewCarriage = await carriageDao.upsert( + { + carriage: new Carriage({ + uuid: uuid(), + cin: 'crew-transport-1', + carries: CarriagePurpose.PASSENGER, + capacity: 19, + }) as HasMetadata, + }, + { dbConnection }, + ); + leadEngineer = await trainEngineerDao.upsert( + { + trainEngineer: new TrainEngineer({ + socialSecurityNumberHash: 'x13!y^a821kx(*12', + certificates: [ + new Certificate({ + type: CertificateType.LOCOMOTIVE_DRIVING, + industryId: 'super-train-driving-deluxe-experience', + }), + ], + licenseUuids: [uuid()], + name: 'Burt', + }), + }, + { dbConnection }, + ); }); it('should be able to upsert', async () => { // define the train @@ -378,7 +436,10 @@ describe('generate', () => { }); // upsert it - const upsertedTrain = await trainDao.upsert({ dbConnection, train }); + const upsertedTrain = await trainDao.upsert( + { train }, + { dbConnection }, + ); expect(upsertedTrain).toMatchObject(train); expect(upsertedTrain).toHaveProperty('id', expect.any(Number)); expect(upsertedTrain).toHaveProperty('uuid', expect.any(String)); @@ -398,77 +459,89 @@ describe('generate', () => { }); it('should be able to find by id', async () => { // upsert it - const train = await trainDao.upsert({ - dbConnection, - train: new Train({ - homeStationGeocode: new Geocode({ - latitude: 7, - longitude: 21, + const train = await trainDao.upsert( + { + train: new Train({ + homeStationGeocode: new Geocode({ + latitude: 7, + longitude: 21, + }), + combinationId: `ice-launch-express-${uuid()}`, + locomotiveUuids: [locomotive.uuid], + carriageUuids: [boosterCarriage.uuid, crewCarriage.uuid], + engineerUuids: [leadEngineer.uuid], + leadEngineerUuid: leadEngineer.uuid, + status: TrainStatus.ASSEMBLED, }), - combinationId: `ice-launch-express-${uuid()}`, - locomotiveUuids: [locomotive.uuid], - carriageUuids: [boosterCarriage.uuid, crewCarriage.uuid], - engineerUuids: [leadEngineer.uuid], - leadEngineerUuid: leadEngineer.uuid, - status: TrainStatus.ASSEMBLED, - }), - }); + }, + { dbConnection }, + ); // now find it - const foundTrain = await trainDao.findById({ - dbConnection, - id: train.id, - }); + const foundTrain = await trainDao.findById( + { + id: train.id, + }, + { dbConnection }, + ); expect(foundTrain).toMatchObject(train); }); it('should be able to find by uuid', async () => { // upsert it - const train = await trainDao.upsert({ - dbConnection, - train: new Train({ - homeStationGeocode: new Geocode({ - latitude: 7, - longitude: 21, + const train = await trainDao.upsert( + { + train: new Train({ + homeStationGeocode: new Geocode({ + latitude: 7, + longitude: 21, + }), + combinationId: `ice-launch-express-${uuid()}`, + locomotiveUuids: [locomotive.uuid], + carriageUuids: [boosterCarriage.uuid, crewCarriage.uuid], + engineerUuids: [leadEngineer.uuid], + leadEngineerUuid: leadEngineer.uuid, + status: TrainStatus.ASSEMBLED, }), - combinationId: `ice-launch-express-${uuid()}`, - locomotiveUuids: [locomotive.uuid], - carriageUuids: [boosterCarriage.uuid, crewCarriage.uuid], - engineerUuids: [leadEngineer.uuid], - leadEngineerUuid: leadEngineer.uuid, - status: TrainStatus.ASSEMBLED, - }), - }); + }, + { dbConnection }, + ); // now find it - const foundTrain = await trainDao.findByUuid({ - dbConnection, - uuid: train.uuid, - }); + const foundTrain = await trainDao.findByUuid( + { + uuid: train.uuid, + }, + { dbConnection }, + ); expect(foundTrain).toMatchObject(train); }); it('should be able to find by unique', async () => { // upsert it - const train = await trainDao.upsert({ - dbConnection, - train: new Train({ - homeStationGeocode: new Geocode({ - latitude: 7, - longitude: 21, + const train = await trainDao.upsert( + { + train: new Train({ + homeStationGeocode: new Geocode({ + latitude: 7, + longitude: 21, + }), + combinationId: `ice-launch-express-${uuid()}`, + locomotiveUuids: [locomotive.uuid], + carriageUuids: [boosterCarriage.uuid, crewCarriage.uuid], + engineerUuids: [leadEngineer.uuid], + leadEngineerUuid: leadEngineer.uuid, + status: TrainStatus.ASSEMBLED, }), - combinationId: `ice-launch-express-${uuid()}`, - locomotiveUuids: [locomotive.uuid], - carriageUuids: [boosterCarriage.uuid, crewCarriage.uuid], - engineerUuids: [leadEngineer.uuid], - leadEngineerUuid: leadEngineer.uuid, - status: TrainStatus.ASSEMBLED, - }), - }); + }, + { dbConnection }, + ); // now find it - const foundTrain = await trainDao.findByUnique({ - dbConnection, - combinationId: train.combinationId, - }); + const foundTrain = await trainDao.findByUnique( + { + combinationId: train.combinationId, + }, + { dbConnection }, + ); expect(foundTrain).toMatchObject(train); }); }); @@ -504,132 +577,146 @@ describe('generate', () => { }); // upsert it - const upsertedInvoice = await invoiceDao.upsert({ - dbConnection, - invoice, - }); + const upsertedInvoice = await invoiceDao.upsert( + { + invoice, + }, + { dbConnection }, + ); expect(upsertedInvoice).toMatchObject(invoice); expect(upsertedInvoice).toHaveProperty('id', expect.any(Number)); expect(upsertedInvoice).toHaveProperty('uuid', expect.any(String)); }); it('should be able to find by id', async () => { // upsert it - const invoice = await invoiceDao.upsert({ - dbConnection, - invoice: new Invoice({ - externalId: `cnc-machine:${uuid()}`, - items: [ - new InvoiceLineItem({ - price: new Price({ - amount: 72100, - currency: SvcPaymentsPaymentTransactionCurrency.USD, + const invoice = await invoiceDao.upsert( + { + invoice: new Invoice({ + externalId: `cnc-machine:${uuid()}`, + items: [ + new InvoiceLineItem({ + price: new Price({ + amount: 72100, + currency: SvcPaymentsPaymentTransactionCurrency.USD, + }), + title: 'Open Source CNC Machine', + explanation: 'This is an Open Source CNC Machine.', }), - title: 'Open Source CNC Machine', - explanation: 'This is an Open Source CNC Machine.', - }), - new InvoiceLineItem({ - price: new Price({ - amount: 8100, - currency: SvcPaymentsPaymentTransactionCurrency.USD, + new InvoiceLineItem({ + price: new Price({ + amount: 8100, + currency: SvcPaymentsPaymentTransactionCurrency.USD, + }), + title: 'Laser Cutter Adapter', + explanation: + 'This is the Laser Cutting adapter for the Open Source CNC Machine.', }), - title: 'Laser Cutter Adapter', - explanation: - 'This is the Laser Cutting adapter for the Open Source CNC Machine.', + ], + totalPrice: new Price({ + amount: 72100 + 8100, + currency: SvcPaymentsPaymentTransactionCurrency.USD, }), - ], - totalPrice: new Price({ - amount: 72100 + 8100, - currency: SvcPaymentsPaymentTransactionCurrency.USD, + status: InvoiceStatus.PROPOSED, }), - status: InvoiceStatus.PROPOSED, - }), - }); + }, + { dbConnection }, + ); // now find it - const foundInvoice = await invoiceDao.findById({ - dbConnection, - id: invoice.id, - }); + const foundInvoice = await invoiceDao.findById( + { + id: invoice.id, + }, + { dbConnection }, + ); expect(foundInvoice).toMatchObject(invoice); }); it('should be able to find by uuid', async () => { // upsert it - const invoice = await invoiceDao.upsert({ - dbConnection, - invoice: new Invoice({ - externalId: `cnc-machine:${uuid()}`, - items: [ - new InvoiceLineItem({ - price: new Price({ - amount: 72100, - currency: SvcPaymentsPaymentTransactionCurrency.USD, + const invoice = await invoiceDao.upsert( + { + invoice: new Invoice({ + externalId: `cnc-machine:${uuid()}`, + items: [ + new InvoiceLineItem({ + price: new Price({ + amount: 72100, + currency: SvcPaymentsPaymentTransactionCurrency.USD, + }), + title: 'Open Source CNC Machine', + explanation: 'This is an Open Source CNC Machine.', }), - title: 'Open Source CNC Machine', - explanation: 'This is an Open Source CNC Machine.', - }), - new InvoiceLineItem({ - price: new Price({ - amount: 8100, - currency: SvcPaymentsPaymentTransactionCurrency.USD, + new InvoiceLineItem({ + price: new Price({ + amount: 8100, + currency: SvcPaymentsPaymentTransactionCurrency.USD, + }), + title: 'Laser Cutter Adapter', + explanation: + 'This is the Laser Cutting adapter for the Open Source CNC Machine.', }), - title: 'Laser Cutter Adapter', - explanation: - 'This is the Laser Cutting adapter for the Open Source CNC Machine.', + ], + totalPrice: new Price({ + amount: 72100 + 8100, + currency: SvcPaymentsPaymentTransactionCurrency.USD, }), - ], - totalPrice: new Price({ - amount: 72100 + 8100, - currency: SvcPaymentsPaymentTransactionCurrency.USD, + status: InvoiceStatus.PROPOSED, }), - status: InvoiceStatus.PROPOSED, - }), - }); + }, + { dbConnection }, + ); // now find it - const foundInvoice = await invoiceDao.findByUuid({ - dbConnection, - uuid: invoice.uuid, - }); + const foundInvoice = await invoiceDao.findByUuid( + { + uuid: invoice.uuid, + }, + { dbConnection }, + ); expect(foundInvoice).toMatchObject(invoice); }); it('should be able to find by unique', async () => { // upsert it - const invoice = await invoiceDao.upsert({ - dbConnection, - invoice: new Invoice({ - externalId: `cnc-machine:${uuid()}`, - items: [ - new InvoiceLineItem({ - price: new Price({ - amount: 72100, - currency: SvcPaymentsPaymentTransactionCurrency.USD, + const invoice = await invoiceDao.upsert( + { + invoice: new Invoice({ + externalId: `cnc-machine:${uuid()}`, + items: [ + new InvoiceLineItem({ + price: new Price({ + amount: 72100, + currency: SvcPaymentsPaymentTransactionCurrency.USD, + }), + title: 'Open Source CNC Machine', + explanation: 'This is an Open Source CNC Machine.', }), - title: 'Open Source CNC Machine', - explanation: 'This is an Open Source CNC Machine.', - }), - new InvoiceLineItem({ - price: new Price({ - amount: 8100, - currency: SvcPaymentsPaymentTransactionCurrency.USD, + new InvoiceLineItem({ + price: new Price({ + amount: 8100, + currency: SvcPaymentsPaymentTransactionCurrency.USD, + }), + title: 'Laser Cutter Adapter', + explanation: + 'This is the Laser Cutting adapter for the Open Source CNC Machine.', }), - title: 'Laser Cutter Adapter', - explanation: - 'This is the Laser Cutting adapter for the Open Source CNC Machine.', + ], + totalPrice: new Price({ + amount: 72100 + 8100, + currency: SvcPaymentsPaymentTransactionCurrency.USD, }), - ], - totalPrice: new Price({ - amount: 72100 + 8100, - currency: SvcPaymentsPaymentTransactionCurrency.USD, + status: InvoiceStatus.PROPOSED, }), - status: InvoiceStatus.PROPOSED, - }), - }); + }, + { dbConnection }, + ); // now find it - const foundInvoice = await invoiceDao.findByUnique({ - dbConnection, - externalId: invoice.externalId, - }); + const foundInvoice = await invoiceDao.findByUnique( + { + externalId: invoice.externalId, + }, + { dbConnection }, + ); expect(foundInvoice).toMatchObject(invoice); }); }); 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 2337ad2..47476b4 100644 --- a/src/logic/config/getConfig/__snapshots__/readConfig.integration.test.ts.snap +++ b/src/logic/config/getConfig/__snapshots__/readConfig.integration.test.ts.snap @@ -5,6 +5,72 @@ exports[`readConfig should be able to read the example config provisioned in __t "dialect": "10.7", "for": { "objects": [ + DomainObjectMetadata { + "decorations": { + "unique": [ + "stationUuid", + "trainLocatedEventUuid", + ], + "updatable": [ + "status", + ], + }, + "extends": "DomainEntity", + "name": "AsyncTaskPredictStationCongestion", + "properties": { + "createdAt": DomainObjectPropertyMetadata { + "name": "createdAt", + "nullable": false, + "required": false, + "type": "DATE", + }, + "id": DomainObjectPropertyMetadata { + "name": "id", + "nullable": false, + "required": false, + "type": "NUMBER", + }, + "stationUuid": DomainObjectPropertyMetadata { + "name": "stationUuid", + "nullable": false, + "required": true, + "type": "STRING", + }, + "status": DomainObjectPropertyMetadata { + "name": "status", + "nullable": false, + "of": [ + "HALTED", + "SCHEDULED", + "QUEUED", + "ATTEMPTED", + "FULFILLED", + "FAILED", + "CANCELED", + ], + "required": true, + "type": "ENUM", + }, + "trainLocatedEventUuid": DomainObjectPropertyMetadata { + "name": "trainLocatedEventUuid", + "nullable": false, + "required": true, + "type": "STRING", + }, + "updatedAt": DomainObjectPropertyMetadata { + "name": "updatedAt", + "nullable": false, + "required": false, + "type": "DATE", + }, + "uuid": DomainObjectPropertyMetadata { + "name": "uuid", + "nullable": false, + "required": false, + "type": "STRING", + }, + }, + }, DomainObjectMetadata { "decorations": { "unique": [ diff --git a/src/logic/config/getConfig/extractDomainObjectMetadatasFromConfigCriteria.integration.test.ts b/src/logic/config/getConfig/extractDomainObjectMetadatasFromConfigCriteria.integration.test.ts index bf94a79..fd81c48 100644 --- a/src/logic/config/getConfig/extractDomainObjectMetadatasFromConfigCriteria.integration.test.ts +++ b/src/logic/config/getConfig/extractDomainObjectMetadatasFromConfigCriteria.integration.test.ts @@ -10,7 +10,7 @@ describe('extractDomainObjectMetadatasFromConfigCriteria', () => { exclude: null, }); // console.log(JSON.stringify(metadatas, null, 2)); - expect(metadatas.length).toEqual(11); + expect(metadatas.length).toEqual(12); }); it('should find all of the domain objects findable by all search paths in a directory, with no dupes', async () => { const metadatas = await extractDomainObjectMetadatasFromConfigCriteria({ @@ -27,7 +27,7 @@ describe('extractDomainObjectMetadatasFromConfigCriteria', () => { exclude: null, }); // console.log(JSON.stringify(metadatas, null, 2)); - expect(metadatas.length).toEqual(11); + expect(metadatas.length).toEqual(12); }); it('should only find the domain objects imported from the files in the search paths', async () => { const metadatas = await extractDomainObjectMetadatasFromConfigCriteria({ @@ -55,7 +55,7 @@ describe('extractDomainObjectMetadatasFromConfigCriteria', () => { exclude: ['TrainLocatedEvent'], }); // console.log(JSON.stringify(metadatas, null, 2)); - expect(metadatas.length).toEqual(10); + expect(metadatas.length).toEqual(11); }); it('should only keep the one named in the `include` list, if provided', async () => { const metadatas = await extractDomainObjectMetadatasFromConfigCriteria({ diff --git a/src/logic/config/getConfig/getAllPathsMatchingGlobs.test.ts b/src/logic/config/getConfig/getAllPathsMatchingGlobs.test.ts index 9b1e3b5..7b4e7f7 100644 --- a/src/logic/config/getConfig/getAllPathsMatchingGlobs.test.ts +++ b/src/logic/config/getConfig/getAllPathsMatchingGlobs.test.ts @@ -16,7 +16,7 @@ describe('getAllPathsMatchingGlobs', () => { expect(files).toContain('src/domain/objects/Train.ts'); expect(files).toContain('src/domain/objects/TrainLocatedEvent.ts'); expect(files).toContain('src/domain/objects/Station.ts'); - expect(files.length).toEqual(13); + expect(files.length).toEqual(14); }); it('should return paths that match each glob', async () => { const files = await getAllPathsMatchingGlobs({ diff --git a/src/logic/config/getConfig/readConfig.integration.test.ts b/src/logic/config/getConfig/readConfig.integration.test.ts index 378bee6..a3819c6 100644 --- a/src/logic/config/getConfig/readConfig.integration.test.ts +++ b/src/logic/config/getConfig/readConfig.integration.test.ts @@ -37,7 +37,7 @@ describe('readConfig', () => { }, }, }); - expect(config.for.objects.length).toEqual(10); + expect(config.for.objects.length).toEqual(11); expect(config).toMatchSnapshot({ rootDir: expect.anything() }); // to log an example of the output; ignore the rootDir, to make it machine independent }); }); diff --git a/src/logic/define/databaseAccessObjects/__snapshots__/defineDaoCodeFilesForDomainObject.test.ts.snap b/src/logic/define/databaseAccessObjects/__snapshots__/defineDaoCodeFilesForDomainObject.test.ts.snap index 0eb81a7..e814d0b 100644 --- a/src/logic/define/databaseAccessObjects/__snapshots__/defineDaoCodeFilesForDomainObject.test.ts.snap +++ b/src/logic/define/databaseAccessObjects/__snapshots__/defineDaoCodeFilesForDomainObject.test.ts.snap @@ -56,15 +56,16 @@ export const sql = \` WHERE geocode.id = :id; \`; -export const findById = async ({ - dbConnection, - id, -}: { - dbConnection: DatabaseConnection; - id: number; -}): Promise | null> => { +export const findById = async ( + { + id, + }: { + id: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindGeocodeById({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { id, @@ -99,17 +100,18 @@ export const sql = \` AND geocode.longitude = :longitude; \`; -export const findByUnique = async ({ - dbConnection, - latitude, - longitude, -}: { - dbConnection: DatabaseConnection; - latitude: number; - longitude: number; -}): Promise | null> => { +export const findByUnique = async ( + { + latitude, + longitude, + }: { + latitude: number; + longitude: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindGeocodeByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { latitude, @@ -143,15 +145,16 @@ export const sql = \` WHERE geocode.uuid = :uuid; \`; -export const findByUuid = async ({ - dbConnection, - uuid, -}: { - dbConnection: DatabaseConnection; - uuid: string; -}): Promise | null> => { +export const findByUuid = async ( + { + uuid, + }: { + uuid: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindGeocodeByUuid({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { uuid, @@ -182,15 +185,16 @@ export const sql = \` ) as dgv; \`; -export const upsert = async ({ - dbConnection, - geocode, -}: { - dbConnection: DatabaseConnection; - geocode: Geocode; -}): Promise> => { +export const upsert = async ( + { + geocode, + }: { + geocode: Geocode; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> => { const results = await sqlQueryUpsertGeocode({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { latitude: geocode.latitude, @@ -257,15 +261,16 @@ export const sql = \` WHERE geocode.id = :id; \`; -export const findById = async ({ - dbConnection, - id, -}: { - dbConnection: DatabaseConnection; - id: number; -}): Promise | null> => { +export const findById = async ( + { + id, + }: { + id: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindGeocodeById({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { id, @@ -299,17 +304,18 @@ export const sql = \` AND geocode.longitude = :longitude; \`; -export const findByUnique = async ({ - dbConnection, - latitude, - longitude, -}: { - dbConnection: DatabaseConnection; - latitude: number; - longitude: number; -}): Promise | null> => { +export const findByUnique = async ( + { + latitude, + longitude, + }: { + latitude: number; + longitude: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindGeocodeByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { latitude, @@ -341,15 +347,16 @@ export const sql = \` ) as dgv; \`; -export const upsert = async ({ - dbConnection, - geocode, -}: { - dbConnection: DatabaseConnection; - geocode: Geocode; -}): Promise> => { +export const upsert = async ( + { + geocode, + }: { + geocode: Geocode; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> => { const results = await sqlQueryUpsertGeocode({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { latitude: geocode.latitude, diff --git a/src/logic/define/databaseAccessObjects/__snapshots__/defineDaoCodeFilesForDomainObjects.integration.test.ts.snap b/src/logic/define/databaseAccessObjects/__snapshots__/defineDaoCodeFilesForDomainObjects.integration.test.ts.snap index 597abf8..2958380 100644 --- a/src/logic/define/databaseAccessObjects/__snapshots__/defineDaoCodeFilesForDomainObjects.integration.test.ts.snap +++ b/src/logic/define/databaseAccessObjects/__snapshots__/defineDaoCodeFilesForDomainObjects.integration.test.ts.snap @@ -8,6 +8,242 @@ import { findByUnique } from './findByUnique'; import { findByUuid } from './findByUuid'; import { upsert } from './upsert'; +export const asyncTaskPredictStationCongestionDao = { + findById, + findByUnique, + findByUuid, + upsert, +}; + +// include an alias, for improved ease of access via autocomplete +export const daoAsyncTaskPredictStationCongestion = asyncTaskPredictStationCongestionDao;", + "relpath": "asyncTaskPredictStationCongestionDao/index.ts", + }, + GeneratedCodeFile { + "content": "import { HasMetadata } from 'type-fns'; + +import { AsyncTaskPredictStationCongestion } from '$PATH_TO_DOMAIN_OBJECT'; +import { SqlQueryFindAsyncTaskPredictStationCongestionByIdOutput } from '$PATH_TO_GENERATED_SQL_TYPES'; + +export const castFromDatabaseObject = ( + dbObject: SqlQueryFindAsyncTaskPredictStationCongestionByIdOutput, +): HasMetadata => + new AsyncTaskPredictStationCongestion({ + id: dbObject.id, + uuid: dbObject.uuid, + createdAt: dbObject.created_at, + updatedAt: dbObject.updated_at, + status: dbObject.status as AsyncTaskPredictStationCongestion['status'], + stationUuid: dbObject.station_uuid, + trainLocatedEventUuid: dbObject.train_located_event_uuid, + }) as HasMetadata;", + "relpath": "asyncTaskPredictStationCongestionDao/castFromDatabaseObject.ts", + }, + GeneratedCodeFile { + "content": "import { HasMetadata } from 'type-fns'; + +import { DatabaseConnection } from '$PATH_TO_DATABASE_CONNECTION'; +import { log } from '$PATH_TO_LOG_OBJECT'; +import { AsyncTaskPredictStationCongestion } from '$PATH_TO_DOMAIN_OBJECT'; +import { sqlQueryFindAsyncTaskPredictStationCongestionById } from '$PATH_TO_GENERATED_SQL_QUERY_FUNCTIONS'; +import { castFromDatabaseObject } from './castFromDatabaseObject'; + +export const sql = \` + -- query_name = find_async_task_predict_station_congestion_by_id + SELECT + async_task_predict_station_congestion.id, + async_task_predict_station_congestion.uuid, + async_task_predict_station_congestion.created_at, + async_task_predict_station_congestion.updated_at, + async_task_predict_station_congestion.status, + ( + SELECT train_station.uuid + FROM train_station WHERE train_station.id = async_task_predict_station_congestion.station_id + ) AS station_uuid, + ( + SELECT train_located_event.uuid + FROM train_located_event WHERE train_located_event.id = async_task_predict_station_congestion.train_located_event_id + ) AS train_located_event_uuid + FROM view_async_task_predict_station_congestion_current AS async_task_predict_station_congestion + WHERE async_task_predict_station_congestion.id = :id; +\`; + +export const findById = async ( + { + id, + }: { + id: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { + const results = await sqlQueryFindAsyncTaskPredictStationCongestionById({ + dbExecute: context.dbConnection.query, + logDebug: log.debug, + input: { + id, + }, + }); + const [dbObject, ...moreDbObjects] = results; + if (moreDbObjects.length) throw new Error('expected only one db object for this query'); + if (!dbObject) return null; + return castFromDatabaseObject(dbObject); +};", + "relpath": "asyncTaskPredictStationCongestionDao/findById.ts", + }, + GeneratedCodeFile { + "content": "import { HasMetadata } from 'type-fns'; + +import { DatabaseConnection } from '$PATH_TO_DATABASE_CONNECTION'; +import { log } from '$PATH_TO_LOG_OBJECT'; +import { AsyncTaskPredictStationCongestion } from '$PATH_TO_DOMAIN_OBJECT'; +import { sqlQueryFindAsyncTaskPredictStationCongestionByUnique } from '$PATH_TO_GENERATED_SQL_QUERY_FUNCTIONS'; +import { castFromDatabaseObject } from './castFromDatabaseObject'; + +export const sql = \` + -- query_name = find_async_task_predict_station_congestion_by_unique + SELECT + async_task_predict_station_congestion.id, + async_task_predict_station_congestion.uuid, + async_task_predict_station_congestion.created_at, + async_task_predict_station_congestion.updated_at, + async_task_predict_station_congestion.status, + ( + SELECT train_station.uuid + FROM train_station WHERE train_station.id = async_task_predict_station_congestion.station_id + ) AS station_uuid, + ( + SELECT train_located_event.uuid + FROM train_located_event WHERE train_located_event.id = async_task_predict_station_congestion.train_located_event_id + ) AS train_located_event_uuid + FROM view_async_task_predict_station_congestion_current AS async_task_predict_station_congestion + WHERE 1=1 + AND async_task_predict_station_congestion.station_id = (SELECT id FROM train_station WHERE train_station.uuid = :stationUuid) + AND async_task_predict_station_congestion.train_located_event_id = (SELECT id FROM train_located_event WHERE train_located_event.uuid = :trainLocatedEventUuid); +\`; + +export const findByUnique = async ( + { + stationUuid, + trainLocatedEventUuid, + }: { + stationUuid: string; + trainLocatedEventUuid: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { + const results = await sqlQueryFindAsyncTaskPredictStationCongestionByUnique({ + dbExecute: context.dbConnection.query, + logDebug: log.debug, + input: { + stationUuid, + trainLocatedEventUuid, + }, + }); + const [dbObject, ...moreDbObjects] = results; + if (moreDbObjects.length) throw new Error('expected only one db object for this query'); + if (!dbObject) return null; + return castFromDatabaseObject(dbObject); +};", + "relpath": "asyncTaskPredictStationCongestionDao/findByUnique.ts", + }, + GeneratedCodeFile { + "content": "import { HasMetadata } from 'type-fns'; + +import { DatabaseConnection } from '$PATH_TO_DATABASE_CONNECTION'; +import { log } from '$PATH_TO_LOG_OBJECT'; +import { AsyncTaskPredictStationCongestion } from '$PATH_TO_DOMAIN_OBJECT'; +import { sqlQueryFindAsyncTaskPredictStationCongestionByUuid } from '$PATH_TO_GENERATED_SQL_QUERY_FUNCTIONS'; +import { castFromDatabaseObject } from './castFromDatabaseObject'; + +export const sql = \` + -- query_name = find_async_task_predict_station_congestion_by_uuid + SELECT + async_task_predict_station_congestion.id, + async_task_predict_station_congestion.uuid, + async_task_predict_station_congestion.created_at, + async_task_predict_station_congestion.updated_at, + async_task_predict_station_congestion.status, + ( + SELECT train_station.uuid + FROM train_station WHERE train_station.id = async_task_predict_station_congestion.station_id + ) AS station_uuid, + ( + SELECT train_located_event.uuid + FROM train_located_event WHERE train_located_event.id = async_task_predict_station_congestion.train_located_event_id + ) AS train_located_event_uuid + FROM view_async_task_predict_station_congestion_current AS async_task_predict_station_congestion + WHERE async_task_predict_station_congestion.uuid = :uuid; +\`; + +export const findByUuid = async ( + { + uuid, + }: { + uuid: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { + const results = await sqlQueryFindAsyncTaskPredictStationCongestionByUuid({ + dbExecute: context.dbConnection.query, + logDebug: log.debug, + input: { + uuid, + }, + }); + const [dbObject, ...moreDbObjects] = results; + if (moreDbObjects.length) throw new Error('expected only one db object for this query'); + if (!dbObject) return null; + return castFromDatabaseObject(dbObject); +};", + "relpath": "asyncTaskPredictStationCongestionDao/findByUuid.ts", + }, + GeneratedCodeFile { + "content": "import { HasMetadata } from 'type-fns'; + +import { DatabaseConnection } from '$PATH_TO_DATABASE_CONNECTION'; +import { AsyncTaskPredictStationCongestion } from '$PATH_TO_DOMAIN_OBJECT'; +import { log } from '$PATH_TO_LOG_OBJECT'; +import { sqlQueryUpsertAsyncTaskPredictStationCongestion } from '$PATH_TO_GENERATED_SQL_QUERY_FUNCTIONS'; + +export const sql = \` + -- query_name = upsert_async_task_predict_station_congestion + SELECT + dgv.id, dgv.uuid, dgv.created_at, dgv.updated_at + FROM upsert_async_task_predict_station_congestion( + :status, + (SELECT id FROM train_station WHERE train_station.uuid = :stationUuid), + (SELECT id FROM train_located_event WHERE train_located_event.uuid = :trainLocatedEventUuid) + ) as dgv; +\`; + +export const upsert = async ( + { + asyncTaskPredictStationCongestion, + }: { + asyncTaskPredictStationCongestion: AsyncTaskPredictStationCongestion; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> => { + const results = await sqlQueryUpsertAsyncTaskPredictStationCongestion({ + dbExecute: context.dbConnection.query, + logDebug: log.debug, + input: { + status: asyncTaskPredictStationCongestion.status, + stationUuid: asyncTaskPredictStationCongestion.stationUuid, + trainLocatedEventUuid: asyncTaskPredictStationCongestion.trainLocatedEventUuid, + }, + }); + const { id, uuid, created_at: createdAt, updated_at: updatedAt } = results[0]!; // grab the db generated values + return new AsyncTaskPredictStationCongestion({ ...asyncTaskPredictStationCongestion, id, uuid, createdAt, updatedAt }) as HasMetadata; +};", + "relpath": "asyncTaskPredictStationCongestionDao/upsert.ts", + }, + GeneratedCodeFile { + "content": "import { findById } from './findById'; +import { findByUnique } from './findByUnique'; +import { findByUuid } from './findByUuid'; +import { upsert } from './upsert'; + export const carriageDao = { findById, findByUnique, @@ -58,15 +294,16 @@ export const sql = \` WHERE carriage.id = :id; \`; -export const findById = async ({ - dbConnection, - id, -}: { - dbConnection: DatabaseConnection; - id: number; -}): Promise | null> => { +export const findById = async ( + { + id, + }: { + id: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindCarriageById({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { id, @@ -101,15 +338,16 @@ export const sql = \` AND carriage.uuid = :uuid; \`; -export const findByUnique = async ({ - dbConnection, - uuid, -}: { - dbConnection: DatabaseConnection; - uuid: string; -}): Promise | null> => { +export const findByUnique = async ( + { + uuid, + }: { + uuid: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindCarriageByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { uuid, @@ -143,15 +381,16 @@ export const sql = \` WHERE carriage.uuid = :uuid; \`; -export const findByUuid = async ({ - dbConnection, - uuid, -}: { - dbConnection: DatabaseConnection; - uuid: string; -}): Promise | null> => { +export const findByUuid = async ( + { + uuid, + }: { + uuid: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindCarriageByUuid({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { uuid, @@ -184,15 +423,16 @@ export const sql = \` ) as dgv; \`; -export const upsert = async ({ - dbConnection, - carriage, -}: { - dbConnection: DatabaseConnection; - carriage: HasUuid; -}): Promise> => { +export const upsert = async ( + { + carriage, + }: { + carriage: HasUuid; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> => { const results = await sqlQueryUpsertCarriage({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { uuid: carriage.uuid, @@ -256,15 +496,16 @@ export const sql = \` WHERE certificate.id = :id; \`; -export const findById = async ({ - dbConnection, - id, -}: { - dbConnection: DatabaseConnection; - id: number; -}): Promise | null> => { +export const findById = async ( + { + id, + }: { + id: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindCertificateById({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { id, @@ -298,17 +539,18 @@ export const sql = \` AND certificate.industry_id = :industryId; \`; -export const findByUnique = async ({ - dbConnection, - type, - industryId, -}: { - dbConnection: DatabaseConnection; - type: Certificate['type']; - industryId: string; -}): Promise | null> => { +export const findByUnique = async ( + { + type, + industryId, + }: { + type: Certificate['type']; + industryId: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindCertificateByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { type, @@ -340,15 +582,16 @@ export const sql = \` ) as dgv; \`; -export const upsert = async ({ - dbConnection, - certificate, -}: { - dbConnection: DatabaseConnection; - certificate: Certificate; -}): Promise> => { +export const upsert = async ( + { + certificate, + }: { + certificate: Certificate; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> => { const results = await sqlQueryUpsertCertificate({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { type: certificate.type, @@ -435,15 +678,16 @@ export const sql = \` WHERE train_engineer.id = :id; \`; -export const findById = async ({ - dbConnection, - id, -}: { - dbConnection: DatabaseConnection; - id: number; -}): Promise | null> => { +export const findById = async ( + { + id, + }: { + id: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindTrainEngineerById({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { id, @@ -495,15 +739,16 @@ export const sql = \` AND train_engineer.social_security_number_hash = :socialSecurityNumberHash; \`; -export const findByUnique = async ({ - dbConnection, - socialSecurityNumberHash, -}: { - dbConnection: DatabaseConnection; - socialSecurityNumberHash: string; -}): Promise | null> => { +export const findByUnique = async ( + { + socialSecurityNumberHash, + }: { + socialSecurityNumberHash: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindTrainEngineerByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { socialSecurityNumberHash, @@ -554,15 +799,16 @@ export const sql = \` WHERE train_engineer.uuid = :uuid; \`; -export const findByUuid = async ({ - dbConnection, - uuid, -}: { - dbConnection: DatabaseConnection; - uuid: string; -}): Promise | null> => { +export const findByUuid = async ( + { + uuid, + }: { + uuid: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindTrainEngineerByUuid({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { uuid, @@ -596,19 +842,20 @@ export const sql = \` ) as dgv; \`; -export const upsert = async ({ - dbConnection, - trainEngineer, -}: { - dbConnection: DatabaseConnection; - trainEngineer: TrainEngineer; -}): Promise> => { +export const upsert = async ( + { + trainEngineer, + }: { + trainEngineer: TrainEngineer; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> => { const results = await sqlQueryUpsertTrainEngineer({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { socialSecurityNumberHash: trainEngineer.socialSecurityNumberHash, - certificateIds: await Promise.all(trainEngineer.certificates.map(async (certificate) => certificate.id ? certificate.id : (await certificateDao.upsert({ dbConnection, certificate })).id)), + certificateIds: await Promise.all(trainEngineer.certificates.map(async (certificate) => certificate.id ? certificate.id : (await certificateDao.upsert({ certificate }, context)).id)), licenseUuids: trainEngineer.licenseUuids, name: trainEngineer.name, }, @@ -668,15 +915,16 @@ export const sql = \` WHERE geocode.id = :id; \`; -export const findById = async ({ - dbConnection, - id, -}: { - dbConnection: DatabaseConnection; - id: number; -}): Promise | null> => { +export const findById = async ( + { + id, + }: { + id: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindGeocodeById({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { id, @@ -710,17 +958,18 @@ export const sql = \` AND geocode.longitude = :longitude; \`; -export const findByUnique = async ({ - dbConnection, - latitude, - longitude, -}: { - dbConnection: DatabaseConnection; - latitude: number; - longitude: number; -}): Promise | null> => { +export const findByUnique = async ( + { + latitude, + longitude, + }: { + latitude: number; + longitude: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindGeocodeByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { latitude, @@ -752,15 +1001,16 @@ export const sql = \` ) as dgv; \`; -export const upsert = async ({ - dbConnection, - geocode, -}: { - dbConnection: DatabaseConnection; - geocode: Geocode; -}): Promise> => { +export const upsert = async ( + { + geocode, + }: { + geocode: Geocode; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> => { const results = await sqlQueryUpsertGeocode({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { latitude: geocode.latitude, @@ -865,15 +1115,16 @@ export const sql = \` WHERE train.id = :id; \`; -export const findById = async ({ - dbConnection, - id, -}: { - dbConnection: DatabaseConnection; - id: number; -}): Promise | null> => { +export const findById = async ( + { + id, + }: { + id: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindTrainById({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { id, @@ -940,15 +1191,16 @@ export const sql = \` AND train.combination_id = :combinationId; \`; -export const findByUnique = async ({ - dbConnection, - combinationId, -}: { - dbConnection: DatabaseConnection; - combinationId: string; -}): Promise | null> => { +export const findByUnique = async ( + { + combinationId, + }: { + combinationId: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindTrainByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { combinationId, @@ -1014,15 +1266,16 @@ export const sql = \` WHERE train.uuid = :uuid; \`; -export const findByUuid = async ({ - dbConnection, - uuid, -}: { - dbConnection: DatabaseConnection; - uuid: string; -}): Promise | null> => { +export const findByUuid = async ( + { + uuid, + }: { + uuid: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindTrainByUuid({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { uuid, @@ -1077,18 +1330,19 @@ export const sql = \` ) as dgv; \`; -export const upsert = async ({ - dbConnection, - train, -}: { - dbConnection: DatabaseConnection; - train: Train; -}): Promise> => { +export const upsert = async ( + { + train, + }: { + train: Train; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> => { const results = await sqlQueryUpsertTrain({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { - homeStationGeocodeId: train.homeStationGeocode.id ? train.homeStationGeocode.id : (await geocodeDao.upsert({ dbConnection, geocode: train.homeStationGeocode })).id, + homeStationGeocodeId: train.homeStationGeocode.id ? train.homeStationGeocode.id : (await geocodeDao.upsert({ geocode: train.homeStationGeocode }, context)).id, combinationId: train.combinationId, locomotiveUuids: train.locomotiveUuids, carriageUuids: train.carriageUuids, @@ -1166,15 +1420,16 @@ export const sql = \` WHERE locomotive.id = :id; \`; -export const findById = async ({ - dbConnection, - id, -}: { - dbConnection: DatabaseConnection; - id: number; -}): Promise | null> => { +export const findById = async ( + { + id, + }: { + id: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindLocomotiveById({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { id, @@ -1213,15 +1468,16 @@ export const sql = \` AND locomotive.ein = :ein; \`; -export const findByUnique = async ({ - dbConnection, - ein, -}: { - dbConnection: DatabaseConnection; - ein: string; -}): Promise | null> => { +export const findByUnique = async ( + { + ein, + }: { + ein: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindLocomotiveByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { ein, @@ -1259,15 +1515,16 @@ export const sql = \` WHERE locomotive.uuid = :uuid; \`; -export const findByUuid = async ({ - dbConnection, - uuid, -}: { - dbConnection: DatabaseConnection; - uuid: string; -}): Promise | null> => { +export const findByUuid = async ( + { + uuid, + }: { + uuid: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindLocomotiveByUuid({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { uuid, @@ -1300,15 +1557,16 @@ export const sql = \` ) as dgv; \`; -export const upsert = async ({ - dbConnection, - locomotive, -}: { - dbConnection: DatabaseConnection; - locomotive: Locomotive; -}): Promise> => { +export const upsert = async ( + { + locomotive, + }: { + locomotive: Locomotive; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> => { const results = await sqlQueryUpsertLocomotive({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { ein: locomotive.ein, @@ -1385,15 +1643,16 @@ export const sql = \` WHERE train_located_event.id = :id; \`; -export const findById = async ({ - dbConnection, - id, -}: { - dbConnection: DatabaseConnection; - id: number; -}): Promise | null> => { +export const findById = async ( + { + id, + }: { + id: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindTrainLocatedEventById({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { id, @@ -1438,17 +1697,18 @@ export const sql = \` AND train_located_event.occurred_at = :occurredAt; \`; -export const findByUnique = async ({ - dbConnection, - trainUuid, - occurredAt, -}: { - dbConnection: DatabaseConnection; - trainUuid: string; - occurredAt: string; -}): Promise | null> => { +export const findByUnique = async ( + { + trainUuid, + occurredAt, + }: { + trainUuid: string; + occurredAt: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindTrainLocatedEventByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { trainUuid, @@ -1482,20 +1742,21 @@ export const sql = \` ) as dgv; \`; -export const upsert = async ({ - dbConnection, - trainLocatedEvent, -}: { - dbConnection: DatabaseConnection; - trainLocatedEvent: TrainLocatedEvent; -}): Promise> => { +export const upsert = async ( + { + trainLocatedEvent, + }: { + trainLocatedEvent: TrainLocatedEvent; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> => { const results = await sqlQueryUpsertTrainLocatedEvent({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { trainUuid: trainLocatedEvent.trainUuid, occurredAt: trainLocatedEvent.occurredAt, - geocodeId: trainLocatedEvent.geocode.id ? trainLocatedEvent.geocode.id : (await geocodeDao.upsert({ dbConnection, geocode: trainLocatedEvent.geocode })).id, + geocodeId: trainLocatedEvent.geocode.id ? trainLocatedEvent.geocode.id : (await geocodeDao.upsert({ geocode: trainLocatedEvent.geocode }, context)).id, }, }); const { id } = results[0]!; // grab the db generated values @@ -1565,15 +1826,16 @@ export const sql = \` WHERE train_station.id = :id; \`; -export const findById = async ({ - dbConnection, - id, -}: { - dbConnection: DatabaseConnection; - id: number; -}): Promise | null> => { +export const findById = async ( + { + id, + }: { + id: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindTrainStationById({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { id, @@ -1614,15 +1876,16 @@ export const sql = \` AND train_station.geocode_id = :geocodeId; \`; -export const findByUnique = async ({ - dbConnection, - geocode, -}: { - dbConnection: DatabaseConnection; - geocode: HasId; -}): Promise | null> => { +export const findByUnique = async ( + { + geocode, + }: { + geocode: HasId; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindTrainStationByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { geocodeId: geocode.id, @@ -1662,15 +1925,16 @@ export const sql = \` WHERE train_station.uuid = :uuid; \`; -export const findByUuid = async ({ - dbConnection, - uuid, -}: { - dbConnection: DatabaseConnection; - uuid: string; -}): Promise | null> => { +export const findByUuid = async ( + { + uuid, + }: { + uuid: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindTrainStationByUuid({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { uuid, @@ -1702,18 +1966,19 @@ export const sql = \` ) as dgv; \`; -export const upsert = async ({ - dbConnection, - trainStation, -}: { - dbConnection: DatabaseConnection; - trainStation: TrainStation; -}): Promise> => { +export const upsert = async ( + { + trainStation, + }: { + trainStation: TrainStation; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> => { const results = await sqlQueryUpsertTrainStation({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { - geocodeId: trainStation.geocode.id ? trainStation.geocode.id : (await geocodeDao.upsert({ dbConnection, geocode: trainStation.geocode })).id, + geocodeId: trainStation.geocode.id ? trainStation.geocode.id : (await geocodeDao.upsert({ geocode: trainStation.geocode }, context)).id, name: trainStation.name, }, }); @@ -1772,15 +2037,16 @@ export const sql = \` WHERE price.id = :id; \`; -export const findById = async ({ - dbConnection, - id, -}: { - dbConnection: DatabaseConnection; - id: number; -}): Promise | null> => { +export const findById = async ( + { + id, + }: { + id: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindPriceById({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { id, @@ -1814,17 +2080,18 @@ export const sql = \` AND price.currency = :currency; \`; -export const findByUnique = async ({ - dbConnection, - amount, - currency, -}: { - dbConnection: DatabaseConnection; - amount: number; - currency: Price['currency']; -}): Promise | null> => { +export const findByUnique = async ( + { + amount, + currency, + }: { + amount: number; + currency: Price['currency']; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindPriceByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { amount, @@ -1856,15 +2123,16 @@ export const sql = \` ) as dgv; \`; -export const upsert = async ({ - dbConnection, - price, -}: { - dbConnection: DatabaseConnection; - price: Price; -}): Promise> => { +export const upsert = async ( + { + price, + }: { + price: Price; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> => { const results = await sqlQueryUpsertPrice({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { amount: price.amount, @@ -1936,15 +2204,16 @@ export const sql = \` WHERE invoice_line_item.id = :id; \`; -export const findById = async ({ - dbConnection, - id, -}: { - dbConnection: DatabaseConnection; - id: number; -}): Promise | null> => { +export const findById = async ( + { + id, + }: { + id: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindInvoiceLineItemById({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { id, @@ -1987,19 +2256,20 @@ export const sql = \` AND invoice_line_item.explanation = :explanation; \`; -export const findByUnique = async ({ - dbConnection, - price, - title, - explanation, -}: { - dbConnection: DatabaseConnection; - price: HasId; - title: string; - explanation: string; -}): Promise | null> => { +export const findByUnique = async ( + { + price, + title, + explanation, + }: { + price: HasId; + title: string; + explanation: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindInvoiceLineItemByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { priceId: price.id, @@ -2034,18 +2304,19 @@ export const sql = \` ) as dgv; \`; -export const upsert = async ({ - dbConnection, - invoiceLineItem, -}: { - dbConnection: DatabaseConnection; - invoiceLineItem: InvoiceLineItem; -}): Promise> => { +export const upsert = async ( + { + invoiceLineItem, + }: { + invoiceLineItem: InvoiceLineItem; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> => { const results = await sqlQueryUpsertInvoiceLineItem({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { - priceId: invoiceLineItem.price.id ? invoiceLineItem.price.id : (await priceDao.upsert({ dbConnection, price: invoiceLineItem.price })).id, + priceId: invoiceLineItem.price.id ? invoiceLineItem.price.id : (await priceDao.upsert({ price: invoiceLineItem.price }, context)).id, title: invoiceLineItem.title, explanation: invoiceLineItem.explanation, }, @@ -2146,15 +2417,16 @@ export const sql = \` WHERE invoice.id = :id; \`; -export const findById = async ({ - dbConnection, - id, -}: { - dbConnection: DatabaseConnection; - id: number; -}): Promise | null> => { +export const findById = async ( + { + id, + }: { + id: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindInvoiceById({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { id, @@ -2221,15 +2493,16 @@ export const sql = \` AND invoice.external_id = :externalId; \`; -export const findByUnique = async ({ - dbConnection, - externalId, -}: { - dbConnection: DatabaseConnection; - externalId: string; -}): Promise | null> => { +export const findByUnique = async ( + { + externalId, + }: { + externalId: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindInvoiceByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { externalId, @@ -2295,15 +2568,16 @@ export const sql = \` WHERE invoice.uuid = :uuid; \`; -export const findByUuid = async ({ - dbConnection, - uuid, -}: { - dbConnection: DatabaseConnection; - uuid: string; -}): Promise | null> => { +export const findByUuid = async ( + { + uuid, + }: { + uuid: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindInvoiceByUuid({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { uuid, @@ -2338,20 +2612,21 @@ export const sql = \` ) as dgv; \`; -export const upsert = async ({ - dbConnection, - invoice, -}: { - dbConnection: DatabaseConnection; - invoice: Invoice; -}): Promise> => { +export const upsert = async ( + { + invoice, + }: { + invoice: Invoice; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> => { const results = await sqlQueryUpsertInvoice({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { externalId: invoice.externalId, - itemIds: await Promise.all(invoice.items.map(async (invoiceLineItem) => invoiceLineItem.id ? invoiceLineItem.id : (await invoiceLineItemDao.upsert({ dbConnection, invoiceLineItem })).id)), - totalPriceId: invoice.totalPrice.id ? invoice.totalPrice.id : (await priceDao.upsert({ dbConnection, price: invoice.totalPrice })).id, + itemIds: await Promise.all(invoice.items.map(async (invoiceLineItem) => invoiceLineItem.id ? invoiceLineItem.id : (await invoiceLineItemDao.upsert({ invoiceLineItem }, context)).id)), + totalPriceId: invoice.totalPrice.id ? invoice.totalPrice.id : (await priceDao.upsert({ price: invoice.totalPrice }, context)).id, status: invoice.status, }, }); diff --git a/src/logic/define/databaseAccessObjects/__snapshots__/defineDaoFindByMethodCodeForDomainObject.test.ts.snap b/src/logic/define/databaseAccessObjects/__snapshots__/defineDaoFindByMethodCodeForDomainObject.test.ts.snap index 7806f65..c623406 100644 --- a/src/logic/define/databaseAccessObjects/__snapshots__/defineDaoFindByMethodCodeForDomainObject.test.ts.snap +++ b/src/logic/define/databaseAccessObjects/__snapshots__/defineDaoFindByMethodCodeForDomainObject.test.ts.snap @@ -36,15 +36,16 @@ export const sql = \` WHERE train_located_event.id = :id; \`; -export const findById = async ({ - dbConnection, - id, -}: { - dbConnection: DatabaseConnection; - id: number; -}): Promise | null> => { +export const findById = async ( + { + id, + }: { + id: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindTrainLocatedEventById({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { id, @@ -113,15 +114,16 @@ export const sql = \` WHERE train.id = :id; \`; -export const findById = async ({ - dbConnection, - id, -}: { - dbConnection: DatabaseConnection; - id: number; -}): Promise | null> => { +export const findById = async ( + { + id, + }: { + id: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindTrainById({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { id, @@ -155,15 +157,16 @@ export const sql = \` WHERE carriage.id = :id; \`; -export const findById = async ({ - dbConnection, - id, -}: { - dbConnection: DatabaseConnection; - id: number; -}): Promise | null> => { +export const findById = async ( + { + id, + }: { + id: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindCarriageById({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { id, @@ -196,15 +199,16 @@ export const sql = \` WHERE geocode.id = :id; \`; -export const findById = async ({ - dbConnection, - id, -}: { - dbConnection: DatabaseConnection; - id: number; -}): Promise | null> => { +export const findById = async ( + { + id, + }: { + id: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindGeocodeById({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { id, @@ -254,17 +258,18 @@ export const sql = \` AND train_located_event.occurred_at = :occurredAt; \`; -export const findByUnique = async ({ - dbConnection, - trainUuid, - occurredAt, -}: { - dbConnection: DatabaseConnection; - trainUuid: string; - occurredAt: Date; -}): Promise | null> => { +export const findByUnique = async ( + { + trainUuid, + occurredAt, + }: { + trainUuid: string; + occurredAt: Date; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindTrainLocatedEventByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { trainUuid, @@ -345,21 +350,22 @@ export const sql = \` AND train.lead_engineer_id = (SELECT id FROM train_engineer WHERE train_engineer.uuid = :leadEngineerUuid); \`; -export const findByUnique = async ({ - dbConnection, - homeStationGeocode, - badges, - locomotiveUuids, - leadEngineerUuid, -}: { - dbConnection: DatabaseConnection; - homeStationGeocode: HasId; - badges: HasId[]; - locomotiveUuids: string[]; - leadEngineerUuid: string; -}): Promise | null> => { +export const findByUnique = async ( + { + homeStationGeocode, + badges, + locomotiveUuids, + leadEngineerUuid, + }: { + homeStationGeocode: HasId; + badges: HasId[]; + locomotiveUuids: string[]; + leadEngineerUuid: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindTrainByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { homeStationGeocodeId: homeStationGeocode.id, @@ -430,15 +436,16 @@ export const sql = \` AND train.tin = :tin; \`; -export const findByUnique = async ({ - dbConnection, - tin, -}: { - dbConnection: DatabaseConnection; - tin: string; -}): Promise | null> => { +export const findByUnique = async ( + { + tin, + }: { + tin: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindTrainByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { tin, @@ -473,15 +480,16 @@ export const sql = \` AND carriage.cin = :cin; \`; -export const findByUnique = async ({ - dbConnection, - cin, -}: { - dbConnection: DatabaseConnection; - cin: string; -}): Promise | null> => { +export const findByUnique = async ( + { + cin, + }: { + cin: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindCarriageByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { cin, @@ -515,15 +523,16 @@ export const sql = \` AND carriage.uuid = :uuid; \`; -export const findByUnique = async ({ - dbConnection, - uuid, -}: { - dbConnection: DatabaseConnection; - uuid: string; -}): Promise | null> => { +export const findByUnique = async ( + { + uuid, + }: { + uuid: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindCarriageByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { uuid, @@ -557,17 +566,18 @@ export const sql = \` AND geocode.longitude = :longitude; \`; -export const findByUnique = async ({ - dbConnection, - latitude, - longitude, -}: { - dbConnection: DatabaseConnection; - latitude: number; - longitude: number; -}): Promise | null> => { +export const findByUnique = async ( + { + latitude, + longitude, + }: { + latitude: number; + longitude: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindGeocodeByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { latitude, @@ -608,15 +618,16 @@ export const sql = \` AND invoice_line_item.price_id = :priceId; \`; -export const findByUnique = async ({ - dbConnection, - price, -}: { - dbConnection: DatabaseConnection; - price: HasId; -}): Promise | null> => { +export const findByUnique = async ( + { + price, + }: { + price: HasId; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindInvoiceLineItemByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { priceId: price.id, @@ -656,15 +667,16 @@ export const sql = \` AND station.geocode_id = :geocodeId; \`; -export const findByUnique = async ({ - dbConnection, - geocode, -}: { - dbConnection: DatabaseConnection; - geocode: HasId; -}): Promise | null> => { +export const findByUnique = async ( + { + geocode, + }: { + geocode: HasId; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindStationByUnique({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { geocodeId: geocode.id, @@ -712,15 +724,16 @@ export const sql = \` WHERE train_located_event.uuid = :uuid; \`; -export const findByUuid = async ({ - dbConnection, - uuid, -}: { - dbConnection: DatabaseConnection; - uuid: string; -}): Promise | null> => { +export const findByUuid = async ( + { + uuid, + }: { + uuid: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindTrainLocatedEventByUuid({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { uuid, @@ -787,15 +800,16 @@ export const sql = \` WHERE train.uuid = :uuid; \`; -export const findByUuid = async ({ - dbConnection, - uuid, -}: { - dbConnection: DatabaseConnection; - uuid: string; -}): Promise | null> => { +export const findByUuid = async ( + { + uuid, + }: { + uuid: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindTrainByUuid({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { uuid, @@ -829,15 +843,16 @@ export const sql = \` WHERE carriage.uuid = :uuid; \`; -export const findByUuid = async ({ - dbConnection, - uuid, -}: { - dbConnection: DatabaseConnection; - uuid: string; -}): Promise | null> => { +export const findByUuid = async ( + { + uuid, + }: { + uuid: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindCarriageByUuid({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { uuid, @@ -869,15 +884,16 @@ export const sql = \` WHERE geocode.uuid = :uuid; \`; -export const findByUuid = async ({ - dbConnection, - uuid, -}: { - dbConnection: DatabaseConnection; - uuid: string; -}): Promise | null> => { +export const findByUuid = async ( + { + uuid, + }: { + uuid: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => { const results = await sqlQueryFindGeocodeByUuid({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { uuid, diff --git a/src/logic/define/databaseAccessObjects/__snapshots__/defineDaoUpsertMethodCodeForDomainObject.test.ts.snap b/src/logic/define/databaseAccessObjects/__snapshots__/defineDaoUpsertMethodCodeForDomainObject.test.ts.snap index 3499ba4..8a813ba 100644 --- a/src/logic/define/databaseAccessObjects/__snapshots__/defineDaoUpsertMethodCodeForDomainObject.test.ts.snap +++ b/src/logic/define/databaseAccessObjects/__snapshots__/defineDaoUpsertMethodCodeForDomainObject.test.ts.snap @@ -20,20 +20,21 @@ export const sql = \` ) as dgv; \`; -export const upsert = async ({ - dbConnection, - trainLocatedEvent, -}: { - dbConnection: DatabaseConnection; - trainLocatedEvent: TrainLocatedEvent; -}): Promise> => { +export const upsert = async ( + { + trainLocatedEvent, + }: { + trainLocatedEvent: TrainLocatedEvent; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> => { const results = await sqlQueryUpsertTrainLocatedEvent({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { trainUuid: trainLocatedEvent.trainUuid, occurredAt: trainLocatedEvent.occurredAt, - geocodeIds: await Promise.all(trainLocatedEvent.geocodes.map(async (geocode) => geocode.id ? geocode.id : (await geocodeDao.upsert({ dbConnection, geocode })).id)), + geocodeIds: await Promise.all(trainLocatedEvent.geocodes.map(async (geocode) => geocode.id ? geocode.id : (await geocodeDao.upsert({ geocode }, context)).id)), }, }); const { id } = results[0]!; // grab the db generated values @@ -70,21 +71,22 @@ export const sql = \` ) as dgv; \`; -export const upsert = async ({ - dbConnection, - train, -}: { - dbConnection: DatabaseConnection; - train: Train; -}): Promise> => { +export const upsert = async ( + { + train, + }: { + train: Train; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> => { const results = await sqlQueryUpsertTrain({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { tin: train.tin, - homeStationGeocodeId: train.homeStationGeocode.id ? train.homeStationGeocode.id : (await geocodeDao.upsert({ dbConnection, geocode: train.homeStationGeocode })).id, + homeStationGeocodeId: train.homeStationGeocode.id ? train.homeStationGeocode.id : (await geocodeDao.upsert({ geocode: train.homeStationGeocode }, context)).id, leadEngineerUuid: train.leadEngineerUuid, - badgeIds: await Promise.all(train.badges.map(async (trainBadge) => trainBadge.id ? trainBadge.id : (await trainBadgeDao.upsert({ dbConnection, trainBadge })).id)), + badgeIds: await Promise.all(train.badges.map(async (trainBadge) => trainBadge.id ? trainBadge.id : (await trainBadgeDao.upsert({ trainBadge }, context)).id)), locomotiveUuids: train.locomotiveUuids, }, }); @@ -112,15 +114,16 @@ export const sql = \` ) as dgv; \`; -export const upsert = async ({ - dbConnection, - carriage, -}: { - dbConnection: DatabaseConnection; - carriage: Carriage; -}): Promise> => { +export const upsert = async ( + { + carriage, + }: { + carriage: Carriage; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> => { const results = await sqlQueryUpsertCarriage({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { cin: carriage.cin, @@ -151,15 +154,16 @@ export const sql = \` ) as dgv; \`; -export const upsert = async ({ - dbConnection, - geocode, -}: { - dbConnection: DatabaseConnection; - geocode: Geocode; -}): Promise> => { +export const upsert = async ( + { + geocode, + }: { + geocode: Geocode; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> => { const results = await sqlQueryUpsertGeocode({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { latitude: geocode.latitude, @@ -190,15 +194,16 @@ export const sql = \` ) as dgv; \`; -export const upsert = async ({ - dbConnection, - carriage, -}: { - dbConnection: DatabaseConnection; - carriage: Carriage; -}): Promise> => { +export const upsert = async ( + { + carriage, + }: { + carriage: Carriage; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> => { const results = await sqlQueryUpsertCarriage({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { cin: carriage.cin, diff --git a/src/logic/define/databaseAccessObjects/defineDaoFindByMethodCodeForDomainObject.test.ts b/src/logic/define/databaseAccessObjects/defineDaoFindByMethodCodeForDomainObject.test.ts index a3cec8f..b74829a 100644 --- a/src/logic/define/databaseAccessObjects/defineDaoFindByMethodCodeForDomainObject.test.ts +++ b/src/logic/define/databaseAccessObjects/defineDaoFindByMethodCodeForDomainObject.test.ts @@ -809,15 +809,16 @@ describe('defineDaoFindByMethodCodeForDomainObject', () => { expect(code).toContain(' AND geocode.longitude = :longitude'); // condition expect(code).toContain( ` -async ({ - dbConnection, - latitude, - longitude, -}: { - dbConnection: DatabaseConnection; - latitude: number; - longitude: number; -}): Promise | null> => +async ( + { + latitude, + longitude, + }: { + latitude: number; + longitude: number; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => `.trim(), ); expect(code).toContain('await sqlQueryFindGeocodeByUnique({'); @@ -956,13 +957,13 @@ async ({ expect(code).toContain(' AND carriage.cin = :cin'); // condition expect(code).toContain( ` -async ({ - dbConnection, - cin, -}: { - dbConnection: DatabaseConnection; - cin: string; -}) +async ( + { + cin, + }: { + cin: string; + }, + context: `.trim(), ); expect(code).toContain('await sqlQueryFindCarriageByUnique({'); @@ -1025,13 +1026,13 @@ async ({ expect(code).toContain(' AND carriage.uuid = :uuid'); // condition expect(code).toContain( ` -async ({ - dbConnection, - uuid, -}: { - dbConnection: DatabaseConnection; - uuid: string; -}) +async ( + { + uuid, + }: { + uuid: string; + }, + context: `.trim(), ); expect(code).toContain('await sqlQueryFindCarriageByUnique({'); @@ -1123,13 +1124,13 @@ async ({ expect(code).toContain(' AND station.geocode_id = :geocodeId'); // condition expect(code).toContain( ` -async ({ - dbConnection, - geocode, -}: { - dbConnection: DatabaseConnection; - geocode: HasId; -}) +async ( + { + geocode, + }: { + geocode: HasId; + }, + context `.trim(), ); expect(code).toContain('await sqlQueryFindStationByUnique({'); @@ -1234,15 +1235,16 @@ async ({ ); // condition expect(code).toContain( ` -async ({ - dbConnection, - trainUuid, - occurredAt, -}: { - dbConnection: DatabaseConnection; - trainUuid: string; - occurredAt: Date; -}) +async ( + { + trainUuid, + occurredAt, + }: { + trainUuid: string; + occurredAt: Date; + }, + context: { dbConnection: DatabaseConnection }, +) `.trim(), ); expect(code).toContain('await sqlQueryFindTrainLocatedEventByUnique('); @@ -1614,19 +1616,20 @@ async ({ ); // condition expect(code).toContain( ` -async ({ - dbConnection, - homeStationGeocode, - badges, - locomotiveUuids, - leadEngineerUuid, -}: { - dbConnection: DatabaseConnection; - homeStationGeocode: HasId; - badges: HasId[]; - locomotiveUuids: string[]; - leadEngineerUuid: string; -}): Promise | null> => +async ( + { + homeStationGeocode, + badges, + locomotiveUuids, + leadEngineerUuid, + }: { + homeStationGeocode: HasId; + badges: HasId[]; + locomotiveUuids: string[]; + leadEngineerUuid: string; + }, + context: { dbConnection: DatabaseConnection }, +): Promise | null> => `.trim(), ); expect(code).toContain('await sqlQueryFindTrainByUnique('); diff --git a/src/logic/define/databaseAccessObjects/defineDaoFindByMethodCodeForDomainObject.ts b/src/logic/define/databaseAccessObjects/defineDaoFindByMethodCodeForDomainObject.ts index 7d29bcc..e4216f0 100644 --- a/src/logic/define/databaseAccessObjects/defineDaoFindByMethodCodeForDomainObject.ts +++ b/src/logic/define/databaseAccessObjects/defineDaoFindByMethodCodeForDomainObject.ts @@ -353,17 +353,18 @@ export const sql = \` WHERE ${whereConditions}; \`; -export const findBy${findByQueryType} = async ({ - dbConnection, - ${Object.keys(parameters).join(',\n ')}, -}: { - dbConnection: DatabaseConnection; - ${Object.entries(parameters) - .map((entry) => `${entry[0]}: ${entry[1]}`) - .join(';\n ')}; -}): Promise<${outputType} | null> => { +export const findBy${findByQueryType} = async ( + { + ${Object.keys(parameters).join(',\n ')}, + }: { + ${Object.entries(parameters) + .map((entry) => `${entry[0]}: ${entry[1]}`) + .join(';\n ')}; + }, + context: { dbConnection: DatabaseConnection }, +): Promise<${outputType} | null> => { const results = await sqlQueryFind${domainObject.name}By${findByQueryType}({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { ${queryFunctionInputExpressions.join(',\n ')}, diff --git a/src/logic/define/databaseAccessObjects/defineDaoUpsertMethodCodeForDomainObject.test.ts b/src/logic/define/databaseAccessObjects/defineDaoUpsertMethodCodeForDomainObject.test.ts index e99a428..7a04005 100644 --- a/src/logic/define/databaseAccessObjects/defineDaoUpsertMethodCodeForDomainObject.test.ts +++ b/src/logic/define/databaseAccessObjects/defineDaoUpsertMethodCodeForDomainObject.test.ts @@ -52,13 +52,14 @@ describe('defineDaoUpsertMethodCodeForDomainObject', () => { ); // calls upsert correctly expect(code).toContain( ` -export const upsert = async ({ - dbConnection, - geocode, -}: { - dbConnection: DatabaseConnection; - geocode: Geocode; -}): Promise> +export const upsert = async ( + { + geocode, + }: { + geocode: Geocode; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> `.trim(), ); // defines fn correctly expect(code).toContain('await sqlQueryUpsertGeocode({'); @@ -135,13 +136,14 @@ export const upsert = async ({ ); // calls upsert correctly expect(code).toContain( ` -async ({ - dbConnection, - carriage, -}: { - dbConnection: DatabaseConnection; - carriage: Carriage; -}): Promise> +async ( + { + carriage, + }: { + carriage: Carriage; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> `.trim(), ); // defines fn correctly expect(code).toContain('await sqlQueryUpsertCarriage({'); @@ -225,13 +227,14 @@ async ({ ); // calls upsert correctly expect(code).toContain( ` -async ({ - dbConnection, - carriage, -}: { - dbConnection: DatabaseConnection; - carriage: Carriage; -}): Promise> +async ( + { + carriage, + }: { + carriage: Carriage; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> `.trim(), ); // defines fn correctly expect(code).toContain('await sqlQueryUpsertCarriage({'); @@ -341,13 +344,14 @@ async ({ ); // calls upsert correctly expect(code).toContain( ` -async ({ - dbConnection, - trainLocatedEvent, -}: { - dbConnection: DatabaseConnection; - trainLocatedEvent: TrainLocatedEvent; -}): Promise> +async ( + { + trainLocatedEvent, + }: { + trainLocatedEvent: TrainLocatedEvent; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> `.trim(), ); // defines fn correctly expect(code).toContain('await sqlQueryUpsertTrainLocatedEvent({'); @@ -356,7 +360,7 @@ async ({ input: { trainUuid: trainLocatedEvent.trainUuid, occurredAt: trainLocatedEvent.occurredAt, - geocodeIds: await Promise.all(trainLocatedEvent.geocodes.map(async (geocode) => geocode.id ? geocode.id : (await geocodeDao.upsert({ dbConnection, geocode })).id)), + geocodeIds: await Promise.all(trainLocatedEvent.geocodes.map(async (geocode) => geocode.id ? geocode.id : (await geocodeDao.upsert({ geocode }, context)).id)), } `.trim(), ); // defines inputs correctly @@ -542,13 +546,14 @@ async ({ ); // calls upsert correctly expect(code).toContain( ` -async ({ - dbConnection, - train, -}: { - dbConnection: DatabaseConnection; - train: Train; -}): Promise> +async ( + { + train, + }: { + train: Train; + }, + context: { dbConnection: DatabaseConnection }, +): Promise> `.trim(), ); // defines fn correctly expect(code).toContain('await sqlQueryUpsertTrain({'); @@ -556,9 +561,9 @@ async ({ ` input: { tin: train.tin, - homeStationGeocodeId: train.homeStationGeocode.id ? train.homeStationGeocode.id : (await geocodeDao.upsert({ dbConnection, geocode: train.homeStationGeocode })).id, + homeStationGeocodeId: train.homeStationGeocode.id ? train.homeStationGeocode.id : (await geocodeDao.upsert({ geocode: train.homeStationGeocode }, context)).id, leadEngineerUuid: train.leadEngineerUuid, - badgeIds: await Promise.all(train.badges.map(async (trainBadge) => trainBadge.id ? trainBadge.id : (await trainBadgeDao.upsert({ dbConnection, trainBadge })).id)), + badgeIds: await Promise.all(train.badges.map(async (trainBadge) => trainBadge.id ? trainBadge.id : (await trainBadgeDao.upsert({ trainBadge }, context)).id)), locomotiveUuids: train.locomotiveUuids, } `.trim(), diff --git a/src/logic/define/databaseAccessObjects/defineDaoUpsertMethodCodeForDomainObject.ts b/src/logic/define/databaseAccessObjects/defineDaoUpsertMethodCodeForDomainObject.ts index b49daa9..84fb55b 100644 --- a/src/logic/define/databaseAccessObjects/defineDaoUpsertMethodCodeForDomainObject.ts +++ b/src/logic/define/databaseAccessObjects/defineDaoUpsertMethodCodeForDomainObject.ts @@ -112,17 +112,18 @@ export const sql = \` ) as dgv; \`; -export const upsert = async ({ - dbConnection, - ${camelCase(domainObject.name)}, -}: { - dbConnection: DatabaseConnection; - ${camelCase(domainObject.name)}: ${ +export const upsert = async ( + { + ${camelCase(domainObject.name)}, + }: { + ${camelCase(domainObject.name)}: ${ isUniqueOnUuid ? `HasUuid<${domainObject.name}>` : domainObject.name }; -}): Promise<${outputType}> => { + }, + context: { dbConnection: DatabaseConnection }, +): Promise<${outputType}> => { const results = await sqlQueryUpsert${domainObject.name}({ - dbExecute: dbConnection.query, + dbExecute: context.dbConnection.query, logDebug: log.debug, input: { ${queryFunctionInputExpressions.join(',\n ')}, diff --git a/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.test.ts b/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.test.ts index 5ff721a..c9f90b5 100644 --- a/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.test.ts +++ b/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.test.ts @@ -136,7 +136,7 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { context: GetTypescriptCodeForPropertyContext.FOR_UPSERT_QUERY, }); expect(expression).toEqual( - 'geocodeId: trainLocatedEvent.geocode.id ? trainLocatedEvent.geocode.id : (await geocodeDao.upsert({ dbConnection, geocode: trainLocatedEvent.geocode })).id', + '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, nullable, DIRECT_BY_NESTING reference', () => { @@ -204,7 +204,7 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { context: GetTypescriptCodeForPropertyContext.FOR_UPSERT_QUERY, }); expect(expression).toEqual( - 'geocodeId: trainLocatedEvent.geocode === null ? null : trainLocatedEvent.geocode.id ? trainLocatedEvent.geocode.id : (await geocodeDao.upsert({ dbConnection, geocode: trainLocatedEvent.geocode })).id', + 'geocodeId: trainLocatedEvent.geocode === null ? null : trainLocatedEvent.geocode.id ? trainLocatedEvent.geocode.id : (await geocodeDao.upsert({ geocode: trainLocatedEvent.geocode }, context)).id', ); }); it('should define the input expression correctly for an array of IMPLICIT_BY_UUID references', () => { @@ -314,7 +314,7 @@ describe('defineQueryFunctionInputExpressionForDomainObjectProperty', () => { context: GetTypescriptCodeForPropertyContext.FOR_UPSERT_QUERY, }); expect(expression).toEqual( - 'geocodeIds: await Promise.all(trainLocatedEvent.geocodes.map(async (geocode) => geocode.id ? geocode.id : (await geocodeDao.upsert({ dbConnection, geocode })).id))', + 'geocodeIds: await Promise.all(trainLocatedEvent.geocodes.map(async (geocode) => geocode.id ? geocode.id : (await geocodeDao.upsert({ geocode }, context)).id))', ); }); }); diff --git a/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.ts b/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.ts index beda457..dff9d94 100644 --- a/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.ts +++ b/src/logic/define/databaseAccessObjects/defineQueryFunctionInputExpressionForDomainObjectProperty.ts @@ -78,14 +78,14 @@ export const defineQueryFunctionInputExpressionForDomainObjectProperty = ({ : ''; if (context === GetTypescriptCodeForPropertyContext.FOR_UPSERT_QUERY) - // e.g.,: `geocodeId: location.geocode.id ? location.geocode.id : (await geocodeDao.upsert({ dbConnection, geocode: location.geocode })).id` + // e.g.,: `geocodeId: location.geocode.id ? location.geocode.id : (await geocodeDao.upsert({ geocode: location.geocode }, context)).id` return `${camelCase( sqlSchemaProperty.name, )}: ${nullabilityPrefix}${domainObjectPropertyVariableName}.id ? ${domainObjectPropertyVariableName}.id : (await ${castDomainObjectNameToDaoName( referencedDomainObjectName, - )}.upsert({ dbConnection, ${camelCase( + )}.upsert({ ${camelCase( referencedSqlSchemaName, - )}: ${domainObjectPropertyVariableName} })).id`; + )}: ${domainObjectPropertyVariableName} }, context)).id`; // e.g., `geocodeId: geocode.id` return `${camelCase(sqlSchemaProperty.name)}: ${nullabilityPrefix}${ @@ -96,7 +96,7 @@ export const defineQueryFunctionInputExpressionForDomainObjectProperty = ({ // handle array of references if (sqlSchemaProperty.isArray) { if (context === GetTypescriptCodeForPropertyContext.FOR_UPSERT_QUERY) - // e.g.,: `geocodeIds: await Promise.all(location.geocodes.map(async (geocode) => geocode.id ? geocode.id : (await geocodeDao.upsert({ dbConnection, geocode: location.geocode })).id)` + // 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}.${ @@ -107,9 +107,7 @@ export const defineQueryFunctionInputExpressionForDomainObjectProperty = ({ referencedSqlSchemaName, )}.id : (await ${castDomainObjectNameToDaoName( referencedDomainObjectName, - )}.upsert({ dbConnection, ${camelCase( - referencedSqlSchemaName, - )} })).id))`; + )}.upsert({ ${camelCase(referencedSqlSchemaName)} }, context)).id))`; // e.g., `geocodeIds: geocodes.map(geocode => geocode.id)` return `${camelCase(sqlSchemaProperty.name)}: ${ diff --git a/src/logic/define/sqlSchemaControl/__snapshots__/defineSqlSchemaControlCodeFilesForDomainObjects.integration.test.ts.snap b/src/logic/define/sqlSchemaControl/__snapshots__/defineSqlSchemaControlCodeFilesForDomainObjects.integration.test.ts.snap index 95e2948..3329155 100644 --- a/src/logic/define/sqlSchemaControl/__snapshots__/defineSqlSchemaControlCodeFilesForDomainObjects.integration.test.ts.snap +++ b/src/logic/define/sqlSchemaControl/__snapshots__/defineSqlSchemaControlCodeFilesForDomainObjects.integration.test.ts.snap @@ -80,7 +80,7 @@ GeneratedCodeFile { - type: resource path: ./tables/train_to_carriage.sql - type: resource - path: ./tables/train_to_train_engineer.sql + path: ./tables/train_to_engineer.sql - type: resource path: ./tables/train_version.sql - type: resource @@ -114,7 +114,7 @@ GeneratedCodeFile { - type: resource path: ./tables/invoice_version.sql - type: resource - path: ./tables/invoice_version_to_invoice_line_item.sql + path: ./tables/invoice_version_to_line_item.sql - type: resource path: ./tables/invoice_cvp.sql - type: resource diff --git a/src/logic/define/sqlSchemaGenerator/__snapshots__/defineSqlSchemaGeneratorCodeFilesForDomainObjects.integration.test.ts.snap b/src/logic/define/sqlSchemaGenerator/__snapshots__/defineSqlSchemaGeneratorCodeFilesForDomainObjects.integration.test.ts.snap index 4552c86..63b0152 100644 --- a/src/logic/define/sqlSchemaGenerator/__snapshots__/defineSqlSchemaGeneratorCodeFilesForDomainObjects.integration.test.ts.snap +++ b/src/logic/define/sqlSchemaGenerator/__snapshots__/defineSqlSchemaGeneratorCodeFilesForDomainObjects.integration.test.ts.snap @@ -3,7 +3,8 @@ exports[`defineSqlSchemaGeneratorCodeFilesForDomainObjects should work on the example project 1`] = ` [ GeneratedCodeFile { - "content": "import { carriage } from './carriage'; + "content": "import { asyncTaskPredictStationCongestion } from './asyncTaskPredictStationCongestion'; +import { carriage } from './carriage'; import { certificate } from './certificate'; import { geocode } from './geocode'; import { invoice } from './invoice'; @@ -19,6 +20,7 @@ import { trainStation } from './trainStation'; * all of the domain objects which we want to create sql-schema-resources for with sql-schema-generator */ export const generateSqlSchemasFor = [ + asyncTaskPredictStationCongestion, carriage, certificate, trainEngineer, @@ -36,6 +38,26 @@ export const generateSqlSchemasFor = [ GeneratedCodeFile { "content": "import { Entity, prop } from 'sql-schema-generator'; +import { trainLocatedEvent } from './trainLocatedEvent'; +import { trainStation } from './trainStation'; + +/** + * sql-schema for the domain entity 'AsyncTaskPredictStationCongestion' + */ +export const asyncTaskPredictStationCongestion: Entity = new Entity({ + name: 'async_task_predict_station_congestion', + properties: { + status: { ...prop.ENUM(['HALTED', 'SCHEDULED', 'QUEUED', 'ATTEMPTED', 'FULFILLED', 'FAILED', 'CANCELED']), updatable: true }, + station_id: prop.REFERENCES(trainStation), + train_located_event_id: prop.REFERENCES(trainLocatedEvent), + }, + unique: ['station_id', 'train_located_event_id'], +});", + "relpath": "asyncTaskPredictStationCongestion.ts", + }, + GeneratedCodeFile { + "content": "import { Entity, prop } from 'sql-schema-generator'; + /** * sql-schema for the domain entity 'Carriage' */ 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 a212952..0bff2d5 100644 --- a/src/logic/define/sqlSchemaRelationship/__snapshots__/defineSqlSchemaRelationshipsForDomainObject.integration.test.ts.snap +++ b/src/logic/define/sqlSchemaRelationship/__snapshots__/defineSqlSchemaRelationshipsForDomainObject.integration.test.ts.snap @@ -2,6 +2,170 @@ exports[`defineSqlSchemaRelationshipsForDomainObjects should work on the example project 1`] = ` [ + SqlSchemaToDomainObjectRelationship { + "decorations": { + "unique": { + "domainObject": [ + "stationUuid", + "trainLocatedEventUuid", + ], + "sqlSchema": [ + "station_id", + "train_located_event_id", + ], + }, + }, + "name": { + "domainObject": "AsyncTaskPredictStationCongestion", + "sqlSchema": "async_task_predict_station_congestion", + }, + "properties": [ + { + "domainObject": DomainObjectPropertyMetadata { + "name": "id", + "nullable": false, + "required": false, + "type": "NUMBER", + }, + "sqlSchema": SqlSchemaPropertyMetadata { + "isArray": false, + "isDatabaseGenerated": true, + "isNullable": false, + "isUpdatable": false, + "name": "id", + "reference": null, + }, + }, + { + "domainObject": DomainObjectPropertyMetadata { + "name": "uuid", + "nullable": false, + "required": false, + "type": "STRING", + }, + "sqlSchema": SqlSchemaPropertyMetadata { + "isArray": false, + "isDatabaseGenerated": true, + "isNullable": false, + "isUpdatable": false, + "name": "uuid", + "reference": null, + }, + }, + { + "domainObject": DomainObjectPropertyMetadata { + "name": "createdAt", + "nullable": false, + "required": false, + "type": "DATE", + }, + "sqlSchema": SqlSchemaPropertyMetadata { + "isArray": false, + "isDatabaseGenerated": true, + "isNullable": false, + "isUpdatable": false, + "name": "created_at", + "reference": null, + }, + }, + { + "domainObject": null, + "sqlSchema": SqlSchemaPropertyMetadata { + "isArray": false, + "isDatabaseGenerated": true, + "isNullable": false, + "isUpdatable": false, + "name": "effective_at", + "reference": null, + }, + }, + { + "domainObject": DomainObjectPropertyMetadata { + "name": "updatedAt", + "nullable": false, + "required": false, + "type": "DATE", + }, + "sqlSchema": SqlSchemaPropertyMetadata { + "isArray": false, + "isDatabaseGenerated": true, + "isNullable": false, + "isUpdatable": false, + "name": "updated_at", + "reference": null, + }, + }, + { + "domainObject": DomainObjectPropertyMetadata { + "name": "status", + "nullable": false, + "of": [ + "HALTED", + "SCHEDULED", + "QUEUED", + "ATTEMPTED", + "FULFILLED", + "FAILED", + "CANCELED", + ], + "required": true, + "type": "ENUM", + }, + "sqlSchema": SqlSchemaPropertyMetadata { + "isArray": false, + "isDatabaseGenerated": false, + "isNullable": false, + "isUpdatable": true, + "name": "status", + "reference": null, + }, + }, + { + "domainObject": DomainObjectPropertyMetadata { + "name": "stationUuid", + "nullable": false, + "required": true, + "type": "STRING", + }, + "sqlSchema": SqlSchemaPropertyMetadata { + "isArray": false, + "isDatabaseGenerated": false, + "isNullable": false, + "isUpdatable": false, + "name": "station_id", + "reference": SqlSchemaReferenceMetadata { + "method": "IMPLICIT_BY_UUID", + "of": DomainObjectReferenceMetadata { + "extends": "DomainEntity", + "name": "TrainStation", + }, + }, + }, + }, + { + "domainObject": DomainObjectPropertyMetadata { + "name": "trainLocatedEventUuid", + "nullable": false, + "required": true, + "type": "STRING", + }, + "sqlSchema": SqlSchemaPropertyMetadata { + "isArray": false, + "isDatabaseGenerated": false, + "isNullable": false, + "isUpdatable": false, + "name": "train_located_event_id", + "reference": SqlSchemaReferenceMetadata { + "method": "IMPLICIT_BY_UUID", + "of": DomainObjectReferenceMetadata { + "extends": "DomainEvent", + "name": "TrainLocatedEvent", + }, + }, + }, + }, + ], + }, SqlSchemaToDomainObjectRelationship { "decorations": { "unique": {