npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tsbean-driver-postgres

v3.0.0

Published

PostgreSQL driver for TSBean-ORM.

Downloads

260

Readme

TSBean-ORM PostgreSQL Driver

npm version

This a PostgreSQL driver for tsbean-orm.

Based on pg package.

Installation

npm install --save tsbean-driver-postgres

Usage

import { DataSourceDriver, DataSource } from "tsbean-orm";
import { PostgreSQLDriver } from "tsbean-driver-postgres"

const mySource = PostgreSQLDriver.createDataSource({
    host: "localhost",
    port: 5432,
    user: "postgres",
    password: "postgres",
    database: "my_database"
});

DataSource.set(DataSource.DEFAULT, mySource);

Correspondence of identifiers

This driver maps from camel case to snake case. By default, we expect all the database table and column identifiers in snake case. Those identifiers will be converted to camel case before passing to tsbean, so you can work with camel case identifiers in your code.

Here is an example:

CREATE TABLE "person" (
    "id" BIGINT NOT NULL PRIMARY KEY,
    "name" VARCHAR(255) NOT NULL,
    "surname" VARCHAR(255) NOT NULL,
    "age" INT,
    "has_driver_license" BOOLEAN,
    "preferences" TEXT,
    "birth_date" TIMESTAMPTZ
);
const SOURCE = DataSource.DEFAULT;
const TABLE = "person";
const PRIMARY_KEY = "id";

export class Person extends DataModel {

    public static finder = new DataFinder<Person>(
        SOURCE, // The data source
        TABLE, // The table or collection name
        PRIMARY_KEY, // The primary key. Leave blank if no primary key
        (data: GenericRow) => {
            return new Person(data);
        },
    );

    public id: number;
    public name: string;
    public surname: string;
    public age: number;
    public hasDriverLicense: boolean;
    public preferences: string[];
    public birthDate: Date;

    constructor(data: GenericRow) {
        // First, we call DataModel constructor 
        super(
            SOURCE, // The data source
            TABLE, // The table or collection name
            PRIMARY_KEY // The primary key. Leave blank if no primary key
        );

        // Second, we set the class properties
        // The recommended way is to set one by one to prevent prototype pollution
        // You can also enforce the types if you do not trust the data source
        // In that case you can use the enforceType utility function

        this.id = enforceType(data.id, "int");
        this.name = enforceType(data.name, "string");
        this.surname = enforceType(data.surname, "string");
        this.age = enforceType(data.age, "int");
        this.hasDriverLicense = enforceType(data.hasDriverLicense, "boolean");
        this.preferences = enforceType(data.preferences, "array");
        this.birthDate = enforceType(data.birthDate, "date");

        // Finally, we must call init()
        this.init();
    }
}

If you want to disable this behaviour, and instead use identifiers as-it, you can use the disableIdentifierConversion option of the data source constructor:

const mySource = PostgreSQLDriver.createDataSource({
    host: "localhost",
    port: 5432,
    user: "postgres",
    password: "postgres",
    database: "my_database",
    disableIdentifierConversion: true
});

You can also set customIdentifierConversion to implement your own identifier conversion.