|
1 | 1 | # :fire: flamebase-database-node |
2 | 2 |
|
3 | | -Real time JSON database server-side. |
| 3 | +Real time JSON database. Database |
4 | 4 |
|
5 | 5 | ### Usage |
6 | 6 |
|
| 7 | +- Import library: |
| 8 | + |
7 | 9 | ```javascript |
8 | 10 | var FlamebaseDatabase = require("flamebase-database-node"); |
| 11 | +``` |
| 12 | +- Define database and reference to instance as JSON object: |
| 13 | +```javascript |
| 14 | +var database = "chats"; // database's name |
| 15 | +var path = "groupA"; // path to JSON reference |
| 16 | +var FD = new FlamebaseDatabase(database, path); |
| 17 | +FD.syncFromDatabase(); // first load |
| 18 | +``` |
| 19 | +Now `FD.ref` is `groupA` JSON object. |
| 20 | + |
| 21 | +- Do some work and sync: |
| 22 | +This JSON reference (`groupA`) contains this information: |
| 23 | +```json |
| 24 | +{ |
| 25 | + "people": { |
| 26 | + "john_travolta@ddd.com": { |
| 27 | + "name": "John Travolta", |
| 28 | + "token": "AsDadfsdfsDFCGsdfgEgWEcgcEgcwEgWegwEgeGWHTrydhdfsDFCGsdfgEgWEcgcEgcwEgWegwEgrty", |
| 29 | + "os": "android" |
| 30 | + }, |
| 31 | + "donal_duck@aaa.com": { |
| 32 | + "name": "Donal Duck", |
| 33 | + "token": "DSfgsfgSgrtuyjYuIKyUyjRtyFytrydhdfsDFCGsdfgEgWEcgcEfgSgrtuyjYuIgcwEgWegwEgrty", |
| 34 | + "os": "android" |
| 35 | + } |
| 36 | + }, |
| 37 | + "messages": { |
| 38 | + "1495171941114": { |
| 39 | + "author": "john_travolta@ddd.com", |
| 40 | + "text": "Message 1" |
| 41 | + }, |
| 42 | + "1495171941127": { |
| 43 | + "author": "donal_duck@aaa.com", |
| 44 | + "text": "Message 2" |
| 45 | + }, |
| 46 | + "1495171941159": { |
| 47 | + "author": "john_travolta@ddd.com", |
| 48 | + "text": "Message 3" |
| 49 | + }, |
| 50 | + "1495171941327": { |
| 51 | + "author": "donal_duck@aaa.com", |
| 52 | + "text": "Message 4" |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | +For insert new messages on this conversation compose a new message JSON object: |
| 58 | +```javascript |
| 59 | +var message = {}; |
| 60 | +message.author = "john_travolta@ddd.com"; |
| 61 | +message.text = "Message 5"; |
| 62 | + |
| 63 | +var messageId = new Date().getTime().toString(); |
| 64 | + |
| 65 | +FD.ref.messages[messageId] = message; |
| 66 | + |
| 67 | +FD.syncToDatabase(); |
| 68 | +``` |
| 69 | +At this point we have a JSON reference synchronized with our JSON database. |
| 70 | +Define some configuration properties to keep devices up to date when JSON reference changes. |
| 71 | +```javascript |
| 72 | +var config = {}; |
9 | 73 |
|
10 | | -var database = "myDatabase"; // name of db |
11 | | -var path = "cars/"; // path to JSON reference |
12 | | - |
13 | | -/** |
14 | | -* db reference |
| 74 | +/** |
| 75 | +* server API key for firebase cloud messaging |
15 | 76 | */ |
16 | | -var FD = new FlamebaseDatabase(database, path); |
17 | | -var queue = FD.getQueue(); |
| 77 | +config.APIKey = function() { |
| 78 | + return "GsdfgSVDfvsdAwejhFDGhbdASD"; // server key - FCM |
| 79 | +}; |
18 | 80 |
|
19 | | -/** |
20 | | -* load JSON reference on FD.ref |
| 81 | +/** |
| 82 | +* all device objects must have token and os info in order |
| 83 | +* to slice big JSON changes for android or ios push notifications |
21 | 84 | */ |
22 | | -FD.syncFromDatabase(); |
| 85 | +config.devices = function() { |
| 86 | + var devices = []; |
| 87 | + var keys = Object.keys(FD.ref.people); |
| 88 | + for (var i = 0; i < people.length; i++) { |
| 89 | + var person = FD.ref.people[keys[i]]; |
| 90 | + |
| 91 | + var device = {}; |
| 92 | + device.token = person.token; |
| 93 | + device.os = person.os; |
| 94 | + |
| 95 | + devices.push(device); |
| 96 | + } |
| 97 | + return devices; |
| 98 | +}; |
23 | 99 |
|
24 | | -var object = this; |
| 100 | +/** |
| 101 | +* tag that informs android/ios client which action is being called |
| 102 | +*/ |
| 103 | +config.tag = function() { |
| 104 | + return path + "_sync"; // groupA_sync |
| 105 | +}; |
25 | 106 |
|
26 | 107 | /** |
27 | | -* devices to keep up to date |
| 108 | +* custom id for client database (used as primary key) |
28 | 109 | */ |
29 | | -var devices = []; |
30 | | - |
31 | | -var deviceA = {}; |
32 | | -deviceA.token = "TOKEN_DEVICE_A"; |
33 | | -deviceA.os = FC.OS.ANDROID; |
34 | | - |
35 | | -devices.push(deviceA); |
36 | | - |
37 | | - |
38 | | -// ################ ios lib not ready yet |
39 | | -var deviceB = {}; |
40 | | -deviceB.token = "TOKEN_DEVICE_B"; |
41 | | -deviceB.os = FC.OS.IOS; |
42 | | - |
43 | | -devices.push(deviceB); |
| 110 | +config.referenceId = function() { |
| 111 | + return path; // groupA |
| 112 | +}; |
44 | 113 |
|
45 | 114 | /** |
46 | | -* config db synchronization |
| 115 | +* custom notification to send when database reference changes. |
| 116 | +* return null if not needed |
47 | 117 | */ |
48 | | -this.setConfig = function() { |
49 | | - |
50 | | - /** |
51 | | - * config for db synchronization (server - client) |
52 | | - * @type {{}} |
53 | | - */ |
54 | | - var config = {}; |
55 | | - |
56 | | - /** |
57 | | - * server notification key |
58 | | - */ |
59 | | - config.APIKey = function() { |
60 | | - return "YOUR_FCM_PUSH_KEY"; // server key - FCM |
61 | | - }; |
62 | | - |
63 | | - /** |
64 | | - * devices to keep up to date |
65 | | - */ |
66 | | - config.devices = function() { |
67 | | - return devices; |
68 | | - }; |
69 | | - |
70 | | - /** |
71 | | - * custom tag for sync |
72 | | - * - used in android client |
73 | | - */ |
74 | | - config.tag = function() { |
75 | | - return "user_sync"; |
76 | | - }; |
77 | | - |
78 | | - /** |
79 | | - * custom id for database reference |
80 | | - * - used in android client |
81 | | - */ |
82 | | - config.referenceId = function() { |
83 | | - return "CUSTOM_ID"; |
84 | | - }; |
85 | | - |
86 | | - /** |
87 | | - * custom notification info to send when database reference changes. |
88 | | - * set null if not needed |
89 | | - * |
90 | | - * - used in android client |
91 | | - * |
92 | | - * config.notification = null; |
93 | | - */ |
94 | | - config.notification = function() { |
95 | | - return { |
96 | | - type: "custom_type", |
97 | | - name: object.FD.ref.name, |
98 | | - image: object.FD.ref.photoURL |
99 | | - } |
100 | | - }; |
101 | | - |
102 | | - /** |
103 | | - * sync config |
104 | | - */ |
105 | | - FD.setSyncConfig(config); |
106 | | - |
107 | | - /** |
108 | | - * enable debug logs |
109 | | - */ |
110 | | - FD.debug(true); |
| 118 | +config.notification = function() { |
| 119 | + return { |
| 120 | + type: path + "_type", // notification type |
| 121 | + name: "Database changed", // name or text message |
| 122 | + image: "http://..." // image url to show on left notification icon |
| 123 | + } |
111 | 124 | }; |
112 | | -``` |
| 125 | + |
| 126 | +FD.setSyncConfig(config); |
| 127 | +``` |
| 128 | +- Enable debug logs: |
| 129 | +```javascript |
| 130 | +FD.debug(true); |
| 131 | +``` |
0 commit comments