Skip to content

Instantly share code, notes, and snippets.

@RubyRubenstahl
Last active January 29, 2020 15:30
Show Gist options
  • Save RubyRubenstahl/7c0d107482fae0c8a456fee6a59c3bcd to your computer and use it in GitHub Desktop.
Save RubyRubenstahl/7c0d107482fae0c8a456fee6a59c3bcd to your computer and use it in GitHub Desktop.
const createEntityValidationHooks = require('./create-entity-validation-hooks');
/**
* Create a service backed by the entity service.
* @param {object} app - Feathers app instance
* @param {string} options.entityType - Name of the entity type
* @param {string} options.serviceName - Name of the service
* @param {object} options.schema - The schema used to validate create/patch operations
* @param {object} options.hooks - Feathers hooks
* @param {string} options.singular - Name of a single entity
* @param {string} options.plural - Plural name for entities
* @param {string} options.icon - Fontawesome or $vuetify.icons.* icon
*/
async function createEntityService(app, options) {
const buildParams = params => {
if (params === undefined) {
params = {};
}
if (params.query === undefined) {
params.query = {};
}
params.service = options.serviceName;
return params;
};
await app.dbReady
let { entityType, serviceName, schema, hooks } = options;
if (!entityType) {
throw new Error('entityType Option is required when creating an entity service')
}
if (!schema) {
throw new Error(
"schema option is required when creating an entity service"
);
}
const { validateCreate, validatePatch } = createEntityValidationHooks(entityType, schema);
// Inject the hooks
hooks.before.create = [...hooks.before.create, validateCreate];
hooks.before.patch = [...hooks.before.patch, validatePatch];
const entityService = app.service('entities')
// Build the service
let service = {
async find(params) {
params = buildParams(params);
params.query.entityType = entityType;
return entityService.find(params);
},
async get(id, params) {
params = buildParams(params);
params.query.entityType = entityType;
return entityService.get(id, params)
},
async create(data, params) {
params = buildParams(params);
params.entityType = entityType;
return entityService.create(data, params);
},
async update(id, data, params) {
params = buildParams(params);
params.query.entityType = entityType;
},
async patch(id, data, params) {
params = buildParams(params);
params.query.entityType = entityType;
return entityService.patch(id, data, params);
},
async remove(id, params) {
params = buildParams(params);
params.query.entityType = entityType;
return entityService.remove(id, params);
},
async setup(app, path) {
console.log(path)
}
};
app.use(serviceName, service);
app.service(serviceName).hooks(hooks)
// Register the entity type
await app.service('entity-types').create(options);
const createdService = app.service(serviceName);
createdService.options = options
return createdService;
}
module.exports = createEntityService;
const schema = require('./nodes.schema');
const hooks = require("./nodes.hooks");
const logger = require("../../logger");
const initNode = require("./initNode");
const createEntityService = require("../create-entity-service");
module.exports = async function (app) {
logger.info('Configuring node service')
const service = await createEntityService(app, {
entityType: "node",
hooks,
schema,
serviceName: "nodes",
singular: "Node",
plural: "Nodes",
icon: "$vuetify.icons.node",
sync: {
up: {
discard: ["upstreamServer"]
},
down: {
discard: ["upstreamServer"]
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment