GithubHelp home page GithubHelp logo

forkedit / resquel Goto Github PK

View Code? Open in Web Editor NEW

This project forked from formio/resquel

0.0 1.0 0.0 45 KB

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

License: MIT License

JavaScript 100.00%

resquel's Introduction

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 currently supports 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/:customerId - Returns a single customer
  • POST: /customer - Creates a new customer
  • PUT: /customer/:customerId - Updates a customer
  • DELETE: /customer/:customerId - Deletes a customer.

Please refer to the example folder to see how this library is used to achieve the following.

How to use

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

var express = require('express');
var resquel = require('resquel');

// Create the Express.js application.
var app = express();

// Include the resquel library.
app.use(resquel(config));

// Listen to port 3000.
app.listen(3000);

The configuration passed into the resquel library is where the magic happens.

{
  "db": {
    "user": "-- YOUR DATABASE USERNAME --",
    "password": "-- YOUR DATABASE PASSWORD --",
    "server": "-- YOUR DATABASE SERVER --",
    "database": "-- YOUR DATABASE NAME --",
    "options": {
      "instanceName": "-- THE SERVER INSTANCE --"
    }
  },
  "routes": [
    {
      "method": "get",
      "endpoint": "/customer",
      "query": "SELECT * FROM customers;"
    },
    {
      "method": "post",
      "endpoint": "/customer",
      "query": "INSERT INTO customers (firstName, lastName, email) VALUES ('{{ data.firstName }}', '{{ data.lastName }}', '{{ data.email }}');SELECT * FROM customers WHERE id=SCOPE_IDENTITY();"
    },
    {
      "method": "get",
      "endpoint": "/customer/:customerId",
      "query": "SELECT * FROM customers WHERE id={{ params.customerId }};"
    },
    {
      "method": "put",
      "endpoint": "/customer/:customerId",
      "query": "UPDATE customers SET firstName='{{ data.firstName }}', lastName='{{ data.lastName }}', email='{{ data.email }}' WHERE id={{ params.customerId }};SELECT * FROM customers WHERE id={{ params.customerId }};"
    },
    {
      "method": "delete",
      "endpoint": "/customer/:customerId",
      "query": "DELETE FROM customers WHERE id={{ params.customerId }};"
    }
  ]
}

Each route defines a new endpoint and maps a query to that enpoint. Within the query, you have access to the following.

  • data - The req.body of the request.
  • params - The req.params of the request. Like when you use /customer/:customerId
  • query - The req.query of the request. Used with GET parameters like this /customer?company=1234

Enjoy!

  • The Form.io Team

resquel's People

Contributors

zackurben avatar travist avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.