UNPKG

slonik

Version:

A Node.js PostgreSQL client with strict types, detailed logging and assertions.

69 lines 2.52 kB
"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 expect_type_1 = require("expect-type"); const zod_1 = require("zod"); const driverFactory = (0, pg_driver_1.createPgDriverFactory)(); const { test } = (0, createTestRunner_1.createTestRunner)(driverFactory, 'pg'); const sql = (0, sql_tag_1.createSqlTag)(); test('returns value of the first column from the first row', async (t) => { const pool = await (0, createPool_1.createPool)(t.context.dsn, { driverFactory, }); const result = await pool.oneFirst(sql.unsafe ` SELECT * FROM (VALUES (1)) as t(id) `); t.is(result, 1); }); test('throws an error if no rows are returned', async (t) => { const pool = await (0, createPool_1.createPool)(t.context.dsn, { driverFactory, }); const error = await t.throwsAsync(pool.oneFirst(sql.unsafe ` SELECT * FROM (VALUES (1)) as t(id) WHERE false `)); t.true(error instanceof errors_1.NotFoundError); }); 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.oneFirst(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.oneFirst(sql.unsafe ` SELECT * FROM (VALUES (1, 'foo')) as t(id, name) `)); t.true(error instanceof errors_1.UnexpectedStateError); }); test('describes zod object associated with the query', async (t) => { const pool = await (0, createPool_1.createPool)(t.context.dsn, { driverFactory, }); const zodObject = zod_1.z.object({ id: zod_1.z.number(), }); const query = sql.type(zodObject) ` SELECT * FROM (VALUES (1)) as t(id) `; const result = await pool.oneFirst(query); (0, expect_type_1.expectTypeOf)(result).toMatchTypeOf(); t.is(result, 1); }); //# sourceMappingURL=oneFirst.test.js.map