Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fastify-atdatabase-pg

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-atdatabase-pg

This module provides a way to use postgressql database.

  • 0.2.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

fastify-atdatabase-pg

This module provides a way to connect to PostgreSQL database using @databases/pg

Install

npm i fastify-atdatabase-pg

Usage

Add it to you project with register and you are done!

const fastify = require('fastify')()

fastify.register(require('fastify-atdatabase-pg'), {
    connectionString: 'postgres://postgres:postgres@localhost:5432/postgres',
    bigIntMode: 'string'
})

fastify.get('/', async (req, reply) => {
  const result = await fastify.pg.db.query(fastify.pg.sql`SELECT NOW()`)
  return reply.send(result)
})

fastify.listen({ port: 3000 }, err => {
  if (err) throw err
})

You can also connect with different databases using namespaces to differentiate each other:

const fastify = require('fastify')()

fastify.register(require('fastify-atdatabase-pg'), {
  namespace: 'primary',
  connectionString: 'postgres://postgres:postgres@primary:5432/primary',
  bigIntMode: 'string'
})

fastify.register(require('fastify-atdatabase-pg'), {
  namespace: 'secondary',
  connectionString: 'postgres://postgres:postgres@secondary:5432/secondary',
  bigIntMode: 'string'
})

fastify.get('/primary', async (req, reply) => {
  const result = await fastify.pg.primary.db.query(fastify.pg.primary.sql`SELECT NOW()`)
  return reply.send(result)
})

fastify.get('/secondary', async (req, reply) => {
  const result = await fastify.pg.secondary.db.query(fastify.pg.secondary.sql`SELECT NOW()`)
  return reply.send(result)
})

fastify.listen({ port: 3000 }, err => {
  if (err) throw err
})

Bulk insert is also available:

const fastify = require('fastify')()

fastify.register(require('fastify-atdatabase-pg'), {
    connectionString: 'postgres://postgres:postgres@localhost:5432/postgres',
    bigIntMode: 'string'
})

fastify.get('/:text', async (req, reply) => {
  const {text} = req.params

  const options = {
    tableName: 'samples',
    columnTypes: {
      sample_data: fastify.pg.sql`TEXT`
    }
  }
  const columnsToInsert = [
    'sample_data'
  ]
  const records = [
    { sample_data: text },
  ]

  await fastify.pg.bulkInsert(options, columnsToInsert, records)
  const results = await fastify.pg.db.query(fastify.pg.sql`SELECT * FROM samples`)
  return reply.send(results)
})

fastify.listen({ port: 3000 }, err => {
  if (err) throw err
})

Documentation

More details about @databases/pg documentation, see @databases/pg

License

MIT License

Keywords

FAQs

Package last updated on 06 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc