UNPKG

@sequeljs/ast

Version:

A SQL AST manager for JavaScript

50 lines 1.42 kB
import InsertStatement from '../nodes/InsertStatement'; import SQLLiteral from '../nodes/SQLLiteral'; import ValuesList from '../nodes/ValuesList'; import TreeManager from './TreeManager'; export default class InsertManager extends TreeManager { get columns() { return this.ast.columns; } set values(val) { this.ast.values = val; } constructor() { super(new InsertStatement()); this.ctx = this.ast; } createValues(values) { return new ValuesList([values]); } createValuesList(rows) { return new ValuesList(rows); } insert(fields) { if (!fields || (Array.isArray(fields) && fields.length <= 0)) { return this; } if (typeof fields === 'string') { this.ast.values = new SQLLiteral(fields); } else { if (!this.ast.relation) { this.ast.relation = fields[0][0].relation; } const values = []; fields.forEach(([column, value]) => { this.ast.columns.push(column); values.push(value); }); this.ast.values = this.createValues(values); } return this; } into(table) { this.ctx.relation = table; return this; } select(select) { this.ast.select = select; } } //# sourceMappingURL=InsertManager.js.map