Skip to content

Commit

Permalink
Enable noUncheckedIndexedAccess
Browse files Browse the repository at this point in the history
  • Loading branch information
jgeurts committed Jan 3, 2023
1 parent 347344c commit 28dc383
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 69 deletions.
21 changes: 12 additions & 9 deletions src/ReadonlyRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,9 @@ export class ReadonlyRepository<T extends Entity> implements IReadonlyRepository

const pool = poolOverride || modelInstance._readonlyPool;
const results = await pool.query<Partial<QueryResult<T>>>(query, params);
if (results.rows && results.rows.length) {
const result = modelInstance._buildInstance(results.rows[0]);
const firstResult = _.first(results.rows);
if (firstResult) {
const result = modelInstance._buildInstance(firstResult);

if (populates.length) {
await modelInstance.populateFields([result], populates);
Expand Down Expand Up @@ -503,7 +504,8 @@ export class ReadonlyRepository<T extends Entity> implements IReadonlyRepository
const pool = poolOverride || modelInstance._readonlyPool;
const result = await pool.query<{ count: string }>(query, params);

const originalValue = result.rows[0].count;
const firstResult = _.first(result.rows);
const originalValue = firstResult ? firstResult.count : 0;
return await resolve(Number(originalValue));
} catch (ex) {
const typedException = ex as Error;
Expand Down Expand Up @@ -749,8 +751,10 @@ export class ReadonlyRepository<T extends Entity> implements IReadonlyRepository
} as FindArgs<Entity>);

if (entities.length === 1) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(entities[0][populate.propertyName as string & keyof QueryResult<T>] as any) = populateResults;
for (const entity of entities) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(entity[populate.propertyName as string & keyof QueryResult<T>] as any) = populateResults;
}
} else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const populateResultsByEntityId = _.groupBy(populateResults, column.via) as Record<PrimaryId, any>;
Expand Down Expand Up @@ -809,11 +813,10 @@ export class ReadonlyRepository<T extends Entity> implements IReadonlyRepository
const entityId = mapRecord[column.via] as PrimaryId;
const populatedId = mapRecord[relatedModelColumn.via] as PrimaryId;
populateIds.add(populatedId);
if (!populateIdsByEntityId[entityId]) {
populateIdsByEntityId[entityId] = [];
}
const entityPopulateIds = populateIdsByEntityId[entityId] || [];
entityPopulateIds.push(populatedId);

populateIdsByEntityId[entityId].push(populatedId);
populateIdsByEntityId[entityId] = entityPopulateIds;
}

const populateWhere = _.merge(
Expand Down
5 changes: 3 additions & 2 deletions src/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ export class Repository<T extends Entity> extends ReadonlyRepository<T> implemen
return this._buildInstances(results.rows);
}

if (results.rows && results.rows.length) {
return this._buildInstance(results.rows[0]);
const firstResult = _.first(results.rows);
if (firstResult) {
return this._buildInstance(firstResult);
}

throw new Error('Unknown error getting created rows back from the database');
Expand Down
13 changes: 9 additions & 4 deletions src/SqlHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,12 @@ export function getInsertQueryAndParams<T extends Entity, K extends string & key
}
}

valueCollections[entityIndex].push(value);
const valuesForEntityIndex = valueCollections[entityIndex];
if (!valuesForEntityIndex) {
throw new Error('Error trying to get insert values for entity index');
}

valuesForEntityIndex.push(value);
}
}

