-
Notifications
You must be signed in to change notification settings - Fork 18
/
es6-generators.js
66 lines (52 loc) · 2.06 KB
/
es6-generators.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
// ES6-generators is a powerful way to approach async workflow
// This example uses [co](https://github.com/visionmedia/co) and treo-promise
// to provide great readability.
// It's a near future, because Chrome enabled ES6-Generators by default and
// ES7 async/await proposal is basically the same approach.
// Right now, we can compile generators with https://github.com/facebook/regenerator
// and write nice code today.
var treo = require('treo');
var promise = require('treo/plugins/treo-promise');
var co = require('co');
// define schema
var schema = treo.schema()
.version(1)
.addStore('books')
.addIndex('byTitle', 'title', { unique: true })
.addIndex('byAuthor', 'author')
.addStore('locals')
.version(2)
.getStore('books')
.addIndex('byYear', 'year')
.version(3)
.addStore('magazines', { key: 'id' })
.addIndex('byPublisher', 'publisher')
.addIndex('byFrequency', 'frequency')
.addIndex('byWords', 'words', { multi: true });
// create db with promises support
var db = treo('library', schema)
.use(promise());
// wrap async operations with generator.
co(function*() {
var books = db.store('books');
var magazines = db.store('magazines');
// load initial data
yield books.batch({
1: { title: 'Quarry Memories', author: 'Fred', isbn: 1, year: 2012 },
2: { title: 'Water Buffaloes', author: 'Fred', isbn: 2, year: 2013 },
3: { title: 'Bedrock Nights', author: 'Barney', isbn: 3, year: 2012 },
});
yield magazines.batch([
{ id: 'id1', title: 'Quarry Memories', publisher: 'Bob' },
{ id: 'id2', title: 'Water Buffaloes', publisher: 'Bob' },
{ id: 'id3', title: 'Bedrocky Nights', publisher: 'Tim' },
{ id: 'id4', title: 'Waving Wings', publisher: 'Ken' },
]);
// run queries
var book = yield books.index('byTitle').get('Bedrock Nights');
console.log('Find book by unique index:', book);
var byAuthor = yield books.index('byAuthor').get('Fred');
console.log('Filter books:', byAuthor);
var magazinesCount = yield magazines.count();
console.log('Count magazines:', magazinesCount);
});