Skip to content

Commit

Permalink
chore: minor code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
edobrb committed Aug 23, 2022
1 parent a5cea68 commit 2332ab1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/dal/dao/dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,10 @@ export abstract class AbstractDAO<T extends DAOGenerics> implements DAO<T> {
}
}

protected get dbIdField(): string {
return this.schema[this.idField].alias ?? this.idField
}

private ignoreNullsWhenRequired(changes: T['update']): T['update'] {
function isNullAndRequired(schema: Schema<T['scalars']>, key: string, value: string): boolean {
if (value !== null) {
Expand Down
6 changes: 3 additions & 3 deletions src/dal/drivers/in-memory/dao.memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class AbstractInMemoryDAO<T extends InMemoryDAOGenerics> extends Abstract
protected async _insertOne(params: InsertParams<T>): Promise<T['plainModel']> {
const record = deepMerge(params.record, this.generateRecordWithId())
const t = (await transformObject(this.entityManager.adapters.memory, 'modelToDB', record, this.schema)) as T['insert']
const id = t[this.schema[this.idField].alias ?? this.idField]
const id = t[this.dbIdField]
this.stateManager.insertElement(id, t)
return _.cloneDeep(await transformObject(this.entityManager.adapters.memory, 'dbToModel', t, this.schema))
}
Expand Down Expand Up @@ -187,14 +187,14 @@ export class AbstractInMemoryDAO<T extends InMemoryDAOGenerics> extends Abstract

protected async _deleteOne(params: DeleteParams<T>): Promise<void> {
for await (const { record, index } of this.entities(params.filter, false)) {
this.stateManager.deleteElement(record[this.schema[this.idField].alias ?? this.idField], index)
this.stateManager.deleteElement(record[this.dbIdField], index)
break
}
}

protected async _deleteAll(params: DeleteParams<T>): Promise<void> {
for await (const { record, index } of this.entities(params.filter, false)) {
this.stateManager.deleteElement(record[this.schema[this.idField].alias ?? this.idField], index)
this.stateManager.deleteElement(record[this.dbIdField], index)
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/dal/drivers/no-sql/mongodb/dao.mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ export class AbstractMongoDBDAO<T extends MongoDBDAOGenerics> extends AbstractDA
protected _findAll<P extends AnyProjection<T['projection']>>(params: FindParams<T, P>): Promise<PartialDeep<T['model']>[]> {
return this.runQuery('findAll', async () => {
const filter = await this.buildFilter(params.filter)
const projection = isEmptyProjection(params.projection) ? { [this.schema[this.idField].alias ?? this.idField]: true } : this.buildProjection(params.projection)
const projection = isEmptyProjection(params.projection) ? { [this.dbIdField]: true } : this.buildProjection(params.projection)
const sort = this.buildSort(params.sorts)
/*if (sort.length > 0 && sort.map((v) => v[0]).includes(this.dbIdField)) { // see #288
sort.push([this.dbIdField, 1])
}*/
const options = { projection, sort, skip: params.skip, limit: params.limit ?? this.pageSize } as FindOptions
return [
async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/dal/drivers/sql/knexjs/dao.knexjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class AbstractKnexJsDAO<T extends KnexJsDAOGenerics> extends AbstractDAO<
return { skipReason: 'Limit is 0. Skip.' }
}

const select = isEmptyProjection(params.projection) ? { builder: this.qb().select([this.schema[this.idField].alias ?? this.idField]) } : await this.buildSelect(params.projection)
const select = isEmptyProjection(params.projection) ? { builder: this.qb().select([this.dbIdField]) } : await this.buildSelect(params.projection)
const where = await this.buildWhere(params.filter, select.builder)
const sort = await this.buildSort(params.sorts, where.builder)
return {
Expand Down

0 comments on commit 2332ab1

Please sign in to comment.