• Stars
    star
    121
  • Rank 293,924 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easily convert your SQL database into a REST API using Express.js

Easily convert your SQL database into a REST API.

This is a lightweight Express.js middleware library that is able to convert SQL databases into a REST API. This library also works seamlessly with the Form.io platform where you can build Angular.js and React.js applications on top of your SQL database. Please go to https://form.io to learn more.

This library has been validated with Microsoft SQL Server, MySQL, and PostgreSQL.

How it works

This module works by assigning routes to specific queries, which you define, that are executed when the routes are triggered. For example, lets say you have the following customer table.

customer

  • firstName
  • lastName
  • email

This library is able to convert this into the following REST API.

  • GET: /customer - Returns a list of all customers.
  • GET: /customer/:id - Returns a single customer
  • POST: /customer - Creates a new customer
  • PUT: /customer/:id - Updates a customer
  • DELETE: /customer/:id - Deletes a customer.

Please refer to the FULL CRUD Example below to see how example configurations to achieve the following.

How to use

This library is pretty simple. You include it in your Express.js application like the following.

const { Resquel } = require('resquel');
const express = require('express');
const app = express();

(async function () {
    const resquel = new Resquel({
      db: {
        client: 'mysql',
        connection: {
          host: 'localhost',
          database: 'formio',
          user: 'root',
          password: 'CHANGEME'
        }
      },
      routes: [
        {
          method: 'get',
          endpoint: '/customer',
          query: 'SELECT * FROM customers'
        },
        ...
      ]
    });
    await resquel.init();
    app.use(resquel.router);

    // Listen to port 3010.
    app.listen(3010);
})();

DB Configuration

Please review Knex documentation for specific details on configuring the database connection. The paramaters are passed through to Knex, so all options that are valid there for your database server will work.

Routes

The routes definition is where you will define all of your SQL routes for this library. It is an array of routes that are each defined as follows.

{
  method: 'get|post|put|delete',
  endpoint: '/your/endpoint/:withParams',
  query: 'SELECT * FROM customer'
}

Advanced Queries

The query property in routes can be provided in 3 forms:

  1. Simple query
query: 'SELECT * FROM customer'

This is very limited in use, and mostly provided as shorthand

  1. Multiple queries
query: [
  'TRUNCATE customer',
  'SELECT * FROM customer'
]

When multiple queries are provided, only the return response from the last query appears in the reply

  1. Prepared queries
query: [
  [
    'UPDATE customer SET firstName=?, lastName=?, email=? WHERE id=?',
    'body.firstName',
    'body.lastName',
    'body.email',
    'params.id'
  ],
  [
    'SELECT * FROM customer id=?',
    'params.id'
  ]
]

This is the true intended way to use the library. In the inner arrays, the first item MUST be the query. All subsequent items are substitution values for the ? in the query in the format of object paths on the req object. All properties are accessible, including (but not limited to): headers, params, query, body.

If not all values required by the prepared query are available, then an error will be emitted and execution of queries on that route will be halted (if there are followup queries present).

Note: When using prepared queries, mixing in shorthand style queries will result in an error. Invalid example:

query: [
  [
    'DELETE FROM customer WHERE id=?',
    'params.customerId'
  ],
  'SELECT COUNT(*) AS num FROM customer'
]

Full CRUD example

Let's suppose that you have a SQL table called "customers" that have the following fields.

  • First Name (firstName)
  • Last Name (lastName)
  • Email (email)

The following configuration would generate a full CRUD REST API for this SQL Table.

