So this isn't really a question, more a post on how I solved this specific situation:
Desired Situation
I wanted to be able to select data from a feathersjs find
service call. However, I didn't want to have all columns returned, but different ones depending on the situation and have associated models in the results too. If that wasn't enough, I needed to pass filter criteria to be used within the whole selection. Also it wasn't to be done client side, well as little as possible, so manipulation wouldn't be a risk
Developed Solution
This is the solution I came up with. I'm using vuex to do the api call from the client, and in the backend I'm using feathers-custom-methods to have a service with several methods in it so I can bundle it per module, I call these services helper.service
. So one would be e.g. terms
with a method getSectionTerms
for basic selection for a specific site section, getTerms
for administration (returning everything). I haven't described every single step to depth, but may do it later.
Step 1: Install feathers-custom-methods as described in the initial documentation
Step 2: Create a service file. Mine is like this.
module.exports = function () {
const app = this;
const helpers = {
create () {},
setup (app, path) {},
findTerms(oFilters) {
const oTerms = null;
return getData(
app,
oFilters,
['term_type'],
['term_description'],
['metadata_code'],
['metadata_code']
);
}
};
app.use('helper_terms', helpers);
};
function getData(app, oFilters, arrColTerms, arrColTranslations, arrColLanguage, arrColSection) {
return app.service('mtp_sys_app_terms').find({
query: {
is_active: oFilters.is_active,
$select: arrColTerms
},
sequelize: {
raw: false,
include: [
{
model: app.service('/mtp_trans_sys_app_terms').Model,
as: 'translations',
attributes: arrColTranslations,
include: [
{
model: app.service('/mtp_sys_app_metadata').Model,
as: 'language',
attributes: arrColLanguage,
where: {
metadata_type: 'app_language'
}
}
]
}, {
model: app.service('/mtp_sys_app_metadata').Model,
as: 'section',
attributes: arrColSection,
where: {
metadata_type: 'app_section'
}
}
]
}
});
};
The first part in the file is making the service itself. create
and setup
are required to get it working. Then you can add any amount of methods you need. Since I'll be repeating this type of data selection but want different columns and filters, I made a function to call the initial featherjs service find method. This is the second part. It's been configured in such a way that you needn't rewrite numerous times. Okay, here and there I need to add some checks and setting with default values in case a parameter isn't passed.
Step 3: Configure the service to be used. I did it in my index.js
file
...
app.configure(helper_terms);
app.configure(customMethods({
methods: {
helper_terms: [
'findTerms'
]
}
}));
...
In this file, I'll add more options to the array of helper_terms
we I make another entry in the file of step 2.
Step 4: configure the client. In my case a call through the vuex store
const getters = {
getTerms () {
return oFilters => api.service('helper_terms')
.findTerms(
oFilters
);
}
};
This will pass filter criteria to the backend. I'm still change this with more options but it is working for now. I pass an object to the backend with any amount of filters in it such as is_active: 0
or language: 'en'
Step 5 Call the vuex function somewhere to get the data
this.$store.getters.getTerms({
strLanguage: this.$store.state.sysLanguage,
is_active: 1
}).then(oTerms => {
this.oTerms = oTerms;
});