Skip to content

Instantly share code, notes, and snippets.

@bas080
Created October 1, 2017 11:07
Show Gist options
  • Save bas080/e08145e90ce474e80956f870018ecba2 to your computer and use it in GitHub Desktop.
Save bas080/e08145e90ce474e80956f870018ecba2 to your computer and use it in GitHub Desktop.
'use strict';
const knex = require('knex');
module.exports = {
knexline,
}
function knexline(model, qb) {
const modelTable = model.identity;
qb = qb || knex(modelTable);
const methods = {
criteria,
join,
populate,
queryBuilder: () => qb,
};
return methods;
function populate() {
// once it's necessary
}
function criteria(criteria) {
/* already built */
qb = qb
return methods;
}
function join(property) {
const [table, field] = property.split('.');
qb = qb.join(
table,
`${modelTable}.id`,
`${table}.id`
);
return methods;
}
}
const columns = [{
property: 'location.name',
alias: 'Location',
}];
//playground
const model = {
identity: 'machine',
};
const criteria = {}
const joins = ['location.name'];
const selects = ['name', 'id'];
const query = joins.reduce(
(kl, join) => kl.join(join),
knexline(model).criteria(criteria)
)
.queryBuilder()
.select(selects)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment