Git Product home page Git Product logo

fastify-pg's Introduction

      ##          ##
        ##      ##
      ##############
    ####  ######  ####
  ######################
  ##  ##############  ##
  ##  ##          ##  ##
        ####  ####

fastify-pg

JavaScript Style Guide

Fastify PostgreSQL plugin inspired by fastify-postgres and typeorm.

Dependencies:

Install

npm i fastify fastify-plugin fastify-pg pg --save

Usage

const pg = require('fastify-pg')

or

import pg from 'fastify-pg'

Register plugin

fastify.register(pg, options)

This plugin will add the pg namespace in your Fastify instance, with the following properties:

const {
  Client, // client constructor for a single query
  pool, // pool instance
  connect, // function to get a connection from the pool
  query, // utility to perform a query WITHOUT a transaction
  transaction, // utility to perform multiple queries WITH a transaction
  QueryBuilder // query builder
} = fastify.pg

For examples, please see the fastify-postgres and node-postgres documentation.

QueryBuilder

const { QueryBuilder, transaction } = fastify.pg

.table(tableName)

await QueryBuilder
  .of(fastify.pg)
  .table('users')
  .execute()
TABLE users;

.insert()

await QueryBuilder
  .of(fastify.pg)
  .insert()
  .into('users')
  .defaultValues()
  .returning(['*'])
  .execute()
INSERT INTO users DEFAULT VALUES RETURNING *;

.select(elements)

await QueryBuilder
  .of(fastify.pg)
  .select(['*'])
  .from('users')
  .where('users.id = :id', {id: 1})
  .andWhere('users.is_active = :is_active', {is_active: true})
  .orWhere('users.deleted = :deleted', {deleted: false})
  .getMany()
SELECT * FROM users WHERE users.id = 1 AND users.is_active = true OR users.deleted = false;
await QueryBuilder
  .of(fastify.pg)
  .insert()
  .into('users')
  .columns(['first_name', 'last_name'])
  .values([[`'Artem'`, `'Tolstykh'`], [`'Maksym'`, `'Bezruchko'`]])
  .returning(['*'])
  .execute()
INSERT INTO users (first_name, last_name) VALUES ('Artem', 'Tolstykh'), ('Maksym', 'Bezruchko') RETURNING *;

.update(tableName)

await QueryBuilder
  .of(fastify.pg)
  .update('users')
  .set({first_name: `'Artem'`, last_name: `'Tolstykh'`})
  .where('id = :id', {id: 1})
  .execute()
UPDATE users SET first_name = 'Artem', last_name = 'Tolstykh' WHERE id = 1;

.delete()

await QueryBuilder
  .of(fastify.pg)
  .delete()
  .from('users')
  .execute()
DELETE FROM users;
await QueryBuilder
  .of(fastify.pg)
  .delete()
  .from('users')
  .where('id = :id', {id: 1})
  .returning(['*'])
  .execute()
DELETE FROM users WHERE id = 1 RETURNING *;

.innerJoin(tableName, expression)

await QueryBuilder
  .of(fastify.pg)
  .select(['users.*', 'photos.*'])
  .from('users')
  .innerJoin('photos', 'photos.user = users.id')
  .andWhere('photos.isRemoved = :isRemoved', {isRemoved: false})
  .where('users.name = :username', {username: `'arttolstykh'`})
  .getOne()
SELECT users.*, photos.* FROM users INNER JOIN photos ON photos.user = users.id AND photos.isRemoved = false WHERE users.name = 'arttolstykh';

transaction

transaction(async client => {
    const id 
        = await QueryBuilder
        .of(client, ['arttolstykh'])
        .insert()
        .into('users')
        .columns(['username'])
        .values([['$1']])
        .returning(['id'])
        .execute()
    // etc
    return id
})
BEGIN;
INSERT INTO users (username) VALUES ('arttolstykh') RETURNING id;
-- etc
COMMIT;

QueryBuilder API

  • of
  • with
  • withRecursive
  • insert
  • table
  • select
  • update
  • delete
  • into
  • from
  • innerJoin
  • set
  • where
  • andWhere
  • orWhere
  • columns
  • values
  • defaultValues
  • limit
  • offset
  • groupBy
  • orderBy
  • onConflict
  • returning
  • getQuery
  • execute
  • getOne
  • getMany

License

Licensed under MIT.

fastify-pg's People

Contributors

rozquit avatar

Stargazers

Iryna Onyshchuk avatar  avatar Aleks Malamud avatar  avatar Serhii Viazkov avatar gblok avatar Maksym Bezruchko avatar  avatar  avatar

Watchers

Maksym Bezruchko avatar Roman Alexeychenko avatar  avatar  avatar

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.