Expand Down Expand Up @@ -1030,7 +1035,7 @@ function buildWhere<T extends Entity>({
}

if (orConstraints.length === 1) {
return orConstraints[0];
return orConstraints[0] ?? '';
}

if (isNegated) {
Expand Down Expand Up @@ -1107,7 +1112,7 @@ function buildOrOperatorStatement<T extends Entity>({
}

if (orClauses.length === 1) {
return orClauses[0];
return orClauses[0] ?? '';
}

if (isNegated) {
Expand Down Expand Up @@ -1177,7 +1182,7 @@ function buildLikeOperatorStatement<T extends Entity>({ model, propertyName, isN
}

if (orConstraints.length === 1) {
return orConstraints[0];
return orConstraints[0] ?? '';
}

if (isNegated) {
Expand Down
27 changes: 17 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,21 @@ export function initialize({ models, pool, readonlyPool = pool, connections = {}
// Add dictionary to quickly find a column by propertyName, for applying ColumnModifierMetadata records
const columnsByPropertyNameForModel: Record<string, ColumnsByPropertyName> = {};
for (const column of metadataStorage.columns) {
columnsByPropertyNameForModel[column.target] = columnsByPropertyNameForModel[column.target] || {};
columnsByPropertyNameForModel[column.target][column.propertyName] = column;
const columns = columnsByPropertyNameForModel[column.target] || {};
columns[column.propertyName] = column;

columnsByPropertyNameForModel[column.target] = columns;
}

type ColumnModifiersByPropertyName = Record<string, ColumnModifierMetadata[]>;
const columnModifiersByPropertyNameForModel: Record<string, ColumnModifiersByPropertyName> = {};
for (const columnModifier of metadataStorage.columnModifiers) {
columnModifiersByPropertyNameForModel[columnModifier.target] = columnModifiersByPropertyNameForModel[columnModifier.target] || {};
columnModifiersByPropertyNameForModel[columnModifier.target][columnModifier.propertyName] = columnModifiersByPropertyNameForModel[columnModifier.target][columnModifier.propertyName] || [];
columnModifiersByPropertyNameForModel[columnModifier.target][columnModifier.propertyName].push(columnModifier);
const columnModifiersForModel = columnModifiersByPropertyNameForModel[columnModifier.target] || {};
const columnModifiersForProperty = columnModifiersForModel[columnModifier.propertyName] || [];
columnModifiersForProperty.push(columnModifier);

columnModifiersForModel[columnModifier.propertyName] = columnModifiersForProperty;
columnModifiersByPropertyNameForModel[columnModifier.target] = columnModifiersForModel;
}

// Aggregate columns from inherited classes
Expand All @@ -105,7 +110,7 @@ export function initialize({ models, pool, readonlyPool = pool, connections = {}
let modelMetadata: ModelMetadata<Entity> | undefined;
let inheritedColumnsByPropertyName: ColumnsByPropertyName = {};
const inheritedColumnModifiersByPropertyName: ColumnModifiersByPropertyName = {};
for (const inheritedClass of inheritanceTreesByModelName[model.name]) {
for (const inheritedClass of inheritanceTreesByModelName[model.name] ?? []) {
modelMetadata = modelMetadataByModelName[inheritedClass.name] || modelMetadata;
const columnsByPropertyName = columnsByPropertyNameForModel[inheritedClass.name] || {};

Expand Down Expand Up @@ -140,9 +145,9 @@ export function initialize({ models, pool, readonlyPool = pool, connections = {}

// Process all column modifiers to augment any @column definitions
for (const [modelName, columnModifiersByPropertyName] of Object.entries(columnModifiersByPropertyNameForModel)) {
columnsByPropertyNameForModel[modelName] = columnsByPropertyNameForModel[modelName] || {};
const columnsByPropertyName = columnsByPropertyNameForModel[modelName] || {};
for (const [propertyName, columnModifiers] of Object.entries(columnModifiersByPropertyName)) {
const column = columnsByPropertyNameForModel[modelName][propertyName];
const column = columnsByPropertyName[propertyName];
if (column) {
for (const columnModifier of columnModifiers) {
Object.assign(column, _.omit(columnModifier, ['target', 'name', 'propertyName', 'type', 'model']));
Expand All @@ -168,20 +173,22 @@ export function initialize({ models, pool, readonlyPool = pool, connections = {}
}

if (columnDetails.model) {
columnsByPropertyNameForModel[modelName][propertyName] = new ColumnModelMetadata({
columnsByPropertyName[propertyName] = new ColumnModelMetadata({
...columnDetails,
name: columnDetails.name,
model: columnDetails.model,
});
} else if (columnDetails.type) {
columnsByPropertyNameForModel[modelName][propertyName] = new ColumnTypeMetadata({
columnsByPropertyName[propertyName] = new ColumnTypeMetadata({
...columnDetails,
name: columnDetails.name,
type: columnDetails.type,
});
}
}
}

columnsByPropertyNameForModel[modelName] = columnsByPropertyName;
}

const repositoriesByModelNameLowered: Record<string, IReadonlyRepository<Entity> | IRepository<Entity>> = {};
Expand Down
Loading

0 comments on commit 28dc383

Please sign in to comment.