forked from Sigura/inDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·185 lines (154 loc) · 5.84 KB
/
main.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
var customerData = [
{ ssn: '444-44-4444', name: 'Bill', age: 35, email: 'bill@company.com' },
{ ssn: '333-33-3333', name: 'David', age: 65, email: 'david@company.com' },
{ ssn: '222-22-2222', name: 'Artur', age: 29, email: 'artur@company.com' },
{ ssn: '111-11-1111', name: 'Maria', age: 40, email: 'maria@company.com' },
{ ssn: '555-55-5555', name: 'Donna', age: 25, email: 'donna@home.org' }
];
var init = function(event) {
if(this.containts('customers'))
return;
this.createStore('customers', { keyPath: 'ssn' })
.createIndex('name', { unique: false })
.createIndex('age', { unique: false })
.createIndex('email', { unique: true })
.complete(function(event) {
console.info('store customers created', event);
})
.add(customerData)
.error(errorHandler)
.success(function(event) {
console.info('add success', event);
});
};
var $db = new inDB({ name: 'testDatabase', version: 42 })
.error(errorHandler)
// before ready
.init(init)
.versionChange( function(event) {
console.log('version changed, timeStamp:', event.timeStamp, ', new version:', this.version);
})
// WebKit, as of 2012-02-22, does not yet implement this.
.upgradeNeeded(function(event) {
console.log('onupgradeneeded, newVersion:', event.newVersion, ', oldVersion:', event.oldVersion, ', timeStamp:', event.timeStamp, event);
init.call(this, event);
})
.ready(function(event) {
console.info('db.name =', $db.name, '; db.version =', $db.version);
var storeNames = [];
$db.each(function(storeName){
storeNames.push(storeName);
});
console.info('stores: ', storeNames.length > 0 ? storeNames.join(', ') : 'none');
var $store = this.openStore('customers', this.readWrite);
var tdd = { ssn: '000-00-0000', name: 'for tdd', age: 100, email: 'tdd@company.com' };
var listenEvent = ['insert', 'insertd', 'update', 'updated', 'delete', 'deleted'];
listenEvent.forEach(function(label, i){
$store.eventListener.add(label, function(event){
console.log(label, event);
});
});
// get by key
$store
.get('444-44-4444')
.error(errorHandler)
.success(function(event) {
var customer = this.result;
$store.del('555-55-5555')
.success(function(event){
console.log('customer Donna deleted', event);
});
//$db.close();
});
// get one by index
$store
.get('name', 'Artur')
.error(errorHandler)
.success(function(event) {
console.info('found', this.result);
});
// get all. cursor way
$store
.get()
// add filter, in future may be add support that - linq js
.where(function(item){
return item.age > 20 && item.email.substr(-8).toLowerCase() != 'home.org';
})
.error(errorHandler)
// begin read
.start(function(context) {
console.time('get all');
context.result = [];
})
// read ended
.ended(function(context) {
console.timeEnd('get all', context.result);
})
// read next, must be at the end in this case
.success(function(event, context) {
var customer = this.result.value;
console.info('result ', customer.ssn, customer);
context.result.push(customer);
});
// new transaction for update
$store
.cloneTran()
// get all by IDBKeyRange. cursor way
.get(function(query) {
return query
// .lowerBound('name', 'Bill') // all name ≥ 'Bill'
.bound('age', 30, 60, true, true); // all age 30 > x && < 60
// only one predicate by design index db, please use where after get
})
.error(errorHandler)
// begin read
.start(function(context) {
context.result = [];
console.time('get all by IDBKeyRange');
})
// read ended
.ended(function(context) {
console.timeEnd('get all by IDBKeyRange', context.result);
})
// read next, must be at the end in this case
.success(function(event, context) {
var customer = this.result.value;
console.info('getted by IDBKeyRange', customer.ssn, customer, event, this);
if(customer.ssn == '111-11-1111') {
customer.age = 6;
// only for cursor
context.update(customer);
}
context.result.push(customer);
});
// add tdd entity
$store
.cloneTran()
.add(tdd)
.success(function(event){
// build new read only transaction
var $reStore = $db.openStore('customers');
// get chnaged entity 1
$reStore.get(tdd.ssn)
.success(function(event){
console.log('added:', this.result.ssn, this.result);
$store.cloneTran().del(this.result.ssn);
});
// get chnaged entity 2
// build new transaction as old store transaction
$store.cloneTran()
.get('111-11-1111')
.success(function(event){
console.log('updated:', this.result.ssn, this.result);
});
});
// get count
// WebKit, as of 2012-02-22, does not yet implement this.
$store.count()
.success(function(event){
console.info('count:', this.result);
})
.error(errorHandler);
});
function errorHandler(event) { console.error('get error', event); };
setTimeout(function(){$db.remove(); console.log('removed all by timeout');}, 5000);