瀏覽代碼

add new mongodb config

zhangweicheng 8 年之前
父節點
當前提交
df981e0e85

+ 14 - 1
config/config.js

@@ -17,5 +17,18 @@ module.exports = {
         var me = this;
         me.current.server = me.prod.server;
         me.current.port = me.prod.port;
-    }
+    },
+    options:{
+            "user": "",
+            "pass": "",
+            "auth": {
+                "authdb": ""
+            },
+            "server": {
+                 "socketOptions": {
+                "connectTimeoutMS": 10000
+                }
+            }
+
+        }
 }

+ 15 - 0
config/db/db_manager.js

@@ -45,5 +45,20 @@ module.exports = {
         mg.connect('mongodb://' + config.current.server + ":" + config.current.port + '/' + dbName);
         return mg;
         //*/
+    },
+    connect:function () {
+        var config = require("../config.js");
+        var dbURL = 'mongodb://' + config.current.server + ":" + config.current.port + '/scConstruct';
+
+        var db = mg.connect(dbURL, config.options, function(err) {
+            if (err) {
+                console.log('Could not connect to MongoDB!');
+                console.log(err);
+            }
+        });
+        mg.connection.on('error', function(err) {
+            console.log('MongoDB connection error:', err);
+            process.exit(-1);
+        });
     }
 };

+ 47 - 0
modules/common/fileUtils.js

@@ -0,0 +1,47 @@
+'use strict';
+
+/**
+ * Module dependencies.
+ */
+var _ = require('lodash'),
+	glob = require('glob');
+
+
+/**
+ * Get files by glob patterns
+ */
+module.exports.getGlobbedFiles = function(globPatterns, removeRoot) {
+	// For context switching
+	var _this = this;
+
+	// URL paths regex
+	var urlRegex = new RegExp('^(?:[a-z]+:)?\/\/', 'i');
+
+	// The output array
+	var output = [];
+
+	// If glob pattern is array so we use each pattern in a recursive way, otherwise we use glob
+	if (_.isArray(globPatterns)) {
+		globPatterns.forEach(function(globPattern) {
+			output = _.union(output, _this.getGlobbedFiles(globPattern, removeRoot));
+		});
+	} else if (_.isString(globPatterns)) {
+		if (urlRegex.test(globPatterns)) {
+			output.push(globPatterns);
+		} else {
+			glob(globPatterns, {
+				sync: true
+			}, function(err, files) {
+				if (removeRoot) {
+					files = files.map(function(file) {
+						return file.replace(removeRoot, '');
+					});
+				}
+
+				output = _.union(output, files);
+			});
+		}
+	}
+
+	return output;
+};

+ 14 - 0
modules/ration_glj/controllers/ration_glj_controller.js

@@ -0,0 +1,14 @@
+/**
+ * Created by chen on 2017/6/29.
+ */
+let mongoose = require("mongoose")
+module.exports={
+    createRationGLJ:createRationGLJ
+}
+function createRationGLJ() {
+    let gls = mongoose.model('ration_glj');
+    gls.create({'GLJID':1,'basePrice':23.23,'name':"testgls"},function(err, gls){
+        console.log(gls)
+    })
+}
+

+ 30 - 0
modules/ration_glj/models/ration_glj.js

@@ -0,0 +1,30 @@
+/**
+ * Created by chen on 2017/6/29.
+ */
+var mongoose = require('mongoose'),
+    Schema = mongoose.Schema;
+
+/**
+ * Schema
+ */
+var ration_glj = new Schema({
+    ID: String,
+    GLJID:Number,
+    projectID: Number,
+    name:String,
+    code:String,
+    specs:String,
+    unit:String,
+    basePrice:Number,
+    gljDistType:String,
+    gljType:Number,
+    quantity:Number,
+    customQuantity:Number,
+    rationItemQuantity:Number,
+    marketPrice:Number,
+    adjustPrice:Number,
+    marketPriceAdjest:Number,
+    isEstimate:Boolean
+});
+
+mongoose.model('ration_glj', ration_glj);

+ 4 - 2
package.json

@@ -18,11 +18,13 @@
     "request": "^2.79.0",
     "tape": "^4.6.3",
     "nodemon": "^1.11.0",
-    "lodash": "^3.10.1"
+    "lodash": "^3.10.1",
+    "glob": "~4.0.5"
   },
   "dependencies": {
     "bluebird": "^3.5.0",
-    "jszip": "^3.1.3"
+    "jszip": "^3.1.3",
+    "uuid": "^3.1.0"
   },
   "scripts": {
     "start": "nodemon server.js"

+ 8 - 0
server.js

@@ -1,6 +1,8 @@
 let express = require('express');
 
 let config = require("./config/config.js");
+let fileUtils = require("./modules/common/fileUtils");
+let dbm = require("./config/db/db_manager");
 //config.setToLocalDb();
 config.setToQaDb();
 //config.setupCache();
@@ -13,6 +15,12 @@ let DBStore = require('connect-mongo')(session);
 
 let URL = require('url')
 
+dbm.connect();
+//这里现在只引入了定额工料机里的models,当其它模块的models修改后使用:./modules/**/models/*.js引入所有的模块
+fileUtils.getGlobbedFiles('./modules/ration_glj/models/*.js').forEach(function(modelPath) {
+    require(path.resolve(modelPath));
+})
+
 let app = express();
 let _rootDir = __dirname;
 app.use(express.static(_rootDir));