const { Resquel } = require('resquel');
const express = require('express');
const app = express();
(async function () {
    const resquel = new Resquel({
        db: {
            client: process.env.DB_TYPE,
            connection: {
                host: process.env.DB_HOST,
                database: process.env.DB_NAME,
                user: process.env.DB_USER,
                password: process.env.DB_PASS
            },
        },
        routes: [
            {
              method: 'get',
              endpoint: '/customer',
              query: 'SELECT * FROM customers'
            },
            {
              method: 'post',
              endpoint: '/customer',
              query: [
                [
                  'INSERT INTO customers (firstname, lastName, email) VALUES (?, ?, ?, ?)',
                  'body.data.firstName',
                  'body.data.lastName',
                  'body.data.email'
                ],
                [
                  'SELECT * FROM customers WHERE id=LAST_INSERT_ID();'
                ]
              ]
            },
            {
              method: 'get',
              endpoint: '/customer/:id',
              query: [
                'SELECT * FROM customers WHERE id=?',
                'params.id'
              ]
            },
            {
              method: 'put',
              endpoint: '/customer/:id',
              query: [
                [
                  'UPDATE customers SET firstName=?, lastName=?, email=? WHERE id=?',
                  'body.data.firstName',
                  'body.data.lastName',
                  'body.data.email',
                  'params.id'
                ],
                [
                  'SELECT * FROM customers WHERE id=?',
                  'params.id'
                ]
              ]
            },
            {
              method: 'delete',
              endpoint: '/customer/:id',
              query: [
                'DELETE FROM customers WHERE id=?',
                'params.id'
              ]
            }
        ]
    });
    await resquel.init();
    app.use(resquel.router);
    app.listen(3010);
})();

Troubleshooting

Using with MySQL 8

With the latest version of MySQL 8, you will need to ensure that the "root" user is able to login with the password provider. Otherwise you will get an error. If you are using this library with MySQL 8, please make sure you run the following query within your database to ensure that you are able to authenticate properly.

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'YourRootPassword';
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourRootPassword';

Enjoy!

  • The Form.io Team

More Repositories

1

formio

A Form and Data Management Platform for Progressive Web Applications.
JavaScript
2,005
star
2

formio.js

JavaScript powered Forms with JSON Form Builder
JavaScript
1,838
star
3

angular

JSON powered forms for Angular
TypeScript
634
star
4

react

JSON powered forms for React.js
TypeScript
301
star
5

ngFormBuilder

The Form.IO Form Builder Application
JavaScript
130
star
6

vue

Javascript Powered forms and JSON form builder for Vue.js
TypeScript
118
star
7

ngFormio

JSON powered form rendering library for AngularJS + Form.io.
JavaScript
99
star
8

angular-demo

A Demo application with Angular + Form.io
TypeScript
62
star
9

react-app-starterkit

A react starterkit for creating new web applications with Form.io
TypeScript
56
star
10

aurelia-formio

Form.io JSON Forms and Form Builder for Aurelia
TypeScript
45
star
11

angular-material

JSON powered forms for Angular Material
TypeScript
45
star
12

help.form.io

The help site for Form.io
JavaScript
42
star
13

angular-app-starterkit

Angular 7 + Bootstrap 4 application starter kit using Form.io
TypeScript
40
star
14

formio-viewer

Landing page viewer for forms and wizards within Form.io
HTML
33
star
15

keycred

A key credential generator for Office 365 App-Only API applications.
JavaScript
20
star
16

contrib

Contributed components for Form.io
HTML
18
star
17

react-native-formio

React Native Version of Formio Renderer
JavaScript
18
star
18

formio-service

The Form.IO API Service library for Node.js applications
JavaScript
18
star
19

cli

The Form.io command line interface.
JavaScript
17
star
20

formio-php

PHP Integration with Form.io
PHP
13
star
21

vue-app-starterkit

Starterkit for building apps with form.io and vue
Vue
13
star
22

formio-webhook-receiver

An example webhook receiver application.
JavaScript
13
star
23

formio-app-formio

The Form.io main front-end application.
JavaScript
13
star
24

formio-app-servicetracker

JavaScript
11
star
25

ngFormioGrid

Provides a way to display Form.io submission data within the Angular UI-Grid.
JavaScript
11
star
26

uswds

