Skip to content

Commit 526c374

Browse files
alxndrsnspwoodcock
authored andcommitted
UserPreferences: simplify DELETE queries (getodk#1427)
Also use existing return value from postgres wire protocol instead of generating an extra value.
1 parent 2f1bffc commit 526c374

File tree

1 file changed

+13
-27
lines changed

1 file changed

+13
-27
lines changed

lib/model/query/user-preferences.js

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// except according to the terms contained in the LICENSE file.
99

1010
const { sql } = require('slonik');
11-
11+
const Option = require('../../util/option');
1212

1313
const getForUser = (userId) => ({ one }) =>
1414
one(sql`
@@ -87,37 +87,23 @@ const _writeProperty = (tablename, subject, userId, propertyName, propertyValue)
8787
`);
8888
};
8989

90-
91-
const _removeProperty = (tablename, subject, userId, propertyName) => ({ maybeOne }) => {
92-
const targetColumns = ['userId', 'propertyName']
93-
.concat((subject === null) ? [] : ['projectId'])
94-
.map(el => sql.identifier([el]));
95-
96-
const values = [userId, propertyName]
97-
.concat((subject === null) ? [] : [subject]);
98-
99-
return maybeOne(sql`
100-
DELETE FROM ${sql.identifier([tablename])}
101-
WHERE
102-
(${sql.join(targetColumns, `, `)})
103-
=
104-
(${sql.join(values, `, `)})
105-
RETURNING
106-
1 AS "deleted_count"
107-
`);
108-
};
109-
110-
11190
const writeSiteProperty = (userId, propertyName, propertyValue) => ({ one }) =>
11291
_writeProperty('user_site_preferences', null, userId, propertyName, propertyValue)({ one });
11392

114-
const removeSiteProperty = (userId, propertyName) => ({ maybeOne }) =>
115-
_removeProperty('user_site_preferences', null, userId, propertyName)({ maybeOne });
116-
11793
const writeProjectProperty = (userId, projectId, propertyName, propertyValue) => ({ one }) =>
11894
_writeProperty('user_project_preferences', projectId, userId, propertyName, propertyValue)({ one });
11995

120-
const removeProjectProperty = (userId, projectId, propertyName) => ({ maybeOne }) =>
121-
_removeProperty('user_project_preferences', projectId, userId, propertyName)({ maybeOne });
96+
const removeSiteProperty = (userId, propertyName) => ({ db }) => db.query(sql`
97+
DELETE FROM user_site_preferences
98+
WHERE "userId" = ${userId}
99+
AND "propertyName" = ${propertyName}
100+
`).then(({ rowCount }) => Option.of(rowCount || null));
101+
102+
const removeProjectProperty = (userId, projectId, propertyName) => async ({ db }) => db.query(sql`
103+
DELETE FROM user_project_preferences
104+
WHERE "userId" = ${userId}
105+
AND "propertyName" = ${propertyName}
106+
AND "projectId" = ${projectId}
107+
`).then(({ rowCount }) => Option.of(rowCount || null));
122108

123109
module.exports = { removeSiteProperty, writeSiteProperty, writeProjectProperty, removeProjectProperty, getForUser };

0 commit comments

Comments
 (0)