Skip to content

Commit

Permalink
Rename utils/db's equals() to sqlEquals() (#1258)
Browse files Browse the repository at this point in the history
Prevent collisions with ramda's equals() fn.
  • Loading branch information
alxndrsn authored Nov 3, 2024
1 parent ee3cd5a commit 644fe0f
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 36 deletions.
8 changes: 4 additions & 4 deletions lib/model/query/assignments.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

const { sql } = require('slonik');
const { Actor, Assignment } = require('../frames');
const { extender, equals, QueryOptions } = require('../../util/db');
const { extender, sqlEquals, QueryOptions } = require('../../util/db');
const { getOrReject } = require('../../util/promise');
const Problem = require('../../util/problem');
const { construct } = require('../../util/util');
Expand Down Expand Up @@ -37,7 +37,7 @@ const grantSystem = (actor, systemName, actee) => ({ Assignments, Roles }) =>
.then((role) => Assignments.grant(actor, role, actee));

const _revoke = (actor, roleId, acteeId) => ({ db }) =>
db.query(sql`delete from assignments where ${equals({ actorId: actor.id, roleId, acteeId })}`)
db.query(sql`delete from assignments where ${sqlEquals({ actorId: actor.id, roleId, acteeId })}`)
.then(({ rowCount }) => Number(rowCount) > 0);

_revoke.audit = (actor, roleId, acteeId) => (log) => {
Expand All @@ -61,7 +61,7 @@ const revokeByActeeId = (acteeId) => ({ run }) =>
const _get = extender(Assignment)(Actor)((fields, extend, options) => sql`
select ${fields} from assignments
${extend|| sql`inner join actors on actors.id=assignments."actorId"`}
where ${equals(options.condition)}`);
where ${sqlEquals(options.condition)}`);
const getByActeeId = (acteeId, options = QueryOptions.none) => ({ all }) =>
_get(all, options.withCondition({ 'assignments.acteeId': acteeId }));
const getByActeeAndRoleId = (acteeId, roleId, options = QueryOptions.none) => ({ all }) =>
Expand All @@ -71,7 +71,7 @@ const _getForForms = extender(Assignment, Assignment.FormSummary)(Actor)((fields
select ${fields} from assignments
inner join forms on forms."acteeId"=assignments."acteeId"
${extend|| sql`inner join actors on actors.id=assignments."actorId"`}
where ${equals(options.condition)}
where ${sqlEquals(options.condition)}
and forms."deletedAt" is null`);
const getForFormsByProjectId = (projectId, options = QueryOptions.none) => ({ all }) =>
_getForForms(all, options.withCondition({ projectId }));
Expand Down
4 changes: 2 additions & 2 deletions lib/model/query/audits.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
const { sql } = require('slonik');
const { map, mergeLeft } = require('ramda');
const { Actee, Actor, Audit, Dataset, Entity, Form, Project, Submission } = require('../frames');
const { extender, equals, page, QueryOptions, unjoiner } = require('../../util/db');
const { extender, sqlEquals, page, QueryOptions, unjoiner } = require('../../util/db');
const Option = require('../../util/option');
const { construct } = require('../../util/util');

Expand Down Expand Up @@ -83,7 +83,7 @@ select ${fields} from audits
left outer join form_defs on form_defs.id=forms."currentDefId"
left outer join datasets on datasets."acteeId"=audits."acteeId"
left outer join actees on actees.id=audits."acteeId"`}
where ${equals(options.condition)} and ${auditFilterer(options)}
where ${sqlEquals(options.condition)} and ${auditFilterer(options)}
order by "loggedAt" desc, audits.id desc
${page(options)}`);
const get = (options = QueryOptions.none) => ({ all }) =>
Expand Down
8 changes: 4 additions & 4 deletions lib/model/query/datasets.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
// except according to the terms contained in the LICENSE file.

const { sql } = require('slonik');
const { extender, insert, QueryOptions, equals, unjoiner, updater } = require('../../util/db');
const { extender, insert, QueryOptions, sqlEquals, unjoiner, updater } = require('../../util/db');
const { Dataset, Form, Audit } = require('../frames');
const { validatePropertyName } = require('../../data/dataset');
const { difference, isEmpty, isNil, either, reduceBy, groupBy, uniqWith, equals: rEquals, map } = require('ramda');
const { difference, isEmpty, isNil, either, reduceBy, groupBy, uniqWith, equals, map } = require('ramda');

