@expo/entity-database-adapter-knex 0.55.0 → 0.57.0

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/build/src/AuthorizationResultBasedKnexEntityLoader.d.ts +279 -0
  2. package/build/src/AuthorizationResultBasedKnexEntityLoader.js +127 -0
  3. package/build/src/AuthorizationResultBasedKnexEntityLoader.js.map +1 -0
  4. package/build/src/BasePostgresEntityDatabaseAdapter.d.ts +150 -0
  5. package/build/src/BasePostgresEntityDatabaseAdapter.js +119 -0
  6. package/build/src/BasePostgresEntityDatabaseAdapter.js.map +1 -0
  7. package/build/src/BaseSQLQueryBuilder.d.ts +61 -0
  8. package/build/src/BaseSQLQueryBuilder.js +87 -0
  9. package/build/src/BaseSQLQueryBuilder.js.map +1 -0
  10. package/build/src/EnforcingKnexEntityLoader.d.ts +124 -0
  11. package/build/src/EnforcingKnexEntityLoader.js +166 -0
  12. package/build/src/EnforcingKnexEntityLoader.js.map +1 -0
  13. package/build/src/KnexEntityLoaderFactory.d.ts +25 -0
  14. package/build/src/KnexEntityLoaderFactory.js +39 -0
  15. package/build/src/KnexEntityLoaderFactory.js.map +1 -0
  16. package/build/src/PaginationStrategy.d.ts +30 -0
  17. package/build/src/PaginationStrategy.js +35 -0
  18. package/build/src/PaginationStrategy.js.map +1 -0
  19. package/build/src/PostgresEntity.d.ts +25 -0
  20. package/build/src/PostgresEntity.js +39 -0
  21. package/build/src/PostgresEntity.js.map +1 -0
  22. package/build/src/PostgresEntityDatabaseAdapter.d.ts +12 -5
  23. package/build/src/PostgresEntityDatabaseAdapter.js +32 -11
  24. package/build/src/PostgresEntityDatabaseAdapter.js.map +1 -1
  25. package/build/src/PostgresEntityDatabaseAdapterProvider.d.ts +9 -0
  26. package/build/src/PostgresEntityDatabaseAdapterProvider.js +5 -1
  27. package/build/src/PostgresEntityDatabaseAdapterProvider.js.map +1 -1
  28. package/build/src/ReadonlyPostgresEntity.d.ts +25 -0
  29. package/build/src/ReadonlyPostgresEntity.js +39 -0
  30. package/build/src/ReadonlyPostgresEntity.js.map +1 -0
  31. package/build/src/SQLOperator.d.ts +261 -0
  32. package/build/src/SQLOperator.js +464 -0
  33. package/build/src/SQLOperator.js.map +1 -0
  34. package/build/src/index.d.ts +15 -0
  35. package/build/src/index.js +15 -0
  36. package/build/src/index.js.map +1 -1
  37. package/build/src/internal/EntityKnexDataManager.d.ts +147 -0
  38. package/build/src/internal/EntityKnexDataManager.js +453 -0
  39. package/build/src/internal/EntityKnexDataManager.js.map +1 -0
  40. package/build/src/internal/getKnexDataManager.d.ts +3 -0
  41. package/build/src/internal/getKnexDataManager.js +19 -0
  42. package/build/src/internal/getKnexDataManager.js.map +1 -0
  43. package/build/src/internal/getKnexEntityLoaderFactory.d.ts +3 -0
  44. package/build/src/internal/getKnexEntityLoaderFactory.js +11 -0
  45. package/build/src/internal/getKnexEntityLoaderFactory.js.map +1 -0
  46. package/build/src/internal/utilityTypes.d.ts +5 -0
  47. package/build/src/internal/utilityTypes.js +5 -0
  48. package/build/src/internal/utilityTypes.js.map +1 -0
  49. package/build/src/internal/weakMaps.d.ts +9 -0
  50. package/build/src/internal/weakMaps.js +20 -0
  51. package/build/src/internal/weakMaps.js.map +1 -0
  52. package/build/src/knexLoader.d.ts +18 -0
  53. package/build/src/knexLoader.js +31 -0
  54. package/build/src/knexLoader.js.map +1 -0
  55. package/package.json +6 -5
  56. package/src/AuthorizationResultBasedKnexEntityLoader.ts +538 -0
  57. package/src/BasePostgresEntityDatabaseAdapter.ts +317 -0
  58. package/src/BaseSQLQueryBuilder.ts +114 -0
  59. package/src/EnforcingKnexEntityLoader.ts +271 -0
  60. package/src/KnexEntityLoaderFactory.ts +130 -0
  61. package/src/PaginationStrategy.ts +32 -0
  62. package/src/PostgresEntity.ts +118 -0
  63. package/src/PostgresEntityDatabaseAdapter.ts +78 -24
  64. package/src/PostgresEntityDatabaseAdapterProvider.ts +11 -1
  65. package/src/ReadonlyPostgresEntity.ts +115 -0
  66. package/src/SQLOperator.ts +603 -0
  67. package/src/__integration-tests__/EntityCreationUtils-test.ts +25 -31
  68. package/src/__integration-tests__/PostgresEntityIntegration-test.ts +3192 -330
  69. package/src/__integration-tests__/PostgresEntityQueryContextProvider-test.ts +7 -7
  70. package/src/__testfixtures__/PostgresTestEntity.ts +17 -3
  71. package/src/__tests__/AuthorizationResultBasedKnexEntityLoader-test.ts +1167 -0
  72. package/src/__tests__/BasePostgresEntityDatabaseAdapter-test.ts +160 -0
  73. package/src/__tests__/EnforcingKnexEntityLoader-test.ts +384 -0
  74. package/src/__tests__/EntityFields-test.ts +1 -1
  75. package/src/__tests__/PostgresEntity-test.ts +172 -0
  76. package/src/__tests__/ReadonlyEntity-test.ts +32 -0
  77. package/src/__tests__/SQLOperator-test.ts +831 -0
  78. package/src/__tests__/fixtures/StubPostgresDatabaseAdapter.ts +302 -0
  79. package/src/__tests__/fixtures/StubPostgresDatabaseAdapterProvider.ts +17 -0
  80. package/src/__tests__/fixtures/TestEntity.ts +131 -0
  81. package/src/__tests__/fixtures/TestPaginationEntity.ts +107 -0
  82. package/src/__tests__/fixtures/createUnitTestPostgresEntityCompanionProvider.ts +42 -0
  83. package/src/index.ts +15 -0
  84. package/src/internal/EntityKnexDataManager.ts +832 -0
  85. package/src/internal/__tests__/EntityKnexDataManager-test.ts +378 -0
  86. package/src/internal/__tests__/weakMaps-test.ts +25 -0
  87. package/src/internal/getKnexDataManager.ts +43 -0
  88. package/src/internal/getKnexEntityLoaderFactory.ts +60 -0
  89. package/src/internal/utilityTypes.ts +11 -0
  90. package/src/internal/weakMaps.ts +19 -0
  91. package/src/knexLoader.ts +110 -0
