-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing-live-query-and-indexing
132 lines (104 loc) · 5.63 KB
/
testing-live-query-and-indexing
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
const { AceBaseServer } = require('acebase-server');
const { AceBaseClient } = require('acebase-client');
const dbname = 'acebase_testing';
var db = null;
const settings = {
host: 'localhost',
port: 8090,
logLevel: 'error',
authentication: {
enabled: true,
allowUserSignup: false,
defaultAccessRule: 'auth',
defaultAdminPassword: 'Q$ThSjQv6$ZZx)pC'
}
};
const server = new AceBaseServer(dbname, settings);
server.on("ready", () => {
console.log("SERVER ready");
db = new AceBaseClient({ host: settings.host, port: settings.port, dbname: dbname, https: false, logLevel: 'error', debug: true });
db.ready(() => {
db.auth.signIn('admin', 'Q$ThSjQv6$ZZx)pC')
.then(async result => {
console.log(`Signed in as ${result.user.username}, got access token ${result.accessToken}`);
//TEST 1
await db.indexes.create('items', 'location');
await db.indexes.create('items', 'category');
await db.indexes.create('items', 'status');
TEST 2
await db.indexes.create('items', 'location');
//await db.indexes.create('items', 'category');
//await db.indexes.create('items', 'status');
const eventStats = { add: 0, change: 0, remove: 0 };
// Setup realtime query
const realtimeQuery = db.query('items')
.filter('category', '==', 'main')
.filter('status', '==', 1)
.on('add', event => {
eventStats.add++;
const item = event.snapshot.val();
console.log(`Added item ${event.ref.key}:`, item);
})
.on('change', event => {
eventStats.change++;
const item = event.snapshot.val();
console.log(`Changed item ${event.ref.key}:`, item);
})
.on('remove', event => {
eventStats.remove++;
console.log(`Removed item ${event.ref.key}`);
});
// Get initial (no) results
const results = await realtimeQuery.get();
// Add a bunch of items, should trigger "add" events on realtime query
const TEST_SIZE = 1000;
const itemIds = [];
const locations = ['Amsterdam', 'Cape Town', 'Sydney', 'Miami', 'Toronto', 'Berlin', 'Paris'];
for (let i = 0; i < TEST_SIZE; i++) {
const ref = await db.ref('items').push({
location: locations[Math.floor(Math.random() * locations.length)],
category: 'main',
status: 1,
});
itemIds.push(ref.key);
}
// Update every other item to status 2, should trigger 500 "remove" events on realtime query
for (let i = 0; i < itemIds.length; i += 2) {
await db.ref('items').child(itemIds[i]).update({
status: 2,
});
}
// Update every 3rd item to location 'Amsterdam', should trigger "change" events on realtime query
for (let i = 0; i < itemIds.length; i += 3) {
await db.ref('items').child(itemIds[i]).update({
location: 'Amsterdam',
});
}
// Update every 3rd item to status 3, should trigger some more "remove" events on realtime query
for (let i = 0; i < itemIds.length; i += 3) {
await db.ref('items').child(itemIds[i]).update({
status: 3,
});
}
// Remove items with status 1, should trigger all remaining "remove" events on realtime query (0 results left)
await db.query('items').filter('status', '==', 1).remove();
// Wait for all remove events to fire
console.log('Waiting for all remove events to fire');
await new Promise(resolve => {
let eventsFired = eventStats.remove;
const check = () => {
if (eventsFired === eventStats.remove) {
// All fired
return resolve();
}
eventsFired = eventStats.remove;
setTimeout(check, 100); // Schedule next check
};
setTimeout(check, 1000); // Schedule first check
});
// Stop live query
await realtimeQuery.stop();
console.log(`${eventStats.add} added, ${eventStats.change} changed, ${eventStats.remove} removed`);
})
});
});