1 | module.exports = class ScryptaDB {
|
2 | constructor(isBrowser = false){
|
3 | const db = this
|
4 | db.isBrowser = isBrowser
|
5 | db.data = {}
|
6 | db.dir = './db'
|
7 | if(!isBrowser){
|
8 | db.fs = require('fs')
|
9 | }
|
10 | }
|
11 |
|
12 | loadBrowserDB(){
|
13 | const db = this
|
14 | return new Promise(response => {
|
15 | const collections = ["wallet","sxidcache","usxocache","txidcache","utxocache","nodes","checksums","messages","identity"]
|
16 | for(let x in collections){
|
17 | if (localStorage.getItem(collections[x]) !== null) {
|
18 | db.data[collections[x]] = JSON.parse(localStorage.getItem(collections[x]))
|
19 | }else{
|
20 | db.data[collections[x]] = []
|
21 | localStorage.setItem(collections[x], JSON.stringify([]))
|
22 | }
|
23 | }
|
24 | response(true)
|
25 | })
|
26 | }
|
27 |
|
28 | loadNodeDB(){
|
29 | const db = this
|
30 | return new Promise(response => {
|
31 | db.dir = './db';
|
32 | const collections = ["wallet","sxidcache","usxocache","txidcache","utxocache","nodes","checksums","messages","identity"]
|
33 | if (db.fs.existsSync(db.dir) === false){
|
34 | db.fs.mkdirSync(db.dir)
|
35 | }
|
36 | for(let x in collections){
|
37 | if (db.fs.existsSync(db.dir + '/' + collections[x] + '.json')) {
|
38 | try{
|
39 | db.data[collections[x]] = JSON.parse(db.fs.readFileSync(db.dir + '/' + collections[x] + '.json'))
|
40 | }catch(e){
|
41 | console.log('ERROR WHILE LOADING DB')
|
42 | }
|
43 | }else{
|
44 | db.data[collections[x]] = []
|
45 | db.fs.writeFileSync(db.dir + '/' + collections[x] + '.json', '[]')
|
46 | }
|
47 | }
|
48 | response(true)
|
49 | })
|
50 | }
|
51 |
|
52 | put(collection, doc){
|
53 | const db = this
|
54 |
|
55 | return new Promise(async response => {
|
56 | if(db.isBrowser){
|
57 | await db.loadBrowserDB()
|
58 | let found = false
|
59 | for(let x in db.data[collection]){
|
60 | if(JSON.stringify(doc) === JSON.stringify(db.data[collection][x])){
|
61 | found = true
|
62 | }
|
63 | }
|
64 | if(!found){
|
65 | db.data[collection].push(doc)
|
66 | localStorage.setItem(collection, JSON.stringify(db.data[collection]))
|
67 | }
|
68 | response(true)
|
69 | }else{
|
70 | await db.loadNodeDB()
|
71 | let found = false
|
72 | for(let x in db.data[collection]){
|
73 | if(JSON.stringify(doc) === JSON.stringify(db.data[collection][x])){
|
74 | found = true
|
75 | }
|
76 | }
|
77 | if(!found){
|
78 | if(db.data[collection] !== undefined){
|
79 | db.data[collection].push(doc)
|
80 | db.fs.writeFileSync(db.dir + '/' + collection + '.json', JSON.stringify(db.data[collection]))
|
81 | }
|
82 | }
|
83 | response(true)
|
84 | }
|
85 | })
|
86 | }
|
87 |
|
88 | get(collection, selector = '', id = ''){
|
89 | const db = this
|
90 | return new Promise(async response => {
|
91 | if(db.isBrowser){
|
92 | await db.loadBrowserDB()
|
93 | }else{
|
94 | await db.loadNodeDB()
|
95 | }
|
96 | if(selector !== '' && id !== ''){
|
97 | let found = false
|
98 | let doc
|
99 | for(let x in db.data[collection]){
|
100 | if(!found){
|
101 | if(db.data[collection][x][selector] === id){
|
102 | found = true
|
103 | doc = db.data[collection][x]
|
104 | }
|
105 | }
|
106 | }
|
107 |
|
108 | if(found){
|
109 | response(doc)
|
110 | }else{
|
111 | response(false)
|
112 | }
|
113 | }else{
|
114 | response(db.data[collection])
|
115 | }
|
116 | })
|
117 | }
|
118 |
|
119 | update(collection, selector, id, doc){
|
120 | const db = this
|
121 | return new Promise(async response => {
|
122 | if(db.isBrowser){
|
123 | await db.loadBrowserDB()
|
124 | }else{
|
125 | await db.loadNodeDB()
|
126 | }
|
127 |
|
128 | let found = false
|
129 | for(let x in db.data[collection]){
|
130 | if(!found){
|
131 | if(db.data[collection][x][selector] === id){
|
132 | found = true
|
133 | db.data[collection][x] = doc
|
134 | }
|
135 | }
|
136 | }
|
137 |
|
138 | if(found){
|
139 | if(db.isBrowser){
|
140 | localStorage.setItem(collection, JSON.stringify(db.data[collection]))
|
141 | }else{
|
142 | db.fs.writeFileSync(db.dir + '/' + collection + '.json', JSON.stringify(db.data[collection]))
|
143 | }
|
144 | response(doc)
|
145 | }else{
|
146 | response(false)
|
147 | }
|
148 | })
|
149 | }
|
150 |
|
151 | destroy(collection){
|
152 | const db = this
|
153 | db.data[collection] = []
|
154 | return new Promise(async response => {
|
155 | if(db.isBrowser){
|
156 | await db.loadBrowserDB()
|
157 | localStorage.setItem(collection, '[]')
|
158 | }else{
|
159 | await db.loadNodeDB()
|
160 | db.fs.writeFileSync(db.dir + '/' + collection + '.json', '[]')
|
161 | }
|
162 | response(true)
|
163 | })
|
164 | }
|
165 | } |
\ | No newline at end of file |