hediet / ts-typed-sql

A fully typed sql builder. Not maintained anymore, use it for your inspiration.
https://hediet.github.io/ts-typed-sql/
54 stars 4 forks source link

Allow getting result meta information #34

Open phiresky opened 6 years ago

phiresky commented 6 years ago

With pg you can get the number of updated / inserted rows like this:

const {rowCount} = await db.query("update ...."); without needing a RETURNING statement.

specifically, the return type of Query in pg is

export interface QueryResult {
    command: string;
    rowCount: number;
    oid: number;
    rows: any[];
}

This library always returns only the rows property of that interface, which is mostly what you want, but in some cases getting the rowCount is useful (e.g. for performance of not having to return an array).

xialvjun commented 5 years ago

I think we can wrap db.query so:

let persons = await wrapped_query(from(Persons).select(Persons.$all));
// here we know the type of persons is (typeof Persons.obj)[]

// or no wrapped version
let persons: (typeof Persons.obj)[] = db.query(from(Persons).select(Persons.$all));