@beardedtim/harpocrates

0.0.0-ALPHA-0.3 • Public • Published

Examples

# Some .env file
# related to docker-compose.yml file
DB_HOST=some.db.host
DB_PORT=5432
DB_PASSWORD=password
DB_USERNAME=username
DB_NAME=harpocrates
import * as secrets from '@beardedtim/harpocrates'
import R from 'ramda'
import PG from 'pg'

const { Pool } = PG
const { prop, head } = R

const log = {
  enter: group_name => console.group(group_name),
  exit: () => console.groupEnd(),
  ...console
}

// We create some "service"
const pool = secrets.create_service({
  name: 'pool',
  // which is a wrap around some factory function
  create: (secret) => new Pool({
    // that passes in access to any secrets that
    // were set
    host: secret.db_host(),
    port: secret.db_port(),
    password: secret.db_password(),
    user: secret.db_username(),
    database: secret.db_name()
  })
})

const query = (sql, values) => pool.query(sql, values).then(prop('rows'))
const querySingle = (sql, values) => query(sql, values).then(head)

const dummy_async_plugin = async () => {
  // make some call to maybe AWS Param Store
  const paramStore = await Promise.resolve([
    {
      name: 'FOO',
      value: 'bar'
    }
  ])
  for (const secret of paramStore) {
    process.env[secret.name] = secret.value
  }
}

const example = async (header, fn) => {
  log.enter(header)
  try {
    await fn()
  } catch (e) {
    log.error(e)
  }
  log.exit()
}

const main = async () => {
  await example(
    'This will fail because we never set the environment',
    async () => {
      await secrets.init({})

      const result = await querySingle(`SELECT NOW()`)

      log.dir(result)
    }
  )

  await example(
    'This will not because we set the environment using dotenv',
    async () => {
      await secrets.init({ dotenv: true })
      const result = await querySingle(`SELECT NOW()`)
      console.dir(result)
    }
  )

  example(
    'This will use the dummy plugin above and set a new variable call foo',
    async () => {
      await secrets.init({ plugins: [dummy_async_plugin] })
      const value = await secrets.env.foo()
      console.dir(value)
    }
  )
}

main()

Readme

Keywords

none

Package Sidebar

Install

npm i @beardedtim/harpocrates

Weekly Downloads

0

Version

0.0.0-ALPHA-0.3

License

MIT

Unpacked Size

6.44 kB

Total Files

7

Last publish

Collaborators

  • beardedtim