Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var R = require('ramda');
- var knex = require('knex')({
- client: 'pg',
- connection: {
- host: 'localhost',
- port: '35432',
- database: 'vamos',
- password: 'vamos',
- user: 'vamos',
- }
- });
- const withEntity = (qb, table, fk, alias = table) => qb.leftJoin(table, fk, `${table}.id`).select(knex.raw(`to_json(${table}) ${alias}`))
- const queryWrapper = (query, resolver) => {
- let mappings = {};
- return {
- query,
- addContact(...path) {
- if (mappings.contact) throw new Error('addContact called twice');
- mappings.contact = path.length > 0 ? R.flatten(path) : ['contact'];
- query.modify(withEntity, 'contact', 'beuz.contact_id')
- return this;
- },
- addCompany(...path) {
- if (mappings.company) throw new Error('addCompany called twice');
- mappings.company = path.length > 0 ? R.flatten(path) : ['company'];
- query.modify(withEntity, 'company', 'contact.company_id')
- return this;
- },
- then(...args) {
- return query
- .map(row => {
- const keys = Object.keys(mappings);
- return keys.reduce((acc, key) => R.assocPath(mappings[key], row[key], acc), R.omit(keys, row));
- })
- .map(resolver)
- .then(...args);
- }
- }
- }
- const contactR = contact => ({
- id: contact.id,
- firstname: contact.firstname,
- lastname: contact.lastname,
- company: contact.company
- });
- const resolver = beuz => ({
- id: beuz.id,
- contact: contactR(beuz.contact),
- })
- const db = {
- getBeuz(filters, sqlParams) {
- return queryWrapper(knex('beuz').select('beuz.id').where('beuz.id', 1), resolver);
- },
- }
- const a = db.getBeuz({}, {})
- .addContact()
- .addCompany(['contact', 'company'])
- .then(res => {
- console.log(res);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement