slonik
Version:
A Node.js PostgreSQL client with strict types, detailed logging and assertions.
26 lines • 942 B
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.maybeOne = void 0;
const query_1 = require("./query");
const errors_1 = require("@slonik/errors");
const utilities_1 = require("@slonik/utilities");
/**
* Makes a query and expects exactly one result.
* @throws DataIntegrityError If query returns multiple rows.
*/
const maybeOne = async (log, connection, clientConfiguration, slonikQuery, inheritedQueryId) => {
const queryId = inheritedQueryId ?? (0, utilities_1.generateUid)();
const { rows } = await (0, query_1.query)(log, connection, clientConfiguration, slonikQuery, queryId);
if (rows.length === 0) {
return null;
}
if (rows.length > 1) {
log.error({
queryId,
}, 'DataIntegrityError');
throw new errors_1.DataIntegrityError(slonikQuery);
}
return rows[0];
};
exports.maybeOne = maybeOne;
//# sourceMappingURL=maybeOne.js.map
;