const Problem = require('../../util/problem');
const { construct } = require('../../util/util');
Expand Down Expand Up @@ -337,7 +337,7 @@ const _get = extender(Dataset)(Dataset.Extended)((fields, extend, options, publi
) as filtered
on filtered.id=datasets."projectId"
`}
WHERE ${equals(options.condition)}
WHERE ${sqlEquals(options.condition)}
${publishedOnly ? sql`AND "publishedAt" IS NOT NULL` : sql``}
ORDER BY name ASC
`);
Expand Down Expand Up @@ -481,7 +481,7 @@ const getProperties = (datasetId, includeForms = false) => ({ all }) => all(sql`
`)
.then((rows) => (includeForms
? rows.map(({ formName, xmlFormId, ...row }) => new Dataset.Property(row, { formName, xmlFormId }))
: uniqWith(rEquals, rows).map(construct(Dataset.Property))));
: uniqWith(equals, rows).map(construct(Dataset.Property))));

// Returns list of Fields augmented by dataset property name.
// This it used by the submission XML parser to create an entity from a submission.
Expand Down
6 changes: 3 additions & 3 deletions lib/model/query/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
const config = require('config');
const { sql } = require('slonik');
const { Actor, Audit, Entity, Submission, Form } = require('../frames');
const { equals, extender, unjoiner, page, markDeleted, insertMany } = require('../../util/db');
const { sqlEquals, extender, unjoiner, page, markDeleted, insertMany } = require('../../util/db');
const { map, mergeRight, pickAll } = require('ramda');
const { blankStringToNull, construct } = require('../../util/util');
const { QueryOptions } = require('../../util/db');
Expand Down Expand Up @@ -674,7 +674,7 @@ const _get = (includeSource) => {
) submissions ON submissions.id = submission_defs."submissionId"
LEFT JOIN forms ON submissions."formId" = forms.id
`}
WHERE ${equals(options.condition)} AND entities."deletedAt" is ${deleted ? sql`not` : sql``} null
WHERE ${sqlEquals(options.condition)} AND entities."deletedAt" is ${deleted ? sql`not` : sql``} null
ORDER BY entities."createdAt" DESC, entities.id DESC
`);
};
Expand Down Expand Up @@ -706,7 +706,7 @@ const _getDef = extender(Entity.Def, Entity.Def.Source)(Actor.into('creator'))((
${extend||sql`
LEFT JOIN actors ON actors.id=entity_defs."creatorId"
`}
where ${equals(options.condition)} AND entities."deletedAt" IS NULL
where ${sqlEquals(options.condition)} AND entities."deletedAt" IS NULL
order by entity_defs."createdAt", entity_defs.id
`);

Expand Down
4 changes: 2 additions & 2 deletions lib/model/query/field-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
const { sql } = require('slonik');
const { Frame, readable } = require('../frame');
const { Actor, FieldKey } = require('../frames');
const { QueryOptions, extender, equals } = require('../../util/db');
const { QueryOptions, extender, sqlEquals } = require('../../util/db');

const create = (fk, project) => ({ Actors, Sessions }) =>
Actors.createSubtype(fk.with({ projectId: project.id }), project)
Expand All @@ -30,7 +30,7 @@ select ${fields} from field_keys
where action='submission.create'
group by "actorId") as last_usage
on last_usage."actorId"=actors.id`}
where ${equals(options.condition)} and actors."deletedAt" is null
where ${sqlEquals(options.condition)} and actors."deletedAt" is null
order by (sessions.token is not null) desc, actors."createdAt" desc`);

const getAllForProject = (project, options = QueryOptions.none) => ({ all }) =>
Expand Down
4 changes: 2 additions & 2 deletions lib/model/query/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { Actor, Blob, Form } = require('../frames');
const { getFormFields, merge, compare } = require('../../data/schema');
const { getDataset } = require('../../data/dataset');
const { generateToken } = require('../../util/crypto');
const { unjoiner, extender, updater, equals, insert, insertMany, markDeleted, markUndeleted, QueryOptions } = require('../../util/db');
const { unjoiner, extender, updater, sqlEquals, insert, insertMany, markDeleted, markUndeleted, QueryOptions } = require('../../util/db');
const { resolve, reject, timebound } = require('../../util/promise');
const { splitStream } = require('../../util/stream');
const { construct } = require('../../util/util');
Expand Down Expand Up @@ -650,7 +650,7 @@ inner join
on assignment."acteeId" in ('*', 'project', projects."acteeId")
group by id) as filtered
on filtered.id=forms."projectId"`}
where ${equals(options.condition)} and forms."deletedAt" is ${deleted ? sql`not` : sql``} null
where ${sqlEquals(options.condition)} and forms."deletedAt" is ${deleted ? sql`not` : sql``} null
${(actorId == null) ? sql`` : sql`and (form_defs."publishedAt" is not null or filtered."showDraft" = 1)`}
${(actorId == null) ? sql`` : sql`and (state != 'closed' or filtered."showNonOpen" = 1)`}
order by coalesce(form_defs.name, "xmlFormId") asc`);
Expand Down
4 changes: 2 additions & 2 deletions lib/model/query/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

const { sql } = require('slonik');
const { Key, Project } = require('../frames');
const { extender, equals, insert, updater, markDeleted, QueryOptions } = require('../../util/db');
const { extender, sqlEquals, insert, updater, markDeleted, QueryOptions } = require('../../util/db');
const { generateManagedKey, generateVersionSuffix, stripPemEnvelope } = require('../../util/crypto');
const { reject } = require('../../util/promise');
const Problem = require('../../util/problem');
Expand Down Expand Up @@ -116,7 +116,7 @@ inner join
) as filtered
on filtered.id=projects.id
`}
where "deletedAt" is null and ${equals(options.condition)}
where "deletedAt" is null and ${sqlEquals(options.condition)}
order by coalesce(archived, false) asc, name asc`);

const _getWithoutVerbs = extender(Project)(Project.Extended)(_getSql);
Expand Down
4 changes: 2 additions & 2 deletions lib/model/query/public-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
const { sql } = require('slonik');
const { Frame, readable } = require('../frame');
const { Actor, PublicLink } = require('../frames');
const { extender, equals, QueryOptions } = require('../../util/db');
const { extender, sqlEquals, QueryOptions } = require('../../util/db');
const Option = require('../../util/option');

const create = (publicLink, form) => ({ Actors, Assignments, Sessions }) =>
Expand All @@ -30,7 +30,7 @@ select ${fields} from public_links
join actors on public_links."actorId"=actors.id
left outer join sessions on public_links."actorId"=sessions."actorId"
${extend|| sql`join actors as created_by on public_links."createdBy"=created_by.id`}
where actors."deletedAt" is null and ${equals(options.condition)}
where actors."deletedAt" is null and ${sqlEquals(options.condition)}
order by (sessions.token is not null) desc, actors."createdAt" desc`);

