From fb1078f8e3e8d363f0b35589571be7d5c299df23 Mon Sep 17 00:00:00 2001 From: Jhagas Date: Fri, 14 Apr 2023 04:02:04 +0700 Subject: [PATCH] documentation and bumping version number --- LICENSE | 2 +- README.md | 136 +++-- database-example/README.md | 3 + .../combined-example/combined-example.ino | 46 +- examples/delete/delete.ino | 0 examples/insert/insert.ino | 2 +- examples/select/select.ino | 56 ++ examples/update/update.ino | 34 ++ keywords.txt | 44 +- library.json | 18 +- library.properties | 6 +- src/ESP32_Supabase.h | 59 +-- src/Supabase.cpp | 494 ++++++++++-------- 13 files changed, 563 insertions(+), 337 deletions(-) create mode 100644 database-example/README.md delete mode 100644 examples/delete/delete.ino diff --git a/LICENSE b/LICENSE index 002f141..4e3502b 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 Jhagas Hana Winaya +Copyright (c) 2023 Jhagas Hana Winaya, Achmad Nashruddin Riskynanda Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 6cc84f0..dbfd0eb 100644 --- a/README.md +++ b/README.md @@ -10,67 +10,111 @@ For further information : - [Supabase Documentation](https://supabase.com/docs) - [PostgREST API Documentation](https://postgrest.org/en/stable/api.html) +## Table of Contents + +- [ESP32 Supabase](#esp32-supabase) + - [Table of Contents](#table-of-contents) + - [Using This Library](#using-this-library) + - [Examples](#examples) + - [Available Method](#available-method) + - [Directly Makes Connection to Database](#directly-makes-connection-to-database) + - [Building The Queries](#building-the-queries) + - [Horizontal Filtering (comparison) Operator](#horizontal-filtering-comparison-operator) + - [Ordering, Limiting or Offseting the Result](#ordering-limiting-or-offseting-the-result) + - [Getting the Query URL (for debugging)](#getting-the-query-url-for-debugging) + - [Reset the Query URL](#reset-the-query-url) + - [To-do (sorted by priority)](#to-do-sorted-by-priority) + +## Using This Library + +This library is available at Arduino's Library Manager, as well as PlatformIO Library Manager +- [Arduino Library Manager Guide](http://arduino.cc/en/guide/libraries) + ## Examples See all examples in `examples` folder ## Available Method -| Method | Description | -| ----------------------------------------------------- | --------------------------------------------------------------------------- | -| `begin(String url_a, String key_a);` | `url_a` is a Supabase URL and `key_a` is supabase anon key. Returns `void` | -| `login_email(String email_a, String password_a)` | Returns http response code `int` | -| `login_phone(String phone_a, String password_a)` | Returns http response code `int` | -| `insert(String table, String json, bool upsert)` | Returns http response code `int` | -| `get_payload(String table, String json, bool upsert)` | Returns payload from select and rpc method `String` | - -### Horizontal Filtering Operators - -| Abbreviation | Meaning | -| ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| eq | equals | -| gt | greater than | -| gte | greater than or equal | -| lt | less than | -| lte | less than or equal | -| neq | not equal | -| in | one of a list of values, e.g. `?a=in.(1,2,3)` – also supports commas in quoted strings like `?a=in.("hi,there","yes,you")` | -| is | checking for exact equality (null,true,false,unknown) | -| cs | contains e.g. `?tags=cs.{example, new}` | -| cd | contained in e.g. `?values=cd.{1,2,3}` | -| ov | overlap (have points in common), e.g. `?period=ov.[2017-01-01,2017-06-30]` – also supports array types, use curly braces instead of square brackets e.g. `?arr=ov.{1,3}` | -| sl | strictly left of, e.g. `?range=sl.(1,10)` | -| sr | strictly right of | -| nxr | does not extend to the right of, e.g. `?range=nxr.(1,10)` | -| nxl | does not extend to the left of | -| adj | is adjacent to, e.g. `?range=adj.(1,10)` | -| not | negates another operator, see [Logical operators](#logical-operators) | -| or | logical OR, see [Logical operators](#logical-operators) | -| and | logical AND, see [Logical operators](#logical-operators) | - -#### Logical operators - -Multiple conditions on columns are evaluated using AND by default, but you can combine them using OR with the or operator. For example, to return people under 18 or over 21: - -```curl -curl "http://localhost:3000/people?or=(age.lt.18,age.gt.21)" +### Directly Makes Connection to Database + +| Method | Description | +| ------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------ | +| `begin(String url_a, String key_a);` | `url_a` is a Supabase URL and `key_a` is supabase anon key. Returns `void` | +| `login_email(String email_a, String password_a)` | Returns http response code `int` | +| `login_phone(String phone_a, String password_a)` | Returns http response code `int` | +| `insert(String table, String json, bool upsert)` | Returns http response code `int`. If you want to do upsert, set thirt parameter to `true` | +| `.doSelect()` | Called at the end of select query chain, see [Examples](#examples). Returns http response payload (your data) from Supabase `String` | +| `.doUpdate(String json)` | Called at the end of update query chain, see [Examples](#examples). Returns http response code from Supabase `int` | + +### Building The Queries + +When building the queries, you can chaining the method like this example. + +> Remember in `.select()` method, it is mandatory to put some low amount of `.limit()`, so you can avoid your microcontroller's memory get overflowed + +```arduino +String read = db.from("table").select("*").eq("column", "value").order("column", "asc", true).limit(1).doSelect(); +``` + +| Methods | Description | +| ------------------------ | --------------------------------------------------------------------------------------- | +| `.from(String table);` | Specify which table you want to query. It will append `?table_name` in Request URL | +| `.select(String colls);` | Specify that you want to do select query. It will append `&select=colls` in Request URL | +| `.update(String table);` | Specify that you want to do update query. It will append `&update` in Request URL | + + +#### Horizontal Filtering (comparison) Operator + +| Methods | Description | +| ---------------------------------- | ---------------------------------------------------------------------------------------------------------- | +| `.eq(String colls, String value)` | Equals | +| `.gt(String colls, String value)` | Greater than | +| `.gte(String colls, String value)` | Greater than or equal | +| `.lt(String colls, String value)` | Less than | +| `.lte(String colls, String value)` | Less than or equal | +| `.neq(String colls, String value)` | Not equal | +| `.in(String colls, String value)` | One of a list of values, e.g. `1,2,3` – also supports commas in quoted strings like `"hi,there","yes,you"` | +| `.is(String colls, String value)` | Checking for exact equality `` | +| `.cs(String colls, String value)` | Contains e.g. `example, new` | +| `.cd(String colls, String value)` | Contained in e.g. `1, 2, 3` | +| `.ov(String colls, String value)` | Overlap (have points in common), e.g. `2017-01-01, 2017-06-30` | +| `.sl(String colls, String value)` | Strictly left of, e.g. `1,10` | +| `.sr(String colls, String value)` | Strictly right of | +| `.nxr(String colls, String value)` | Does not extend to the right of, e.g. `1,10` | +| `.nxl(String colls, String value)` | Does not extend to the left of | +| `.adj(String colls, String value)` | Is adjacent to, e.g. `1,10` | + +#### Ordering, Limiting or Offseting the Result + +| Methods | Description | +| --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `.order(String coll, String by, bool nulls);` | This reorders the response rows. `coll` parameter is column name, `by` parameter is either `asc` or `desc`. The last parameter specifies the position of nulls, `nullsfirst` or `nullslast` | +| `.limit(unsigned int by);` | Limit the amount of response rows. THIS IS MANDATORY FOR SELECT METHOD!!! | +| `.offset(int by);` | Request response rows with offset is with its parameters. | + +#### Getting the Query URL (for debugging) + +```arduino +db.urlQuery_reset(); ``` -To negate any operator, you can prefix it with not like `?a=not.eq.2` or `?not.and=(a.gte.0,a.lte.100)`. +#### Reset the Query URL -You can also apply complex logic to the conditions: +This method calls in mandatory, must be called after one opetation (let's say `doSelect()`, or `doUpdate()`) before doing anything else. -```curl -curl "http://localhost:3000/people?grade=gte.90&student=is.true&or=(age.eq.14,not.and(age.gte.11,age.lte.17))" +```arduino +db.urlQuery_reset(); ``` ## To-do (sorted by priority) - [x] Make Select API (GET Request), full with row limits (one by default) -- [ ] Make filtering query builder method in Select, Update and Delete -- [ ] Make order/sort query builder method to in Select -- [ ] Implement Update with PATCH HTTPS Request -- [ ] Implement Delete with DELETE HTTPS Request +- [x] Make filtering query builder method in Select and update +- [x] Make order/sort query builder method to in Select +- [x] Implement Update with PATCH HTTPS Request - [ ] Implement calling RPC function with HTTPS Request - [ ] Implement several methods to implement [Supabase Realtime](https://supabase.com/docs/guides/realtime) +- [ ] Port to ESP8266 +Better documentation is always a welcoming change 😄️😄️ diff --git a/database-example/README.md b/database-example/README.md new file mode 100644 index 0000000..c719b31 --- /dev/null +++ b/database-example/README.md @@ -0,0 +1,3 @@ +## Database Playground + +Use this as a template to explore your supabase feature! Please make sure to enable RLS, you can enhance security with user authentication feature \ No newline at end of file diff --git a/examples/combined-example/combined-example.ino b/examples/combined-example/combined-example.ino index c01c687..458b39b 100644 --- a/examples/combined-example/combined-example.ino +++ b/examples/combined-example/combined-example.ino @@ -6,35 +6,61 @@ Supabase db; // Put your supabase URL and Anon key here... // Because Login already implemented, there's no need to use secretrole key -String supabase_url = "https://bzdazgjthqumbxiexchy.supabase.co"; -String anon_key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImJ6ZGF6Z2p0aHF1bWJ4aWV4Y2h5Iiwicm9sZSI6ImFub24iLCJpYXQiOjE2ODExNjAwNTcsImV4cCI6MTk5NjczNjA1N30.nQb4GMqJY9DGt-i7gsd4DSSHmBwo7ucUOJHqhp5OlHE"; +String supabase_url = ""; +String anon_key = ""; // put your WiFi credentials (SSID and Password) here -const char* ssid = "FALKO 1"; -const char* psswd = "12345678"; +const char *ssid = ""; +const char *psswd = ""; // Put Supabase account credentials here -const String email = "jhagas@e.email"; -const String password = "thisispassword"; +const String email = ""; +const String password = ""; -void setup() { + +// Put your JSON that you want to insert rows +// You can also use library like ArduinoJson generate this +String JSON = ""; + +bool upsert = false; + +void setup() +{ Serial.begin(9600); + // Connecting to Wi-Fi Serial.print("Connecting to WiFi"); WiFi.begin(ssid, psswd); - while (WiFi.status() != WL_CONNECTED) { + while (WiFi.status() != WL_CONNECTED) + { delay(100); Serial.print("."); } Serial.println("Connected!"); + // Beginning Supabase Connection db.begin(supabase_url, anon_key); + + // Logging in with your account you made in Supabase db.login_email(email, password); - String read = db.from("examples").select("*").eq("role","praktikan").order("date","asc", true).limit(1).doSelect(); + // Select query with filter and order, limiting the result is mandatory here + String read = db.from("table").select("*").eq("column", "value").order("column", "asc", true).limit(1).doSelect(); Serial.println(read); + + // Reset Your Query before doing everything else + db.urlQuery_reset(); + + // Join operation with other table that connected via PK or FK + String read = db.from("table").select("*, other_table(other_table_column1, other_table_column2, another_table(*))").order("column", "asc", true).limit(1).doSelect(); + Serial.println(read); + db.urlQuery_reset(); + + // Insert operation + int HTTPresponseCode = db.insert("table", JSON, upsert); } -void loop() { +void loop() +{ delay(10); } \ No newline at end of file diff --git a/examples/delete/delete.ino b/examples/delete/delete.ino deleted file mode 100644 index e69de29..0000000 diff --git a/examples/insert/insert.ino b/examples/insert/insert.ino index 073ce06..2386def 100644 --- a/examples/insert/insert.ino +++ b/examples/insert/insert.ino @@ -17,7 +17,7 @@ void setup() { Serial.begin(9600); Serial.print("Connecting to WiFi"); - WiFi.begin("Jhagas H.", "tibetanplateau"); + WiFi.begin("ssid", "password"); while (WiFi.status() != WL_CONNECTED) { delay(100); Serial.print("."); diff --git a/examples/select/select.ino b/examples/select/select.ino index e69de29..1f56622 100644 --- a/examples/select/select.ino +++ b/examples/select/select.ino @@ -0,0 +1,56 @@ +#include +#include +#include + +Supabase db; + +// Put your supabase URL and Anon key here... +// Because Login already implemented, there's no need to use secretrole key +String supabase_url = ""; +String anon_key = ""; + +// put your WiFi credentials (SSID and Password) here +const char *ssid = ""; +const char *psswd = ""; + +// Put Supabase account credentials here +const String email = ""; +const String password = ""; + +void setup() +{ + Serial.begin(9600); + + // Connecting to Wi-Fi + Serial.print("Connecting to WiFi"); + WiFi.begin(ssid, psswd); + while (WiFi.status() != WL_CONNECTED) + { + delay(100); + Serial.print("."); + } + Serial.println("Connected!"); + + // Beginning Supabase Connection + db.begin(supabase_url, anon_key); + + // Logging in with your account you made in Supabase + db.login_email(email, password); + + // Select query with filter and order, limiting the result is mandatory here + String read = db.from("examples").select("*").eq("column", "value").order("column", "asc", true).limit(1).doSelect(); + Serial.println(read); + + // Reset Your Query before doing everything else + db.urlQuery_reset(); + + // Join operation with other table that connected via PK or FK + String read = db.from("examples").select("*, other_table(other_table_column1, other_table_column2, another_table(*))").order("column", "asc", true).limit(1).doSelect(); + Serial.println(read); + db.urlQuery_reset(); +} + +void loop() +{ + delay(10); +} \ No newline at end of file diff --git a/examples/update/update.ino b/examples/update/update.ino index e69de29..a089e0e 100644 --- a/examples/update/update.ino +++ b/examples/update/update.ino @@ -0,0 +1,34 @@ +#include +#include +#include + +Supabase db; + +// Put your target table here +String table = ""; + +// Put your JSON that you want to insert rows +// You can also use library like ArduinoJson generate this +String JSON = ""; + +bool upsert = false; + +void setup() { + Serial.begin(9600); + + Serial.print("Connecting to WiFi"); + WiFi.begin("ssid", "password"); + while (WiFi.status() != WL_CONNECTED) { + delay(100); + Serial.print("."); + } + Serial.println("Connected!"); + + int code = db.update("table").eq("column", "value").doUpdate(JSON); + Serial.println(code); + db.urlQuery_reset(); +} + +void loop() { + delay(10); +} \ No newline at end of file diff --git a/keywords.txt b/keywords.txt index 7ff2f97..348cae0 100644 --- a/keywords.txt +++ b/keywords.txt @@ -6,24 +6,42 @@ # Datatypes (KEYWORD1) ####################################### -Supabase KEYWORD1 +Supabase KEYWORD2 ####################################### # Methods and Functions (KEYWORD2) ####################################### -insert KEYWORD2 -select KEYWORD2 -delete KEYWORD2 -update KEYWORD2 -get_payload KEYWORD2 - -login_email KEYWORD2 -login_phone KEYWORD2 - -add_filter KEYWORD2 -clear_filter KEYWORD2 -sort KEYWORD2 +begin KEYWORD2 +getQuery KEYWORD2 +urlQuery_reset KEYWORD2 +from KEYWORD2 +insert KEYWORD2 +select KEYWORD2 +update KEYWORD2 +eq KEYWORD2 +gt KEYWORD2 +gte KEYWORD2 +lt KEYWORD2 +lte KEYWORD2 +neq KEYWORD2 +in KEYWORD2 +is KEYWORD2 +cs KEYWORD2 +cd KEYWORD2 +ov KEYWORD2 +sl KEYWORD2 +sr KEYWORD2 +nxr KEYWORD2 +nxl KEYWORD2 +adj KEYWORD2 +order KEYWORD2 +limit KEYWORD2 +offset KEYWORD2 +doSelect KEYWORD2 +doUpdate KEYWORD2 +login_email KEYWORD2 +login_phone KEYWORD2 ####################################### # Constants (LITERAL1) diff --git a/library.json b/library.json index 321fded..e8251ef 100644 --- a/library.json +++ b/library.json @@ -1,12 +1,18 @@ { - "name": "ESP32-Supabase", + "name": "ESP32 Supabase", "keywords": "supabase, postgresql, rest, database, IoT", - "version": "0.0.2", + "version": "0.0.3", "description": "A Arduino Platform Library for connecting ESP32 to Supabase, including user authentication", - "authors": { - "name": "Jhagas Hana Winaya", - "email": "jhagas@e.email" - }, + "authors": [ + { + "name": "Jhagas Hana Winaya", + "email": "jhagas@e.email" + }, + { + "name": "Achmad Nashruddin Riskynanda", + "email": "achmad.riskynanda01@gmail.com" + } + ], "license": "MIT", "frameworks": "arduino", "platforms": "espressif32", diff --git a/library.properties b/library.properties index 96acd6d..10ba097 100644 --- a/library.properties +++ b/library.properties @@ -1,6 +1,6 @@ -name=ESP32_Supabase -version=0.0.2 -author=Jhagas Hana Winaya +name=ESP32 Supabase +version=0.0.3 +author=Jhagas Hana Winaya , Achmad Nashruddin Riskynanda maintainer=Jhagas Hana Winaya sentence=A Arduino Platform Library for interfacing ESP32 to Supabase, including user authentication. paragraph=This allows developers to interface their ESP32 microcontroller with Supabase, an open-source alternative to Firebase. The library simplifies the process of connecting to Supabase by providing a set of functions that abstract away the complexity of the underlying API. diff --git a/src/ESP32_Supabase.h b/src/ESP32_Supabase.h index b51aec1..dec4771 100644 --- a/src/ESP32_Supabase.h +++ b/src/ESP32_Supabase.h @@ -9,12 +9,12 @@ class Supabase String hostname; String key; String USER_TOKEN; - + String url_query; WiFiClientSecure client; HTTPClient https; - + bool useAuth; unsigned long loginTime; String phone_or_email; @@ -22,48 +22,47 @@ class Supabase String data; String loginMethod; String filter; - -void _check_last_string(); -int _login_process(); + void _check_last_string(); + int _login_process(); public: - Supabase(){} - ~Supabase(){} + Supabase() {} + ~Supabase() {} void begin(String hostname_a, String key_a); String getQuery(); // query reset void urlQuery_reset(); - //membuat Query Builder - Supabase& from(String table); + // membuat Query Builder + Supabase &from(String table); int insert(String table, String json, bool upsert); - Supabase& select(String colls); - Supabase& update(String table); + Supabase &select(String colls); + Supabase &update(String table); // Comparison Operator - Supabase& eq(String coll, String conditions); - Supabase& gt(String coll, String conditions); - Supabase& gte(String coll, String conditions); - Supabase& lt(String coll, String conditions); - Supabase& lte(String coll, String conditions); - Supabase& neq(String coll, String conditions); - Supabase& in(String coll, String conditions); - Supabase& is(String coll, String conditions); - Supabase& cs(String coll, String conditions); - Supabase& cd(String coll, String conditions); - Supabase& ov(String coll, String conditions); - Supabase& sl(String coll, String conditions); - Supabase& sr(String coll, String conditions); - Supabase& nxr(String coll, String conditions); - Supabase& nxl(String coll, String conditions); - Supabase& adj(String coll, String conditions); + Supabase &eq(String coll, String conditions); + Supabase >(String coll, String conditions); + Supabase >e(String coll, String conditions); + Supabase <(String coll, String conditions); + Supabase <e(String coll, String conditions); + Supabase &neq(String coll, String conditions); + Supabase &in(String coll, String conditions); + Supabase &is(String coll, String conditions); + Supabase &cs(String coll, String conditions); + Supabase &cd(String coll, String conditions); + Supabase &ov(String coll, String conditions); + Supabase &sl(String coll, String conditions); + Supabase &sr(String coll, String conditions); + Supabase &nxr(String coll, String conditions); + Supabase &nxl(String coll, String conditions); + Supabase &adj(String coll, String conditions); // Ordering - Supabase& order(String coll, String by, bool nulls); - Supabase& limit(unsigned int by); - Supabase& offset(int by); + Supabase &order(String coll, String by, bool nulls); + Supabase &limit(unsigned int by); + Supabase &offset(int by); // do select. execute this after building your query String doSelect(); diff --git a/src/Supabase.cpp b/src/Supabase.cpp index 7423dd7..0c92bc5 100644 --- a/src/Supabase.cpp +++ b/src/Supabase.cpp @@ -1,39 +1,38 @@ #include "ESP32_Supabase.h" -namespace Supa{ - String command[] = { - "nullslast","nullsfirst" - }; - const int NULLSLAST = 0; - const int NULLSFIRST = 1; -} - -void Supabase::_check_last_string(){ - unsigned int last = url_query.length()-1; - if(url_query[last] != '?'){ +void Supabase::_check_last_string() +{ + unsigned int last = url_query.length() - 1; + if (url_query[last] != '?') + { url_query += "&"; } } -int Supabase::_login_process(){ +int Supabase::_login_process() +{ int httpCode; StaticJsonDocument<1024> doc; Serial.println("Beginning to login.."); - if (https.begin(client, hostname + "/auth/v1/token?grant_type=password")){ + if (https.begin(client, hostname + "/auth/v1/token?grant_type=password")) + { https.addHeader("apikey", key); https.addHeader("Content-Type", "application/json"); String query = "{\"" + loginMethod + "\": \"" + phone_or_email + "\", \"password\": \"" + password + "\"}"; httpCode = https.POST(query); - if (httpCode > 0){ + if (httpCode > 0) + { String data = https.getString(); deserializeJson(doc, data); USER_TOKEN = doc["access_token"].as(); Serial.println("Login Success"); Serial.println(USER_TOKEN); - }else{ + } + else + { Serial.println(phone_or_email); Serial.println(password); @@ -43,247 +42,288 @@ int Supabase::_login_process(){ https.end(); loginTime = millis(); - }else{ + } + else + { return -100; } return httpCode; } - void Supabase::begin(String hostname_a, String key_a){ - client.setInsecure(); - hostname = hostname_a; - key = key_a; - } - String Supabase::getQuery(){ - String temp = url_query; - urlQuery_reset(); - return hostname +"/rest/v1/"+ temp; - } - // query reset - void Supabase::urlQuery_reset(){ - url_query = ""; - } - //membuat Query Builder - Supabase& Supabase::Supabase::from(String table){ - url_query += (table+"?"); - return *this; - } - int Supabase::insert(String table, String json, bool upsert){ - int httpCode; - if (https.begin(client, hostname + "/rest/v1/" + table)){ - https.addHeader("apikey", key); - https.addHeader("Content-Type", "application/json"); - if(upsert){ - https.addHeader("Authorization", "resolution=merge-duplicates"); - } - if (useAuth){ - unsigned long t_now = millis(); - if (t_now - loginTime >= 3500000){ - _login_process(); - } - https.addHeader("Authorization", "Bearer " + USER_TOKEN); - } - httpCode = https.POST(json); - https.end(); - }else{ - return -100; +void Supabase::begin(String hostname_a, String key_a) +{ + client.setInsecure(); + hostname = hostname_a; + key = key_a; +} +String Supabase::getQuery() +{ + String temp = url_query; + urlQuery_reset(); + return hostname + "/rest/v1/" + temp; +} +// query reset +void Supabase::urlQuery_reset() +{ + url_query = ""; +} +// membuat Query Builder +Supabase &Supabase::Supabase::from(String table) +{ + url_query += (table + "?"); + return *this; +} +int Supabase::insert(String table, String json, bool upsert) +{ + int httpCode; + if (https.begin(client, hostname + "/rest/v1/" + table)) + { + https.addHeader("apikey", key); + https.addHeader("Content-Type", "application/json"); + if (upsert) + { + https.addHeader("Authorization", "resolution=merge-duplicates"); } - return httpCode; - } - Supabase& Supabase::select(String colls){ - url_query += ("select="+colls); - return *this; + if (useAuth) + { + unsigned long t_now = millis(); + if (t_now - loginTime >= 3500000) + { + _login_process(); + } + https.addHeader("Authorization", "Bearer " + USER_TOKEN); + } + httpCode = https.POST(json); + https.end(); } - Supabase& Supabase::update(String table){ - url_query += (table+"?"); - return *this; + else + { + return -100; } - // Supabase& Supabase::drop(String table){ - // url_query += (table+"?"); - // return *this; - // } + return httpCode; +} +Supabase &Supabase::select(String colls) +{ + url_query += ("select=" + colls); + return *this; +} +Supabase &Supabase::update(String table) +{ + url_query += (table + "?"); + return *this; +} +// Supabase& Supabase::drop(String table){ +// url_query += (table+"?"); +// return *this; +// } - // Comparison Operator - Supabase& Supabase::eq(String coll, String conditions){ - _check_last_string(); - url_query += (coll+"=eq."+conditions); - return *this; - } - Supabase& Supabase::gt(String coll, String conditions){ - _check_last_string(); - url_query += (coll+"=gt."+conditions); - return *this; - } - Supabase& Supabase::gte(String coll, String conditions){ - _check_last_string(); - url_query += (coll+"=gte."+conditions); - return *this; - } - Supabase& Supabase::lt(String coll, String conditions){ - _check_last_string(); - url_query += (coll+"=lt."+conditions); - return *this; - } - Supabase& Supabase::lte(String coll, String conditions){ - _check_last_string(); - url_query += (coll+"=lte."+conditions); - return *this; - } - Supabase& Supabase::neq(String coll, String conditions){ - _check_last_string(); - url_query += (coll+"=neq."+conditions); - return *this; - } - Supabase& Supabase::in(String coll, String conditions){ - _check_last_string(); - url_query += (coll+"=in.("+conditions+")"); - return *this; - } - Supabase& Supabase::is(String coll, String conditions){ - _check_last_string(); - url_query += (coll+"=is."+conditions); - return *this; - } - Supabase& Supabase::cs(String coll, String conditions){ - _check_last_string(); - url_query += (coll+"=cs.{"+conditions+"}"); - return *this; - } - Supabase& Supabase::cd(String coll, String conditions){ - _check_last_string(); - url_query += (coll+"=cd.{"+conditions+"}"); - return *this; - } - Supabase& Supabase::ov(String coll, String conditions){ - _check_last_string(); - url_query += (coll+"=cd.{"+conditions+"}"); - return *this; - } - Supabase& Supabase::sl(String coll, String conditions){ - _check_last_string(); - url_query += (coll+"=sl.("+conditions+")"); - return *this; - } - Supabase& Supabase::sr(String coll, String conditions){ - _check_last_string(); - url_query += (coll+"=sr.("+conditions+")"); - return *this; - } - Supabase& Supabase::nxr(String coll, String conditions){ - _check_last_string(); - url_query += (coll+"=nxr.("+conditions+")"); - return *this; - } - Supabase& Supabase::nxl(String coll, String conditions){ - _check_last_string(); - url_query += (coll+"=nxl.("+conditions+")"); - return *this; - } - Supabase& Supabase::adj(String coll, String conditions){ - _check_last_string(); - url_query += (coll+"=adj.("+conditions+")"); - return *this; - } - // Supabase& Supabase::logic(String mylogic){ - // url_query += (mylogic); - // } +// Comparison Operator +Supabase &Supabase::eq(String coll, String conditions) +{ + _check_last_string(); + url_query += (coll + "=eq." + conditions); + return *this; +} +Supabase &Supabase::gt(String coll, String conditions) +{ + _check_last_string(); + url_query += (coll + "=gt." + conditions); + return *this; +} +Supabase &Supabase::gte(String coll, String conditions) +{ + _check_last_string(); + url_query += (coll + "=gte." + conditions); + return *this; +} +Supabase &Supabase::lt(String coll, String conditions) +{ + _check_last_string(); + url_query += (coll + "=lt." + conditions); + return *this; +} +Supabase &Supabase::lte(String coll, String conditions) +{ + _check_last_string(); + url_query += (coll + "=lte." + conditions); + return *this; +} +Supabase &Supabase::neq(String coll, String conditions) +{ + _check_last_string(); + url_query += (coll + "=neq." + conditions); + return *this; +} +Supabase &Supabase::in(String coll, String conditions) +{ + _check_last_string(); + url_query += (coll + "=in.(" + conditions + ")"); + return *this; +} +Supabase &Supabase::is(String coll, String conditions) +{ + _check_last_string(); + url_query += (coll + "=is." + conditions); + return *this; +} +Supabase &Supabase::cs(String coll, String conditions) +{ + _check_last_string(); + url_query += (coll + "=cs.{" + conditions + "}"); + return *this; +} +Supabase &Supabase::cd(String coll, String conditions) +{ + _check_last_string(); + url_query += (coll + "=cd.{" + conditions + "}"); + return *this; +} +Supabase &Supabase::ov(String coll, String conditions) +{ + _check_last_string(); + url_query += (coll + "=cd.{" + conditions + "}"); + return *this; +} +Supabase &Supabase::sl(String coll, String conditions) +{ + _check_last_string(); + url_query += (coll + "=sl.(" + conditions + ")"); + return *this; +} +Supabase &Supabase::sr(String coll, String conditions) +{ + _check_last_string(); + url_query += (coll + "=sr.(" + conditions + ")"); + return *this; +} +Supabase &Supabase::nxr(String coll, String conditions) +{ + _check_last_string(); + url_query += (coll + "=nxr.(" + conditions + ")"); + return *this; +} +Supabase &Supabase::nxl(String coll, String conditions) +{ + _check_last_string(); + url_query += (coll + "=nxl.(" + conditions + ")"); + return *this; +} +Supabase &Supabase::adj(String coll, String conditions) +{ + _check_last_string(); + url_query += (coll + "=adj.(" + conditions + ")"); + return *this; +} +// Supabase& Supabase::logic(String mylogic){ +// url_query += (mylogic); +// } +// Ordering +Supabase &Supabase::order(String coll, String by, bool nulls = 1) +{ + String subq[] = {"nullsfirst", "nullslast"}; + _check_last_string(); + url_query += ("order=" + coll + "." + by + "." + subq[(int)nulls]); + return *this; +} +Supabase &Supabase::limit(unsigned int by) +{ + _check_last_string(); + url_query += ("limit=" + String(by)); + return *this; +} +Supabase &Supabase::offset(int by) +{ + _check_last_string(); + url_query += ("offset=" + String(by)); + return *this; +} +// do select. execute this after building your query +String Supabase::doSelect() +{ + https.begin(client, hostname + "/rest/v1/" + url_query); + https.addHeader("apikey", key); + https.addHeader("Content-Type", "application/json"); - // Ordering - Supabase& Supabase::order(String coll, String by, bool nulls=1){ - String subq[] = {"nullsfirst","nullslast"}; - _check_last_string(); - url_query += ("order="+coll+"."+by+"."+subq[(int)nulls]); - return *this; + if (useAuth) + { + unsigned long t_now = millis(); + if (t_now - loginTime >= 60 * 60 * 1000) + { + _login_process(); + } + https.addHeader("Authorization", "Bearer " + USER_TOKEN); } - Supabase& Supabase::limit(unsigned int by){ - _check_last_string(); - url_query += ("limit="+String(by)); - return *this; + + int httpCode = 0; + while (httpCode <= 0) + { + httpCode = https.GET(); } - Supabase& Supabase::offset(int by){ - _check_last_string(); - url_query += ("offset="+String(by)); - return *this; + + if (httpCode > 0) + { + data = https.getString(); } - // do select. execute this after building your query - String Supabase::doSelect(){ - https.begin(client, hostname +"/rest/v1/"+ url_query); + https.end(); + urlQuery_reset(); + return data; +} +// do update. execute this after querying your update +int Supabase::doUpdate(String json) +{ + int httpCode; + if (https.begin(client, hostname + "/rest/v1/" + url_query)) + { https.addHeader("apikey", key); https.addHeader("Content-Type", "application/json"); - - if (useAuth){ + if (useAuth) + { unsigned long t_now = millis(); - if (t_now - loginTime >= 60 * 60 * 1000) + if (t_now - loginTime >= 3500000) { _login_process(); } https.addHeader("Authorization", "Bearer " + USER_TOKEN); } - - int httpCode = 0; - while (httpCode <= 0){ - httpCode = https.GET(); - } - - if (httpCode > 0){ - data = https.getString(); - } + httpCode = https.PATCH(json); https.end(); - urlQuery_reset(); - return data; } - // do update. execute this after querying your update - int Supabase::doUpdate(String json){ - int httpCode; - if (https.begin(client, hostname + "/rest/v1/" + url_query)){ - https.addHeader("apikey", key); - https.addHeader("Content-Type", "application/json"); - if (useAuth){ - unsigned long t_now = millis(); - if (t_now - loginTime >= 3500000){ - _login_process(); - } - https.addHeader("Authorization", "Bearer " + USER_TOKEN); - } - httpCode = https.PATCH(json); - https.end(); - }else{ - return -100; - } - urlQuery_reset(); - return httpCode; + else + { + return -100; } + urlQuery_reset(); + return httpCode; +} - +int Supabase::login_email(String email_a, String password_a) +{ + useAuth = true; + loginMethod = "email"; + phone_or_email = email_a; + password = password_a; - int Supabase::login_email(String email_a, String password_a) + int httpCode = 0; + while (httpCode <= 0) { - useAuth = true; - loginMethod = "email"; - phone_or_email = email_a; - password = password_a; - - int httpCode = 0; - while (httpCode <= 0) - { - httpCode = _login_process(); - } - return httpCode; + httpCode = _login_process(); } + return httpCode; +} - int Supabase::login_phone(String phone_a, String password_a) - { - useAuth = true; - loginMethod = "phone"; - phone_or_email = phone_a; - password = password_a; +int Supabase::login_phone(String phone_a, String password_a) +{ + useAuth = true; + loginMethod = "phone"; + phone_or_email = phone_a; + password = password_a; - int httpCode = 0; - while (httpCode <= 0) - { - httpCode = _login_process(); - } - return httpCode; - } \ No newline at end of file + int httpCode = 0; + while (httpCode <= 0) + { + httpCode = _login_process(); + } + return httpCode; +} \ No newline at end of file