Skip to content

Commit

Permalink
fix: add modelName to query to avoid ambiguous fields
Browse files Browse the repository at this point in the history
  • Loading branch information
gwynndp committed Nov 12, 2021
1 parent c98caeb commit 8ec649e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
16 changes: 16 additions & 0 deletions src/js/buildFilterQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ export function buildFilterQuery(selectStmt, params) {

const whereObjClause = connector._buildWhere(modelName, safeWhere);

//add the modelName to each requested field to avoid ambiguity in more complex queries
console.log('whereObjClause.sql -------> ', whereObjClause.sql);

const tableName =
modelName.toLowerCase() === 'planterregistration'
? 'planter_registrations'
: modelName.toLowerCase();
const newQueryFields = whereObjClause.sql
.replace(/"/, `"${tableName}.`)
.replace(/AND "/gi, `AND "${tableName}.`)
.replace(/"/g, '');

whereObjClause.sql = newQueryFields;

if (whereObjClause.sql) {
const hasWhere = /WHERE(?![^(]*\))/i.test(selectStmt);
query.sql += ` ${hasWhere ? 'AND' : 'WHERE'} ${whereObjClause.sql}`;
Expand All @@ -47,5 +61,7 @@ export function buildFilterQuery(selectStmt, params) {
}
}

console.log('QUERY ---------', query);

return query;
}
9 changes: 8 additions & 1 deletion src/mixins/utils.repository-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ export function UtilsRepositoryMixin<
const planterIds = await this.getNonOrganizationPlanterIds();
// if planter repository request
if (model === 'planter') {
// return {
// and: [
// { organizationId: null },
// { 'planter.id': { inq: planterIds } },
// ],
// };
// could just do this if we didn't need to use 'planter.id' to avoid errors with adding other filters
return { id: { inq: planterIds } };
} else {
// if trees or other repository request
Expand All @@ -102,7 +109,7 @@ export function UtilsRepositoryMixin<
return {
or: [
{ organizationId: { inq: entityIds } },
{ id: { inq: planterIds } },
{ 'planter.id': { inq: planterIds } },
],
};
} else {
Expand Down
16 changes: 10 additions & 6 deletions src/repositories/planter.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Planter, PlanterRelations, PlanterRegistration } from '../models';
import { TreetrackerDataSource } from '../datasources';
import { PlanterRegistrationRepository } from './planterRegistration.repository';
import { UtilsRepositoryMixin } from '../mixins/utils.repository-mixin';
import expect from 'expect-runtime';
// import expect from 'expect-runtime';
import { buildFilterQuery } from '../js/buildFilterQuery';
import { utils } from '../js/utils';

Expand Down Expand Up @@ -70,17 +70,22 @@ export class PlanterRepository extends UtilsRepositoryMixin<

try {
if (this.dataSource.connector) {
const columnNames = this.dataSource.connector
.buildColumnNames('Planter', filter)
.replace('"id"', 'planter.id as "id"');
// const columnNames = this.dataSource.connector
// .buildColumnNames('Planter', filter)
// .replace('"id"', 'planter.id as "id"')
// .replace('"first_name"', 'planter.first_name as "first_name"')
// .replace('"last_name"', 'planter.last_name as "last_name"')
// .replace('"email"', 'planter.email as "email"')
// .replace('"organization"', 'planter.organization as "organization"')
// .replace('"phone"', 'planter.phone as "phone"');

let selectStmt;
if (deviceIdentifier) {
selectStmt = `SELECT planter.* FROM planter ${this.getPlanterRegistrationJoinClause(
deviceIdentifier,
)}`;
} else {
selectStmt = `SELECT ${columnNames} FROM planter`;
selectStmt = `SELECT planter.* FROM planter`;
}

const params = {
Expand All @@ -90,7 +95,6 @@ export class PlanterRepository extends UtilsRepositoryMixin<
};

const query = buildFilterQuery(selectStmt, params);
// console.log('query ---------', query);

const result = await this.execute(query.sql, query.params, options);
return <Planter[]>result.map((planter) => utils.convertCamel(planter));
Expand Down

0 comments on commit 8ec649e

Please sign in to comment.