forked from pantsel/konga
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Worked on creating databases at runtime
- Loading branch information
Showing
5 changed files
with
329 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Created by user on 06/10/2017. | ||
*/ | ||
|
||
'use strict' | ||
|
||
var mysql = require("../../node_modules/sails-mysql/node_modules/mysql"); | ||
var dbConf = require("../../config/connections"); | ||
|
||
module.exports = { | ||
run : function (next) { | ||
console.log("Using MySQL DB Adapter."); | ||
return this.create(next); | ||
}, | ||
|
||
|
||
create : function(next) { | ||
|
||
var connection = mysql.createConnection({ | ||
host : dbConf.connections.mysql.host, | ||
port : dbConf.connections.mysql.port, | ||
user : dbConf.connections.mysql.user, | ||
password : dbConf.connections.mysql.password | ||
}); | ||
|
||
console.log("Creating database `" + dbConf.connections.postgres.database + "` if not exists."); | ||
|
||
connection.query('CREATE DATABASE IF NOT EXISTS ' + dbConf.connections.postgres.database, function (error, results, fields) { | ||
if (error) { | ||
console.error(error); | ||
return next(error); | ||
} | ||
|
||
return next(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Created by user on 06/10/2017. | ||
*/ | ||
|
||
'use strict' | ||
|
||
var pg = require("../../node_modules/sails-postgresql/node_modules/pg"); | ||
var dbConf = require("../../config/connections"); | ||
var _ = require("lodash"); | ||
|
||
module.exports = { | ||
run : function (next) { | ||
|
||
console.log("Using postgres DB Adapter."); | ||
|
||
var self = this; | ||
var user = dbConf.connections.postgres.user; | ||
var password = dbConf.connections.postgres.password; | ||
var dbName = dbConf.connections.postgres.database; | ||
var dbHost = dbConf.connections.postgres.host; | ||
var dbPort = dbConf.connections.postgres.port; | ||
var ssl = dbConf.connections.postgres.ssl; | ||
|
||
var opts = { | ||
user: user, | ||
host: dbHost, | ||
database: dbName, | ||
password: password, | ||
port: dbPort, | ||
} | ||
|
||
// console.log("Connection Options =>", opts); | ||
|
||
pg.connect(opts, function (err, client, done) { | ||
if (err) { | ||
|
||
if(err.code == "3D000") | ||
{ | ||
console.log(dbName + " does not exist. Creating..."); | ||
done(); | ||
return self.create(opts,next); | ||
|
||
}else{ | ||
console.error("Failed to connect to DB named `" + dbName + "`",err); | ||
return next(err); | ||
} | ||
}else{ | ||
console.log("`" + dbName + "` database exists. Continue..."); | ||
return next(); | ||
} | ||
|
||
}); | ||
}, | ||
|
||
|
||
create : function(opts,next) { | ||
|
||
// Hook up to postgres db so we can create a new one | ||
var defaultDbOpts = _.merge(_.cloneDeep(opts),{ | ||
database : "postgres" | ||
}); | ||
|
||
pg.connect(defaultDbOpts, function (err, client, done) { | ||
if (err) { | ||
console.log(err); | ||
done(); | ||
return next(err); | ||
} | ||
|
||
client.query('CREATE DATABASE ' + opts.database, function (err, res) { | ||
if (err) { | ||
console.log("Failed to create `" + opts.database +"`",err); | ||
done(); | ||
return next(err); | ||
|
||
} | ||
|
||
console.log("Database `" + opts.database + "` created! Continue..."); | ||
|
||
return next(); | ||
|
||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Created by user on 06/10/2017. | ||
*/ | ||
'use strict' | ||
|
||
|
||
module.exports = function (next) { | ||
switch (process.env.DB_ADAPTER) { | ||
case("postgres"): | ||
return require("./dbs/pg").run(next); | ||
case("mysql"): | ||
return require("./dbs/mysql").run(next); | ||
default: | ||
console.log("No DB Adapter defined. Using localDB..."); | ||
return next(); | ||
|
||
} | ||
} | ||
|
||
|
||
|
||
|
Oops, something went wrong.