quickpostgres
Version:
An easy, beginner-friendly PostgreSQL database wrapper similar to quick.db.
21 lines (17 loc) • 566 B
text/typescript
import { Options, Params } from "../index";
import { Client } from "pg";
import get from "lodash/get";
export default async (db: Client, params: Params, options: Options) => {
// Fetch entry
let fetched = await db.query(
`SELECT * FROM ${options.table} WHERE ID = ($1)`,
[ ]
);
// If empty, return null
if (!fetched.rows?.length) return null;
fetched = JSON.parse(fetched.rows[0].json);
// Check if target was supplied
if (params.ops.target) fetched = get(fetched, params.ops.target);
// Return value
return typeof fetched;
};