Skip to content

Commit

Permalink
Setting up MySQL, migrations and dotenv
Browse files Browse the repository at this point in the history
 * uses db-migrate
  • Loading branch information
ashleysimons committed May 23, 2017
1 parent e862788 commit d45a7df
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .env.example
@@ -0,0 +1,5 @@
MYSQL_USER=root
MYSQL_PASSWORD=
MYSQL_HOST=127.0.0.1
MYSQL_DATABASE_NAME=demo
MYSQL_TEST_DATABASE_NAME=demo_test
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -116,3 +116,4 @@ npm-debug.log
nbproject
.idea
.node_history
.env
11 changes: 11 additions & 0 deletions api/controllers/UserController.js
@@ -0,0 +1,11 @@
/**
* UserController
*
* @description :: Server-side logic for managing users
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/

module.exports = {

};

47 changes: 47 additions & 0 deletions api/models/User.js
@@ -0,0 +1,47 @@
/**
* User.js
*
* @description :: This represents a user in the system.
* @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models
*/
module.exports = {
attributes: {
firstname: {
type: 'string',
required: 'true',
},
surname: {
type: 'string',
required: 'true',
},
password: {
type: 'string',
minLength: 10,
required: true,
},
email: {
type: 'string',
unique: true,
index: true,
required: true,
},
terms: {
type: 'boolean',
required: true,
boolean: true,
},
toJSON: function(){
const obj = this.toObject();
delete obj.password;
return obj;
}
},
beforeValidate: function(user, cb){
user.terms = (user.terms == 'on' || user.terms == true) ? true : undefined;
cb();
},
beforeCreate: function(user, cb) {
// hash password
cb();
}
}
2 changes: 2 additions & 0 deletions config/bootstrap.js
@@ -1,3 +1,5 @@
require('dotenv').config();

/**
* Bootstrap
* (sails.config.bootstrap)
Expand Down
16 changes: 16 additions & 0 deletions config/connections.js
Expand Up @@ -48,6 +48,22 @@ module.exports.connections = {
// database: 'YOUR_MYSQL_DB' //optional
// },

someMysqlServer: {
adapter: 'sails-mysql',
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE_NAME
},

testMysqlServer: {
adapter: 'sails-mysql',
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_TEST_DATABASE_NAME
},

/***************************************************************************
* *
* MongoDB is the leading NoSQL database. *
Expand Down
4 changes: 2 additions & 2 deletions config/models.js
Expand Up @@ -17,7 +17,7 @@ module.exports.models = {
* connections (see `config/connections.js`) *
* *
***************************************************************************/
// connection: 'localDiskDb',
connection: 'someMysqlServer',

/***************************************************************************
* *
Expand All @@ -27,6 +27,6 @@ module.exports.models = {
* See http://sailsjs.org/#!/documentation/concepts/ORM/model-settings.html *
* *
***************************************************************************/
// migrate: 'alter'
migrate: 'safe'

};
10 changes: 10 additions & 0 deletions database.json
@@ -0,0 +1,10 @@
{
"dev": {
"host": { "ENV" : "MYSQL_HOST" },
"user": { "ENV" : "MYSQL_USER" },
"password" : { "ENV" : "MYSQL_PASSWORD" },
"database": { "ENV" : "MYSQL_DATABASE_NAME" },
"driver": "mysql",
"multipleStatements": true
}
}
38 changes: 38 additions & 0 deletions migrations/20170523032446-users.js
@@ -0,0 +1,38 @@
'use strict';

var dbm;
var type;
var seed;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
};

exports.up = function(db) {
return db.runSql("CREATE TABLE `user` (\
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,\
`firstname` varchar(255) DEFAULT NULL,\
`surname` varchar(255) DEFAULT NULL,\
`email` varchar(255) DEFAULT NULL,\
`password` varchar(255) DEFAULT NULL,\
`terms` tinyint(1) DEFAULT '0',\
`createdAt` datetime DEFAULT NULL,\
`updatedAt` datetime DEFAULT NULL,\
PRIMARY KEY (`id`),\
UNIQUE KEY `email` (`email`)\
) ENGINE=InnoDB DEFAULT CHARSET=utf8");
};

exports.down = function(db) {
return db.runSql("DROP TABLE `user`");
};

exports._meta = {
"version": 1
};
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -6,6 +6,7 @@
"keywords": [],
"dependencies": {
"babel-polyfill": "^6.23.0",
"dotenv": "^4.0.0",
"ejs": "2.3.4",
"grunt": "1.0.1",
"grunt-contrib-clean": "1.0.0",
Expand All @@ -24,7 +25,8 @@
"react": "^15.5.4",
"react-dom": "^15.5.4",
"sails": "~0.12.13",
"sails-disk": "~0.10.9"
"sails-disk": "~0.10.9",
"sails-mysql": "^0.11.5"
},
"scripts": {
"debug": "node debug app.js",
Expand Down

0 comments on commit d45a7df

Please sign in to comment.