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

node-api-generator

v0.1.5

Published

Node API (with SequelizeORM) generator

Downloads

8

Readme

Installation

$ npm install -g node-api-generator

Quick Start

Based of the crufty express generator that scaffolds a basic Node API with support for Sequelize ORM The quickest way to get started with express is to install the package globally and run the executable node-api CLI command to generate an application as shown below:

Create the application:

$ node-api my_api

Install dependencies using yarn or npm:

$ npm install
$ yarn install

Run the dummy test included in the scaffold

$ npm test
$ yarn test

Command Line Options

This generator can also be further configured with the following command line flags.

-h, --help          output usage information
-V, --version       output the version number
    --git           add .gitignore
-f, --force         force on non-empty directory

Key dependencies

Sequelize 6 is a promise-based ORM (Object Relational Mapper) for Node. It is a tool or a level of abstraction which maps/converts data in a relational database into programmatic objects.

SequelizeCLI is the Sequelize Command Line Interface (CLI) that brings thee power Sequelize generators to the your terminal.

Mocha is a test framework running on Node. Used in both unit tests as well as in request specs (features).

Chai is a BDD / TDD assertion library for Node.

FactoryGirl provides factory methods to create test fixtures for automated software testing. Used in both unit tests as well as in request specs (features).

FakerJS helps you real looking data to your tests or database seeders, and it also can be pretty entertaining.

License

MIT

Notes

Once you've generated your application, cd'ed (is that a verb?) into the project folder and installed the dependencies, you need to run a few commands to get the database started.

NOTE: You need to install SequelizeCLI separately or use it with npx for the commands below.

$ npm install -g sequelize-cli
$ sequelize --help
// or
$ npx sequelize --help

Create the database

$ sequelize db:create && NODE_ENV=test sequelize db:create && sequelize db:migrate && NODE_ENV=test sequelize db:migrate

You should get an output similar to this:

Sequelize CLI [Node: 14.8.0, CLI: 6.2.0, ORM: 6.6.2]

Loaded configuration file "database/config/config.json".
Using environment "development".
Database test_api_development created.

Sequelize CLI [Node: 14.8.0, CLI: 6.2.0, ORM: 6.6.2]

Loaded configuration file "database/config/config.json".
Using environment "test".
Database test_api_test created.

Sequelize CLI [Node: 14.8.0, CLI: 6.2.0, ORM: 6.6.2]

Loaded configuration file "database/config/config.json".
Using environment "development".
No migrations were executed, database schema was already up to date.

Sequelize CLI [Node: 14.8.0, CLI: 6.2.0, ORM: 6.6.2]

Loaded configuration file "database/config/config.json".
Using environment "test".
No migrations were executed, database schema was already up to date.

Run tests

We've created one request spec, and one model spec for you as reference. Execute them by running:

$ yarn test

Run tests in VSCode

You can configure your VSCode to run your tests. That will allow you to set breakpoints and use the debugger command and halt the execution of your code.

Go through the process of creating the launch.json file in VSCode and add the following configuration:

"configurations": [
    {
      "name": "Launch Program",
      "program": "${workspaceFolder}/bin/www",
      "request": "launch",
      "skipFiles": [
        "<node_internals>/**"
      ],
      "type": "pwa-node"
    },
    {
      "type": "pwa-node",
      "request": "launch",
      "name": "Mocha tests",
      "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
      "args": [
        "--recursive",
        "--timeout",
        "999999",
        "--colors",
        "--exit",
        "specs"
      ],
      "env": {
        "NODE_ENV": "test",
        "NODE_NO_WARNINGS": "1" // let's stick our head in the sand and silence all warnings
      },
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen"
    }
  ]

Defining Factories

Visit the FactoryGirl documentation for more details. Here, we will show you how to create a basic factory that you can use to test your model and to create fake instances for use in tests. Lets say you have a basic User model:

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class User extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  };
  User.init({
    firstName: DataTypes.STRING,
    lastName: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'User',
  });
  return User;
};

Then, in your specs/factories folder, create a users.js with the following code:

module.exports = (factory, Models) => {
  factory.define('User', Models.User, {
    firstName: 'Random',
    lastName: 'Guy',
    createdAt: new Date(),
    updatedAt: new Date()
  })
}

You can use your factory to create one or many instances of the User model:

let author = await factory.create('User')
await factory.createMany('Book', 2, [
      { firstName: "Custom", lastName: "Name" },
      { firstName: "Another", lastName:"Name" }
    ])

There are many other use cases for factories. Make sure to check the documentation.