|
| 1 | +import type { Pool } from 'pg'; |
| 2 | +import { v4 as uuid } from 'uuid'; |
| 3 | +import { |
| 4 | + type DbClient, |
| 5 | + type PongoCollection, |
| 6 | + type PongoDeleteResult, |
| 7 | + type PongoFilter, |
| 8 | + type PongoInsertResult, |
| 9 | + type PongoUpdate, |
| 10 | + type PongoUpdateResult, |
| 11 | +} from '../main'; |
| 12 | +import { constructFilterQuery } from './filter'; |
| 13 | +import { getPool } from './pool'; |
| 14 | +import { constructUpdateQuery } from './update'; |
| 15 | +import { sql } from './execute'; |
| 16 | + |
| 17 | +export const postgresClient = ( |
| 18 | + connectionString: string, |
| 19 | + database?: string, |
| 20 | +): DbClient => { |
| 21 | + const pool = getPool({ connectionString, database }); |
| 22 | + |
| 23 | + return { |
| 24 | + connect: () => Promise.resolve(), |
| 25 | + close: () => Promise.resolve(), |
| 26 | + collection: <T>(name: string) => postgresCollection<T>(name, pool), |
| 27 | + }; |
| 28 | +}; |
| 29 | + |
| 30 | +export const postgresCollection = <T>( |
| 31 | + collectionName: string, |
| 32 | + pool: Pool, |
| 33 | +): PongoCollection<T> => { |
| 34 | + const createCollection = async (): Promise<void> => { |
| 35 | + await sql( |
| 36 | + pool, |
| 37 | + 'CREATE TABLE IF NOT EXISTS %I (id UUID PRIMARY KEY, data JSONB)', |
| 38 | + collectionName, |
| 39 | + ); |
| 40 | + }; |
| 41 | + |
| 42 | + return { |
| 43 | + createCollection, |
| 44 | + insertOne: async (document: T): Promise<PongoInsertResult> => { |
| 45 | + await createCollection(); |
| 46 | + |
| 47 | + const id = uuid(); |
| 48 | + |
| 49 | + const result = await sql( |
| 50 | + pool, |
| 51 | + 'INSERT INTO %I (id, data) VALUES (%L, %L)', |
| 52 | + collectionName, |
| 53 | + id, |
| 54 | + JSON.stringify({ ...document, _id: id }), |
| 55 | + ); |
| 56 | + |
| 57 | + return result.rowCount |
| 58 | + ? { insertedId: id, insertedCount: result.rowCount } |
| 59 | + : { insertedId: null, insertedCount: null }; |
| 60 | + }, |
| 61 | + updateOne: async ( |
| 62 | + filter: PongoFilter<T>, |
| 63 | + update: PongoUpdate<T>, |
| 64 | + ): Promise<PongoUpdateResult> => { |
| 65 | + const filterQuery = constructFilterQuery(filter); |
| 66 | + const updateQuery = constructUpdateQuery(update); |
| 67 | + |
| 68 | + const result = await sql( |
| 69 | + pool, |
| 70 | + 'UPDATE %I SET data = %s WHERE %s', |
| 71 | + collectionName, |
| 72 | + updateQuery, |
| 73 | + filterQuery, |
| 74 | + ); |
| 75 | + return { modifiedCount: result.rowCount }; |
| 76 | + }, |
| 77 | + deleteOne: async (filter: PongoFilter<T>): Promise<PongoDeleteResult> => { |
| 78 | + const filterQuery = constructFilterQuery(filter); |
| 79 | + const result = await sql( |
| 80 | + pool, |
| 81 | + 'DELETE FROM %I WHERE %s', |
| 82 | + collectionName, |
| 83 | + filterQuery, |
| 84 | + ); |
| 85 | + return { deletedCount: result.rowCount }; |
| 86 | + }, |
| 87 | + findOne: async (filter: PongoFilter<T>): Promise<T | null> => { |
| 88 | + const filterQuery = constructFilterQuery(filter); |
| 89 | + const result = await sql( |
| 90 | + pool, |
| 91 | + 'SELECT data FROM %I WHERE %s LIMIT 1', |
| 92 | + collectionName, |
| 93 | + filterQuery, |
| 94 | + ); |
| 95 | + return (result.rows[0]?.data ?? null) as T | null; |
| 96 | + }, |
| 97 | + find: async (filter: PongoFilter<T>): Promise<T[]> => { |
| 98 | + const filterQuery = constructFilterQuery(filter); |
| 99 | + const result = await sql( |
| 100 | + pool, |
| 101 | + 'SELECT data FROM %I WHERE %s LIMIT 1', |
| 102 | + collectionName, |
| 103 | + filterQuery, |
| 104 | + ); |
| 105 | + |
| 106 | + return result.rows.map((row) => row.data as T); |
| 107 | + }, |
| 108 | + }; |
| 109 | +}; |
0 commit comments