From 530a5cbcc5cf106541042568d7733b517717ae59 Mon Sep 17 00:00:00 2001
From: Denis Kruschinski <4989565+kruschid@users.noreply.github.com>
Date: Sat, 28 Dec 2024 09:03:45 +0100
Subject: [PATCH] v0.3.1 fixes startswith/endswith operators

---
 lib/package.json   |  2 +-
 lib/src/filters.ts |  8 ++++----
 lib/src/test.ts    | 48 +++++++++++++++++++++++-----------------------
 3 files changed, 29 insertions(+), 29 deletions(-)

diff --git a/lib/package.json b/lib/package.json
index 1b96c46..5546d48 100644
--- a/lib/package.json
+++ b/lib/package.json
@@ -1,7 +1,7 @@
 {
   "name": "refine-pocketbase",
   "description": "PocketBase auth, data & live providers for Refine",
-  "version": "0.3.0",
+  "version": "0.3.1",
   "author": "Denis Kruschinski <4989565+kruschid@users.noreply.github.com>",
   "source": "src/index.ts",
   "module": "build/index.mjs",
diff --git a/lib/src/filters.ts b/lib/src/filters.ts
index 010f5d5..698efe5 100644
--- a/lib/src/filters.ts
+++ b/lib/src/filters.ts
@@ -88,15 +88,15 @@ const logicalOperators: Record<
   nnull: ({ field, value }: TypedLogicalFilter<boolean>) =>
     value === true ? `${field} != null` : `${field} = null`,
   startswith: ({ field, value }: TypedLogicalFilter<string>) =>
-    `${field} = '${escape(value)}%'`,
+    `${field} ~ '${escape(value)}%'`,
   nstartswith: ({ field, value }: TypedLogicalFilter<string>) =>
-    `${field} != '${escape(value)}%'`,
+    `${field} !~ '${escape(value)}%'`,
   startswiths: undefined,
   nstartswiths: undefined,
   endswith: ({ field, value }: TypedLogicalFilter<string>) =>
-    `${field} = '%${escape(value)}'`,
+    `${field} ~ '%${escape(value)}'`,
   nendswith: ({ field, value }: TypedLogicalFilter<string>) =>
-    `${field} != '%${escape(value)}'`,
+    `${field} !~ '%${escape(value)}'`,
   endswiths: undefined,
   nendswiths: undefined,
 };
diff --git a/lib/src/test.ts b/lib/src/test.ts
index 7341c67..a6a390b 100644
--- a/lib/src/test.ts
+++ b/lib/src/test.ts
@@ -79,14 +79,14 @@ test("logical filters", (t) => {
     ["null", false, "(a != null)"],
     ["nnull", true, "(a != null)"],
     ["nnull", false, "(a = null)"],
-    ["startswith", "a", "(a = 'a%')"],
-    ["startswith", "%b", "(a = '\\%b%')"],
-    ["nstartswith", "c", "(a != 'c%')"],
-    ["nstartswith", "%d", "(a != '\\%d%')"],
-    ["endswith", "e", "(a = '%e')"],
-    ["endswith", "f%", "(a = '%f\\%')"],
-    ["nendswith", "g", "(a != '%g')"],
-    ["nendswith", "h%", "(a != '%h\\%')"],
+    ["startswith", "a", "(a ~ 'a%')"],
+    ["startswith", "%b", "(a ~ '\\%b%')"],
+    ["nstartswith", "c", "(a !~ 'c%')"],
+    ["nstartswith", "%d", "(a !~ '\\%d%')"],
+    ["endswith", "e", "(a ~ '%e')"],
+    ["endswith", "f%", "(a ~ '%f\\%')"],
+    ["nendswith", "g", "(a !~ '%g')"],
+    ["nendswith", "h%", "(a !~ '%h\\%')"],
   ]).forEach(([operator, value, output]) =>
     t.equals(
       transformFilter([
@@ -132,14 +132,14 @@ test("nested logical filters", (t) => {
     ["null", false, "((a != null) && (b = '4'))"],
     ["nnull", true, "((a != null) && (b = '4'))"],
     ["nnull", false, "((a = null) && (b = '4'))"],
-    ["startswith", "a", "((a = 'a%') && (b = '4'))"],
-    ["startswith", "%b", "((a = '\\%b%') && (b = '4'))"],
-    ["nstartswith", "c", "((a != 'c%') && (b = '4'))"],
-    ["nstartswith", "%d", "((a != '\\%d%') && (b = '4'))"],
-    ["endswith", "e", "((a = '%e') && (b = '4'))"],
-    ["endswith", "f%", "((a = '%f\\%') && (b = '4'))"],
-    ["nendswith", "g", "((a != '%g') && (b = '4'))"],
-    ["nendswith", "h%", "((a != '%h\\%') && (b = '4'))"],
+    ["startswith", "a", "((a ~ 'a%') && (b = '4'))"],
+    ["startswith", "%b", "((a ~ '\\%b%') && (b = '4'))"],
+    ["nstartswith", "c", "((a !~ 'c%') && (b = '4'))"],
+    ["nstartswith", "%d", "((a !~ '\\%d%') && (b = '4'))"],
+    ["endswith", "e", "((a ~ '%e') && (b = '4'))"],
+    ["endswith", "f%", "((a ~ '%f\\%') && (b = '4'))"],
+    ["nendswith", "g", "((a !~ '%g') && (b = '4'))"],
+    ["nendswith", "h%", "((a !~ '%h\\%') && (b = '4'))"],
   ]).forEach(([operator, value, output]) =>
     t.equals(
       transformFilter([
@@ -199,14 +199,14 @@ test("deeply nested logical filters", (t) => {
     ["null", false, "(((a != null) && (b = '4')) || (c > 1))"],
     ["nnull", true, "(((a != null) && (b = '4')) || (c > 1))"],
     ["nnull", false, "(((a = null) && (b = '4')) || (c > 1))"],
-    ["startswith", "a", "(((a = 'a%') && (b = '4')) || (c > 1))"],
-    ["startswith", "%b", "(((a = '\\%b%') && (b = '4')) || (c > 1))"],
-    ["nstartswith", "c", "(((a != 'c%') && (b = '4')) || (c > 1))"],
-    ["nstartswith", "%d", "(((a != '\\%d%') && (b = '4')) || (c > 1))"],
-    ["endswith", "e", "(((a = '%e') && (b = '4')) || (c > 1))"],
-    ["endswith", "f%", "(((a = '%f\\%') && (b = '4')) || (c > 1))"],
-    ["nendswith", "g", "(((a != '%g') && (b = '4')) || (c > 1))"],
-    ["nendswith", "h%", "(((a != '%h\\%') && (b = '4')) || (c > 1))"],
+    ["startswith", "a", "(((a ~ 'a%') && (b = '4')) || (c > 1))"],
+    ["startswith", "%b", "(((a ~ '\\%b%') && (b = '4')) || (c > 1))"],
+    ["nstartswith", "c", "(((a !~ 'c%') && (b = '4')) || (c > 1))"],
+    ["nstartswith", "%d", "(((a !~ '\\%d%') && (b = '4')) || (c > 1))"],
+    ["endswith", "e", "(((a ~ '%e') && (b = '4')) || (c > 1))"],
+    ["endswith", "f%", "(((a ~ '%f\\%') && (b = '4')) || (c > 1))"],
+    ["nendswith", "g", "(((a !~ '%g') && (b = '4')) || (c > 1))"],
+    ["nendswith", "h%", "(((a !~ '%h\\%') && (b = '4')) || (c > 1))"],
   ]).forEach(([operator, value, output]) =>
     t.equals(
       transformFilter([