const getAllForForm = (form, options) => ({ all }) =>
Expand Down
12 changes: 6 additions & 6 deletions lib/model/query/submissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const { Frame, table } = require('../frame');
const { Actor, Form, Submission } = require('../frames');
const { odataFilter, odataOrderBy, odataExcludeDeleted } = require('../../data/odata-filter');
const { odataToColumnMap, odataSubTableToColumnMap } = require('../../data/submission');
const { unjoiner, extender, equals, page, updater, QueryOptions, insertMany, markDeleted, markUndeleted } = require('../../util/db');
const { unjoiner, extender, sqlEquals, page, updater, QueryOptions, insertMany, markDeleted, markUndeleted } = require('../../util/db');
const { blankStringToNull, construct } = require('../../util/util');
const Problem = require('../../util/problem');
const { streamEncBlobs } = require('../../util/blob');
Expand Down Expand Up @@ -185,7 +185,7 @@ const _get = extender(Submission, Submission.Def.into('currentVersion'))(Actor.i
left outer join actors on actors.id=submissions."submitterId"
left outer join actors current_version_actors on current_version_actors.id=submission_defs."submitterId"
`}
where ${equals(options.condition)} and draft=${draft} and submissions."deletedAt" is null
where ${sqlEquals(options.condition)} and draft=${draft} and submissions."deletedAt" is null
order by submissions."createdAt" desc, submissions.id desc
${page(options)}`);

Expand Down Expand Up @@ -223,14 +223,14 @@ const joinOnSkiptoken = (skiptoken, formId, draft) => {
return sql`INNER JOIN (
SELECT id, "createdAt"
FROM submissions
WHERE ${equals({ formId, draft })} AND "instanceId" = ${skiptoken.instanceId}
WHERE ${sqlEquals({ formId, draft })} AND "instanceId" = ${skiptoken.instanceId}
) AS cursor ON
submissions."createdAt" < cursor."createdAt" OR
(submissions."createdAt" = cursor."createdAt" AND submissions.id < cursor.id)`;
};

