/
x.js
189 lines (178 loc) · 6.65 KB
/
x.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
GLOBAL.DEBUG = true;
test = require("assert");
var Db = require('../lib/mongodb').Db,
Connection = require('../lib/mongodb').Connection,
Server = require('../lib/mongodb').Server,
after = require("after"),
Stak = require("stak");
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT;
var LINE_SIZE = 120;
var dbStack = Stak.make(
function openDatabase() {
this.db.open(this.next)
},
function dropDatabase(err, db) {
this.db = db;
db.dropDatabase(this.next);
},
function getAuthors(err, results) {
console.log("===================================================================================");
console.log(">> Adding Authors");
this.db.collection('authors', this.next);
},
function createIndex(err, collection) {
this.collection = collection;
collection.createIndex(
[
"meta",
['_id', 1],
['name', 1],
['age', 1]
],
this.next
);
},
function insertAuthors(err, indexName) {
console.log("===================================================================================");
this.authors = {};
this.collection.insert(
[{
"name": "William Shakespear",
"email": "william@shakespeare.com",
"age": 587
}, {
"name": "Jorge Luis Borges",
"email": "jorge@borges.com",
"age": 123
}],
this.next
);
},
function saveAuthors(err, docs) {
docs.forEach(function (doc) {
console.dir(doc);
this.author[doc.name]
}, this);
this.next();
},
function findAuthors() {
console.log("===================================================================================");
console.log(">> Authors ordered by age ascending");
console.log("===================================================================================");
this.collection.find({}, {
'sort': [['age', 1]]
}, this.next);
},
function (err, cursor) {
cursor.each(function (err, author) {
if (author != null) {
console.log("[" + author.name + "]:[" + author.email + "]:[" + author.age + "]");
} else {
console.log("===================================================================================");
console.log(">> Adding users");
console.log("===================================================================================");
authorStack.handle(Object.create(this));
}
});
}
);
var authorStack = Stak.make(
function openUserCollection() {
this.db.collection("users", this.next);
},
function insertUsers(err, userCollection) {
this.users = {};
this.userCollection = userCollection;
userCollection.insert(
[{
"login": "jdoe",
"name": "John Doe",
"email": "john@doe.com"
}, {
"login": "lsmith",
"name": "Lucy Smith",
"email": "lucy@smith.com"
}],
this.next
);
},
function storeUsers(err, docs) {
docs.forEach(function (doc) {
console.dir(doc);
this.users[doc.login] = doc;
}, this);
this.next();
},
function findUsers() {
console.log("===================================================================================");
console.log(">> Users ordered by login ascending");
console.log("===================================================================================");
this.userCollection.find({}, {
"sort": [['login', 1]]
}, this.next);
},
function (err, cursor) {
cursor.each(function (err, user) {
if (user != null) {
console.log("[" + user.login + "]:[" + user.name + "]:[" + user.email + "]");
} else {
console.log("===================================================================================");
console.log(">> Adding articles");
console.log("===================================================================================");
articleStack.handle(Object.create(this));
}
});
}
);
var articleStack = Stak.make(
function () {
this.db.collection("article", this.next)
},
function (err, articlesCollection) {
this.articlesCollection = articlesCollection;
articlesCollection.insert([
{
'title':'Caminando por Buenos Aires',
'body':'Las callecitas de Buenos Aires tienen ese no se que...',
'author_id': this.authors['Jorge Luis Borges']._id
},
{
'title':'I must have seen thy face before',
'body':'Thine eyes call me in a new way',
'author_id': this.authors['William Shakespeare']._id,
'comments':[{'user_id': this.users['jdoe']._id, 'body':"great article!"}]
}
], this.next);
},
function (err, docs) {
docs.forEach(function (doc) {
console.dir(doc);
});
this.next();
},
function () {
console.log("===================================================================================");
console.log(">> Articles ordered by title ascending");
console.log("===================================================================================");
this.articlesCollection.find({}, {
"sort": [["title", 1]]
}, this.next);
},
function (err, cursor) {
var next = after(cursor.length, this.next);
cursor.each(function (err, article) {
if (article != null) {
console.log("[" + article.title + "]:[" + article.body + "]:[" + article.author_id.toHexString() + "]");
console.log(">> Closing connection");
next();
}
});
},
function () {
this.db.close();
}
);
console.log("Connecting to " + host + ":" + port);
var db = new Db('node-mongo-blog', new Server(host, port, {}), {native_parser:true});
dbStack.handle({ db: db });