Skip to content

Commit

Permalink
Merge pull request #90 from tyler-neal/main
Browse files Browse the repository at this point in the history
Allow null and undefined on maxLength columns
  • Loading branch information
jgeurts authored Jan 12, 2024
2 parents 82f076d + 00323eb commit c98d873
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/SqlHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export function getInsertQueryAndParams<T extends Entity, K extends string & key
const normalizedValues = (Array.isArray(entityValues) ? entityValues : [entityValues]) as string[];

for (const normalizedValue of normalizedValues) {
if (normalizedValue.length > maxLength) {
if (normalizedValue?.length > maxLength) {
throw new QueryError(`Create statement for "${model.name}" contains a value that exceeds maxLength on field: ${column.propertyName}`, model);
}
}
Expand Down Expand Up @@ -456,7 +456,7 @@ export function getUpdateQueryAndParams<T extends Entity>({
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);
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/models/ImportedItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ export class ImportedItem extends Entity {
required: false,
name: 'external_id_no_max_length',
})
public externalIdNoMaxLength?: string;
public externalIdNoMaxLength?: string | null;

@column({
type: 'string',
required: false,
name: 'external_id_string',
maxLength: 5,
})
public externalIdString?: string;
public externalIdString?: string | null;

@column({
type: 'string[]',
required: false,
name: 'external_id_string_array',
maxLength: 10,
})
public externalIdStringArray?: string[];
public externalIdStringArray?: string[] | null;

@column({
type: 'integer',
Expand Down
80 changes: 80 additions & 0 deletions tests/sqlHelper.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImportedItem>,
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<ImportedItem>,
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();
Expand Down Expand Up @@ -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<ImportedItem>,
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<ImportedItem>,
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();
Expand Down

0 comments on commit c98d873

Please sign in to comment.