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

jsonapi-store-relationaldb

v5.0.0

Published

Relational data store for jsonapi-server.

Downloads

71

Readme

Coverage Status Build Status npm version Code Climate Dependencies Status

jsonapi-store-relationaldb

:warning: PLEASE NOTE: Version 4.x releases erroneously created columns for string properties as Sequelize text type. The behaviour in version 5.x has reverted back to creating columns with the Sequelize string type, which is the more adequate type and was the behaviour in versions 3.x and older. The text columns created by version 4.x will need to be manually migrated to string columns.

jsonapi-store-relationaldb is a relational database backed data store for jsonapi-server.

This project conforms to the specification laid out in the jsonapi-server handler documentation.

Supported Databases

  • Postgres
  • MySQL
  • MariaDB

Usage

var RelationalDbStore = require("jsonapi-store-relationaldb");

jsonApi.define({
  resource: "comments",
  handlers: new RelationalDbStore({
    dialect: "mysql",
    dialectOptions: {
      supportBigNumbers: true
    },
    host: "localhost",
    port: 3306,
    database: "jsonapi", // If not provided, defaults to the name of the resource
    username: "root",
    password: null,
    logging: false
  })
});

Note: the logging property controls the logging of the emitted SQL and can either be false (which will mean it will be captured by the internal debugging module under the namespace jsonApi:store:relationaldb:sequelize) or a user provided function (e.g. console.log) to which a string containing the information to be logged will be passed as the first argument.

Alternative Usage - Provide Sequelize instance

If you are already using sequelize or need to have access to the sequelize instance, you may provide an instance to the store to be used instead of having the store create a new instance from the given config.

var RelationalDbStore = require("jsonapi-store-relationaldb");
var Sequelize = require("Sequelize");

var sequelize = new Sequelize("jsonapi", "root", null, {dialect: "mysql"}));

jsonApi.define({
  resource: "comments",
  handlers: new RelationalDbStore({
    sequelize: sequelize
  })
});

Features

  • Search, Find, Create, Delete, Update
  • Efficient lookups via appropriate indexes
  • Filtering happens at the database layer
  • Transactional queries

Getting to Production

Getting this data store to production isn't too bad...

  1. Bring up your relational database stack.
  2. Create the database(s).
  3. Create the database tables. You can call (new RelationalDbStore()).populate() to have this module attempt to create the require tables. If you enable debugging via DEBUG=jsonApi:store:* you'll see the create-table statements - you can target a local database, call populate(), grab the queries, review them and finally run them against your production stack manually.
  4. Deploy your code.
  5. Celebrate.

When deploying schema changes, you'll need to correct your database schema - database migrations are left as an exercise for the user. If your schema are likely to change frequently, maybe consider using a different (less schema-driven) data store.

When changing columns in a production database, a typical approach might be to create a new table that is a clone of the table in production, copy all data from the production table into the new table, run an ALTER-TABLE command on the new table to adjust the columns (this may take a while and will lock the table), then run a RENAME-TABLES to swap the production table out for the new one.

Note: When populating database tables, you can use the force config option to DROP and CREATE tables. This is helpful in development stage, when your data doesn't matter and you want your Tables schemas to change according to the DAOs without having to manually write migrations.

(new RelationalDbStore()).populate({force: true}, () => {
  //tables dropped and created
})

Gotchas

Relational databases don't differentiate between undefined and null values. Joi does differentiate between undefined and null values. Some undefined properties will pass validation, whilst null properties may not. For example, the default articles resource contains a created attribute of type "date" - this won't pass validation with a null value, so the Joi schema will need tweaking.