@riao/dbal
Version:
226 lines • 8.89 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.QueryRepository = void 0;
const repository_1 = require("../repository");
const functions_1 = require("../functions");
/**
* Use the Query Repository to query a database
*/
class QueryRepository extends repository_1.Repository {
constructor(options) {
var _a, _b, _c;
super(options);
this.table = (_a = options.table) !== null && _a !== void 0 ? _a : this.table;
this.identifiedBy = (_b = options.identifiedBy) !== null && _b !== void 0 ? _b : this.identifiedBy;
this.queryBuilderType =
(_c = options.queryBuilderType) !== null && _c !== void 0 ? _c : this.queryBuilderType;
}
init(options) {
super.init(options);
this.schema = options.schema;
this.getIdentifier();
this.isReady = true;
}
getTableName() {
var _a;
return (_a = this.table) !== null && _a !== void 0 ? _a : null;
}
getIdentifier() {
var _a;
if (!this.identifiedBy &&
this.table &&
this.schema &&
this.table in this.schema.tables) {
this.identifiedBy = this.schema.tables[this.table].primaryKey;
}
return (_a = this.identifiedBy) !== null && _a !== void 0 ? _a : null;
}
getQueryBuilder() {
return new this.queryBuilderType();
}
/**
* Finds entities matching given criteria
*
* @param selectQuery Select query
* @returns Found entities
*/
find(selectQuery) {
return __awaiter(this, void 0, void 0, function* () {
selectQuery.table = selectQuery.table || this.table;
const query = this.getQueryBuilder()
.select(selectQuery)
.toDatabaseQuery();
const { results } = yield this.query(query);
return (results !== null && results !== void 0 ? results : []);
});
}
/**
* Finds one entity matching given criteria
*
* @param selectQuery Select query
* @returns Found entity or `null`
*/
findOne(selectQuery) {
return __awaiter(this, void 0, void 0, function* () {
selectQuery.table = selectQuery.table || this.table;
const query = this.getQueryBuilder()
.select(Object.assign(Object.assign({}, selectQuery), { limit: 1 }))
.toDatabaseQuery();
const { results } = yield this.query(query);
if (!(results === null || results === void 0 ? void 0 : results.length)) {
return null;
}
return results[0];
});
}
findById(id) {
return __awaiter(this, void 0, void 0, function* () {
// Ready-check before throwing a PK error
this.readyCheck();
if (!this.getIdentifier()) {
throw new Error('findById() Could not determine PK for table "' +
this.table +
'"');
}
return yield this.findOne({
where: { [this.identifiedBy]: id },
});
});
}
/**
* Finds one entity matching given criteria, or throws error
*
* @param selectQuery Select query
* @param error Error to throw
* @returns Found entity
*/
findOneOrFail(selectQuery, error) {
return __awaiter(this, void 0, void 0, function* () {
selectQuery.table = selectQuery.table || this.table;
const result = yield this.findOne(selectQuery);
if (result === null) {
throw error !== null && error !== void 0 ? error : new Error('Result not found!');
}
return result;
});
}
count(selectQuery = {}, params) {
return __awaiter(this, void 0, void 0, function* () {
const { count } = yield this.findOneOrFail(Object.assign(Object.assign({}, selectQuery), { columns: [
{
query: functions_1.DatabaseFunctions.count(params),
as: 'count',
},
] }));
return +count;
});
}
/**
* Insert one or multiple items into the database
*
* @param insertOptions Insert options
* @returns Inserted item(s)
*/
insert(insertOptions) {
return __awaiter(this, void 0, void 0, function* () {
const table = insertOptions.table || this.table;
if (!table) {
throw new Error('Cannot insert on query repository without a table.');
}
const query = this.getQueryBuilder()
.insert(Object.assign({ table, primaryKey: undefined }, insertOptions))
.toDatabaseQuery();
yield this.query(query);
});
}
/**
* Insert one item into the database
*
* @param insertOptions Insert options
* @returns Inserted item
*/
insertOne(insertOptions) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
// Ready-check before throwing a PK error
this.readyCheck();
const table = insertOptions.table || this.table;
if (!table) {
throw new Error('Query repository cannot insertOne without a table.');
}
insertOptions.primaryKey =
insertOptions.primaryKey ||
((_b = (_a = this.schema) === null || _a === void 0 ? void 0 : _a.tables[table]) === null || _b === void 0 ? void 0 : _b.primaryKey) ||
this.identifiedBy;
if (!insertOptions.primaryKey && !insertOptions.ignoreReturnId) {
throw new Error('insertOne() cannot determine PK to return. Pass a primaryKey argument or ignoreReturnId: true to suppress this safety. https://github.com/riao-project/riao-dbal/issues/4');
}
const query = this.getQueryBuilder()
.insert(Object.assign(Object.assign({ table }, insertOptions), { records: insertOptions.record }))
.toDatabaseQuery();
const { results } = yield this.query(query);
if (insertOptions.primaryKey && Array.isArray(results)) {
return results[0];
}
else {
return {};
}
});
}
/**
* Update items in the database
*
* @param updateOptions Update options
*/
update(updateOptions) {
return __awaiter(this, void 0, void 0, function* () {
const table = updateOptions.table || this.table;
if (!table) {
throw new Error('Query repository cannot update() without a table.');
}
const query = this.getQueryBuilder()
.update(Object.assign({ table }, updateOptions))
.toDatabaseQuery();
yield this.query(query);
});
}
/**
* Set a variable
*
* @param setOptions
*/
set(setOptions) {
return __awaiter(this, void 0, void 0, function* () {
const query = this.getQueryBuilder().set(setOptions).toDatabaseQuery();
yield this.query(query);
});
}
/**
* Delete items from the database
*
* @param deleteOptions Delete options
*/
delete(deleteOptions) {
return __awaiter(this, void 0, void 0, function* () {
const table = deleteOptions.table || this.table;
if (!table) {
throw new Error('Query repository cannot update() without a table.');
}
const query = this.getQueryBuilder()
.delete(Object.assign({ table }, deleteOptions))
.toDatabaseQuery();
yield this.query(query);
});
}
}
exports.QueryRepository = QueryRepository;
//# sourceMappingURL=query-repository.js.map