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

autonym-sql-store

v0.5.0

Published

Store constructor for Autonym models, powered by Bookshelf.js.

Downloads

24

Readme

autonym-sql-store

Store constructor for Autonym models, powered by Bookshelf.js.

Installation

Install this module as well as the driver for whichever SQL store you intend to use, i.e. one of the following:

npm install autonym-sql-store pg

Usage

The module exports a function that should be called only once per database, not per model. So if you have multiple models that use the same SQL database, you should create a file that calls this function and exports the result. You can see an example of this by referencing the sql-store example in the autonym-examples repository: in particular, src/stores/createSqlStore.js and its usage in src/models/Person.js.

First, import the module.

import createSqlStoreCreator from 'autonym-sql-store'

Then, instantiate a connection to your SQL database by calling the function.

const createSqlStore = createSqlStoreCreator({
  client: 'pg',
  connection: process.env.DATABASE_URL,
})

The arguments passed to this function are the same as those passed to Knex.

The value returned from createSqlStoreCreator is a function, which should be called for each model. The parameters are documented in the API section.

The value returned from that function is a store API with create, find, findOne, findOneAndUpdate, findOneAndDelete, serialize, and unserialize methods. It also includes some methods for accessing the underlying drivers, namely getBsModel() to get the Bookshelf Model instance, getBookshelf() to get the Bookshelf instance, and getKnex() to get the Knex instance.

import { Model } from 'autonym'
import { createSqlStore } from '../stores'

export default new Model({
  // ...
  store: createSqlStore({
    table: 'people',
  }),
})

API

The result of createSqlStoreCreator is a function which accepts a configuration object with the following properties.

| Property | Type | Description | Default | | :----------------------| :------------------ | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------- | | table | string | The name of the database table. | None | | searchableProperties | array<string> | Which properties in your schema the user should be able to filter and sort by. | [] | | sort | string | The default sorting of rows. The first character should be + for ascending or - for descending, followed by the property to sort by. The property must be in the array of searchableProperties. | +id | | limit | number | The maximum number of rows to return in a paginated set. This is the default number of rows to fetch per page, but the user may request fewer. | Infinity | | serializeProperty | function | A function that accepts a property name and must return the corresponding column name in the table. | _.snakeCase | | unserializeColumn | function | A function that accepts a column name and must return the corresponding property name for the model. | _.camelCase | | serialize | function | A function that accepts a model record and must return an object whose keys are column names and whose values are the data to save in the database. | Maps over the object, calling serializeProperty on each key. If serializeProperty returns null or undefined, the property is removed. | | unserialize | function | A function that accepts a row object and must return an object whose keys are model property names and whose values are the data to return in the API. | Maps over the object, calling unserializeColumn on each value | | bsModelOptions | object | Additional properties to pass to the Bookshelf instance. | {} |

Find Requests

Find requests can leverage features specific to this store module by adding information to the query string.

  • ?search[firstName]=John filters to resources whose firstName property is equal to John.
  • ?search[firstName][!=]=John filters to resources whose firstName property is not equal to John.
  • ?search[firstName][~]=j filters to resources whose firstName property contains the string j (case-insensitive).
  • ?search[firstName][!~]=j filters to resources whose firstName property does not contain the string j (case-insensitive).
  • ?sort=+firstName sorts resources by the firstName property, ascending.
  • ?limit=10&offset=20 fetches up to 10 resources, starting at the 20th resource.
  • ?ids[]=1&ids[]=2 fetches the resources with ids 1 and 2.

Meta

Specific properties on the meta object that is passed to the store methods can affect the behavior. You can use these properties to restrict and change queries.

| Property | Type | Description | | :---------------- | :---------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | filters | array<array<any>> | An array of arrays. In each sub-array, the first element is the name of a query method, and the remaining elements are arguments to pass to it. For example, [['where', 'name', '=', 'test']] would add a where condition to the query to execute. | | bsMethodOptions | object | Additional parameters to pass to the Bookshelf model function. |