Last active
May 22, 2018 14:44
-
-
Save RubyRubenstahl/2a09e2a46ddc22628051966438f4c25c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)], | |
}, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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