-
-
Notifications
You must be signed in to change notification settings - Fork 198
/
DbFunctions.js
142 lines (133 loc) · 3.83 KB
/
DbFunctions.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
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
133
134
135
136
137
138
139
140
141
142
import PouchDB from "pouchdb-react-native";
import SQLite from "react-native-sqlite-2";
import SQLiteAdapterFactory from "pouchdb-adapter-react-native-sqlite";
import { getRoot } from "mobx-state-tree";
import FrappeFetch from "react-native-frappe-fetch";
import { Toast } from "native-base";
var validUrl = require("valid-url");
import BackgroundJob from "react-native-background-job";
export function openAndSyncDB(dbName, withSync = false) {
const SQLiteAdapter = SQLiteAdapterFactory(SQLite);
PouchDB.plugin(SQLiteAdapter);
const db = new PouchDB(dbName + ".db", { adapter: "react-native-sqlite" });
PouchDB.plugin(require("pouchdb-find"));
PouchDB.plugin(require("pouchdb-upsert"));
return db;
}
export function syncDB(db, dbName, session) {
// Server URL
const url = `https://${session.db_name}:${session.token}@db.tailpos.com/${
session.db_name
}-${dbName}`;
const opts = { live: true, retry: true };
// Sync from
return db.replicate.from(url).on("complete", function(info) {
db.sync(url, opts);
});
}
export function sync(
jsonObject,
type,
trashObj,
credentials,
jobStatus,
store,
) {
if (credentials.url) {
if (validUrl.isWebUri(credentials.url.toLowerCase())) {
return FrappeFetch.createClient({
url: credentials.url.toLowerCase(),
username: credentials.user_name,
password: credentials.password,
})
.then(() => {
const { Client } = FrappeFetch;
return Client.postApi("tailpos_sync.sync_pos.sync_data", {
tailposData: JSON.parse(jsonObject),
trashObject: JSON.parse(trashObj),
deviceId: credentials.deviceId,
typeOfSync: type,
});
})
.catch(() => {
store.stateStore.setIsNotSyncing();
BackgroundJob.cancel({ jobKey: "AutomaticSync" });
if (!jobStatus) {
Toast.show({
text: "Unable to sync",
type: "danger",
duration: 5000,
});
}
})
.then(response => response.json())
.then(responseJson => {
return responseJson.message.data;
});
} else {
store.stateStore.setIsNotSyncing();
Toast.show({
text: "Invalid URL",
type: "danger",
duration: 5000,
});
BackgroundJob.cancel({ jobKey: "AutomaticSync" });
}
} else {
BackgroundJob.cancel({ jobKey: "AutomaticSync" });
}
}
export function saveSnapshotToDB(db, snapshot) {
let updateObj = false;
db.upsert(snapshot._id, function(doc) {
if (!doc._id) {
doc = snapshot;
updateObj = true;
} else {
Object.keys(snapshot).forEach(function(key) {
if (!(key === "_rev")) {
if (doc[key] !== snapshot[key]) {
doc[key] = snapshot[key];
updateObj = true;
}
}
});
}
if (updateObj) {
return doc;
} else {
return updateObj;
}
});
}
export function editFields(obj, data) {
Object.keys(data).forEach(function(key) {
if (!(key === "_id")) {
obj[key] = data[key];
}
});
}
export function deleteObject(obj, db) {
db.get(obj._id).then(doc => {
db.remove(doc);
});
getRoot(obj).delete(obj);
}
export function getRows(obj, db, numberRows, rowsOptions) {
return new Promise((resolve, reject) => {
rowsOptions.limit = numberRows;
rowsOptions.include_docs = true;
db.allDocs(rowsOptions).then(entries => {
if (entries && entries.rows.length > 0) {
rowsOptions.startkey = entries.rows[entries.rows.length - 1].id;
rowsOptions.skip = 1;
for (var i = 0; i < entries.rows.length; i++) {
if (entries.rows[i].doc.name || entries.rows[i].doc.role) {
obj.add(JSON.parse(JSON.stringify(entries.rows[i].doc)));
}
}
}
});
resolve(obj.rows);
});
}