const countByFormId = (formId, draft, options = QueryOptions.none) => ({ one }) => {
const filter = sql`${equals({ formId, draft })} AND
const filter = sql`${sqlEquals({ formId, draft })} AND
${odataExcludeDeleted(options.filter, odataToColumnMap)} AND
${odataFilter(options.filter, odataToColumnMap)}`;
return one(sql`
Expand Down Expand Up @@ -306,7 +306,7 @@ ${extend|| sql`
left outer join actors on actors.id=submission_defs."submitterId"
inner join (select id, version as "formVersion" from form_defs) as fds
on fds.id=submission_defs."formDefId"`}
where ${equals(options.condition)}
where ${sqlEquals(options.condition)}
order by submission_defs.id desc`);

const getAnyDefByFormAndInstanceId = (formId, instanceId, draft, options = QueryOptions.none) => ({ maybeOne }) =>
Expand Down Expand Up @@ -381,7 +381,7 @@ ${joinOnSkiptoken(options.skiptoken, formId, draft)}
where
${encrypted ? sql`(form_defs."encKeyId" is null or form_defs."encKeyId" in (${sql.join(keyIds, sql`,`)})) and` : sql``}
${odataFilter(options.filter, options.isSubmissionsTable ? odataToColumnMap : odataSubTableToColumnMap)} and
${equals(options.condition)}
${sqlEquals(options.condition)}
and submission_defs.current=true and submissions."formId"=${formId} and ${odataExcludeDeleted(options.filter, options.isSubmissionsTable ? odataToColumnMap : odataSubTableToColumnMap)}
${options.orderby ? sql`
${odataOrderBy(options.orderby, odataToColumnMap, 'submissions.id')}`
Expand Down
4 changes: 2 additions & 2 deletions lib/model/query/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { sql } = require('slonik');
const { map } = require('ramda');
const { Actor, User } = require('../frames');
const { hashPassword } = require('../../util/crypto');
const { unjoiner, page, equals, QueryOptions } = require('../../util/db');
const { unjoiner, page, sqlEquals, QueryOptions } = require('../../util/db');
const { reject } = require('../../util/promise');
const Problem = require('../../util/problem');

Expand Down Expand Up @@ -69,7 +69,7 @@ ${options.ifArg('q', (q) => sql`
left join lateral
greatest(word_similarity("displayName", ${q}) + similarity("displayName", ${q}),
word_similarity(email, ${q}) + similarity(email, ${q})) as score on true`)}
where ${equals(options.condition)} and actors."deletedAt" is null
where ${sqlEquals(options.condition)} and actors."deletedAt" is null
${options.ifArg('q', () => sql`and score > 0.5`)}
order by ${options.ifArg('q', () => sql`score desc,`)} email asc
${page(options)}`;
Expand Down
4 changes: 2 additions & 2 deletions lib/util/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ const markUndeleted = (obj) =>
////////////////////////////////////////
// query fragment util

const equals = (obj) => {
const sqlEquals = (obj) => {
const keys = Object.keys(obj);
if (keys.length === 0) return sql`true`;

Expand Down Expand Up @@ -576,7 +576,7 @@ const postgresErrorToProblem = (x) => {

module.exports = {
connectionString, connectionObject,
unjoiner, extender, equals, page, queryFuncs,
unjoiner, extender, sqlEquals, page, queryFuncs,
insert, insertMany, updater, markDeleted, markUndeleted,
QueryOptions,
postgresErrorToProblem
Expand Down
10 changes: 5 additions & 5 deletions test/unit/util/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,19 +389,19 @@ returning *`);
});
});

describe('equals', () => {
const { equals } = util;
describe('sqlEquals', () => {
const { sqlEquals } = util;
it('should do nothing if given no conditions', () => {
equals({}).should.eql(sql`true`);
sqlEquals({}).should.eql(sql`true`);
});

it('should match k/v pairs', () => {
equals({ x: 2, y: 3 })
sqlEquals({ x: 2, y: 3 })
.should.eql(sql.join([ sql`"x"=${2}`, sql`"y"=${3}` ], sql` and `));
});

it('should split compound keys', () => {
equals({ 'x.y': 2 })
sqlEquals({ 'x.y': 2 })
.should.eql(sql.join([ sql`"x"."y"=${2}` ], sql` and `));
});
});
Expand Down

0 comments on commit 644fe0f

Please sign in to comment.