BVB Source Codes

waterline Show express-example.js Source code

Return Download waterline: download express-example.js Source code - Download waterline Source code - Type:.js
  1. /**
  2.  * A simple example of how to use Waterline v0.10 with Express
  3.  */
  4.  
  5. var express = require('express'),
  6.     _ = require('@sailshq/lodash'),
  7.     app = express(),
  8.     Waterline = require('waterline'),
  9.     bodyParser = require('body-parser'),
  10.     methodOverride = require('method-override');
  11.  
  12.  
  13.  
  14. // Instantiate a new instance of the ORM
  15. var orm = new Waterline();
  16.  
  17.  
  18. //////////////////////////////////////////////////////////////////
  19. // WATERLINE CONFIG
  20. //////////////////////////////////////////////////////////////////
  21.  
  22. // Require any waterline compatible adapters here
  23. var diskAdapter = require('sails-disk'),
  24.     mysqlAdapter = require('sails-mysql');
  25.  
  26.  
  27. // Build A Config Object
  28. var config = {
  29.  
  30.   // Setup Adapters
  31.   // Creates named adapters that have been required
  32.   adapters: {
  33.     'default': diskAdapter,
  34.     disk: diskAdapter,
  35.     mysql: mysqlAdapter
  36.   },
  37.  
  38.   // Build Connections Config
  39.   // Setup connections using the named adapter configs
  40.   connections: {
  41.     myLocalDisk: {
  42.       adapter: 'disk'
  43.     },
  44.  
  45.     myLocalMySql: {
  46.       adapter: 'mysql',
  47.       host: 'localhost',
  48.       database: 'foobar'
  49.     }
  50.   },
  51.  
  52.   defaults: {
  53.     migrate: 'alter'
  54.   }
  55.  
  56. };
  57.  
  58.  
  59. //////////////////////////////////////////////////////////////////
  60. // WATERLINE MODELS
  61. //////////////////////////////////////////////////////////////////
  62.  
  63. var User = Waterline.Model.extend({
  64.  
  65.   identity: 'user',
  66.   connection: 'myLocalDisk',
  67.  
  68.   attributes: {
  69.     first_name: 'string',
  70.     last_name: 'string'
  71.   }
  72. });
  73.  
  74. var Pet = Waterline.Model.extend({
  75.  
  76.   identity: 'pet',
  77.   connection: 'myLocalMySql',
  78.  
  79.   attributes: {
  80.     name: 'string',
  81.     breed: 'string'
  82.   }
  83. });
  84.  
  85.  
  86. // Load the Models into the ORM
  87. orm.registerModel(User);
  88. orm.registerModel(Pet);
  89.  
  90.  
  91.  
  92. //////////////////////////////////////////////////////////////////
  93. // EXPRESS SETUP
  94. //////////////////////////////////////////////////////////////////
  95.  
  96.  
  97. // Setup Express Application
  98. app.use(bodyParser.urlencoded({ extended: false }));
  99. app.use(bodyParser.json());
  100. app.use(methodOverride());
  101.  
  102. // Build Express Routes (CRUD routes for /users)
  103.  
  104. app.get('/users', function(req, res) {
  105.   app.models.user.find().exec(function(err, models) {
  106.     if(err) return res.json({ err: err }, 500);
  107.     res.json(models);
  108.   });
  109. });
  110.  
  111. app.post('/users', function(req, res) {
  112.   app.models.user.create(req.body, function(err, model) {
  113.     if(err) return res.json({ err: err }, 500);
  114.     res.json(model);
  115.   });
  116. });
  117.  
  118. app.get('/users/:id', function(req, res) {
  119.   app.models.user.findOne({ id: req.params.id }, function(err, model) {
  120.     if(err) return res.json({ err: err }, 500);
  121.     res.json(model);
  122.   });
  123. });
  124.  
  125. app.delete('/users/:id', function(req, res) {
  126.   app.models.user.destroy({ id: req.params.id }, function(err) {
  127.     if(err) return res.json({ err: err }, 500);
  128.     res.json({ status: 'ok' });
  129.   });
  130. });
  131.  
  132. app.put('/users/:id', function(req, res) {
  133.   // Don't pass ID to update
  134.   delete req.body.id;
  135.  
  136.   app.models.user.update({ id: req.params.id }, req.body, function(err, model) {
  137.     if(err) return res.json({ err: err }, 500);
  138.     res.json(model);
  139.   });
  140. });
  141.  
  142.  
  143.  
  144. //////////////////////////////////////////////////////////////////
  145. // START WATERLINE
  146. //////////////////////////////////////////////////////////////////
  147.  
  148. // Start Waterline passing adapters in
  149. orm.initialize(config, function(err, models) {
  150.   if(err) throw err;
  151.  
  152.   app.models = models.collections;
  153.   app.connections = models.connections;
  154.  
  155.   // Start Server
  156.   app.listen(3000);
  157.  
  158.   console.log("To see saved users, visit http://localhost:3000/users");
  159. });
  160.  
downloadexpress-example.js Source code - Download waterline Source code
Related Source Codes/Software:
notepad-plus-plus - Notepad++ official repository h... 2017-01-10
che - Eclipse Che: Next-generation Eclipse IDE. Open sou... 2017-01-10
Gource - oftware version control visualization ... 2017-01-10
FDFullscreenPopGesture - A UINavigationController's category to enable full... 2017-01-10
node-style-guide - A guide for styling your node.js / JavaScript code... 2017-01-09
Workerman - An asynchronous event driven PHP framework for eas... 2017-01-10
structor - An advanced visual editor for React components ... 2017-01-10
golearn - Machine Learning for Go 2017-01-10
poisontap - Exploits locked/password protected computers over ... 2017-01-10
kcptun - A Simple UDP Tunnel Based On KCP 2017-01-11
react-templates - Light weight templates for react ... 2017-04-28
afterglow-theme - A minimal dark Theme for Sublime Text 2 and 3 2017-04-28
jwt-go - Golang implementation of JSON Web Tokens (JWT) 2017-04-28
DeerResume - Tool MarkDown online resume, online preview, edit,... 2017-04-28
mailgen - A Node.js package that generates clean, responsive... 2017-04-28
desk - A lightweight workspace manager for the shell 2017-04-28
Glimpse - The open source diagnostics platform for the web ... 2017-04-28
xonsh - Python-powered, cross-platform, Unix-gazing shell ... 2017-04-28
react-monocle - A developer tool to visualize a React application'... 2017-04-28

 Back to top