Skip to content

Instantly share code, notes, and snippets.

@RubyRubenstahl
Last active May 22, 2018 14:44
Show Gist options
  • Save RubyRubenstahl/2a09e2a46ddc22628051966438f4c25c to your computer and use it in GitHub Desktop.
Save RubyRubenstahl/2a09e2a46ddc22628051966438f4c25c to your computer and use it in GitHub Desktop.
const foreignKeyJoin = (
idKey,
populatedKey,
serviceName,
) => args => async (item, context) => {
const res = await context.app.services[serviceName].find({
query: {
_id: { $in: item[idKey] }
}
});
item[populatedKey] = Array.isArray(item[idKey]) ? res[0] : res;
};
module.exports = foreignKeyJoin;
const { authenticate } = require('@feathersjs/authentication').hooks;
const {fastJoin } = require('feathers-hooks-common');
const foreignKeyJoin = require('../../util/foreignKeyJoin');
const resolvers={
joins:{
items: foreignKeyJoin('itemIds', 'items', 'schedule-items'),
conditions: foreignKeyJoin('conditionsGroupId', 'conditions', 'condition-groups')
}
};
const createConditionsGroup = async context => {
const conditionGroup = await context.app.service('condition-groups').create({});
context.data.conditionsGroupId = conditionGroup._id.toHexString();
return context;
};
module.exports = {
before: {
create: [createConditionsGroup],
},
after: {
all: [fastJoin(resolvers)],
},
};
module.exports = function (app) {
const mongooseClient = app.get('mongooseClient');
const { Schema } = mongooseClient;
const schedules = new Schema({
name: {type: String, default: 'New Schedule'},
enabled: {type: Boolean, default: true},
conditionsGroupId: {type: Schema.Types.ObjectId, default: null},
itemIds: {type: [Schema.Types.ObjectId], default: null},
}, {
timestamps: true,
lean: true
});
return mongooseClient.model('schedules', schedules);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment