forked from pubkey/rxdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrenderer.js
71 lines (61 loc) · 2.22 KB
/
renderer.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
const { getDatabase } = require('./shared');
const renderTest = require('./test/render.test.js');
const electron = require('electron');
const { getRxStorageMemory } = require('rxdb/plugins/storage-memory');
const { getRxStorageIpcRenderer } = require('../../plugins/electron');
const heroesList = document.querySelector('#heroes-list');
async function run() {
/**
* to check if rxdb works correctly, we run some integration-tests here
* if you want to use this electron-example as boilerplate, remove this line
*/
await renderTest();
const dbSuffix = await window.getDBSuffix();
const storage = getRxStorageIpcRenderer({
key: 'main-storage',
statics: getRxStorageMemory().statics,
ipcRenderer: electron.ipcRenderer
});
console.log('GET DATABASE');
const db = await getDatabase(
'heroesdb' + dbSuffix, // we add a random timestamp in dev-mode to reset the database on each start
storage
);
console.log('GET DATABASE DONE');
/**
* map the result of the find-query to the heroes-list in the dom
*/
db.heroes.find()
.sort({
name: 'asc'
})
.$.subscribe(function (heroes) {
if (!heroes) {
heroesList.innerHTML = 'Loading..';
return;
}
console.log('observable fired');
console.dir(heroes);
heroesList.innerHTML = heroes
.map(hero => {
return '<li>' +
'<div class="color-box" style="background:' + hero.color + '"></div>' +
'<div class="name" name="' + hero.name + '">' + hero.name + '</div>' +
'</li>';
})
.reduce((pre, cur) => pre += cur, '');
});
window.addHero = async function () {
const name = document.querySelector('input[name="name"]').value;
const color = document.querySelector('input[name="color"]').value;
const obj = {
name: name,
color: color
};
console.log('inserting hero:');
console.dir(obj);
await db.heroes.insert(obj);
console.log('inserting hero DONE');
};
}
run();