@@ -0,0 +1,261 @@
1
+ /**
2
+ * Supported SQL value types that can be safely parameterized.
3
+ * This ensures type safety and prevents passing unsupported types to SQL queries.
4
+ */
5
+ export type SupportedSQLValue = string | number | boolean | null | Date | Buffer | bigint | undefined | readonly SupportedSQLValue[] | Readonly<{
6
+ [key: string]: unknown;
7
+ }>;
8
+ /**
9
+ * Types of bindings that can be used in SQL queries.
10
+ */
11
+ export type SQLBinding<TFields extends Record<string, any>> = {
12
+ type: 'value';
13
+ value: SupportedSQLValue;
14
+ } | {
15
+ type: 'identifier';
16
+ name: string;
17
+ } | {
18
+ type: 'entityField';
19
+ fieldName: keyof TFields;
20
+ };
21
+ /**
22
+ * SQL Fragment class that safely handles parameterized queries.
23
+ */
24
+ export declare class SQLFragment<TFields extends Record<string, any>> {
25
+ readonly sql: string;
26
+ readonly bindings: readonly SQLBinding<TFields>[];
27
+ constructor(sql: string, bindings: readonly SQLBinding<TFields>[]);
28
+ /**
29
+ * Get bindings in the format expected by Knex.
30
+ * Knex expects a flat array where both identifiers and values are mixed in order.
31
+ *
32
+ * @param getColumnForField - function that resolves an entity field name to its database column name
33
+ */
34
+ getKnexBindings(getColumnForField: (fieldName: keyof TFields) => string): readonly (string | SupportedSQLValue)[];
35
+ /**
36
+ * Combine SQL fragments
37
+ */
38
+ append(other: SQLFragment<TFields>): SQLFragment<TFields>;
39
+ /**
40
+ * Join multiple SQL fragments with a comma separator.
41
+ * Useful for combining column lists, value lists, etc.
42
+ *
43
+ * @param fragments - Array of SQL fragments to join
44
+ * @returns - A new SQLFragment with the fragments joined by a comma and space
45
+ */
46
+ static joinWithCommaSeparator<TFields extends Record<string, any>>(...fragments: readonly SQLFragment<TFields>[]): SQLFragment<TFields>;
47
+ /**
48
+ * Concatenate multiple SQL fragments with space separator.
49
+ * Useful for combining SQL clauses like WHERE, ORDER BY, etc.
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * const where = sql`WHERE age > ${18}`;
54
+ * const orderBy = sql`ORDER BY name`;
55
+ * const query = SQLFragment.concat(sql`SELECT * FROM users`, where, orderBy);
56
+ * // Generates: "SELECT * FROM users WHERE age > ? ORDER BY name"
57
+ * ```
58
+ */
59
+ static concat<TFields extends Record<string, any>>(...fragments: readonly SQLFragment<TFields>[]): SQLFragment<TFields>;
60
+ /**
61
+ * Get a debug representation of the query with values inline
62
+ * WARNING: This is for debugging only. Never execute the returned string directly.
63
+ */
64
+ getDebugString(): string;
65
+ /**
66
+ * Format a value for debug output based on its type.
67
+ * Handles all SupportedSQLValue types.
68
+ */
69
+ private static formatDebugValue;
70
+ private static isPlainObjectForDebug;
71
+ }
72
+ /**
73
+ * Helper for SQL identifiers (table/column names).
74
+ * Stores the raw identifier name to be escaped by Knex using ?? placeholder.
75
+ */
76
+ export declare class SQLIdentifier {
77
+ readonly name: string;
78
+ constructor(name: string);
79
+ }
80
+ /**
81
+ * Helper for referencing entity fields that can be used in SQL queries. This allows for type-safe references to fields of an entity
82
+ * and does automatic translation to DB field names.
83
+ */
84
+ export declare class SQLEntityField<TFields extends Record<string, any>> {
85
+ readonly fieldName: keyof TFields;
86
+ constructor(fieldName: keyof TFields);
87
+ }
88
+ /**
89
+ * Helper for raw SQL that should not be parameterized
90
+ * WARNING: Only use this with trusted input to avoid SQL injection
91
+ */
92
+ export declare class SQLUnsafeRaw {
93
+ readonly rawSql: string;
94
+ constructor(rawSql: string);
95
+ }
96
+ /**
97
+ * Create a SQL identifier (table/column name) that will be escaped by Knex using ??.
98
+ *
99
+ * @example
100
+ * ```ts
101
+ * identifier('users') // Will be escaped as "users" in PostgreSQL
102
+ * identifier('my"table') // Will be escaped as "my""table" in PostgreSQL
103
+ * identifier('column"; DROP TABLE users; --') // Will be safely escaped
104
+ * ```
105
+ */
106
+ export declare function identifier(name: string): SQLIdentifier;
107
+ /**
108
+ * Create a reference to an entity field that can be used in SQL queries. This allows for type-safe references to fields of an entity
109
+ * and does automatic translation to DB field names and will be escaped by Knex using ??.
110
+ *
111
+ * @param fieldName - The entity field name to reference.
112
+ */
113
+ export declare function entityField<TFields extends Record<string, any>>(fieldName: keyof TFields): SQLEntityField<TFields>;
114
+ /**
115
+ * Insert raw SQL that will not be parameterized
116
+ * WARNING: This bypasses SQL injection protection. Only use with trusted input.
117
+ *
118
+ * @example
119
+ * ```ts
120
+ * // Dynamic column names
121
+ * const sortColumn = 'created_at';
122
+ * const query = sql`ORDER BY ${unsafeRaw(sortColumn)} DESC`;
123
+ *
124
+ * // Dynamic SQL expressions
125
+ * const query = sql`WHERE ${unsafeRaw('EXTRACT(year FROM created_at)')} = ${2024}`;
126
+ * ```
127
+ */
128
+ export declare function unsafeRaw(sqlString: string): SQLUnsafeRaw;
129
+ /**
130
+ * Tagged template literal function for SQL queries
131
+ *
132
+ * @example
133
+ * ```ts
134
+ * const age = 18;
135
+ * const query = sql`age >= ${age} AND status = ${'active'}`;
136
+ * ```
137
+ */
138
+ export declare function sql<TFields extends Record<string, any>>(strings: TemplateStringsArray, ...values: readonly (SupportedSQLValue | SQLFragment<TFields> | SQLIdentifier | SQLUnsafeRaw | SQLEntityField<TFields>)[]): SQLFragment<TFields>;
139
+ type PickSupportedSQLValueKeys<T> = {
140
+ [K in keyof T]: T[K] extends SupportedSQLValue ? K : never;
141
+ }[keyof T];
142
+ /**
143
+ * Common SQL helper functions for building queries
144
+ */
145
+ export declare const SQLFragmentHelpers: {
146
+ /**
147
+ * IN clause helper
148
+ *
149
+ * @example
150
+ * ```ts
151
+ * const query = SQLFragmentHelpers.inArray<MyFields, 'id'>('status', ['active', 'pending']);
152
+ * // Generates: ?? IN (?, ?) with entityField binding for 'status' and value bindings
153
+ * ```
154
+ */
155
+ inArray<TFields extends Record<string, any>, N extends PickSupportedSQLValueKeys<TFields>>(fieldName: N, values: readonly TFields[N][]): SQLFragment<TFields>;
156
+ /**
157
+ * NOT IN clause helper
158
+ */
159
+ notInArray<TFields extends Record<string, any>, N extends PickSupportedSQLValueKeys<TFields>>(fieldName: N, values: readonly TFields[N][]): SQLFragment<TFields>;
160
+ /**
161
+ * BETWEEN helper
162
+ *
163
+ * @example
164
+ * ```ts
165
+ * const query = SQLFragmentHelpers.between<MyFields, 'id'>('age', 18, 65);
166
+ * // Generates: ?? BETWEEN ? AND ? with entityField binding for 'age' and value bindings
167
+ * ```
168
+ */
169
+ between<TFields extends Record<string, any>, N extends PickSupportedSQLValueKeys<TFields>>(fieldName: N, min: TFields[N], max: TFields[N]): SQLFragment<TFields>;
170
+ /**
171
+ * NOT BETWEEN helper
172
+ */
173
+ notBetween<TFields extends Record<string, any>, N extends PickSupportedSQLValueKeys<TFields>>(fieldName: N, min: TFields[N], max: TFields[N]): SQLFragment<TFields>;
174
+ /**
175
+ * LIKE helper with automatic escaping
176
+ *
177
+ * @example
178
+ * ```ts
179
+ * const query = SQLFragmentHelpers.like<MyFields, 'id'>('name', '%John%');
180
+ * // Generates: ?? LIKE ? with entityField binding for 'name' and value binding
181
+ * ```
182
+ */
183
+ like<TFields extends Record<string, any>>(fieldName: keyof TFields, pattern: string): SQLFragment<TFields>;
184
+ /**
185
+ * NOT LIKE helper
186
+ */
187
+ notLike<TFields extends Record<string, any>>(fieldName: keyof TFields, pattern: string): SQLFragment<TFields>;
188
+ /**
189
+ * ILIKE helper for case-insensitive matching
190
+ */
191
+ ilike<TFields extends Record<string, any>>(fieldName: keyof TFields, pattern: string): SQLFragment<TFields>;
192
+ /**
193
+ * NOT ILIKE helper for case-insensitive non-matching
194
+ */
195
+ notIlike<TFields extends Record<string, any>>(fieldName: keyof TFields, pattern: string): SQLFragment<TFields>;
196
+ /**
197
+ * NULL check helper
198
+ */
199
+ isNull<TFields extends Record<string, any>>(fieldName: keyof TFields): SQLFragment<TFields>;
200
+ /**
201
+ * NOT NULL check helper
202
+ */
203
+ isNotNull<TFields extends Record<string, any>>(fieldName: keyof TFields): SQLFragment<TFields>;
204
+ /**
205
+ * Single-equals-equality operator
206
+ */
207
+ eq<TFields extends Record<string, any>, N extends PickSupportedSQLValueKeys<TFields>>(fieldName: N, value: TFields[N]): SQLFragment<TFields>;
208
+ /**
209
+ * Single-equals-inequality operator
210
+ */
211
+ neq<TFields extends Record<string, any>, N extends PickSupportedSQLValueKeys<TFields>>(fieldName: N, value: TFields[N]): SQLFragment<TFields>;
212
+ /**
213
+ * Greater-than comparison operator
214
+ */
215
+ gt<TFields extends Record<string, any>, N extends PickSupportedSQLValueKeys<TFields>>(fieldName: N, value: TFields[N]): SQLFragment<TFields>;
216
+ /**
217
+ * Greater-than-or-equal-to comparison operator
218
+ */
219
+ gte<TFields extends Record<string, any>, N extends PickSupportedSQLValueKeys<TFields>>(fieldName: N, value: TFields[N]): SQLFragment<TFields>;
220
+ /**
221
+ * Less-than comparison operator
222
+ */
223
+ lt<TFields extends Record<string, any>, N extends PickSupportedSQLValueKeys<TFields>>(fieldName: N, value: TFields[N]): SQLFragment<TFields>;
224
+ /**
225
+ * Less-than-or-equal-to comparison operator
226
+ */
227
+ lte<TFields extends Record<string, any>, N extends PickSupportedSQLValueKeys<TFields>>(fieldName: N, value: TFields[N]): SQLFragment<TFields>;
228
+ /**
229
+ * JSON contains operator (\@\>)
230
+ */
231
+ jsonContains<TFields extends Record<string, any>>(fieldName: keyof TFields, value: unknown): SQLFragment<TFields>;
232
+ /**
233
+ * JSON contained by operator (\<\@\)
234
+ */
235
+ jsonContainedBy<TFields extends Record<string, any>>(fieldName: keyof TFields, value: unknown): SQLFragment<TFields>;
236
+ /**
237
+ * JSON path extraction helper (-\>)
238
+ */
239
+ jsonPath<TFields extends Record<string, any>>(fieldName: keyof TFields, path: string): SQLFragment<TFields>;
240
+ /**
241
+ * JSON path text extraction helper (-\>\>)
242
+ */
243
+ jsonPathText<TFields extends Record<string, any>>(fieldName: keyof TFields, path: string): SQLFragment<TFields>;
244
+ /**
245
+ * Logical AND of multiple fragments
246
+ */
247
+ and<TFields extends Record<string, any>>(...conditions: readonly SQLFragment<TFields>[]): SQLFragment<TFields>;
248
+ /**
249
+ * Logical OR of multiple fragments
250
+ */
251
+ or<TFields extends Record<string, any>>(...conditions: readonly SQLFragment<TFields>[]): SQLFragment<TFields>;
252
+ /**
253
+ * Logical NOT of a fragment
254
+ */
255
+ not<TFields extends Record<string, any>>(condition: SQLFragment<TFields>): SQLFragment<TFields>;
256
+ /**
257
+ * Parentheses helper for grouping conditions
258
+ */
259
+ group<TFields extends Record<string, any>>(condition: SQLFragment<TFields>): SQLFragment<TFields>;
260
+ };
261
+ export {};