npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nutshelllab/aurora-store

v0.0.1

Published

Encapsulate a nutshell connector to connect to a remote Aurora Postgres or a local Postgresdb

Downloads

5

Readme

Goal

This repo was originaly made to provide a serverless aurora postgresql connector to be used in Keyro Login with a strong retro-compatibility with our dynamodb connector in mind. By implementing the same API, it offers the opportunity to easily switch between Aurora and Dynamo.

Usage

Installation

yarn add @nutshelllab/aurora-store

This module allows you to run your code on a local database. To do so, you will also need to have a proper postgresql installation running.

Configuration

Config is done through env variables so that you can use your serverless environnement to handle it as code :

Access to the database

Variable | description ---------|------------ DB_HOST | The ip adress/domain where the SGBD runs. It may be an aws aurora cluster. DB_PORT | Default is postgres's default (5432) DB_NAME | The name of your postgresql database DB_USER | Username who has been given the proper privileges in your db DB_PASSWD | Password of the above user SLS_STAGE | If local is passed, the variables below will be used instead of above. LOCAL_DB_HOST | The ip adress/domain where the SGBD runs. It may be 127.0.0.1. LOCAL_DB_PORT | Default is postgres's default (5432) LOCAL_DB_NAME | The name of your postgresql database LOCAL_DB_USER | Username who has been given the proper privileges in your db LOCAL_DB_PASSWD | Password of the above user

Migrations

You can easily configure a migration source to handle your database schema. Migration are internally done through Knex, so that the configuration is identical. Here's an example with webpack context :

import { configure } from '@nutshelllab/aurora-store' //The module expose a simple configure method

const context = require.context('../../migrations', false, /\.js$/)

const makeMigrationSource = context => {
  return {
    getMigrations() {
      return Promise.resolve(context.keys().sort())
    },
    getMigrationName(migration) {
      return migration
    },
    getMigration(migration) {
      return context(migration)
    }
  }
}

configure({
  migrationSource: makeMigrationSource(context) // Configuration is mutating the state, keep it for initialization as much as possible.
})

Initialize a store

To initialize a store, just call default function of the module :

import createStore from '@nutshelllab/aurora-store'

//[...] configuration has been done at this point

const teamStore = createStore({ kind: 'teams', validate: makeTeam })
const result = await teamStore.find({ id: 'team-00007'})

Options you can pass to the default store builder function are :

Option | description | default ---------|------------|------- kind | The table name you want to manipulate | No default, must be set validate | A validator function to be called on data before writing and after reading | id function (nothing happen) idField | Handle PK with this option as an array of columns names | ['id']

Features

findIn(field, values)

Returns all the records having a field's value intercecting with a predefined array of value.

store.findIn('id', ['001', '002', '003'])

findAll(filters)

Returns all the records matching a pattern

store.findAll({ contracted: true })

find(filters)

Returns exactly one record matching a pattern or throw an error with name attribute set to 'NOT_FOUND'.

store.find({ id: '001' })

put(data)

Write an object with the store. If it does not exist, it will be created.

store.put({ id: 'movie-0001', name: 'Black Widow' })

update(data)

Update the record matching with data id (default is 'id' field, see idField configuration to change it).

store.update({id: 'movie-0001', name: 'Black Windows' })

create(data)

Insert a new record with the given object values. AlreadyExisting will throw.

store.create({id: 'movie-0002', name: 'Black Windows' })

findOrCreate(filters, data)

Fetch a record with filters. A creation attempt is made it nothing is found. Bad filtering may result in throwing already exist error.

store.create({name: 'Black Windows'}, { id: 'newId-002', name: 'Black Windows', company: 'marvelcrosoft'})

remove(filters)

Destroy all records with filters forever.

store.remove({name: 'Black Windows'})

truncate(options)

Destroy all records of a table forever. You must pass force attribute to true to execute this as a way to make it clear that you know what you're doing.

store.truncate() // won't work
store.truncate({ force: true })

knexClient()

Obviously the point of using a relational database was to go beyond dynamo key-values restriction. This function gives you a Knex querybuilder. You can use that to manipulate any table in a sql-query manner :

store.knexClient().then(db =>
  db('movies').innerjoin('authors', 'authors.id', 'movies.authorId').where('authors.name', 'marvelcrosoft').select('movies.*')
)

See knex documentation for further features.

Combine with serverless-offline

With serverless-offline, this module can be used to provide a 100% local execution which may greatly increase your productivity. Keyro Login has been tested and successfully run on a localhost with a local duplicated database of our remote dev environement.

Contribute

  1. Install postgresql
  2. Create a pguser user with a pguser password
  3. yarn
  4. Check integration tests yarn test