Skip to content

Commit 4a9ac10

Browse files
fixed userSettings
1 parent 41d0fca commit 4a9ac10

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

server/hooks/map-create-to-upsert.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// upsert if needed
2+
// https://blog.feathersjs.com/how-to-use-upsert-for-better-performance-with-feathers-mongoose-ec40e45d0d4a
3+
4+
module.exports = function (upsertQuery) {
5+
if (typeof upsertQuery !== 'function') {
6+
console.warn('No `upsertQuery` function was passed to the mapCreateToUpsert hook. Please set params.upsertQuery in the hook context to dynamically declare the function.');
7+
}
8+
9+
return function mapCreateToUpsert (context) {
10+
const { service, data, params } = context; // const data = { address: '123', identifier: 'my-identifier' }
11+
12+
upsertQuery = params.upsertQuery || upsertQuery;
13+
if (typeof upsertQuery !== 'function') {
14+
throw new Error('you must pass a `upsertQuery` function to the mapCreateToUpsert hook in the options or as `params.upsertQuery` in the hook context');
15+
}
16+
17+
params.mongoose = Object.assign({}, params.mongoose, { upsert: true });
18+
params.query = upsertQuery(context); // { address: '123' }
19+
20+
return service.patch(null, data, params)
21+
.then(result => {
22+
context.result = result;
23+
return context;
24+
});
25+
};
26+
};

server/services/users/users.hooks.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const cleanupPersonalData = when(isProvider('external'),
2121
discard('email', 'verifyToken', 'verifyShortToken', 'doiToken')
2222
);
2323

24-
2524
const restrict = [
2625
authenticate('jwt'),
2726
restrictToOwner({
@@ -53,7 +52,7 @@ const candosSchema = {
5352
const userSettingsSchema = {
5453
include: {
5554
service: 'usersettings',
56-
nameAs: 'usersettings',
55+
nameAs: 'userSettings',
5756
parentField: '_id',
5857
childField: 'userId',
5958
asArray: false

server/services/usersettings/usersettings.hooks.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
1+
const { restrictToOwner } = require('feathers-authentication-hooks');
2+
const mapCreateToUpsert = require('../../hooks/map-create-to-upsert');
23

34
module.exports = {
45
before: {
5-
all: [
6-
],
6+
all: [],
77
find: [],
88
get: [],
9-
create: [],
9+
create: [
10+
mapCreateToUpsert(context => {
11+
const { data } = context;
12+
return { userId: data.userId };
13+
})
14+
],
1015
update: [],
1116
patch: [],
1217
remove: []

0 commit comments

Comments
 (0)