-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (48 loc) · 1.08 KB
/
index.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
const User = require('./user')
const connection = require('./connection')
function exit() {
console.log('closing DB')
connection.destroy()
.then(() => 'closed DB')
.catch(console.error)
}
function addUser () {
return User.forge({
email: 'foo@gmail.com',
name: 'Mr Foo',
meta: {
foo: 'bar'
}
}).save()
.then(console.log, console.error)
}
function fetchEach (collection) {
const Bluebird = require('bluebird')
const list = []
collection.each(item => {
// and convert to JSON right away
list.push(item.fetch().then(b => b.toJSON()))
})
return Bluebird.all(list)
}
function selectUsers () {
return User.fetchAll()
.then(fetchEach)
.then(console.log, console.error)
}
function changeMetaFoo (newValue) {
return User.where('email', 'foo@gmail.com')
.fetch()
.then(user => {
const meta = user.get('meta')
meta.foo = newValue
user.set('meta', meta)
return user.save()
})
}
// addUser().then(exit)
// selectUsers().then(exit)
// changeMetaFoo('new bar').then(exit)
addUser()
.then(selectUsers)
.then(exit)