1.1.21 • Published 2 years ago

pgknexlove v1.1.21

Weekly downloads
100
License
-
Repository
github
Last release
2 years ago

pgknexlove

pgknexlove npm

Don't you love postgres + knex? Now love it even more with an easy way to create temporary databases for unit tests, automatic singleton, an initial connection test, and standard environment variables defaults (POSTGRES_HOST, etc.).

Usage

# Installation
npm install pgknexlove
const pgknexlove = require("pgknexlove")

module.exports = async (req, res) => {
  const db = await pgknexlove.default()

  const myUsers = await db("users").select("first_name")

  res.send(
    "Here's a random user's name:" +
      myUsers[Math.floor(Math.random() * myUsers.length)].first_name
  )
}

Setting Defaults

import pgknexlove from "pgknexlove"

const getDB = pgknexlove({
  defaults: { database: "products" },
})

async function main() {
  const db = await getDB()

  const myProducts = await db("product").select("name")
}

Usage in Unit Tests

testMode: true creates a temporary database. This allows you to run database tests in parallel.

const getDB = require("pgknexlove")({
  // Your migration file will automatically be run on the test database (optional)
  migrationFile: require.resolve("./migrate.sql"),

  // A file that will seed the database, great for quick testing (optional)
  seedFile: require.resolve("./seed.sql"),
})

test("some test that uses the database", async (t) => {
  const db = await getDB({ testMode: true, migrate: true, seed: true })

  // a test database was created and migrate with a random name, do whatever you
  // want!
  await db("user").del()

  // ... test creating users with endpoints or whatever
})

getConnectionInfo & getConnectionString

You can use these methods to compute what database should be used from the environment and/or your defaults.

NOTE: The defaults are overridden by the environment. The environment can be overriden by arguments passed into the GetDatabase function (e.g. pgknexlove.default or what's returned by pgknexlove())

import pgknexlove from "pgknexlove"

pgknexlove.default.getConnectionInfo()
// { host: "localhost", "password": "", port: 5432, database: "postgres", user: "postgres" }

pgknexlove.default.getConnectionString()
// postgresql://postgres:@localhost:5432/postgres

let modifiedConn = pgknexlove({
  defaults: { database: "myproduct" },
}).getConnectionInfo()
// { host: "localhost", "password": "", port: 5432, database: "myproduct", user: "postgres" }

Environment Variables

The following environment variables are used (basically standard postgres env variables)

Var NameDescription
POSTGRES_HOSTPostgres Host
POSTGRES_PASS, POSTGRES_PASSWORDPostgres Password
POSTGRES_DATABASE, POSTGRES_DBPostgres Database
POSTGRES_USER, POSTGRES_USERNAMEPostgres User
POSTGRES_PORTPostgres Port
POSTGRES_URI, POSTGRES_URLPostgres URI postgresql://...
POSTGRES_SSLIf set, true
USE_TEST_DBtestMode will default to true

Viewing Debug Logs

Set DEBUG=pgknexlove to see debug logs.

1.1.19

2 years ago

1.1.21

2 years ago

1.1.20

2 years ago

1.1.18

2 years ago

1.1.17

3 years ago

1.1.16

3 years ago

1.1.15

3 years ago

1.1.14

3 years ago

1.1.9

3 years ago

1.1.12

3 years ago

1.1.11

3 years ago

1.1.10

3 years ago

1.1.13

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.1

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.0

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago