1.0.3 • Published 6 years ago

sequelize-inquirer v1.0.3

Weekly downloads
2
License
ISC
Repository
bitbucket
Last release
6 years ago

sequelize-inquirer

The sequelize-inquirer package can be used to quickly develop a CLI tool that is centered around managing persistent database objects.

The package is aimed at users that have both experience with the sequelize as well as the inquirer packages and are in search for a package that glues those two together in a really cool way ;) Sequelize model definitions are used to automatically generate inquirer prompts for:

  • Creating a new instance
  • Updating an existing instance
  • Deleting one or more instances
  • Showing a (sub-)set of all model instances
  • ...

Thanks to awesome inquirer plugins such as autocomplete, sequelize-inquirer is able to handle even larger amounts of data by offering search functionality and pagination. However, the goal is to easily bootstrap the management of simple data objects that are most likely needed in the context of a CLI tool. sequelize-inquirer will reach its limits in terms of usability rather quick with more complex database structures.

Installation

Installation is straight forward with npm:

npm install sequelize-inquirer

Basic Usage

The minimal setup requires three folders to exist:

  • models: folder containing all sequelize model definitions
  • views: folder containing all CLI views (responsible for generating inquirer question arrays)
  • controllers: folder containing all CLI controllers (responsible for processing the user input that resulted from a view)

As you can see, sequelize-inquirer is using the MVC pattern to structure its code.

Let's setup our project accordingly:

├── cli.js
├── controllers/
├── models/
│   └── pet.js
└── views
    └── main-menu.js
// cli.js
const path = require('path');
const Sequelize = require('sequelize');
const Cli = require('../lib/sequelize-inquirer');

const sequelize = new Sequelize('petstore_db', '', '', {
  host: 'localhost',
  dialect: 'sqlite',
  storage: path.join(__dirname, 'petstore.sqlite'),
  operatorsAliases: false,
  logging: false,
});

const cli = new Cli(sequelize, {
  paths: {
    models: path.join(__dirname, 'models'),
    views: path.join(__dirname, 'views'),
    controllers: path.join(__dirname, 'controllers'),
  },
  banner: 'Welcome to the pet store!',
});

sequelize.sync().then(() => {
  cli.start('MAIN_MENU');
});
// models/pet.js
function initialize(sequelize, DataTypes) {
  return sequelize.define('pet', {
    name: DataTypes.STRING,
  });
}

module.exports = {
  initialize,
};
// views/main-menu.js
function initialize(sequelize, views) {
  class MainMenuView extends views.MenuView {
    constructor() {
      super('MAIN_MENU', ['PET_MENU']);
    }
    get label() { return 'Petstore menu'; }
  }

  return new MainMenuView();
}

module.exports = {
  initialize,
};

That's it! You are now able to create new pets, delete existing ones, show all pets or one pet in detail and much more, all out of the box!

Examples

The repository contains an example CLI project for operating the infamous pet-store.

API Documentation

Views

MenuView

Fragments

Model definition: CLI options object

You can extend the default sequelize model definition by a cli object containig all options revelant to that model's processing by the CLI. The following options are available:

NameTypeDefaultDescription
cli.labelStringmodel nameLabel of the model
cli.searchByStringmodel primary keyAttribute used for searching through model instances

Example model definition

const pet = sequelize.define('pet', {
  name: DataTypes.STRING,
});
pet.cli = {
  label: 'awesome-pet',
  searchBy: 'name',
};

Debugging

The debug package is used for printing out debugging information. The main "namespace" of the sequelize-inquirer package is cli, you can therefore activate all debug messages by setting DEBUG=cli* and use more specific values (e.g. DEBUG=cli:view-generator) to only activate debug information for a specific module.

Maintainer

This package is developed and maintained by the guys at Decentro GmbH.

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.79

6 years ago

0.0.78

6 years ago

0.0.77

6 years ago

0.0.76

6 years ago

0.0.75

6 years ago

0.0.74

6 years ago

0.0.73

6 years ago

0.0.72

6 years ago

0.0.71

6 years ago

0.0.7

6 years ago

0.0.61

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.41

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.21

6 years ago

0.0.2

6 years ago