Skip to content

Commit 6dea60d

Browse files
authored
Merge pull request #1 from flamebase/develop
Develop
2 parents 57e3aee + bff9f30 commit 6dea60d

2 files changed

Lines changed: 148 additions & 165 deletions

File tree

README.md

Lines changed: 113 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,131 @@
1-
# flamebase-database-node
1+
# :fire: flamebase-database-node
22

3-
Real time JSON database server-side.
3+
Real time JSON database. Database
44

55
### Usage
66

7+
- Import library:
8+
79
```javascript
810
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 = {};
973

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
1576
*/
16-
var FD = new FlamebaseDatabase(database, path);
17-
var queue = FD.getQueue();
77+
config.APIKey = function() {
78+
return "GsdfgSVDfvsdAwejhFDGhbdASD"; // server key - FCM
79+
};
1880

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
2184
*/
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+
};
2399

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+
};
25106

26107
/**
27-
* devices to keep up to date
108+
* custom id for client database (used as primary key)
28109
*/
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+
};
44113

45114
/**
46-
* config db synchronization
115+
* custom notification to send when database reference changes.
116+
* return null if not needed
47117
*/
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+
}
111124
};
112-
```
125+
126+
FD.setSyncConfig(config);
127+
```
128+
- Enable debug logs:
129+
```javascript
130+
FD.debug(true);
131+
```

index.js

Lines changed: 35 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
var SN = require('sync-node');
32
var JsonDB = require('node-json-db');
43
var FCM = require('fcm-push');
54
var diff = require('rus-diff').diff;
65
var log4js = require('log4js');
6+
var SN = require('sync-node');
77

88
const TAG = "Flamebase Database";
99
var logger = log4js.getLogger(TAG);
@@ -30,10 +30,9 @@ function FlamebaseDatabase(database, path) {
3030
this.lengthLimit = {};
3131
this.lengthLimit.ANDROID = (4096 - lengthMargin);
3232
this.lengthLimit.IOS = (2048 - lengthMargin);
33-
34-
// sync queue
3533
this.queue = SN.createQueue();
3634

35+
3736
// database
3837
this.db = new JsonDB(database, true, true);
3938

@@ -45,54 +44,28 @@ function FlamebaseDatabase(database, path) {
4544
this.pushConfig = null;
4645
this.fcm = null;
4746

48-
//db sync status
49-
this.synchronizingToDatabase = false; // sync data towards db
50-
this.synchronizingFromDatabase = false; // sync data from db
51-
5247
/**
5348
* sync from database
5449
*/
5550
this.syncFromDatabase = function() {
56-
this.queue.pushJob(function(){
57-
return new Promise(function (resolve, reject) {
58-
object.synchronizingFromDatabase = true;
59-
try {
60-
console.log("####################### data path: " + path);
61-
object.ref = object.db.getData(path);
62-
object.synchronizingFromDatabase = false;
63-
object.syncNotifications();
64-
resolve()
65-
} catch(e) {
66-
console.log("####################### data error: " + e);
67-
object.db.delete(path);
68-
object.ref = {};
69-
object.synchronizingFromDatabase = false;
70-
resolve();
71-
}
72-
})
73-
});
51+
try {
52+
console.log("####################### data path: " + path);
53+
object.ref = object.db.getData(path);
54+
object.syncNotifications();
55+
} catch(e) {
56+
console.log("####################### data error: " + e);
57+
console.log("####################### deleting: " + path);
58+
object.db.delete(path);
59+
object.ref = {};
60+
}
7461
};
7562

7663
/**
7764
* sync to database
7865
*/
7966
this.syncToDatabase = function() {
80-
this.queue.pushJob(function(){
81-
return new Promise(function (resolve, reject) {
82-
object.synchronizingToDatabase = true;
83-
try {
84-
object.db.push(path, object.ref);
85-
object.synchronizingToDatabase = false;
86-
object.syncNotifications();
87-
resolve()
88-
} catch(e) {
89-
console.log("####################### data error : " + e);
90-
object.synchronizingToDatabase = false;
91-
resolve();
92-
}
93-
// if (debug) console.info(TAG + " to: " + JSON.stringify(object.ref))
94-
})
95-
});
67+
object.db.push(path, object.ref);
68+
object.syncNotifications();
9669
};
9770

9871
/**
@@ -107,6 +80,7 @@ function FlamebaseDatabase(database, path) {
10780
this.fcm = (this.pushConfig.APIKey() === null || this.pushConfig.APIKey() === 0 ? null : new FCM(this.pushConfig.APIKey()));
10881
}
10982

83+
this.lastStringReference = JSON.stringify({});
11084
};
11185

11286
/**
@@ -229,30 +203,28 @@ function FlamebaseDatabase(database, path) {
229203
};
230204

231205
this.sendPushMessage = function(send) {
232-
this.queue.pushJob(function(){
233-
return new Promise(function (resolve, reject) {
234-
var message = {
235-
registration_ids: send.tokens, // required fill with device token or topics
236-
data: send.data,
237-
notification: send.notification
238-
};
239-
240-
if (object.debugVal) {
241-
logger.debug("Sending to: " + JSON.stringifyAligned(message.registration_ids));
242-
}
206+
this.queue.pushJob(function() {
207+
var message = {
208+
registration_ids: send.tokens, // required fill with device token or topics
209+
data: send.data,
210+
notification: send.notification
211+
};
212+
213+
if (object.debugVal) {
214+
logger.debug("Sending to: " + JSON.stringifyAligned(message.registration_ids));
215+
}
243216

244-
object.fcm.send(message)
245-
.then(function(response){
246-
if (object.debugVal) {
247-
logger.debug("Successfully sent with response: " + JSON.stringifyAligned(JSON.parse(response)));
248-
}
249-
resolve();
250-
})
251-
.catch(function(err){
252-
logger.error("error: " + JSON.stringifyAligned(err));
217+
object.fcm.send(message)
218+
.then(function(response){
219+
if (object.debugVal) {
220+
logger.debug("Successfully sent with response: " + JSON.stringifyAligned(JSON.parse(response)));
253221
resolve();
254-
})
255-
})
222+
}
223+
})
224+
.catch(function(err){
225+
logger.error("error: " + JSON.stringifyAligned(err));
226+
resolve();
227+
})
256228
});
257229
};
258230

@@ -282,14 +254,6 @@ function FlamebaseDatabase(database, path) {
282254
return result;
283255
};
284256

285-
this.getQueue = function() {
286-
return this.queue
287-
};
288-
289-
this.isSynchronizing = function() {
290-
return this.synchronizingToDatabase || this.synchronizingFromDatabase
291-
};
292-
293257
this.exist = function() {
294258
return !(this.ref === null || this.ref === undefined)
295259
};

0 commit comments

Comments
 (0)