-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
159 lines (140 loc) · 4.2 KB
/
Copy pathdatabase.js
File metadata and controls
159 lines (140 loc) · 4.2 KB
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
const supabase = require('./supabase');
/**
* Veri okuma işlemi
* @param {string} table - Tablo adı
* @param {string} key - Anahtar
* @returns {Promise<any>} - Veri
*/
async function get(table, key) {
try {
const { data, error } = await supabase
.from(table)
.select('value')
.eq('key', key)
.maybeSingle();
if (error && error.code !== 'PGRST116') {
console.error(`Veri okuma hatası (${table}/${key}):`, error);
return null;
}
return data ? data.value : null;
} catch (error) {
console.error(`Veri okuma hatası (${table}/${key}):`, error);
return null;
}
}
/**
* Veri yazma işlemi
* @param {string} table - Tablo adı
* @param {string} key - Anahtar
* @param {any} value - Değer
* @returns {Promise<boolean>} - Başarılı mı?
*/
async function set(table, key, value) {
try {
// Önce veriyi kontrol et
const { data, error: selectError } = await supabase
.from(table)
.select('id')
.eq('key', key)
.maybeSingle();
if (selectError && selectError.code !== 'PGRST116') {
console.error(`Veri kontrol hatası (${table}/${key}):`, selectError);
return false;
}
if (data) {
// Veri varsa güncelle
const { error } = await supabase
.from(table)
.update({ value, updated_at: new Date() })
.eq('key', key);
if (error) {
console.error(`Veri güncelleme hatası (${table}/${key}):`, error);
return false;
}
} else {
// Veri yoksa ekle
const { error } = await supabase
.from(table)
.insert({ key, value, created_at: new Date(), updated_at: new Date() });
if (error) {
console.error(`Veri ekleme hatası (${table}/${key}):`, error);
return false;
}
}
return true;
} catch (error) {
console.error(`Veri yazma hatası (${table}/${key}):`, error);
return false;
}
}
/**
* Veri silme işlemi
* @param {string} table - Tablo adı
* @param {string} key - Anahtar
* @returns {Promise<boolean>} - Başarılı mı?
*/
async function del(table, key) {
try {
const { error } = await supabase
.from(table)
.delete()
.eq('key', key);
if (error) {
console.error(`Veri silme hatası (${table}/${key}):`, error);
return false;
}
return true;
} catch (error) {
console.error(`Veri silme hatası (${table}/${key}):`, error);
return false;
}
}
/**
* Tüm anahtarları getir
* @param {string} table - Tablo adı
* @returns {Promise<string[]>} - Anahtarlar
*/
async function keys(table) {
try {
const { data, error } = await supabase
.from(table)
.select('key');
if (error) {
console.error(`Anahtarları getirme hatası (${table}):`, error);
return [];
}
return data ? data.map(item => item.key) : [];
} catch (error) {
console.error(`Anahtarları getirme hatası (${table}):`, error);
return [];
}
}
/**
* Belirli bir prefix ile başlayan tüm anahtarları getir
* @param {string} table - Tablo adı
* @param {string} prefix - Prefix
* @returns {Promise<string[]>} - Anahtarlar
*/
async function keysStartingWith(table, prefix) {
try {
const { data, error } = await supabase
.from(table)
.select('key')
.ilike('key', `${prefix}%`);
if (error) {
console.error(`Prefix ile anahtarları getirme hatası (${table}/${prefix}):`, error);
return [];
}
return data ? data.map(item => item.key) : [];
} catch (error) {
console.error(`Prefix ile anahtarları getirme hatası (${table}/${prefix}):`, error);
return [];
}
}
module.exports = {
get,
set,
del,
keys,
keysStartingWith
};