pglib
TypeScript icon, indicating that this package has built-in type declarations

0.1.7 • Public • Published

PGLIB

Dependency Status Downloads Version

TL;DR

Install

npm install pglib --save

Code

tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "sourceMap": true,
        "target": "es2015"
    },
    "exclude": [
        "node_modules"
    ]
}

file.ts

import * as pglib   from "pglib";

export var PeopleColumns = {
    id: "id",
    name: "name",
    age: "age",
    data: "data"
};

pglib.conString = {
    user: process.env.POSTGRES_PORT_5432_TCP_USER,
    password: process.env.POSTGRES_PORT_5432_TCP_PASSWORD,
    database: process.env.POSTGRES_PORT_5432_TCP_DB,
    port: process.env.POSTGRES_PORT_5432_TCP_PORT,
    host: process.env.POSTGRES_PORT_5432_TCP_ADDR
};

@pglib.Table({ schema: "public", table: "people" })
export class People {

    @pglib.DataType(pglib.Types.string)
    @pglib.ColumnName(PeopleColumns.id)
    id: string;

    @pglib.DataType(pglib.Types.string)
    @pglib.ColumnName(PeopleColumns.name)
    name: string;

    @pglib.ColumnName(PeopleColumns.age)
    @pglib.DataType(pglib.Types.number)
    age: number;

    @pglib.ColumnName(PeopleColumns.data)
    @pglib.DataType(pglib.Types.json)
    data: Object;
}
async function getPeople() {
    try {
        var results = await new pglib.Query<People>((<any>People))
            .lessThan({ age: 50 })
            .orderBy([PeopleColumns.age])
            .select([PeopleColumns.age, PeopleColumns.name])
            .q();
        console.log(results.rows);
    } catch (error) {
        console.log(error);
    }
}
getPeople();

Explanation

To start to use pgLib you have to pass the class, not an instance. Though this can be changed anytime.

@pglib.Table({ schema: "public", table: "people" })
export class People {

    @pglib.DataType(pglib.Types.string)
    @pglib.ColumnName(PeopleColumns.id)
    id: string;

    @pglib.DataType(q.Types.string)
    @pglib.ColumnName(PeopleColumns.name)
    name: string;

    @pglib.ColumnName(PeopleColumns.age)
    @pglib.DataType(q.Types.number)
    age: number;

    @pglib.ColumnName(PeopleColumns.data)
    @pglib.DataType(q.Types.json)
    data: Object;
}

It has to have by the moment (if anyone feels this can be improved just pull request)

  • The class has to have a schema and a table.
  • Every property has to have a column name and a data type.

Currently only Schema, Table, DataType and ColumnName are supported.

The Schema and Table where the table is located:

@pglib.Table({ schema: "public", table: "people" })

The data type of the column

@pglib.DataType(q.Types.string)

Currently only those types are supported:

export enum Types {
    string,
    number,
    json
}

And the name of the column:

@pglib.ColumnName(PeopleColumns.data)

Dependents (0)

Package Sidebar

Install

npm i pglib

Weekly Downloads

1

Version

0.1.7

License

none

Last publish

Collaborators

  • dviejo