sedk-postgres
Version:
Simple SQL builder and validator
85 lines • 3.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.builder = exports.Builder = void 0;
const singletoneConstants_1 = require("./singletoneConstants");
const steps_1 = require("./steps");
const option_1 = require("./option");
const errors_1 = require("./errors");
/**
* @deprecated - use builder() function instead
*/
class Builder {
constructor(database, option) {
this.data = getDataObj(database, option);
this.rootStep = new steps_1.RootStep(this.data);
}
select(...items) {
if (items[0] instanceof singletoneConstants_1.Distinct) {
if (items.length <= 1)
throw new Error('Select step must have at least one parameter after DISTINCT');
items.shift(); //remove first item the DISTINCT item
Builder.throwIfMoreThanOneDistinctOrAll(items);
const newItems = items;
return this.rootStep.selectDistinct(...newItems);
}
if (items[0] instanceof singletoneConstants_1.All) {
items.shift(); //remove first item the ALL item
Builder.throwIfMoreThanOneDistinctOrAll(items);
const newItems = items;
return this.rootStep.selectAll(...newItems);
}
Builder.throwIfMoreThanOneDistinctOrAll(items);
const newItems = items;
return this.rootStep.select(...newItems);
}
selectDistinct(...items) {
return this.rootStep.selectDistinct(...items);
}
selectAll(...items) {
return this.rootStep.selectAll(...items);
}
selectAsteriskFrom(...tables) {
return this.rootStep.select(singletoneConstants_1.ASTERISK).from(...tables);
}
delete() {
return this.rootStep.delete();
}
deleteFrom(fromItem) {
return this.rootStep.delete().from(fromItem);
}
insert() {
return this.rootStep.insert();
}
insertInto(table, ...columns) {
if (columns.length === 0) {
return this.rootStep.insert().into(table);
}
return this.rootStep.insert().into(table, ...columns);
}
update(table) {
return this.rootStep.update(table);
}
/** @deprecated - Not needed since version 0.15.0 */
cleanUp() {
// Do nothing
return this;
}
static throwIfMoreThanOneDistinctOrAll(items) {
items.forEach(it => {
if (it instanceof singletoneConstants_1.Distinct || it instanceof singletoneConstants_1.All)
throw new errors_1.MoreThanOneDistinctOrAllError('You can not have more than one DISTINCT or ALL');
});
}
}
exports.Builder = Builder;
function builder(database, option) {
return new steps_1.RootStep(getDataObj(database, option));
}
exports.builder = builder;
function getDataObj(database, option) {
return {
database: database,
option: (0, option_1.fillUndefinedOptionsWithDefault)(option !== null && option !== void 0 ? option : {}),
};
}
//# sourceMappingURL=builder.js.map