Переглянути джерело

Db connection and SQT for sailsjs

Zane Kansil 10 роки тому
батько
коміт
fa6a28fc2b

+ 61 - 0
frameworks/JavaScript/sailsjs/api/controllers/DatabaseQueryController.js

@@ -0,0 +1,61 @@
+/**
+ * DatabaseQueryController
+ *
+ * @description :: Server-side logic for managing Database Queries
+ */
+
+var Sequelize = require('sequelize')
+var sequelize = new Sequelize(
+  'hello_world', 'benchmarkdbuser', 'benchmarkdbpass',
+  {
+    host: '127.0.0.1',
+    dialect: 'mysql',
+    pool: {
+      max: 5000,
+      min: 0,
+      idle: 5000
+    },
+    logging: true
+  });
+
+
+var World = sequelize.define('World', {
+  id: Sequelize.INTEGER,
+  randomNumber: Sequelize.INTEGER
+},
+{
+  // prevents sequelize from assuming the table is called 'Worlds'
+  freezeTableName: true,
+  timestamps: false,
+})
+
+var randomWorldId = function() {
+  return Math.floor(Math.random() * 10000) + 1;
+}
+
+module.exports = {
+
+
+
+  /**
+   * Test 2: Single Database Query
+   */
+  single: function (req, res) {
+    World.findOne({
+      where: { id: randomWorldId() }
+    }).then(function(results) {
+      return res.json(results.get());
+    })
+  },
+
+
+  /**
+   * Test 3: Multiple Database Query
+   */
+  multiple: function (req, res) {
+    return res.json({
+      todo: 'multiple() is not implemented yet!'
+    });
+  }
+};
+

+ 0 - 15
frameworks/JavaScript/sailsjs/api/models/JsonTest.js

@@ -1,15 +0,0 @@
-/**
-* JsonTest.js
-*
-* @description :: TODO: You might write a short summary of how this model works and what it represents here.
-* @docs        :: http://sailsjs.org/#!documentation/models
-*/
-
-module.exports = {
-
-  attributes: {
-
-    message : { type: 'string' }
-  }
-};
-

+ 2 - 0
frameworks/JavaScript/sailsjs/config/bootstrap.js

@@ -11,6 +11,8 @@
 
 module.exports.bootstrap = function(cb) {
 
+  sails.Sequelize = require('sequelize')
+
   // It's very important to trigger this callback method when you are finished
   // with the bootstrap!  (otherwise your server will never lift, since it's waiting on the bootstrap)
   cb();

+ 2 - 1
frameworks/JavaScript/sailsjs/config/routes.js

@@ -36,7 +36,8 @@ module.exports.routes = {
     view: 'homepage'
   },
 
-  'get /json': 'JsonTestController.get'
+  'get /json': 'JsonTestController.get',
+  'get /db':   'DatabaseQueryController.single'
 
   /***************************************************************************
   *                                                                          *

+ 14 - 12
frameworks/JavaScript/sailsjs/package.json

@@ -5,23 +5,25 @@
   "description": "a Sails application",
   "keywords": [],
   "dependencies": {
-    "sails": "~0.11.0",
-    "sails-disk": "~0.10.0",
-    "rc": "~0.5.0",
-    "include-all": "~0.1.3",
     "ejs": "~0.8.4",
     "grunt": "0.4.2",
-    "grunt-sync": "~0.0.4",
-    "grunt-contrib-copy": "~0.5.0",
     "grunt-contrib-clean": "~0.5.0",
+    "grunt-contrib-coffee": "~0.10.1",
     "grunt-contrib-concat": "~0.3.0",
-    "grunt-sails-linker": "~0.9.5",
-    "grunt-contrib-jst": "~0.6.0",
-    "grunt-contrib-watch": "~0.5.3",
-    "grunt-contrib-uglify": "~0.4.0",
+    "grunt-contrib-copy": "~0.5.0",
     "grunt-contrib-cssmin": "~0.9.0",
+    "grunt-contrib-jst": "~0.6.0",
     "grunt-contrib-less": "0.11.1",
-    "grunt-contrib-coffee": "~0.10.1"
+    "grunt-contrib-uglify": "~0.4.0",
+    "grunt-contrib-watch": "~0.5.3",
+    "grunt-sails-linker": "~0.9.5",
+    "grunt-sync": "~0.0.4",
+    "include-all": "~0.1.3",
+    "mysql": "^2.6.2",
+    "rc": "~0.5.0",
+    "sails": "~0.11.0",
+    "sails-disk": "~0.10.0",
+    "sequelize": "^2.0.6"
   },
   "scripts": {
     "start": "node app.js",
@@ -34,4 +36,4 @@
   },
   "author": "zane",
   "license": ""
-}
+}