GithubHelp home page GithubHelp logo

jakoblorz / postgres-driver Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 3.0 35 KB

pg-promise sugar using proper types for easy (async) crud-commands. Add custom methods.

License: MIT License

TypeScript 35.68% JavaScript 64.32%
postgresql postgres typescript crud sugar sql database storage javascript npm npm-package npm-module npmjs

postgres-driver's Introduction

postgres-driver

pg-promise sugar using proper types for easy (async) crud-commands. Add custom methods.

Build Status

Installation

this module is not listed on npm though it can be installed using it:

npm install git+https://github.com/jakoblorz/postgres-driver.git --save

Usage

To use the methods, just create a new set class which is extending the exported database class:

// import the module
import { Database } from "postgres-driver";

// define a table definition (this is a
// IAccount interface where each key can be undefined)
interface IAccountTableDefinition {
    id?: string;
    name?: string;
    age?: number;
}

// actual account interface
interface IAccount extends IAccountTableDefinition {
    id: string;
    name: string;
    age: number;
}

// create a set class
class AccountSet extends Database<IAccountTableDefinition, IAccount> {
    constructor(){
        super("accounts", "postgres://user:password@host:port/database");
    }

    // define a custom method though this example
    // could be done using the createResource method
    public async createNewAccount(name: string) {

        // get the database interface - this is a pg-promise
        // database object
        const connection = await this.connect();

        // insert the dataset
        await connection.none(
            "INSERT INTO accounts (id, name, age) VALUES ($1, $2, $3)", [0, name, 18]);
    }
}

// use the class
const accounts = new AccountSet();

// invoke the read resource method
accounts.readResource({ id: 0 })
    .then((account) => console.log(account === null ? 
        "" : JSON.stringify(account))); // { id: 0, name: "name", age: 18 }

// invoke a custom method
accounts.createNewAccount("user")
    .then(() => accounts.readResource({ name: "user" }))
    .then((account) => console.log(account === null ?
        "" : JSON.stringify(account))) // { id: 0, name: "user", age: 18 }
    .catch(console.error);

Documentation

method description
async readResource<X, Y>() read a single tuple from the relation - returning null if no row was found
async readResourceList<X, Y>() read multiple tuples from the relation
async createResource<X>() insert a new tuple in the relation
async updateResource<X, Y>() update one (multiple) tuple(s) in the relation
async deleteResource<X>() remove one (multiple) tuple(s)
async connect() obtain the pg-promise database interface with an established connection to execute custom queries

readResource<X, Y>()

  • where: Y select the tuple to read

readResourceList<X, Y>()

  • where: Y select the tuples to read
  • skip?: number specify a total of tuples that should be skipped before selection - NOTE: make use of the orderByAsc/orderByDsc arguments to give sense to the expression 'skip the first x tuples'
  • limit?: number specify a maximum count of tuples in the selection
  • orderByAsc?: (keyof X)[] | keyof X | "" specify columns that should be ordered in ascending order
  • orderByDsc?: (keyof X)[] | keyof X | "" specify columns that should be ordered in descending order

createResource()

  • tuple: X data to insert into the table

updateResource<X, Y>()

  • update: X specify the columns to manipulate
  • where: Y select the tuples to update

deleteResource()

  • where: Y select the tuples to delete

postgres-driver's People

Contributors

jakoblorz avatar theteapot avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

theteapot monatem

postgres-driver's Issues

create simple examples

Add an example directory where simple use-cases of this module are presented.
We all know that - seeing an example improves the learning-curve of a new module dramatically. So just think about examples and implement them ๐Ÿ‘

#10 is linked to this issue - mention the examples in the readme as well!

Cannot use without @types/node and @types/es6-promise

I tried using this node package by installing it and running your example script on my database, however it would fail with the following errors:
node_modules/pg-promise/typescript/pg-promise.d.ts(414,43): error TS2503: Cannot find namespace 'NodeJS'.
node_modules/pg-promise/typescript/pg-subset.d.ts(12,28): error TS2307: Cannot find module 'events'.
node_modules/pg-promise/typescript/pg-subset.d.ts(44,34): error TS2304: Cannot find name 'Buffer'.
node_modules/postgres-driver/src/database.ts(36,18): error TS2705: An async function or method in ES5/ES3 requiresthe 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your --lib option.

I was able to fix these errors by installing the following packages into my test project:
"@types/es6-promise": "0.0.33"
"@types/node": "^8.0.46"

Perhaps these should be moved from devDependencies to dependencies in the package.json?

remove custom select

current state

readResource and readResourceList can be invoked with a custom select object

target state

this should be removed to enforce custom queries

improve the readme/documentation

The readme is currently more a location to store the current state of this module - your task would be to think about a better structure to present the features and the usage of this module as simple as possible.

  • use emojis when needed
  • think about the hierarchy
  • develop samples that show the usage (linked to #11)

๐Ÿ‘ ๐Ÿ’ฏ

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.