-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
206 lines (192 loc) · 8.53 KB
/
index.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// lewis was here qwe
const fs = require('graceful-fs')
const pify = require('pify')
const rakdb = {}
const yaz = pify(fs.writeFileSync)
rakdb.format = (payload) => {
yaz(payload + ".json", '[]', function (err, data) {
if (err) throw err;
})
}
rakdb.yenidb = (payload) => {
try {
fs.readFileSync(payload + '.json', 'utf8')
} catch (error) {
yaz(payload + ".json", '[]', function (err, data) {
if (err) throw err;
})
}
}
var addToObject = function (obj, key, value, index) {
var temp = {};
var i = 0;
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
if (i === index && key && value) {
temp[key] = value;
}
temp[prop] = obj[prop];
i++;
}
}
if (!index && key && value) {
temp[key] = value;
}
return temp;
};
const kaydet = async (db, data) => {
await yaz(db + '.json', JSON.stringify(data, null, 2), function (err, data) {
return 'Başarılı'
})
}
rakdb.getir = (payload) => {
const data = fs.readFileSync(payload + '.json', 'utf8')
var oldData = JSON.parse(data)
try {
return {
veri: oldData,
ekle: function (payload2) {
try {
let databro = []
databro.push(payload2)
var yeniveri = addToObject(databro[0], '_id', JSON.stringify(Date.now()))
oldData.push(yeniveri)
kaydet(payload, oldData)
return databro
} catch (err) {
//console.error(err)
}
},
bul: function (params) {
try {
const element = Object.keys(params)
const values = Object.values(params)
const index = oldData.map(a => a[element]).indexOf(values[0])
if (index < 0) {
return 'Bulunamadı'
}
return {
veri: oldData[index],
sok: function (paramss, params2) {
oldData[index][paramss].push(params2)
yaz(payload + '.json', JSON.stringify(oldData, null, 2), function (err, data) {
if (err) console.error(err)
})
return oldData[index]
},
cikar: function (paramss, parmass2) {
const deger = String(parmass2)
if (deger === "[object Object]") {
var element = Object.keys(parmass2)
var value = Object.values(parmass2)
const idx = oldData[index][paramss].map(a => a[element]).indexOf(value[0])
if (idx < 0) {
return 'Kardeşim bu bulunamadı'
}
oldData[index][paramss].splice(idx, 1)
} else {
const idx = oldData[index][paramss].map(a => a).indexOf(deger)
if (idx < 0) {
return 'Kardeşim bu bulunamadı'
}
oldData[index][paramss].splice(idx, 1)
}
yaz(payload + '.json', JSON.stringify(oldData, null, 2), function (err, data) {
if (err) console.error(err)
})
return oldData[index]
},
güncelle: function (element, value) {
const oldVeri = oldData[index]
var yeniveri = addToObject(oldVeri, element, value)
oldData[index] = yeniveri
yaz(payload + '.json', JSON.stringify(oldData, null, 2), function (err, data) {
if (err) console.error(err)
})
return yeniveri
},
sil: function (element, value){
oldData.splice(index, 1)
yaz(payload + '.json', JSON.stringify(oldData, null, 2), function (err, data) {
if (err) console.error(err)
})
return 'Başarılı Şekilde silindi';
}
}
} catch (error) {
}
},
idbul: function (params) {
try {
const index = oldData.map(a => a._id).indexOf(params)
if (index < 0) {
return 'Bulunamadı'
}
return {
veri: oldData[index],
sok: function (paramss, params2) {
oldData[index][paramss].push(params2)
yaz(payload + '.json', JSON.stringify(oldData, null, 2), function (err, data) {
if (err) console.error(err)
})
return oldData[index]
},
cikar: function (paramss, parmass2) {
const deger = String(parmass2)
if (deger === "[object Object]") {
var element = Object.keys(parmass2)
var value = Object.values(parmass2)
const idx = oldData[index][paramss].map(a => a[element]).indexOf(value[0])
if (idx < 0) {
return 'Kardeşim bu bulunamadı'
}
oldData[index][paramss].splice(idx, 1)
} else {
const idx = oldData[index][paramss].map(a => a).indexOf(deger)
if (idx < 0) {
return 'Kardeşim bu bulunamadı'
}
oldData[index][paramss].splice(idx, 1)
}
yaz(payload + '.json', JSON.stringify(oldData, null, 2), function (err, data) {
if (err) console.error(err)
})
return oldData[index]
},
güncelle: function (element, value) {
const oldVeri = oldData[index]
oldData.splice(index, 1)
var yeniveri = addToObject(oldVeri, element, value)
oldData[index] = yeniveri
yaz(payload + '.json', JSON.stringify(oldData, null, 2), function (err, data) {
if (err) console.error(err)
})
return yeniveri
},
sil: function (element, value){
oldData.splice(index, 1)
yaz(payload + '.json', JSON.stringify(oldData, null, 2), function (err, data) {
if (err) console.error(err)
})
return 'Başarılı Şekilde silindi';
}
}
} catch (err) {
console.error(err)
fs.writeFileSync(payload + '.json', JSON.stringify(oldData, null, 2), function (err, data) {
if (err) throw err;
})
}
},
icindemi: function (key, value) {
const index = oldData.map(a => a[key]).includes(value)
return index
}
}
} catch (err) {
yaz(payload + ".json", JSON.stringify(oldData), function (err, data) {
if (err) throw err;
})
}
}
module.exports = rakdb;