slonik
Version:
A Node.js PostgreSQL client with strict types, detailed logging and assertions.
26 lines • 1.06 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.oneFirst = void 0;
const one_1 = require("./one");
const errors_1 = require("@slonik/errors");
const utilities_1 = require("@slonik/utilities");
/**
* Makes a query and expects exactly one result.
* Returns value of the first column.
* @throws NotFoundError If query returns no rows.
* @throws DataIntegrityError If query returns multiple rows.
*/
const oneFirst = async (log, connection, clientConfiguration, slonikSql, inheritedQueryId) => {
const queryId = inheritedQueryId ?? (0, utilities_1.generateUid)();
const row = await (0, one_1.one)(log, connection, clientConfiguration, slonikSql, queryId);
const keys = Object.keys(row);
if (keys.length > 1) {
log.error({
queryId,
}, 'DataIntegrityError');
throw new errors_1.UnexpectedStateError('Expected query to return one result, but received multiple results.');
}
return row[keys[0]];
};
exports.oneFirst = oneFirst;
//# sourceMappingURL=oneFirst.js.map
;