UNPKG

vitamin

Version:

Data Mapper library for Node.js applications

86 lines (71 loc) 1.45 kB
import { Column } from 'vitamin-query/expression' import { isArray, toArray } from 'lodash' import Builder from 'vitamin-query' /** * @class Query * * the mapper adds the */ export default class { /** * * @param {String} dialect * @constructor */ constructor(dialect) { this.table = null this.alias = null this.builder = new Builder(dialect) } /** * * @param {String} name * @param {String} as * @return this query */ setTable(name, as = null) { this.setAlias(as || name) this.table = name return this } /** * * @param {String} as * @return this query */ setAlias(as) { this.alias = as return this } /** * * @param {Array|String} columns * @return this query */ select(columns) { if (! isArray(columns) ) columns = toArray(arguments) columns.forEach(attr => { if ( attr.indexOf('-') === 0 ) return this.unselect(attr.substr(1)) this.attributes.push(this.getColumn(attr)) }) return this } /** * * @param {Array|String} columns * @return this query */ unselect(columns) { } /** * * @param {String} attr * @return Column instance */ getColumn(attr) { // TODO get the column name of the attribute from the mapper var column = this.mapper.getColumnName(attr) return new Column(column, this.alias) } }