@wxn0brp/db-string-query 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/.github/workflows/build.yml +18 -0
  2. package/CHANGELOG.md +44 -0
  3. package/bun.lock +118 -0
  4. package/dist/sql/handle/collection.d.ts +4 -0
  5. package/dist/sql/handle/collection.js +26 -0
  6. package/dist/sql/handle/delete.d.ts +1 -0
  7. package/dist/sql/handle/delete.js +11 -0
  8. package/dist/sql/handle/insert.d.ts +1 -0
  9. package/dist/sql/handle/insert.js +52 -0
  10. package/dist/sql/handle/select.d.ts +7 -0
  11. package/dist/sql/handle/select.js +55 -0
  12. package/dist/sql/handle/update.d.ts +1 -0
  13. package/dist/sql/handle/update.js +12 -0
  14. package/dist/sql/index.d.ts +2 -2
  15. package/dist/sql/index.js +10 -4
  16. package/dist/sql/utils/join.util.d.ts +10 -0
  17. package/dist/sql/utils/join.util.js +55 -0
  18. package/dist/sql/where.js +54 -17
  19. package/dist/types.d.ts +5 -1
  20. package/package.json +41 -39
  21. package/src/sql/handle/collection.ts +28 -0
  22. package/src/sql/handle/delete.ts +12 -0
  23. package/src/sql/handle/insert.ts +50 -0
  24. package/src/sql/handle/select.ts +68 -0
  25. package/src/sql/handle/update.ts +13 -0
  26. package/src/sql/index.ts +12 -13
  27. package/src/sql/utils/join.util.ts +73 -0
  28. package/src/sql/where.ts +53 -17
  29. package/src/types.ts +9 -1
  30. package/suglite.json +2 -2
  31. package/test/js.test.ts +103 -0
  32. package/test/sql/delete.test.ts +95 -0
  33. package/test/sql/index.test.ts +17 -0
  34. package/test/sql/insert.test.ts +95 -0
  35. package/test/sql/select.test.ts +92 -0
  36. package/test/sql/update.test.ts +85 -0
  37. package/test/sql/utils/join.test.ts +42 -0
  38. package/test/tsconfig.json +19 -0
  39. package/tsconfig.json +7 -3
  40. package/typedocs-generated/.nojekyll +1 -0
  41. package/typedocs-generated/404.html +76 -0
  42. package/typedocs-generated/assets/hierarchy.js +1 -0
  43. package/typedocs-generated/assets/highlight.css +78 -0
  44. package/typedocs-generated/assets/icons.js +18 -0
  45. package/typedocs-generated/assets/icons.svg +1 -0
  46. package/typedocs-generated/assets/main.js +60 -0
  47. package/typedocs-generated/assets/navigation.js +1 -0
  48. package/typedocs-generated/assets/search.js +1 -0
  49. package/typedocs-generated/assets/style.css +1633 -0
  50. package/typedocs-generated/classes/js.default.html +3 -0
  51. package/typedocs-generated/classes/sql_index.default.html +3 -0
  52. package/typedocs-generated/classes/sql_utils_join.util.JoinToRelationsEngine.html +3 -0
  53. package/typedocs-generated/functions/sql_handle_collection.handleCreate.html +1 -0
  54. package/typedocs-generated/functions/sql_handle_collection.handleDrop.html +1 -0
  55. package/typedocs-generated/functions/sql_handle_collection.handleExists.html +1 -0
  56. package/typedocs-generated/functions/sql_handle_collection.handleGet.html +1 -0
  57. package/typedocs-generated/functions/sql_handle_delete.handleDelete.html +1 -0
  58. package/typedocs-generated/functions/sql_handle_insert.handleInsert.html +1 -0
  59. package/typedocs-generated/functions/sql_handle_select.handleSelect.html +1 -0
  60. package/typedocs-generated/functions/sql_handle_select.parseJoinClauses.html +1 -0
  61. package/typedocs-generated/functions/sql_handle_select.parseSelectClause.html +1 -0
  62. package/typedocs-generated/functions/sql_handle_update.handleUpdate.html +1 -0
  63. package/typedocs-generated/functions/sql_utils.parseReturn.html +1 -0
  64. package/typedocs-generated/functions/sql_utils.parseSet.html +1 -0
  65. package/typedocs-generated/functions/sql_where.parseWhere.html +1 -0
  66. package/typedocs-generated/hierarchy.html +1 -0
  67. package/typedocs-generated/index.html +35 -0
  68. package/typedocs-generated/interfaces/types.Opts.html +3 -0
  69. package/typedocs-generated/interfaces/types.ValtheraParser.html +2 -0
  70. package/typedocs-generated/interfaces/types.ValtheraQuery.html +3 -0
  71. package/typedocs-generated/media/LICENSE +21 -0
  72. package/typedocs-generated/modules/.html +1 -0
  73. package/typedocs-generated/modules/index.html +1 -0
  74. package/typedocs-generated/modules/js.html +1 -0
  75. package/typedocs-generated/modules/sql_handle_collection.html +1 -0
  76. package/typedocs-generated/modules/sql_handle_delete.html +1 -0
  77. package/typedocs-generated/modules/sql_handle_insert.html +1 -0
  78. package/typedocs-generated/modules/sql_handle_select.html +1 -0
  79. package/typedocs-generated/modules/sql_handle_update.html +1 -0
  80. package/typedocs-generated/modules/sql_index.html +1 -0
  81. package/typedocs-generated/modules/sql_utils.html +1 -0
  82. package/typedocs-generated/modules/sql_utils_join.util.html +1 -0
  83. package/typedocs-generated/modules/sql_where.html +1 -0
  84. package/typedocs-generated/modules/types.html +1 -0
  85. package/typedocs-generated/modules.html +1 -0
  86. package/typedocs-generated/types/sql_utils_join.util.JoinClause.html +1 -0
  87. package/typedocs-generated/variables/.ValtheraDbParsers.html +1 -0
  88. package/typedocs-generated/variables/.ValtheraDbRelations.html +1 -0
  89. package/dist/sql/handle.d.ts +0 -12
  90. package/dist/sql/handle.js +0 -119
  91. package/src/sql/handle.ts +0 -129
