Skip to content

Commit baf552b

Browse files
committed
version
1 parent eae7e77 commit baf552b

File tree

3 files changed

+139
-14
lines changed

3 files changed

+139
-14
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
[ ![flamebase/flamebase-database-node](https://d25lcipzij17d.cloudfront.net/badge.svg?id=js&type=6&v=1.1.0&x2=0)](https://www.npmjs.com/package/flamebase-database-node)
1+
[ ![flamebase/flamebase-database-node](https://d25lcipzij17d.cloudfront.net/badge.svg?id=js&type=6&v=1.2.0&x2=0)](https://www.npmjs.com/package/flamebase-database-node)
22
# :fire: flamebase-database-node
33

44
Real time JSON database (server node).
55

6+
### What is this?
7+
Flamebase is an open source project that tries to emulate Firebase Database features as much as possible. I like Firebase but it's expensive for what it currently offers.
8+
If you are doing an altruist project with Firebase, pray not to became successful, because the monthly amount will increase considerably.
9+
10+
In this repo you can find the proper package for work with json db as flamebase-database-server-cluster does.
11+
For now it still developing, so please be patient with errors.
12+
613
### Usage
714

815
- Import library:
@@ -68,6 +75,7 @@ FD.ref.messages[messageId] = message;
6875
FD.syncToDatabase();
6976
```
7077
At this point we have a JSON reference synchronized with our JSON database.
78+
7179
Define some configuration properties to keep devices up to date when JSON reference changes.
7280
```javascript
7381
var config = {};

index.js

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

2-
var JsonDB = require('node-json-db');
3-
var FCM = require('fcm-push');
4-
var diff = require('rus-diff').diff;
5-
var log4js = require('log4js');
6-
var SN = require('sync-node');
2+
var JsonDB = require('node-json-db');
3+
var FCM = require('fcm-push');
4+
var diff = require('rus-diff').diff;
5+
var log4js = require('log4js');
6+
var SN = require('sync-node');
7+
var sha1 = require('sha1');
8+
79

810
const TAG = "Flamebase Database";
911
var logger = log4js.getLogger(TAG);
@@ -12,6 +14,7 @@ JSON.stringifyAligned = require('json-align');
1214

1315
var ACTION_SIMPLE_UPDATE = "simple_update";
1416
var ACTION_SLICE_UPDATE = "slice_update";
17+
var ACTION_NO_UPDATE = "no_update";
1518

1619
function FlamebaseDatabase(database, path) {
1720

@@ -144,8 +147,8 @@ function FlamebaseDatabase(database, path) {
144147
}
145148
}
146149

147-
var data_android = this.getPartsFor(this.OS.ANDROID);
148-
var data_ios = this.getPartsFor(this.OS.IOS);
150+
var data_android = this.getPartsFor(this.OS.ANDROID, JSON.parse(this.lastStringReference), this.ref);
151+
var data_ios = this.getPartsFor(this.OS.IOS, JSON.parse(this.lastStringReference), this.ref);
149152

150153
if (object.debugVal) {
151154
logger.debug("android_tokens_size: " + android_tokens.length);
@@ -225,6 +228,117 @@ function FlamebaseDatabase(database, path) {
225228
}
226229
};
227230

231+
this.sendDifferencesForClient = function(before, device) {
232+
233+
var ios_tokens = [];
234+
var android_tokens = [];
235+
236+
var id = this.pushConfig.referenceId();
237+
var notification = this.pushConfig.notification();
238+
239+
if (device.os.indexOf(this.OS.IOS) !== -1) {
240+
ios_tokens.push(device.token);
241+
} else {
242+
android_tokens.push(device.token);
243+
}
244+
245+
var data_android = this.getPartsFor(this.OS.ANDROID, JSON.parse(before), this.ref);
246+
var data_ios = this.getPartsFor(this.OS.IOS, JSON.parse(before), this.ref);
247+
248+
if (object.debugVal) {
249+
logger.debug("android_tokens_size: " + android_tokens.length);
250+
logger.debug("ios_tokens_size: " + ios_tokens.length);
251+
logger.debug("data_android_size: " + data_android.parts.length);
252+
logger.debug("data_ios_size: " + data_ios.parts.length);
253+
}
254+
255+
this.lastStringReference = JSON.stringify(this.ref);
256+
257+
if (android_tokens.length > 0) {
258+
if (data_android.parts.length === 1) {
259+
var data = {};
260+
data.id = id;
261+
data.tag = this.pushConfig.tag();
262+
data.reference = data_android.parts[0];
263+
data.action = ACTION_SIMPLE_UPDATE;
264+
data.size = data_android.parts.length;
265+
data.index = 0;
266+
var send = {};
267+
send.data = data;
268+
send.tokens = android_tokens;
269+
send.notification = notification;
270+
this.sendPushMessage(send);
271+
} else if (data_android.parts.length > 1) {
272+
for (var i = 0; i < data_android.parts.length; i++) {
273+
var dat = {};
274+
dat.id = id;
275+
dat.tag = this.pushConfig.tag();
276+
dat.reference = data_android.parts[i];
277+
dat.action = ACTION_SLICE_UPDATE;
278+
dat.index = i;
279+
dat.size = data_android.parts.length;
280+
var sen = {};
281+
sen.data = dat;
282+
sen.tokens = android_tokens;
283+
sen.notification = notification;
284+
this.sendPushMessage(sen);
285+
}
286+
} else {
287+
var data = {};
288+
data.id = id;
289+
data.tag = this.pushConfig.tag();
290+
data.action = ACTION_NO_UPDATE;
291+
var send = {};
292+
send.data = data;
293+
send.tokens = android_tokens;
294+
send.notification = notification;
295+
this.sendPushMessage(send);
296+
}
297+
}
298+
299+
if (ios_tokens.length > 0) {
300+
if (data_ios.parts.length === 1) {
301+
var da = {};
302+
da.id = id;
303+
da.tag = this.pushConfig.tag();
304+
da.reference = data_ios.parts[0];
305+
da.action = ACTION_SIMPLE_UPDATE;
306+
da.size = data_ios.parts.length;
307+
da.index = 0;
308+
var se = {};
309+
se.data = da;
310+
se.tokens = ios_tokens;
311+
se.notification = notification;
312+
this.sendPushMessage(se);
313+
} else if (data_ios.parts.length > 1) {
314+
for (var i = 0; i < data_ios.parts.length; i++) {
315+
var d = {};
316+
d.id = id;
317+
d.tag = this.pushConfig.tag();
318+
d.reference = data_ios.parts[i];
319+
d.action = ACTION_SLICE_UPDATE;
320+
d.index = i;
321+
d.size = data_ios.parts.length;
322+
var s = {};
323+
s.data = d;
324+
s.tokens = ios_tokens;
325+
s.notification = notification;
326+
this.sendPushMessage(s);
327+
}
328+
} else {
329+
var data = {};
330+
data.id = id;
331+
data.tag = this.pushConfig.tag();
332+
data.action = ACTION_NO_UPDATE;
333+
var send = {};
334+
send.data = data;
335+
send.tokens = ios_tokens;
336+
send.notification = notification;
337+
this.sendPushMessage(send);
338+
}
339+
}
340+
};
341+
228342
this.sendPushMessage = function(send) {
229343
this.queue.pushJob(function() {
230344
return new Promise(function (resolve, reject) {
@@ -255,21 +369,23 @@ function FlamebaseDatabase(database, path) {
255369
});
256370
};
257371

258-
this.getPartsFor = function(os) {
372+
this.getPartsFor = function(os, before, after) {
259373
var notification = this.pushConfig.notification();
260374
var notificationLength = JSON.stringify(notification).length;
261375

262-
var differences = JSON.stringify(diff(JSON.parse(this.lastStringReference), this.ref));
376+
//var differences = JSON.stringify(diff(JSON.parse(this.lastStringReference), this.ref));
377+
var differences = JSON.stringify(diff(before, after));
263378
var partsToSend = [];
264379

265380
if (this.debugVal) {
266381
logger.debug("diff: " + differences);
267382
}
268383

269384
if (differences === "false") {
270-
var currentStringRef = JSON.stringify(this.ref);
271-
if (this.lastStringReference.length != currentStringRef.length) {
272-
logger.error("something went wrong; chars diff: " + (this.lastStringReference.length - currentStringRef.length));
385+
var currentStringAfter = JSON.stringify(after);
386+
var currentStringBefore = JSON.stringify(before);
387+
if (currentStringBefore.length !== currentStringAfter.length) {
388+
logger.error("something went wrong; sha1 diff: " + currentStringBefore.length + " - " + currentStringAfter.length);
273389
}
274390
if (this.debugVal) {
275391
logger.debug("no differences");

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"log4js": "^1.1.1",
1212
"node-json-db": "^0.7.3",
1313
"rus-diff": "^1.1.0",
14+
"sha1": "^1.1.1",
1415
"sync-node": ">= 1.3.1"
1516
},
1617
"description": "Realtime JSON DB",
@@ -47,5 +48,5 @@
4748
"scripts": {
4849
"test": "echo \"Error: no test specified\" && exit 1"
4950
},
50-
"version": "1.1.1"
51+
"version": "1.2.0"
5152
}

0 commit comments

Comments
 (0)