Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/database/postgres/query.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45// Nice async/await interface to doing basic queries.67import { callback } from "awaiting";89const { one_result, all_results } = require("../postgres-base");1011import { PostgreSQL, QueryWhere } from "./types";1213interface QueryOpts {14db: PostgreSQL;15query?: string;16select?: string[];17set?: { [key: string]: any };18jsonb_set?: object;19jsonb_merge?: object;20table?: string;21where?: QueryWhere;22one?: boolean; // if true get back one result; if false get list of all results.23order_by?: string;24limit?: number;25params?: any[];26timeout_s?: number;27}2829export async function query(opts: QueryOpts): Promise<any> {30return await callback(31opts.one ? one_query : all_query,32opts.db,33opts.select,34opts.table,35opts.where,36opts.set,37opts.query,38opts.jsonb_set,39opts.jsonb_merge,40opts.order_by,41opts.limit,42opts.params,43opts.timeout_s44);45}4647function all_query(48db,49select,50table,51where,52set,53query,54jsonb_set,55jsonb_merge,56order_by,57limit,58params,59timeout_s,60cb61): void {62db._query({63select,64table,65where,66set,67query,68jsonb_set,69jsonb_merge,70order_by,71limit,72params,73timeout_s,74cb: all_results(cb),75});76}7778function one_query(79db,80select,81table,82where,83set,84query,85jsonb_set,86jsonb_merge,87order_by,88limit,89params,90timeout_s,91cb92): void {93db._query({94select,95table,96where,97set,98query,99jsonb_set,100jsonb_merge,101order_by,102limit,103params,104timeout_s,105cb: one_result(cb),106});107}108109110