From 00323eb25a84aa0129ffe4af960a4feb0270c2d9 Mon Sep 17 00:00:00 2001 From: Tyler Neal Date: Fri, 12 Jan 2024 12:47:39 -0600 Subject: [PATCH] Allow null and undefined on maxLength columns --- src/SqlHelper.ts | 4 +- tests/models/ImportedItem.ts | 6 +-- tests/sqlHelper.tests.ts | 80 ++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 5 deletions(-) diff --git a/src/SqlHelper.ts b/src/SqlHelper.ts index 7dc4c30..a4bd355 100644 --- a/src/SqlHelper.ts +++ b/src/SqlHelper.ts @@ -223,7 +223,7 @@ export function getInsertQueryAndParams maxLength) { + if (normalizedValue?.length > maxLength) { throw new QueryError(`Create statement for "${model.name}" contains a value that exceeds maxLength on field: ${column.propertyName}`, model); } } @@ -456,7 +456,7 @@ export function getUpdateQueryAndParams({ const normalizedValues = (Array.isArray(value) ? value : [value]) as string[]; for (const normalizedValue of normalizedValues) { - if (normalizedValue.length > maxLength) { + if (normalizedValue?.length > maxLength) { throw new QueryError(`Update statement for "${model.name}" contains a value that exceeds maxLength on field: ${column.propertyName}`, model); } } diff --git a/tests/models/ImportedItem.ts b/tests/models/ImportedItem.ts index fc8c768..00b46e2 100644 --- a/tests/models/ImportedItem.ts +++ b/tests/models/ImportedItem.ts @@ -19,7 +19,7 @@ export class ImportedItem extends Entity { required: false, name: 'external_id_no_max_length', }) - public externalIdNoMaxLength?: string; + public externalIdNoMaxLength?: string | null; @column({ type: 'string', @@ -27,7 +27,7 @@ export class ImportedItem extends Entity { name: 'external_id_string', maxLength: 5, }) - public externalIdString?: string; + public externalIdString?: string | null; @column({ type: 'string[]', @@ -35,7 +35,7 @@ export class ImportedItem extends Entity { name: 'external_id_string_array', maxLength: 10, }) - public externalIdStringArray?: string[]; + public externalIdStringArray?: string[] | null; @column({ type: 'integer', diff --git a/tests/sqlHelper.tests.ts b/tests/sqlHelper.tests.ts index 63e1fc1..bf14545 100644 --- a/tests/sqlHelper.tests.ts +++ b/tests/sqlHelper.tests.ts @@ -847,6 +847,46 @@ describe('sqlHelper', () => { }); }); describe('maxLength', () => { + it('should allow insert when maxLength set and providing null value', () => { + const itemId = faker.string.uuid(); + const itemName = faker.string.uuid(); + + const { query, params } = sqlHelper.getInsertQueryAndParams({ + repositoriesByModelNameLowered, + model: repositoriesByModelNameLowered.importeditem.model as ModelMetadata, + values: [ + { + id: itemId, + name: itemName, + externalIdString: null, + }, + ], + returnRecords: false, + }); + + query.should.equal(`INSERT INTO "${repositoriesByModelNameLowered.importeditem.model.tableName}" ("id","name","external_id_string") VALUES ($1,$2,NULL)`); + params.should.deep.equal([itemId, itemName]); + }); + it('should allow insert when maxLength set and providing undefined value', () => { + const itemId = faker.string.uuid(); + const itemName = faker.string.uuid(); + + const { query, params } = sqlHelper.getInsertQueryAndParams({ + repositoriesByModelNameLowered, + model: repositoriesByModelNameLowered.importeditem.model as ModelMetadata, + values: [ + { + id: itemId, + name: itemName, + externalIdString: undefined, + }, + ], + returnRecords: false, + }); + + query.should.equal(`INSERT INTO "${repositoriesByModelNameLowered.importeditem.model.tableName}" ("id","name") VALUES ($1,$2)`); + params.should.deep.equal([itemId, itemName]); + }); it('should allow insert when maxLength set but column not required AND value not set', () => { const itemId = faker.string.uuid(); const itemName = faker.string.uuid(); @@ -1199,6 +1239,46 @@ describe('sqlHelper', () => { params.should.deep.equal([name, storeId, productId]); }); describe('maxLength', () => { + it('should allow update when maxLength set and providing null', () => { + const itemId = faker.string.uuid(); + const itemName = faker.string.uuid(); + + const { query, params } = sqlHelper.getUpdateQueryAndParams({ + repositoriesByModelNameLowered, + model: repositoriesByModelNameLowered.importeditem.model as ModelMetadata, + where: { + id: itemId, + }, + values: { + name: itemName, + externalIdString: null, + }, + returnRecords: false, + }); + + query.should.equal(`UPDATE "${repositoriesByModelNameLowered.importeditem.model.tableName}" SET "name"=$1,"external_id_string"=NULL WHERE "id"=$2`); + params.should.deep.equal([itemName, itemId]); + }); + it('should allow update when maxLength set and providing undefined', () => { + const itemId = faker.string.uuid(); + const itemName = faker.string.uuid(); + + const { query, params } = sqlHelper.getUpdateQueryAndParams({ + repositoriesByModelNameLowered, + model: repositoriesByModelNameLowered.importeditem.model as ModelMetadata, + where: { + id: itemId, + }, + values: { + name: itemName, + externalIdString: undefined, + }, + returnRecords: false, + }); + + query.should.equal(`UPDATE "${repositoriesByModelNameLowered.importeditem.model.tableName}" SET "name"=$1,"external_id_string"=NULL WHERE "id"=$2`); + params.should.deep.equal([itemName, itemId]); + }); it('should allow update when maxLength set but column not required AND value not set', () => { const itemId = faker.string.uuid(); const itemName = faker.string.uuid();