This repository has been archived by the owner on Mar 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 159
/
callbacks.js
82 lines (59 loc) · 1.82 KB
/
callbacks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var massive = require("../index");
var util = require("util");
var _ = require("underscore")._;
massive.connect("postgres://postgres@localhost/test", function(err,db) {
//massive.connect({user : "root", password : "", database : "test"}, function(err,db) {
var dropProducts = db.dropTable("products").execute(function(err,data){
console.log("Products table dropped");
createProducts();
});
var createProducts = function(){
var createQuery = db.createTable("products", {
name : "string",
price : "money",
timestamps : true
});
createQuery.execute(function(err,data){
console.log("Products table created");
db.loadTables(function(err,_db){
db = _db;
insertProducts();
});
});
}
var insertProducts = function() {
var items = [
{name:"stuffy stuff", price: 12.00},
{name:"poofy poof", price: 24.00}
];
db.products.insert(items).execute(function(err,newProducts){
console.log("Added " + newProducts.length + " products to db - here they are");
showProducts(function(){
updatePrices();
});
});
}
var showProducts = function(callback){
var query = db.products.find();
query.on("row", function(p){
console.log(p);
});
query.on("end", callback)
}
var updatePrices = function(){
console.log("Updating prices due to inflation");
db.products.update({price : 100.00}, {"id >" : 0}).execute(function(err,results){
console.log("Prices updated: " + results.rowCount);
showProducts(function(){
deleteAll();
});
});
}
var deleteAll = function() {
console.log("Deleting everything, outta here!");
var query = db.products.destroy();
query.execute(function(err,results){
console.log("Everything toast!");
});
}
});