package/src/sql/where.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  type QueryObject = Record<string, any>;
2
2
 
3
3
  const operators: Record<string, string> = {
4
- "=": "",
4
+ "=": null,
5
5
  "<": "$lt",
6
6
  ">": "$gt",
7
7
  "<=": "$lte",
@@ -11,6 +11,27 @@ const operators: Record<string, string> = {
11
11
  "NOT IN": "$nin"
12
12
  };
13
13
 
14
+ function mergeQueries(target: QueryObject, source: QueryObject) {
15
+ for (const key in source) {
16
+ if (key.startsWith('$')) {
17
+ // If the key is an operator, simply add it
18
+ target[key] = source[key];
19
+ } else {
20
+ // If the key is a field, check if there is not already an operator in the target
21
+ if (target[key] !== undefined) {
22
+ // If target[key] is an object with an operator, merge the objects
23
+ if (typeof target[key] === 'object' && !Array.isArray(target[key])) {
24
+ Object.assign(target[key], source[key]);
25
+ } else {
26
+ target[key] = source[key];
27
+ }
28
+ } else {
29
+ target[key] = source[key];
30
+ }
31
+ }
32
+ }
33
+ }
34
+
14
35
  export function parseWhere(where: string): QueryObject {
15
36
  if (!where) return {};
16
37
  const trimmed = where.trim();
@@ -47,7 +68,7 @@ export function parseWhere(where: string): QueryObject {
47
68
  if (frames.length === 0) {
48
69
  throw new Error("Unmatched closing parenthesis");
49
70
  }
50
- const parentFrame = frames.pop();
71
+ const parentFrame = frames.pop()!;
51
72
  const parentCurrent = parentFrame.current;
52
73
  const parentOperatorStack = parentFrame.operatorStack;
53
74
  const parentStack = parentFrame.stack;
@@ -59,15 +80,21 @@ export function parseWhere(where: string): QueryObject {
59
80
  parentStack.push(parentCurrent);
60
81
  current = groupedQuery;
61
82
  } else if (op === "AND") {
62
- current = { ...parentCurrent, ...groupedQuery };
83
+ const newCurrent: QueryObject = {};
84
+ mergeQueries(newCurrent, parentCurrent);
85
+ mergeQueries(newCurrent, groupedQuery);
86
+ current = newCurrent;
63
87
  }
64
88
  } else {
65
- current = { ...parentCurrent, ...groupedQuery };
89
+ const newCurrent: QueryObject = {};
90
+ mergeQueries(newCurrent, parentCurrent);
91
+ mergeQueries(newCurrent, groupedQuery);
92
+ current = newCurrent;
66
93
  }
67
94
 
68
95
  // Restore parent's stack and operator stack
69
96
  operatorStack = parentFrame.operatorStack;
70
- stack = parentStack;
97
+ stack = parentFrame.stack;
71
98
  } else if (token.toUpperCase() === "AND" || token.toUpperCase() === "OR") {
72
99
  operatorStack.push(token.toUpperCase());
73
100
  } else {
@@ -82,11 +109,14 @@ export function parseWhere(where: string): QueryObject {
82
109
  }
83
110
 
84
111
  let key = token;
85
- let op = tokens[++i]?.trim();
112
+ if (key.includes(".")) {
113
+ key = key.split(".").pop()!;
114
+ }
115
+ let opToken = tokens[++i]?.trim();
86
116
  let value: any = tokens[++i]?.trim();
87
117
 
88
- if (!key || !op || value === undefined) {
89
- throw new Error(`Invalid condition near '${key} ${op} ${value}'`);
118
+ if (!key || !opToken || value === undefined) {
119
+ throw new Error(`Invalid condition near '${key} ${opToken} ${value}'`);
90
120
  }
91
121
 
92
122
  // Handle quoted values
@@ -100,9 +130,9 @@ export function parseWhere(where: string): QueryObject {
100
130
  }
101
131
 
102
132
  // Handle IN and NOT IN operations
103
- if (op.toUpperCase() === "IN" || op.toUpperCase() === "NOT IN") {
133
+ if (opToken.toUpperCase() === "IN" || opToken.toUpperCase() === "NOT IN") {
104
134
  if (!value.startsWith("(") || !value.endsWith(")")) {
105
- throw new Error(`Invalid syntax for '${op}' near '${value}'`);
135
+ throw new Error(`Invalid syntax for '${opToken}' near '${value}'`);
106
136
  }
107
137
  value = value.slice(1, -1).split(",").map((v: string) => {
108
138
  v = v.trim();
@@ -113,16 +143,22 @@ export function parseWhere(where: string): QueryObject {
113
143
  });
114
144
  }
115
145
 
116
- // Map the operator to MongoDB's query operators
117
- const mappedOp = operators[op.toUpperCase()] || "";
118
- if (mappedOp) {
119
- if (mappedOp === "$not") {
120
- current[key] = { [mappedOp]: value };
146
+ // Map the operator to the query operators
147
+ const mappedOp = operators[opToken.toUpperCase()];
148
+
149
+ if (mappedOp !== undefined) {
150
+ if (mappedOp === null) {
151
+ // "=" operator - direct assignment
152
+ current[key] = value;
121
153
  } else {
122
- current[key] = current[key] || {};
123
- (current[key] as Record<string, any>)[mappedOp] = value;
154
+ // Other operators - they go to the root level
155
+ if (!current[mappedOp]) {
156
+ current[mappedOp] = {};
157
+ }
158
+ (current[mappedOp] as Record<string, any>)[key] = value;
124
159
  }
125
160
  } else {
161
+ // If operator not found, default to direct assignment
126
162
  current[key] = value;
127
163
  }
128
164
  }
package/src/types.ts CHANGED
@@ -3,6 +3,14 @@ export interface ValtheraQuery {
3
3
  args: any[];
4
4
  }
5
5
 
6
+ export interface Opts {
7
+ defaultDbKey: string;
8
+ tableDbMap?: Record<string, string>;
9
+ }
10
+
6
11
  export interface ValtheraParser {
7
- parse(query: string): ValtheraQuery;
12
+ parse(
13
+ query: string,
14
+ opts?: Opts,
15
+ ): ValtheraQuery;
8
16
  }
package/suglite.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "cmd": "yarn build && yarn start",
2
+ "cmd": "tsc --noEmit && bun test",
3
3
  "watch": [
4
4
  "src"
5
5
  ],
@@ -7,4 +7,4 @@
7
7
  "events": {
8
8
  "rs": "clear"
9
9
  }
10
- }
10
+ }
@@ -0,0 +1,103 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { ValtheraDbParsers } from "#index";
3
+
4
+ const jsParser = new ValtheraDbParsers.js();
5
+
6
+ describe("JS-like Parser", () => {
7
+ test("should parse a simple method call with arguments", () => {
8
+ const query = "myMethod(1, 'hello')";
9
+ const parsedQuery = jsParser.parse(query);
10
+
11
+ expect(parsedQuery).toBeDefined();
12
+ expect(parsedQuery.method).toBe("myMethod");
13
+ expect(parsedQuery.args).toHaveLength(2);
14
+ expect(parsedQuery.args[0]).toBe(1);
15
+ expect(parsedQuery.args[1]).toBe("hello");
16
+ });
17
+
18
+ test("should parse method call with object argument", () => {
19
+ const query = `find({ id: 1, name: "John" })`;
20
+ const parsedQuery = jsParser.parse(query);
21
+
22
+ expect(parsedQuery).toBeDefined();
23
+ expect(parsedQuery.method).toBe("find");
24
+ expect(parsedQuery.args).toHaveLength(1);
25
+ expect(parsedQuery.args[0]).toEqual({ id: 1, name: "John" });
26
+ });
27
+
28
+ test("should parse method call with array argument", () => {
29
+ const query = `myMethod([1, 2, 3])`;
30
+ const parsedQuery = jsParser.parse(query);
31
+
32
+ expect(parsedQuery).toBeDefined();
33
+ expect(parsedQuery.method).toBe("myMethod");
34
+ expect(parsedQuery.args).toHaveLength(1);
35
+ expect(parsedQuery.args[0]).toEqual([1, 2, 3]);
36
+ });
37
+
38
+ test("should parse method call with nested objects and arrays", () => {
39
+ const query = `complexMethod({ users: [{ id: 1, active: true }], settings: { theme: "dark" } })`;
40
+ const parsedQuery = jsParser.parse(query);
41
+
42
+ expect(parsedQuery).toBeDefined();
43
+ expect(parsedQuery.method).toBe("complexMethod");
44
+ expect(parsedQuery.args).toHaveLength(1);
45
+ expect(parsedQuery.args[0]).toEqual({
46
+ users: [{ id: 1, active: true }],
47
+ settings: { theme: "dark" }
48
+ });
49
+ });
50
+
51
+ test("should parse method call with mixed argument types", () => {
52
+ const query = `mixedArgs(42, "text", true, null, [1, "two", false])`;
53
+ const parsedQuery = jsParser.parse(query);
54
+
55
+ expect(parsedQuery).toBeDefined();
56
+ expect(parsedQuery.method).toBe("mixedArgs");
57
+ expect(parsedQuery.args).toHaveLength(5);
58
+ expect(parsedQuery.args[0]).toBe(42);
59
+ expect(parsedQuery.args[1]).toBe("text");
60
+ expect(parsedQuery.args[2]).toBe(true);
61
+ expect(parsedQuery.args[3]).toBeNull();
62
+ expect(parsedQuery.args[4]).toEqual([1, "two", false]);
63
+ });
64
+
65
+ test("should handle string arguments with quotes", () => {
66
+ const query = `stringMethod("double quotes", 'single quotes')`;
67
+ const parsedQuery = jsParser.parse(query);
68
+
69
+ expect(parsedQuery).toBeDefined();
70
+ expect(parsedQuery.method).toBe("stringMethod");
71
+ expect(parsedQuery.args).toHaveLength(2);
72
+ expect(parsedQuery.args[0]).toBe("double quotes");
73
+ expect(parsedQuery.args[1]).toBe("single quotes");
74
+ });
75
+
76
+ test("should throw error for invalid query syntax", () => {
77
+ const query = "invalidQuery";
78
+
79
+ expect(() => {
80
+ jsParser.parse(query);
81
+ }).toThrow("Invalid query");
82
+ });
83
+
84
+ test("should throw error for malformed arguments", () => {
85
+ const query = "method({unclosed: object";
86
+
87
+ expect(() => {
88
+ jsParser.parse(query);
89
+ }).toThrow();
90
+ });
91
+
92
+ test("should parse floating point numbers correctly", () => {
93
+ const query = `numberMethod(3.14, -2.5, 0.001)`;
94
+ const parsedQuery = jsParser.parse(query);
95
+
96
+ expect(parsedQuery).toBeDefined();
97
+ expect(parsedQuery.method).toBe("numberMethod");
98
+ expect(parsedQuery.args).toHaveLength(3);
99
+ expect(parsedQuery.args[0]).toBe(3.14);
100
+ expect(parsedQuery.args[1]).toBe(-2.5);
101
+ expect(parsedQuery.args[2]).toBe(0.001);
102
+ });
103
+ });
@@ -0,0 +1,95 @@
1
+ import SQLParser from "#sql";
2
+ import { describe, expect, test } from "bun:test";
3
+
4
+ const sqlParser = new SQLParser();
5
+
6
+ describe("SQL Parser - DELETE", () => {
7
+ test("should parse a simple DELETE query", () => {
8
+ const query = "DELETE FROM users WHERE id = 1";
9
+ const parsedQuery = sqlParser.parse(query);
10
+
11
+ expect(parsedQuery).toBeDefined();
12
+ expect(parsedQuery.method).toBe("remove"); // DELETE maps to "remove" method
13
+ expect(parsedQuery.args).toHaveLength(2);
14
+ expect(parsedQuery.args[0]).toBe("users"); // collection name
15
+ expect(parsedQuery.args[1]).toEqual({ id: 1 }); // where clause
16
+ });
17
+
18
+ test("should parse DELETE query without WHERE clause", () => {
19
+ const query = "DELETE FROM users";
20
+ const parsedQuery = sqlParser.parse(query);
21
+
22
+ expect(parsedQuery).toBeDefined();
23
+ expect(parsedQuery.method).toBe("remove");
24
+ expect(parsedQuery.args).toHaveLength(2);
25
+ expect(parsedQuery.args[0]).toBe("users"); // collection name
26
+ expect(parsedQuery.args[1]).toEqual({}); // empty where clause
27
+ });
28
+
29
+ test("should parse DELETE with complex WHERE conditions", () => {
30
+ const query = "DELETE FROM users WHERE age > 65 AND status = 'inactive'";
31
+ const parsedQuery = sqlParser.parse(query);
32
+
33
+ expect(parsedQuery).toBeDefined();
34
+ expect(parsedQuery.method).toBe("remove");
35
+ expect(parsedQuery.args).toHaveLength(2);
36
+ expect(parsedQuery.args[0]).toBe("users"); // collection name
37
+ expect(parsedQuery.args[1]).toEqual({ $gt: { age: 65 }, status: "inactive" }); // where clause
38
+ });
39
+
40
+ test("should parse DELETE with string values", () => {
41
+ const query = "DELETE FROM posts WHERE title = 'Unwanted Post'";
42
+ const parsedQuery = sqlParser.parse(query);
43
+
44
+ expect(parsedQuery).toBeDefined();
45
+ expect(parsedQuery.method).toBe("remove");
46
+ expect(parsedQuery.args).toHaveLength(2);
47
+ expect(parsedQuery.args[0]).toBe("posts"); // collection name
48
+ expect(parsedQuery.args[1]).toEqual({ title: "Unwanted Post" }); // where clause
49
+ });
50
+
51
+ test("should parse DELETE with numeric values", () => {
52
+ const query = "DELETE FROM products WHERE price = 0";
53
+ const parsedQuery = sqlParser.parse(query);
54
+
55
+ expect(parsedQuery).toBeDefined();
56
+ expect(parsedQuery.method).toBe("remove");
57
+ expect(parsedQuery.args).toHaveLength(2);
58
+ expect(parsedQuery.args[0]).toBe("products"); // collection name
59
+ expect(parsedQuery.args[1]).toEqual({ price: 0 }); // where clause
60
+ });
61
+
62
+ test("should throw error for invalid DELETE syntax", () => {
63
+ const query = "DELETE users WHERE id = 1"; // missing FROM keyword
64
+
65
+ expect(() => {
66
+ sqlParser.parse(query);
67
+ }).toThrow("Invalid DELETE syntax");
68
+ });
69
+
70
+ test("should parse DELETE with quoted string values", () => {
71
+ const query = "DELETE FROM users WHERE name = 'John Doe'";
72
+ const parsedQuery = sqlParser.parse(query);
73
+
74
+ expect(parsedQuery).toBeDefined();
75
+ expect(parsedQuery.method).toBe("remove");
76
+ expect(parsedQuery.args).toHaveLength(2);
77
+ expect(parsedQuery.args[0]).toBe("users"); // collection name
78
+ expect(parsedQuery.args[1]).toEqual({ name: "John Doe" }); // where clause
79
+ });
80
+
81
+ test("should handle DELETE with multiple condition types", () => {
82
+ const query = "DELETE FROM orders WHERE total > 1000 AND status = 'cancelled' AND user_id != 5";
83
+ const parsedQuery = sqlParser.parse(query);
84
+
85
+ expect(parsedQuery).toBeDefined();
86
+ expect(parsedQuery.method).toBe("remove");
87
+ expect(parsedQuery.args).toHaveLength(2);
88
+ expect(parsedQuery.args[0]).toBe("orders"); // collection name
89
+ expect(parsedQuery.args[1]).toEqual({
90
+ $gt: { total: 1000 },
91
+ status: "cancelled",
92
+ $not: { user_id: 5 }
93
+ }); // where clause with multiple operators
94
+ });
95
+ });
@@ -0,0 +1,17 @@
1
+ import SQLParser from "#sql";
2
+ import { describe, expect, test } from "bun:test";
3
+
4
+ const sqlParser = new SQLParser();
5
+
6
+ describe("SQL Parser - SELECT", () => {
7
+ test("should parse with ;", () => {
8
+ const query = "SELECT * FROM users WHERE id = 1;";
9
+ const parsedQuery = sqlParser.parse(query);
10
+
11
+ expect(parsedQuery).toBeDefined();
12
+ expect(parsedQuery.method).toBe("find");
13
+ expect(parsedQuery.args).toHaveLength(4);
14
+ expect(parsedQuery.args[0]).toBe("users"); // collection name
15
+ expect(parsedQuery.args[1]).toEqual({ id: 1 }); // where clause
16
+ });
17
+ });
@@ -0,0 +1,95 @@
1
+ import SQLParser from "#sql";
2
+ import { describe, expect, test } from "bun:test";
3
+
4
+ const sqlParser = new SQLParser();
5
+
6
+ describe("SQL Parser - INSERT", () => {
7
+ test("should parse a simple INSERT query", () => {
8
+ const query = "INSERT INTO users (name, email) VALUES ('John', 'john@example.com')";
9
+ const parsedQuery = sqlParser.parse(query);
10
+
11
+ expect(parsedQuery).toBeDefined();
12
+ expect(parsedQuery.method).toBe("add"); // INSERT maps to "add" method
13
+ expect(parsedQuery.args).toHaveLength(2);
14
+ expect(parsedQuery.args[0]).toBe("users"); // collection name
15
+ expect(parsedQuery.args[1]).toEqual({ name: "John", email: "john@example.com" }); // data
16
+ });
17
+
18
+ test("should parse INSERT with numeric values", () => {
19
+ const query = "INSERT INTO products (id, name, price) VALUES (1, 'Laptop', 999.99)";
20
+ const parsedQuery = sqlParser.parse(query);
21
+
22
+ expect(parsedQuery).toBeDefined();
23
+ expect(parsedQuery.method).toBe("add");
24
+ expect(parsedQuery.args).toHaveLength(2);
25
+ expect(parsedQuery.args[0]).toBe("products"); // collection name
26
+ expect(parsedQuery.args[1]).toEqual({ id: 1, name: "Laptop", price: 999.99 }); // data with numeric values
27
+ });
28
+
29
+ test("should parse INSERT with mixed data types", () => {
30
+ const query = "INSERT INTO users (id, name, active, score) VALUES (1, 'Jane', true, 95.5)";
31
+ const parsedQuery = sqlParser.parse(query);
32
+
33
+ expect(parsedQuery).toBeDefined();
34
+ expect(parsedQuery.method).toBe("add");
35
+ expect(parsedQuery.args).toHaveLength(2);
36
+ expect(parsedQuery.args[0]).toBe("users"); // collection name
37
+ expect(parsedQuery.args[1]).toEqual({ id: 1, name: "Jane", active: "true", score: 95.5 });
38
+ });
39
+
40
+ test("should handle INSERT with quoted values containing commas", () => {
41
+ const query = `INSERT INTO posts (title, content) VALUES ('Hello, World!', 'This is a post, with commas.')`;
42
+ const parsedQuery = sqlParser.parse(query);
43
+
44
+ expect(parsedQuery).toBeDefined();
45
+ expect(parsedQuery.method).toBe("add");
46
+ expect(parsedQuery.args).toHaveLength(2);
47
+ expect(parsedQuery.args[0]).toBe("posts"); // collection name
48
+ expect(parsedQuery.args[1]).toEqual({
49
+ title: "Hello, World!",
50
+ content: "This is a post, with commas."
51
+ });
52
+ });
53
+
54
+ test("should throw error for mismatched columns and values", () => {
55
+ const query = "INSERT INTO users (name, email) VALUES ('John')"; // missing value
56
+
57
+ expect(() => {
58
+ sqlParser.parse(query);
59
+ }).toThrow("Number of columns and values does not match");
60
+ });
61
+
62
+ test("should throw error for invalid INSERT syntax", () => {
63
+ const query = "INSERT users (name) VALUES ('John')"; // missing INTO
64
+
65
+ expect(() => {
66
+ sqlParser.parse(query);
67
+ }).toThrow("Invalid INSERT syntax");
68
+ });
69
+
70
+ test("should handle INSERT with double quotes", () => {
71
+ const query = 'INSERT INTO users (name, email) VALUES ("Jane", "jane@example.com")';
72
+ const parsedQuery = sqlParser.parse(query);
73
+
74
+ expect(parsedQuery).toBeDefined();
75
+ expect(parsedQuery.method).toBe("add");
76
+ expect(parsedQuery.args).toHaveLength(2);
77
+ expect(parsedQuery.args[0]).toBe("users"); // collection name
78
+ expect(parsedQuery.args[1]).toEqual({ name: "Jane", email: "jane@example.com" });
79
+ });
80
+
81
+ test("should handle INSERT with mixed quote types", () => {
82
+ const query = `INSERT INTO users (name, email, description) VALUES ("Jane", 'jane@example.com', "User, with 'special' chars")`;
83
+ const parsedQuery = sqlParser.parse(query);
84
+
85
+ expect(parsedQuery).toBeDefined();
86
+ expect(parsedQuery.method).toBe("add");
87
+ expect(parsedQuery.args).toHaveLength(2);
88
+ expect(parsedQuery.args[0]).toBe("users"); // collection name
89
+ expect(parsedQuery.args[1]).toEqual({
90
+ name: "Jane",
91
+ email: "jane@example.com",
92
+ description: "User, with 'special' chars"
93
+ });
94
+ });
95
+ });
@@ -0,0 +1,92 @@
1
+ import SQLParser from "#sql";
2
+ import { describe, expect, test } from "bun:test";
3
+
4
+ const sqlParser = new SQLParser();
5
+
6
+ describe("SQL Parser - SELECT", () => {
7
+ test("should parse a simple SELECT query", () => {
8
+ const query = "SELECT * FROM users WHERE id = 1";
9
+ const parsedQuery = sqlParser.parse(query);
10
+
11
+ expect(parsedQuery).toBeDefined();
12
+ expect(parsedQuery.method).toBe("find");
13
+ expect(parsedQuery.args).toHaveLength(4);
14
+ expect(parsedQuery.args[0]).toBe("users"); // collection name
15
+ expect(parsedQuery.args[1]).toEqual({ id: 1 }); // where clause
16
+ });
17
+
18
+ test("should parse a SELECT query with specific columns", () => {
19
+ const query = "SELECT name, email FROM users WHERE active = 1";
20
+ const parsedQuery = sqlParser.parse(query);
21
+
22
+ expect(parsedQuery).toBeDefined();
23
+ expect(parsedQuery.method).toBe("find");
24
+ expect(parsedQuery.args).toHaveLength(4);
25
+ expect(parsedQuery.args[0]).toBe("users"); // collection name
26
+ expect(parsedQuery.args[1]).toEqual({ active: 1 }); // where clause
27
+ expect(parsedQuery.args[3]).toEqual({ select: ["name", "email"] }); // select options
28
+ });
29
+
30
+ test("should parse a SELECT query without WHERE clause", () => {
31
+ const query = "SELECT * FROM users";
32
+ const parsedQuery = sqlParser.parse(query);
33
+
34
+ expect(parsedQuery).toBeDefined();
35
+ expect(parsedQuery.method).toBe("find");
36
+ expect(parsedQuery.args).toHaveLength(4);
37
+ expect(parsedQuery.args[0]).toBe("users"); // collection name
38
+ expect(parsedQuery.args[1]).toEqual({}); // empty where clause
39
+ });
40
+
41
+ test("should parse a SELECT query with EXCLUDE clause", () => {
42
+ const query = "SELECT * EXCLUDE password, createdAt FROM users WHERE active = 1";
43
+ const parsedQuery = sqlParser.parse(query);
44
+
45
+ expect(parsedQuery).toBeDefined();
46
+ expect(parsedQuery.method).toBe("find");
47
+ expect(parsedQuery.args).toHaveLength(4);
48
+ expect(parsedQuery.args[0]).toBe("users"); // collection name
49
+ expect(parsedQuery.args[1]).toEqual({ active: 1 }); // where clause
50
+ expect(parsedQuery.args[3]).toEqual({ exclude: ["password", "createdAt"] }); // exclude options
51
+ });
52
+
53
+ test("should parse a SELECT query with complex WHERE conditions", () => {
54
+ const query = "SELECT * FROM users WHERE age > 18 AND status = 'active'";
55
+ const parsedQuery = sqlParser.parse(query);
56
+
57
+ expect(parsedQuery).toBeDefined();
58
+ expect(parsedQuery.method).toBe("find");
59
+ expect(parsedQuery.args).toHaveLength(4);
60
+ expect(parsedQuery.args[0]).toBe("users"); // collection name
61
+ expect(parsedQuery.args[1]).toEqual({ $gt: { age: 18 }, status: "active" }); // where clause
62
+ });
63
+
64
+ test("should parse a SELECT query with a JOIN clause", () => {
65
+ const query = "SELECT posts.*, users.name FROM posts JOIN users ON posts.userId = users.id WHERE posts.id = 1";
66
+ const parsedQuery = sqlParser.parse(query, { defaultDbKey: "db" });
67
+
68
+ expect(parsedQuery).toBeDefined();
69
+ expect(parsedQuery.method).toBe("relation-find");
70
+ expect(parsedQuery.args).toHaveLength(4);
71
+ expect(parsedQuery.args[0]).toEqual(["db", "posts"]); // path
72
+ expect(parsedQuery.args[1]).toEqual({ id: 1 }); // where clause
73
+ expect(parsedQuery.args[2]).toEqual({
74
+ users: {
75
+ type: '1n',
76
+ path: ['db', 'posts'],
77
+ pk: 'id',
78
+ fk: 'userId',
79
+ as: 'users'
80
+ }
81
+ }); // relations
82
+ expect(parsedQuery.args[3]).toEqual({ select: ["posts.*", "users.name"] }); // select options
83
+ });
84
+
85
+ test("should throw error for invalid SELECT syntax", () => {
86
+ const query = "SELECT FROM users";
87
+
88
+ expect(() => {
89
+ sqlParser.parse(query);
90
+ }).toThrow();
91
+ });
92
+ });
@@ -0,0 +1,85 @@
1
+ import SQLParser from "#sql";
2
+ import { describe, expect, test } from "bun:test";
3
+
4
+ const sqlParser = new SQLParser();
5
+
6
+ describe("SQL Parser - UPDATE", () => {
7
+ test("should parse a simple UPDATE query", () => {
8
+ const query = "UPDATE users SET name = 'John' WHERE id = 1";
9
+ const parsedQuery = sqlParser.parse(query);
10
+
11
+ expect(parsedQuery).toBeDefined();
12
+ expect(parsedQuery.method).toBe("update"); // UPDATE maps to "update" method
13
+ expect(parsedQuery.args).toHaveLength(3);
14
+ expect(parsedQuery.args[0]).toBe("users"); // collection name
15
+ expect(parsedQuery.args[1]).toEqual({ id: 1 }); // where clause
16
+ expect(parsedQuery.args[2]).toEqual({ name: "John" }); // set clause
17
+ });
18
+
19
+ test("should parse UPDATE with multiple SET fields", () => {
20
+ const query = "UPDATE users SET name = 'Jane', email = 'jane@example.com' WHERE id = 2";
21
+ const parsedQuery = sqlParser.parse(query);
22
+
23
+ expect(parsedQuery).toBeDefined();
24
+ expect(parsedQuery.method).toBe("update");
25
+ expect(parsedQuery.args).toHaveLength(3);
26
+ expect(parsedQuery.args[0]).toBe("users"); // collection name
27
+ expect(parsedQuery.args[1]).toEqual({ id: 2 }); // where clause
28
+ expect(parsedQuery.args[2]).toEqual({ name: "Jane", email: "jane@example.com" }); // set clause
29
+ });
30
+
31
+ test("should parse UPDATE with numeric values", () => {
32
+ const query = "UPDATE products SET price = 29.99, stock = 100 WHERE id = 1";
33
+ const parsedQuery = sqlParser.parse(query);
34
+
35
+ expect(parsedQuery).toBeDefined();
36
+ expect(parsedQuery.method).toBe("update");
37
+ expect(parsedQuery.args).toHaveLength(3);
38
+ expect(parsedQuery.args[0]).toBe("products"); // collection name
39
+ expect(parsedQuery.args[1]).toEqual({ id: 1 }); // where clause
40
+ expect(parsedQuery.args[2]).toEqual({ price: 29.99, stock: 100 }); // set clause
41
+ });
42
+
43
+ test("should parse UPDATE with string values containing spaces", () => {
44
+ const query = "UPDATE posts SET title = 'Hello World Post', content = 'This is the content' WHERE id = 1";
45
+ const parsedQuery = sqlParser.parse(query);
46
+
47
+ expect(parsedQuery).toBeDefined();
48
+ expect(parsedQuery.method).toBe("update");
49
+ expect(parsedQuery.args).toHaveLength(3);
50
+ expect(parsedQuery.args[0]).toBe("posts"); // collection name
51
+ expect(parsedQuery.args[1]).toEqual({ id: 1 }); // where clause
52
+ expect(parsedQuery.args[2]).toEqual({
53
+ title: "Hello World Post",
54
+ content: "This is the content"
55
+ }); // set clause
56
+ });
57
+
58
+ test("should throw error for invalid UPDATE syntax", () => {
59
+ const query = "UPDATE users name = 'John' WHERE id = 1"; // missing SET keyword
60
+
61
+ expect(() => {
62
+ sqlParser.parse(query);
63
+ }).toThrow("Invalid UPDATE syntax");
64
+ });
65
+
66
+ test("should handle UPDATE with complex WHERE conditions", () => {
67
+ const query = "UPDATE users SET status = 'inactive' WHERE age > 65 AND active = 1";
68
+ const parsedQuery = sqlParser.parse(query);
69
+
70
+ expect(parsedQuery).toBeDefined();
71
+ expect(parsedQuery.method).toBe("update");
72
+ expect(parsedQuery.args).toHaveLength(3);
73
+ expect(parsedQuery.args[0]).toBe("users"); // collection name
74
+ expect(parsedQuery.args[1]).toEqual({ $gt: { age: 65 }, active: 1 }); // where clause
75
+ expect(parsedQuery.args[2]).toEqual({ status: "inactive" }); // set clause
76
+ });
77
+
78
+ test("should throw error when there is no WHERE clause", () => {
79
+ const query = "UPDATE users SET name = 'John'"; // missing WHERE
80
+
81
+ expect(() => {
82
+ sqlParser.parse(query);
83
+ }).toThrow("Invalid UPDATE syntax");
84
+ });
85
+ });