-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupabased.py
391 lines (327 loc) · 13 KB
/
supabased.py
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import supabase
from supabase import create_client, Client
from cryptography.fernet import Fernet
import os
# from penv import SUPABASE_URL, SUPABASE_KEY_SERVICE, BUCKET
def get_env_value(key):
try:
return os.environ[key]
except KeyError:
return 'there is no such key'
SUPABASE_URL = get_env_value('SUPABASE_URL')
SUPABASE_KEY_SERVICE = get_env_value('SUPABASE_KEY_SERVICE')
BUCKET = get_env_value('BUCKET')
def create_supabase_client() -> Client:
return create_client(SUPABASE_URL, SUPABASE_KEY_SERVICE)
def encrypt(message: bytes, key: bytes) -> bytes:
return Fernet(key).encrypt(message)
def decrypt(token: bytes, key: bytes) -> bytes:
return Fernet(key).decrypt(token)
def check_if_data_exists_on_supbase(supaClient, table_name, column_name, column_value):
thing = supaClient.table(table_name).select("*").eq(column_name, column_value).execute()
if len(thing.data) > 0:
return True
return False
def base_query(supaClient, table_name):
return supaClient.table(table_name).select("*")
# DATABASE
def fetch_data(supaClient, table_name, column_name, column_value):
try:
return supaClient.table(table_name).select("*").eq(column_name, column_value).execute()
except Exception as e:
return e.message
def fetch_data_list(supaClient, table_name):
try:
return supaClient.table(table_name).select("*").execute()
except Exception as e:
return e.message
def insert_data(supaClient, table_name, data):
try:
return supaClient.table(table_name).insert(data).execute()
except Exception as e:
return e.message
def update_data(supaClient, table_name, column_name, column_value, data):
try:
return supaClient.table(table_name).update(data).eq(column_name, column_value).execute()
except Exception as e:
return e.message
def upsert_data(supaClient, table_name, column_name, column_value, data):
try:
return supaClient.table(table_name).upsert(data).eq(column_name, column_value).execute()
except Exception as e:
return e.message
def delete_data(supaClient, table_name, column_name, column_value):
try:
return supaClient.table(table_name).delete().eq(column_name, column_value).execute()
except Exception as e:
return e.message
def fetch_data_equals_to(supaClient, table_name, column_name, column_value):
try:
return supaClient.table(table_name).select("*").eq(column_name, column_value).execute()
except Exception as e:
return e.message
def fetch_data_not_equals_to(supaClient, table_name, column_name, column_value):
try:
return supaClient.table(table_name).select("*").neq(column_name, column_value).execute()
except Exception as e:
return e.message
def fetch_data_greater_than(supaClient, table_name, column_name, column_value):
try:
return supaClient.table(table_name).select("*").gt(column_name, column_value).execute()
except Exception as e:
return e.message
# Column is greater than or equal to a value
def fetch_data_greater_than_or_equal_to(supaClient, table_name, column_name, column_value):
try:
return supaClient.table(table_name).select("*").gte(column_name, column_value).execute()
except Exception as e:
return e.message
# Column is less than a value
def fetch_data_less_than(supaClient, table_name, column_name, column_value):
try:
return supaClient.table(table_name).select("*").lt(column_name, column_value).execute()
except Exception as e:
return e.message
# Column is less than or equal to a value
def fetch_data_less_than_or_equal_to(supaClient, table_name, column_name, column_value):
try:
return supaClient.table(table_name).select("*").lte(column_name, column_value).execute()
except Exception as e:
return e.message
# Column matches a pattern
def fetch_data_matches_pattern(supaClient, table_name, column_name, column_value):
try:
return supaClient.table(table_name).select("*").like(column_name, column_value).execute()
except Exception as e:
return e.message
# Column matches a case-insensitive pattern
def fetch_data_matches_case_insensitive_pattern(supaClient, table_name, column_name, column_value):
try:
return supaClient.table(table_name).select("*").ilike(column_name, column_value).execute()
except Exception as e:
return e.message
# Column is a value
def fetch_data_is_a_value(supaClient, table_name, column_name, column_value):
try:
return supaClient.table(table_name).select("*").is_(column_name, column_value).execute()
except Exception as e:
return e.message
# Column is in an array
def fetch_data_is_in_an_array(supaClient, table_name, column_name, column_value):
try:
return supaClient.table(table_name).select("*").is_in(column_name, column_value).execute()
except Exception as e:
return e.message
# Column contains every element in a value
def fetch_data_contains_every_element_in_a_value(supaClient, table_name, column_name, column_value):
try:
return supaClient.table(table_name).select("*").contains(column_name, column_value).execute()
except Exception as e:
return e.message
# Contained by value
def fetch_data_contained_by_value(supaClient, table_name, column_name, column_value):
try:
return supaClient.table(table_name).select("*").contained_by(column_name, column_value).execute()
except Exception as e:
return e.message
# Match an associated value
def fetch_data_match_an_associated_value(supaClient, table_name, column_name, column_value):
try:
# TODO: fix this
return supaClient.table(table_name).select("*").associated(column_name, column_value).execute()
except Exception as e:
return e.message
# Don't match the filter
def fetch_data_dont_match_the_filter(supaClient, table_name, column_name, column_value):
try:
# TODO: fix this
return supaClient.table(table_name).select("*").not_.is_(column_name, column_value).execute()
except Exception as e:
return e.message
# Match the filter
def fetch_data_match_the_filter(supaClient, table_name, column_name, column_value):
try:
# TODO: fix this
return supaClient.table(table_name).select("*").filter(column_name, column_value).execute()
except Exception as e:
return e.message
# Order the results
def fetch_data_order_the_results(supaClient, table_name, column_name, desc=True):
try:
return supaClient.table(table_name).select("*").order(column_name, desc).execute()
except Exception as e:
return e.message
# Limit the number of rows returned
def fetch_data_limit_the_number_of_rows_returned(supaClient, table_name, limit=1):
try:
return supaClient.table(table_name).select("*").limit(limit).execute()
except Exception as e:
return e.message
def fetch_data_with_filter_type(supaClient, table_name, column_name, column_value, filter_type):
query = supaClient.table(table_name).select("*")
if filter_type == 'eq':
query.eq(column_name, column_value)
elif filter_type == 'neq':
query.neq(column_name, column_value)
elif filter_type == 'gt':
query.gt(column_name, column_value)
elif filter_type == 'lt':
query.lt(column_name, column_value)
elif filter_type == 'gte':
query.gte(column_name, column_value)
try:
return query.execute()
except Exception as e:
return e.message
# AUTH
# Create a new user
def create_a_new_user(supaClient, email, password):
try:
return supaClient.auth.sign_up(email, password)
except Exception as e:
return e.message
# Sign in a user
def sign_in_a_user(supaClient, email, password):
try:
return supaClient.auth.sign_in(email, password)
except Exception as e:
return e.message
# Sign in a user through OTP
def sign_in_a_user_through_otp(supaClient, email):
try:
return supaClient.auth.sign_in_otp(email)
except Exception as e:
return e.message
# Sign in a user through 0auth
def sign_in_a_user_through_0auth(supaClient, provider, access_token):
try:
return supaClient.auth.sign_in_with_oauth(provider, access_token)
except Exception as e:
return e.message
# Sign out a user
def sign_out_a_user(supaClient, access_token):
try:
return supaClient.auth.sign_out(access_token)
except Exception as e:
return e.message
# Verify and log in through OTP
def verify_and_log_in_through_otp(supaClient, email, otp):
try:
return supaClient.auth.verify_otp(email, otp)
except Exception as e:
return e.message
# Retrieve a session
def retrieve_a_session(supaClient, access_token):
try:
return supaClient.auth.get_session(access_token)
except Exception as e:
return e.message
# Retrieve a new session
def retrieve_a_new_session(supaClient, refresh_token):
try:
return supaClient.auth.refresh_access_token(refresh_token)
except Exception as e:
return e.message
# Retrieve a user
def retrieve_a_user(supaClient):
try:
return supaClient.auth.get_user()
except Exception as e:
return e.message
# Set the session data
def set_the_session_data(supaClient, access_token, refresh_token, data):
try:
return supaClient.auth.set_session_data(access_token, refresh_token, data)
except Exception as e:
return e.message
# Create a bucket
def create_a_bucket(supaClient, bucket_name):
try:
return supaClient.storage.create_bucket(bucket_name)
except Exception as e:
return e.message
def retrieve_a_bucket(supaClient, bucket_name):
try:
return supaClient.storage.get_bucket(bucket_name)
except Exception as e:
return e.message
def list_all_buckets(supaClient):
try:
return supaClient.storage.list_buckets()
except Exception as e:
return e.message
def delete_a_bucket(supaClient, bucket_name):
try:
return supaClient.storage.remove_bucket(bucket_name)
except Exception as e:
return e.message
def empty_a_bucket(supaClient, bucket_name):
try:
return supaClient.storage.empty_bucket(bucket_name)
except Exception as e:
return e.message
def upload_a_file(supaClient, bucket_name, file, path, file_options):
try:
return supaClient.storage.from_(bucket_name).upload(file=file, path=path, file_options=file_options)
except Exception as e:
return e.message
def download_a_file(supaClient, bucket_name, source):
try:
return supaClient.storage.from_(bucket_name).download(source)
except Exception as e:
return e.message
def list_all_files_in_a_bucket(supaClient, bucket_name):
try:
return supaClient.storage.from_(bucket_name).list()
except Exception as e:
return e.message
def replace_an_existing_file(supaClient, bucket_name, file, path, file_options):
try:
return supaClient.storage.from_(bucket_name).update(file=file, path=path, file_options=file_options)
except Exception as e:
return e.message
def move_an_existing_file(supaClient, bucket_name, source, destination):
try:
return supaClient.storage.from_(bucket_name).move(source, destination)
except Exception as e:
return e.message
def delete_files_in_a_bucket(supaClient, bucket_name, path):
try:
return supaClient.storage.from_(bucket_name).remove(path)
except Exception as e:
return e.message
def create_a_signed_url(supaClient, bucket_name, filepath, expiry_duration):
try:
return supaClient.storage.from_(bucket_name).create_signed_url(filepath, expiry_duration)
except Exception as e:
return e.message
def retrieve_public_url(supaClient, bucket_name, filepath):
try:
return supaClient.storage.from_(bucket_name).get_public_url(filepath)
except Exception as e:
return e.message
def invoke_a_function(supaClient, function_name, invoke_options):
try:
return supaClient.functions.invoke(function_name, invoke_options)
except Exception as e:
return e.message
# Initialize the base query
def filter_by_match_array(supaClient, match_array, table):
query = base_query(supaClient, table)
for item in match_array:
attribute = item["attribute"]
plan_value = item["value"]
value = item["value"]
if attribute.find('->') != -1:
value = f'"{value}"'
# if filter type is like we need to add like
if "type" in item:
if item["type"] == "like":
if attribute.find('->') != -1:
# TODO: https://stackoverflow.com/questions/42918348/postgresql-json-like-query
continue
query = query.like(attribute, f'%{plan_value}%')
# TODO: add other types like bigger than, smaller than, etc
else:
query = query.eq(attribute, value)
return query.execute()