JavaScript Powered Forms for the United States Web Design System
JavaScript
11
star
27

formio-app-basic

A basic starter Angular.js application with User Authentication using Form.io
JavaScript
9
star
28

formio-red

Form.io integration into Node Red - http://nodered.org
HTML
9
star
29

enterprise-release

Release notes for Enterprise Form.io Servers
9
star
30

formio-upload

A file upload server/proxy for use with the Form.io file component.
JavaScript
9
star
31

core

The Form.io Core Javascript Framework
TypeScript
9
star
32

formio-app-formbuilder

A user portal for creating and managing forms
JavaScript
8
star
33

bootstrap

Bootstrap templates for form.io forms
JavaScript
7
star
34

formio-wizard

Angular.js Wizard using Form.io forms.
JavaScript
7
star
35

sw-express

Extend express so it will work in service workers
JavaScript
6
star
36

ng-app-starterkit

An angular 1.x starterkit for creating new web applications with Form.io
JavaScript
6
star
37

ngFormioHelper

A library to help build Angluar.js applications on top of Form.io.
JavaScript
6
star
38

examples.form.io

Examples of Form.IO integration and implementation.
JavaScript
6
star
39

formio-app-gpstracker

An application that allows you to create trails and track GPS coordinates.
JavaScript
5
star
40

formio-app-humanresources

An form and data management application for Human Resources.
JavaScript
5
star
41

formio-workers

A library of workers for the Form.io platform.
JavaScript
5
star
42

formio-server-proxy

A proxy server for the Form.io Enterprise Server.
JavaScript
5
star
43

formio-app-eventmanager

A very basic Event Registration system built on top of Form.io.
JavaScript
5
star
44

formio-app-todo

An example ToDo application for Form.io.
JavaScript
5
star
45

formio-app-movie

An example application using the Form.IO platform.
JavaScript
4
star
46

api

A form manager module
TypeScript
4
star
47

pdf-enterprise

PDF Server for Form.io Enterprise Server
CSS
4
star
48

aws-formio-deployment

Configurations for deploying Form.io into AWS
Roff
4
star
49

edge

A highly performant and extensible Application Server for Form.io Multi-Tenant Deployments.
JavaScript
4
star
50

pdf-libs

The PDF Libraries
C++
3
star
51

cordova-offline-demo

Cordova Offline Application Demo
JavaScript
3
star
52

uswds-viewer

HTML
3
star
53

semantic

Semantic UI Templates for form.io
JavaScript
3
star
54

module-example

An example of how to create a module for the formio.js renderer and builder.
JavaScript
3
star
55

deploy

Deployment strategies for Form.io Enterprise platform
Smarty
3
star
56

generator-formio

A Yeoman Generator for creating Form.IO applications.
JavaScript
3
star
57

bulma

Bulma templates for formio.js
JavaScript
2
star
58

formio-question

A directive that provides question and answer capabilities for Form.io forms.
JavaScript
2
star
59

formio-app-salesquote

An example application in Form.io for managing sales quotes.
JavaScript
2
star
60

instructions

Instructions for using the private package repository
2
star
61

eventmanager

An Event Manager application built in Angular + Form.io
TypeScript
2
star
62

formio-translations-demo

An Angular 5 application that integrates PhraseApp with Form.io
TypeScript
2
star
63

portal

The Form.io Community Edition Portal
TypeScript
2
star
64

aurelia-app-starterkit

HTML
2
star
65

react-native-app

Makefile
1
star
66

pretty-checkbox

Adding pretty-checkbox plugin to Form.io forms
JavaScript
1
star
67

custom-renderer

An example custom renderer that extends the Form.io core renderer.
JavaScript
1
star
68

eslint-config-formio

The ESLint config for Form.io projects
JavaScript
1
star
69

formio-form

A basic Form.IO form template
HTML
1
star
70

lodash

A tiny (3kb gzipped) reduced-set implementation of the Lodash library.
JavaScript
1
star