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

create-mysql-api-rest

v0.0.8

Published

A package for automatically generate a REST API from a MySQL database

Downloads

3

Readme

create-mysql-api-rest

This is a package for automatically generate REST API from a MySQL database. It connects to a database , reads all the tables, builds the models and relations for SequelizeJS via the command line and allows the user to automatically generate REST (GET and DELETE) endpoints for a Express application.

Before you install

This package is intended to be an excercise, to understand how node works, to explore how javascript behave in server side and to publish my first npm package.

Requirements

For this package to work you need to install the following pre-requisites:

MySQL2

To hadle connections to MySQL database

npm install mysql2 --save

Sequelize

To handle models and queries to the database

npm install sequelize --save

Morgan

To handle logs inside the project

npm install morgan --save

Sequelize-cli

To build the directories and index for the models

npm install -g sequelize-cli

Install

npm install -g create-mysql-api-rest

Usage

To create a REST API you need to install this package, the requeriments listed and to follow the next steps:

First, create a folder:

mkdir my-rest-api
cd my-rest-api

Then, install the npm packages and create the sequelize files and folders with the last command:

npm init
npm install express sequelize mysql2 body-parser morgan --save
npm install -g sequelize-cli
npm install -g create-mysql-api-rest

sequelize init

Go into ./config/config.json and replace the content of the development attribute with your database configuration. After that, run this command inside your project root folder:

create-mysql-api-rest

This will populate the ./models and ./routes folders with the automatically generated files.

Finally, run the server using node:

node app.js

Or add a start script to package.json

"scripts": {
    ....
    "start": "node index.js"
},

And run the server using:

npm start

You can add any desired routes inside your main express server file, app.js:

//.............
//..................
//.........
const express = require('express')
//.........
const index = require('./routes/index')
const users = require('./routes/users')
const organizations = require('./routes/organizations')
//.............
//..................
//.........
app.use('/', index)
app.use('/users', users)
app.use('/organizations', organizations)
//.............
//..................
//.........

Database tables naming

To work properly, this package, requires that the tables are named as follows:

  1. All table's names must be written in lowercase (example: ​users)
  2. All table's names must be written in English (example: devices)
  3. In case of having a space in the table's name it must be replaced by an underscore (example: ​device_brands​).
  4. All tables that represent a model must be written in plural (example: ​devices​).
  5. All tables that represent a relation must have as a suffix the model's name in singular (example: ​device_brands​ where ​device​ is the model).
  6. There are two types of relations:
    1. Between models (example: ​organization_users​, where one model is ​organizations​ and the other is ​users​, as this 2 tables exists and are written in plural, therefore they are models)
    2. Extensions of a same model (example: ​device_brands​ where the model is ​devices​ and it has attributes (1 or more) extensibles/related to ​brands​).
  7. All tables must have a PRIMARY KEY called id
  8. All tables who have a relation with another model or an extension of the same model, have a KEY which starts with the suffix id_ followed by the model's name in singular (example: ​id_user​).

TODO

  1. Create relations between models.
  2. Handle relations between models inside routes.
  3. A good log handling.
  4. Update existing models.
  5. Automatic Swagger documentation.