-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportExportDatabase.js
118 lines (111 loc) · 3.39 KB
/
importExportDatabase.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
//packages
//require('dexie-export-import');
//const toBlob = require('stream-to-blob');
const fileSystem = require('fs');
/**
* usa a exportação com objeto vindo do front
*/
var export_database_front = async function export_database_front(datab) {
console.log("Exporting Front Database");
datab.transaction('r', datab.tables, () => {
const p = Promise.all(
datab.tables.map(function (table) {
return table.toArray().then(function (rows) {
return {'table': table.name, 'rows': rows};
});
}));
p.then(function (r) {
try {
fileSystem.writeFile("ExportedDatabase.json", JSON.stringify(r), function (error) {
if (error) {
console.log(error);
} else {
alert("Dados exportados com sucesso. Verifique o arquivo ExportedDatabase.json");
}
});
} catch (error) {
console.error('' + error);
}
console.log("Exported");
});
});
};
var import_database_front = async function import_database_front(datab) {
console.log("Importing Database");
/*const stream = fileSystem.createReadStream("ExportedDatabase.json");
console.log(stream);
stream.on('data', function (d) {
console.log(d);
});*/
fileSystem.readFile('ExportedDatabase.json', (err, data) => {
if (err)
throw err;
let baseDados = JSON.parse(data);
console.log(baseDados);
datab.transaction('rw', datab.tables, () => {
const p = Promise.all(baseDados.map(t =>
datab.table(t.table).clear()
.then(() => datab.table(t.table).bulkAdd(t.rows))));
p.then(function () {
alert("Dados importados com sucesso. Provenientes do arquivo ExportedDatabase.json");
});
});
});
console.log("Imported");
};
/**
* read database file then convert it in a blob, lastly import blob into database
*/
var import_database = async function import_database() {
console.log("Importing Database");
/*const stream = fileSystem.createReadStream("ExportedDatabase.json");
console.log(stream);
stream.on('data', function (d) {
console.log(d);
});*/
fileSystem.readFile('ExportedDatabase.json', (err, data) => {
if (err)
throw err;
let baseDados = JSON.parse(data);
console.log(baseDados);
});
console.log("Imported");
};
async function try_persist_without_promting_user() {
if (!navigator.storage || !navigator.storage.persisted) {
return "never";
}
let persisted = await navigator.storage.persisted();
if (persisted) {
return "persisted";
}
if (!navigator.permissions || !navigator.permissions.query) {
return "prompt";
}
const permission = await navigator.permissions.query({name: "persistent-storage"});
if (permission.status === "granted") {
persisted = await navigator.storage.persist();
if (persisted) {
return "persisted";
} else {
throw new Error("Failed to persist");
}
}
if (permission.status === "prompt") {
return "prompt"; // It MAY be successful to prompt. Don't know.
}
return "never";
}
var init_storage_persistence = async function init_storage_persistence() {
console.log("persisting data");
const persist = await try_persist_without_promting_user();
switch (persist) {
case "never":
return "Not possible to persist storage";
case "persisted":
return "Successfully persisted storage silently";
case "prompt":
return "Not persisted, but we may prompt user when we want to.";
}
};
module.exports = {export_database_front, import_database_front, import_database, init_storage_persistence};