RESTful - is easy to use restful service implementation for express and sequelize
Registers your database context on restful definitions, and service is created with it at github link.
For instance your database table is "Frameworks" in mysql registered as /frameworks
for methods:
- GET /frameworks
- GET /frameworks/:id
- POST /frameworks
- PUT /frameworks/:id
- DELETE /frameworks/:id
and by passing methods args on your registeration in array ["get", "post", "put", "delete"]
you are allowed to manipulate proper methods or register only your needs. P.S. ( as defaults all registered )
supports some default query options;
-
for array Response:
- select=property1,property2 (any property of model itself and some extras: id, href, createdAt, updatedAt)
- sort=property,type (any property of model and type as 'desc' or 'asc' is default )
- limit=number (25 is default)
- offset=number (0 is default)
-
for object Response:
- select=property1,property2 (any property of model itself and some extras: id, href, createdAt, updatedAt)
response are wrapped as follows;
for array responses:
next
or previous
properties might not exists depending on context. (optionals)
data
property can be null
or []
.
for object or primitive responses:
data
property can be null
or {}
.
How to install
with node package manager aka npm;
npm install --save restful-express-sequelize
Documentation for express
or sequelize
How to use
(PS: for database example check model)
if you prefer javascript then in your server.js or index.js file;
//importsvar express = ;var bodyParser = ;var gzip = ;var context = ;// model generted by sequelize-cli// (sequelize model:create --name Framework --attributes name:string,lang:string)var dbContext = ;// bind over ip and port instead of 127.0.0.1var port = processenvPORT || 52192;var host = processenvHOST || "192.168.1.100";// express instancevar server = ;// register body-parser middlewareserver;server;// register compression middlewareserver; // get items from modelsvar models = ;for var property in dbContext // register as options you can add { model: xxx, methods: ["get", "post"] } // methods are (optional) defaults all registered ["get", "post", "put", "delete"] models; // finally register your method(s) on base as '/v1/endpoint'// base is (optional) context.Resource.register(server, model)contextResource;// start servingserver;
if you prefer typescript then in your index.ts or server.ts file;
;;;;;;; ;; ; server.usebodyParser.json;server.usebodyParser.urlencoded; server.usegzip; ifdbContext.sequelize ;for in dbContext Resource.registerserver, models, "/v1/endpoint"; server.listenport, host,;
Models
in /model
folder
as Country.js
'use strict';module { var Country = sequelize; return Country;};
as City.js
'use strict';module { var City = sequelize; return City;};
as index.js
'use strict';var fs = ;var path = ;var Sequelize = ;var basename = path;var config = ;// placeholder for allvar dbContext = {};// connectvar sequelize = configdatabase configusername configpassword configoptions;// imports everything in this directory into entities and register relations later.fs ;// invoke associate methods on modelsObject ;// sync context oncesequelize;// exportsmoduleexports = dbContext;
Changes
- optional port added for local projects.
- bug fix.
License
Copyright 2017 Fatih Şen and contributors.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.