Comparing version 0.1.0 to 0.1.1
@@ -46,2 +46,46 @@ /* Getting Started | ||
}, | ||
//Add columns based on if it's not found in diffs | ||
function(callback) { | ||
async.eachSeries(tables, function(table, callbackEach) { | ||
var t = tables.filter(function(t) { | ||
diffs.map(function(diff) { | ||
if (t.tableName == diffs.table) { | ||
return true; | ||
} | ||
}) | ||
return false; | ||
}); | ||
if (t.length > 0) { | ||
var c = t[0].columns.filter(function(co) { | ||
co.column == diff.column | ||
}); | ||
} else { | ||
return callbackEach(); | ||
} | ||
if (c.length == 0) { | ||
d = c.type; | ||
c = c.null; | ||
var d = diff.dtype.toUpperCase(); | ||
q = | ||
` | ||
ALTER TABLE `+diff.table+` ADD `+diff.column+` | ||
`+d+` `+n+` | ||
` | ||
} else { | ||
return callbackEach(); | ||
} | ||
if (logging == true) console.log(q); | ||
client.query(q, function(err, result) { | ||
if (err) throw err; | ||
return callbackEach(); | ||
}) | ||
}, function() { | ||
return callback(); | ||
}) | ||
}, | ||
//Change diffs based on existing schemas | ||
@@ -54,5 +98,9 @@ function(callback) { | ||
return t.tableName == diff.table | ||
})[0]; | ||
}); | ||
var c = t.columns.filter(function(co) { | ||
if (t.length == 0) { | ||
return callbackEach(); | ||
} | ||
var c = t[0].columns.filter(function(co) { | ||
return co.name == diff.column; | ||
@@ -66,3 +114,3 @@ })[0]; | ||
` | ||
} else { | ||
} else if (c.length == 1 && diff) { | ||
@@ -110,2 +158,4 @@ var dt = diff.dtype.toUpperCase(); | ||
` | ||
} else { | ||
callbackEach(); | ||
} | ||
@@ -137,2 +187,3 @@ | ||
result.rows.map(function(t) { | ||
console.log(t); | ||
diffsTbl.push(t.table_name); | ||
@@ -143,2 +194,30 @@ }) | ||
}, | ||
//Drop tables if they exist | ||
function (callback) { | ||
async.eachSeries(diffsTbl, function(dTbl, callbackEach) { | ||
var m = tables.filter(function(t) { | ||
return t.tableName.toLowerCase() == dTbl.toLowerCase(); | ||
}) | ||
if (m.length == 0) { | ||
var q = | ||
"\ | ||
DROP TABLE IF EXISTS "+dTbl+" \ | ||
"; | ||
if (logging == true) console.log(q); | ||
client.query(q, function(err, result) { | ||
if (err) throw err; | ||
callbackEach(); | ||
}) | ||
} else { | ||
callbackEach(); | ||
} | ||
}, function() { | ||
callback(); | ||
}) | ||
}, | ||
//Create new tables if they don't exist already | ||
@@ -149,3 +228,3 @@ function(callback) { | ||
table.tableName = table.tableName.toLowerCase(); | ||
diffsTbl.map(function(d) { | ||
@@ -152,0 +231,0 @@ if (d == table.tableName) m = true; |
{ | ||
"name": "flexgres", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Flexibly change your Postgres schemas with Flexgres.", | ||
@@ -5,0 +5,0 @@ "main": "flexgres.js", |
# flexgres | ||
## Flexibly change your Postgres schemas with Flexgres | ||
### By Chris Cates :star: | ||
### Email hello@chriscates.ca :mailbox: | ||
@@ -10,7 +12,7 @@ ### Installation | ||
### Usage | ||
You will need to use the `pg-query` package and the `pg` driver. | ||
You will need to use the `pg` driver. | ||
You can install it by writing: | ||
``` | ||
npm install pg-query pg --save | ||
npm install pg --save | ||
``` | ||
@@ -29,3 +31,3 @@ | ||
require("./test.js")({ | ||
require("flexgres")({ | ||
"driver": client, | ||
@@ -71,1 +73,10 @@ "logging": true, | ||
``` | ||
So pretty much there are three JSON key values: | ||
1. driver: this is the `pg` object with query connection string. | ||
2. logging: enable this is if you want to keep track of the SQL queries executed on your database. | ||
3. tables: This is an array of the tables in your JSON object. See above demo to see how to structure | ||
your tables. | ||
### Questions email :mailbox: hello@chriscates.ca |
12050
6
263
80