slonik
Version:
A Node.js PostgreSQL client with strict types, detailed logging and assertions.
52 lines • 1.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const createPool_1 = require("../factories/createPool");
const createTestRunner_1 = require("../helpers.test/createTestRunner");
const errors_1 = require("@slonik/errors");
const pg_driver_1 = require("@slonik/pg-driver");
const sql_tag_1 = require("@slonik/sql-tag");
const driverFactory = (0, pg_driver_1.createPgDriverFactory)();
const { test } = (0, createTestRunner_1.createTestRunner)(driverFactory, 'pg');
const sql = (0, sql_tag_1.createSqlTag)();
test('returns the first row', async (t) => {
const pool = await (0, createPool_1.createPool)(t.context.dsn, {
driverFactory,
});
const result = await pool.maybeOneFirst(sql.unsafe `
SELECT *
FROM (VALUES (1)) as t(id)
`);
t.is(result, 1);
});
test('returns null if no results', async (t) => {
const pool = await (0, createPool_1.createPool)(t.context.dsn, {
driverFactory,
});
const result = await pool.maybeOneFirst(sql.unsafe `
SELECT *
FROM (VALUES (1)) as t(id)
WHERE false
`);
t.is(result, null);
});
test('throws an error if more than one row is returned', async (t) => {
const pool = await (0, createPool_1.createPool)(t.context.dsn, {
driverFactory,
});
const error = await t.throwsAsync(pool.maybeOneFirst(sql.unsafe `
SELECT *
FROM (VALUES (1), (2)) as t(id)
`));
t.true(error instanceof errors_1.DataIntegrityError);
});
test('throws an error if more than one column is returned', async (t) => {
const pool = await (0, createPool_1.createPool)(t.context.dsn, {
driverFactory,
});
const error = await t.throwsAsync(pool.maybeOneFirst(sql.unsafe `
SELECT *
FROM (VALUES (1, 'foo')) as t(id, name)
`));
t.true(error instanceof errors_1.DataIntegrityError);
});
//# sourceMappingURL=maybeOneFirst.test.js.map