@aurabase/aurabase-js 0.3.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 (344) hide show
  1. package/.changeset/config.json +11 -0
  2. package/CHANGELOG.md +30 -0
  3. package/LICENSE +21 -0
  4. package/README.md +833 -0
  5. package/package.json +90 -0
  6. package/packages/ai/LICENSE +21 -0
  7. package/packages/ai/README.md +106 -0
  8. package/packages/ai/dist/index.cjs +229 -0
  9. package/packages/ai/dist/index.d.cts +139 -0
  10. package/packages/ai/dist/index.d.ts +139 -0
  11. package/packages/ai/dist/index.js +226 -0
  12. package/packages/ai/node_modules/.bin/esbuild +19 -0
  13. package/packages/ai/node_modules/.bin/tsc +22 -0
  14. package/packages/ai/node_modules/.bin/tsserver +22 -0
  15. package/packages/ai/node_modules/.bin/tsup +22 -0
  16. package/packages/ai/node_modules/.bin/tsup-node +22 -0
  17. package/packages/ai/node_modules/.bin/vite +22 -0
  18. package/packages/ai/node_modules/.bin/vitest +22 -0
  19. package/packages/ai/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -0
  20. package/packages/ai/package.json +61 -0
  21. package/packages/ai/src/AiService.ts +226 -0
  22. package/packages/ai/src/index.ts +18 -0
  23. package/packages/ai/src/sse.ts +141 -0
  24. package/packages/ai/src/types.ts +30 -0
  25. package/packages/ai/tests/chat-stream.test.ts +151 -0
  26. package/packages/ai/tests/rag-namespaces.test.ts +54 -0
  27. package/packages/ai/tests/sse.test.ts +69 -0
  28. package/packages/ai/tsconfig.json +5 -0
  29. package/packages/ai/tsup.config.ts +11 -0
  30. package/packages/aurabase/LICENSE +21 -0
  31. package/packages/aurabase/README.md +64 -0
  32. package/packages/aurabase/dist/AuraClient-DAtDbfPB.d.cts +77 -0
  33. package/packages/aurabase/dist/AuraClient-DAtDbfPB.d.ts +77 -0
  34. package/packages/aurabase/dist/index.cjs +237 -0
  35. package/packages/aurabase/dist/index.d.cts +46 -0
  36. package/packages/aurabase/dist/index.d.ts +46 -0
  37. package/packages/aurabase/dist/index.js +177 -0
  38. package/packages/aurabase/dist/ssr.cjs +224 -0
  39. package/packages/aurabase/dist/ssr.d.cts +65 -0
  40. package/packages/aurabase/dist/ssr.d.ts +65 -0
  41. package/packages/aurabase/dist/ssr.js +214 -0
  42. package/packages/aurabase/node_modules/.bin/esbuild +19 -0
  43. package/packages/aurabase/node_modules/.bin/tsc +22 -0
  44. package/packages/aurabase/node_modules/.bin/tsserver +22 -0
  45. package/packages/aurabase/node_modules/.bin/tsup +22 -0
  46. package/packages/aurabase/node_modules/.bin/tsup-node +22 -0
  47. package/packages/aurabase/node_modules/.bin/vite +22 -0
  48. package/packages/aurabase/node_modules/.bin/vitest +22 -0
  49. package/packages/aurabase/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -0
  50. package/packages/aurabase/package.json +80 -0
  51. package/packages/aurabase/src/AuraClient.ts +237 -0
  52. package/packages/aurabase/src/index.ts +152 -0
  53. package/packages/aurabase/src/ssr.ts +156 -0
  54. package/packages/aurabase/tests/ai-streaming-rag-e2e.test.ts +82 -0
  55. package/packages/aurabase/tests/ai.test.ts +111 -0
  56. package/packages/aurabase/tests/auth-admin.test.ts +515 -0
  57. package/packages/aurabase/tests/auth-e2e.test.ts +135 -0
  58. package/packages/aurabase/tests/auth-hardening.test.ts +206 -0
  59. package/packages/aurabase/tests/auth.test.ts +947 -0
  60. package/packages/aurabase/tests/database.test.ts +230 -0
  61. package/packages/aurabase/tests/db-types-e2e.test.ts +217 -0
  62. package/packages/aurabase/tests/dispose.test.ts +23 -0
  63. package/packages/aurabase/tests/foreign-token.e2e.test.ts +129 -0
  64. package/packages/aurabase/tests/functions.test.ts +107 -0
  65. package/packages/aurabase/tests/integration.test.ts +413 -0
  66. package/packages/aurabase/tests/live-production-validation.js +241 -0
  67. package/packages/aurabase/tests/live-sdk-production-validation.js +293 -0
  68. package/packages/aurabase/tests/mocks.ts +139 -0
  69. package/packages/aurabase/tests/notifications.test.ts +314 -0
  70. package/packages/aurabase/tests/observability-backup.test.ts +186 -0
  71. package/packages/aurabase/tests/observability-hook.test.ts +130 -0
  72. package/packages/aurabase/tests/production-robustness.test.ts +268 -0
  73. package/packages/aurabase/tests/public-api-surface.test.ts +53 -0
  74. package/packages/aurabase/tests/query-builder.test.ts +329 -0
  75. package/packages/aurabase/tests/readme-smoke-e2e.test.ts +231 -0
  76. package/packages/aurabase/tests/realtime-filter-e2e.test.ts +344 -0
  77. package/packages/aurabase/tests/refresh-locks.test.ts +210 -0
  78. package/packages/aurabase/tests/retry-option.test.ts +17 -0
  79. package/packages/aurabase/tests/rpc-e2e.test.ts +254 -0
  80. package/packages/aurabase/tests/rs256-local-e2e.test.ts +114 -0
  81. package/packages/aurabase/tests/rt-alias-public-e2e.test.ts +235 -0
  82. package/packages/aurabase/tests/rt-http-broadcast-e2e.test.ts +124 -0
  83. package/packages/aurabase/tests/service-role-guard.test.ts +99 -0
  84. package/packages/aurabase/tests/ssr.test.ts +91 -0
  85. package/packages/aurabase/tests/storage-backend-e2e.test.ts +481 -0
  86. package/packages/aurabase/tests/storage-sdk-parity-e2e.test.ts +104 -0
  87. package/packages/aurabase/tests/storage-transform-e2e.test.ts +684 -0
  88. package/packages/aurabase/tests/storage.test.ts +55 -0
  89. package/packages/aurabase/tests/types/client-generic.test-d.ts +79 -0
  90. package/packages/aurabase/tests/types/docs-snippets.test-d.ts +571 -0
  91. package/packages/aurabase/tests/types/error-code.test-d.ts +53 -0
  92. package/packages/aurabase/tsconfig.json +5 -0
  93. package/packages/aurabase/tsconfig.test-d.json +5 -0
  94. package/packages/aurabase/tsup.config.ts +11 -0
  95. package/packages/aurabase/vitest.config.ts +16 -0
  96. package/packages/aurabase/vitest.e2e.config.ts +9 -0
  97. package/packages/auth/LICENSE +21 -0
  98. package/packages/auth/README.md +215 -0
  99. package/packages/auth/dist/index.cjs +1175 -0
  100. package/packages/auth/dist/index.d.cts +397 -0
  101. package/packages/auth/dist/index.d.ts +397 -0
  102. package/packages/auth/dist/index.js +1170 -0
  103. package/packages/auth/node_modules/.bin/esbuild +19 -0
  104. package/packages/auth/node_modules/.bin/tsc +22 -0
  105. package/packages/auth/node_modules/.bin/tsserver +22 -0
  106. package/packages/auth/node_modules/.bin/tsup +22 -0
  107. package/packages/auth/node_modules/.bin/tsup-node +22 -0
  108. package/packages/auth/node_modules/.bin/vite +22 -0
  109. package/packages/auth/node_modules/.bin/vitest +22 -0
  110. package/packages/auth/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -0
  111. package/packages/auth/package.json +61 -0
  112. package/packages/auth/src/AuthAdminService.ts +149 -0
  113. package/packages/auth/src/AuthService.ts +1166 -0
  114. package/packages/auth/src/SessionManager.ts +92 -0
  115. package/packages/auth/src/index.ts +17 -0
  116. package/packages/auth/src/jwks.ts +337 -0
  117. package/packages/auth/src/pkce.ts +27 -0
  118. package/packages/auth/src/types.ts +89 -0
  119. package/packages/auth/tests/auth-lifecycle.test.ts +149 -0
  120. package/packages/auth/tests/detect-session.test.ts +361 -0
  121. package/packages/auth/tests/getclaims.test.ts +780 -0
  122. package/packages/auth/tests/is-mfa-challenge.test.ts +27 -0
  123. package/packages/auth/tests/password-recovery.test.ts +124 -0
  124. package/packages/auth/tests/pkce.test.ts +192 -0
  125. package/packages/auth/tests/session-manager.test.ts +103 -0
  126. package/packages/auth/tests/set-session.test.ts +139 -0
  127. package/packages/auth/tests/verify-otp.test.ts +153 -0
  128. package/packages/auth/tsconfig.json +5 -0
  129. package/packages/auth/tsup.config.ts +11 -0
  130. package/packages/core/LICENSE +21 -0
  131. package/packages/core/README.md +132 -0
  132. package/packages/core/dist/index.cjs +695 -0
  133. package/packages/core/dist/index.d.cts +1137 -0
  134. package/packages/core/dist/index.d.ts +1137 -0
  135. package/packages/core/dist/index.js +673 -0
  136. package/packages/core/node_modules/.bin/esbuild +22 -0
  137. package/packages/core/node_modules/.bin/tsc +22 -0
  138. package/packages/core/node_modules/.bin/tsserver +22 -0
  139. package/packages/core/node_modules/.bin/tsup +22 -0
  140. package/packages/core/node_modules/.bin/tsup-node +22 -0
  141. package/packages/core/node_modules/.bin/vite +22 -0
  142. package/packages/core/node_modules/.bin/vitest +22 -0
  143. package/packages/core/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -0
  144. package/packages/core/package.json +57 -0
  145. package/packages/core/src/AuraError.ts +89 -0
  146. package/packages/core/src/BaseService.ts +44 -0
  147. package/packages/core/src/client.ts +34 -0
  148. package/packages/core/src/cookie-utils.ts +42 -0
  149. package/packages/core/src/fetch.ts +435 -0
  150. package/packages/core/src/index.ts +22 -0
  151. package/packages/core/src/retry.ts +63 -0
  152. package/packages/core/src/serialize.ts +6 -0
  153. package/packages/core/src/stores/BaseAuthStore.ts +164 -0
  154. package/packages/core/src/stores/LocalAuthStore.ts +199 -0
  155. package/packages/core/src/types.ts +1042 -0
  156. package/packages/core/tests/auth-store.test.ts +300 -0
  157. package/packages/core/tests/error-code.test.ts +61 -0
  158. package/packages/core/tests/error.test.ts +24 -0
  159. package/packages/core/tests/fetch.test.ts +286 -0
  160. package/packages/core/tests/mocks.ts +139 -0
  161. package/packages/core/tests/observability.test.ts +86 -0
  162. package/packages/core/tests/refresh-hardening.test.ts +164 -0
  163. package/packages/core/tests/refresh-observability.test.ts +111 -0
  164. package/packages/core/tests/retry.test.ts +106 -0
  165. package/packages/core/tests/serialize.test.ts +12 -0
  166. package/packages/core/tests/timeout.test.ts +79 -0
  167. package/packages/core/tsconfig.json +5 -0
  168. package/packages/core/tsup.config.ts +11 -0
  169. package/packages/db/LICENSE +21 -0
  170. package/packages/db/README.md +152 -0
  171. package/packages/db/VENDORED.md +93 -0
  172. package/packages/db/dist/index.cjs +544 -0
  173. package/packages/db/dist/index.d.cts +1111 -0
  174. package/packages/db/dist/index.d.ts +1111 -0
  175. package/packages/db/dist/index.js +541 -0
  176. package/packages/db/node_modules/.bin/esbuild +19 -0
  177. package/packages/db/node_modules/.bin/tsc +22 -0
  178. package/packages/db/node_modules/.bin/tsserver +22 -0
  179. package/packages/db/node_modules/.bin/tsup +22 -0
  180. package/packages/db/node_modules/.bin/tsup-node +22 -0
  181. package/packages/db/node_modules/.bin/vite +22 -0
  182. package/packages/db/node_modules/.bin/vitest +22 -0
  183. package/packages/db/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -0
  184. package/packages/db/package.json +62 -0
  185. package/packages/db/src/DatabaseService.ts +170 -0
  186. package/packages/db/src/QueryBuilder.ts +830 -0
  187. package/packages/db/src/index.ts +2 -0
  188. package/packages/db/src/pgrst-types/common/common.ts +86 -0
  189. package/packages/db/src/pgrst-types/common/rpc.ts +151 -0
  190. package/packages/db/src/pgrst-types/feature-flags.ts +20 -0
  191. package/packages/db/src/pgrst-types/types.ts +134 -0
  192. package/packages/db/src/select-query-parser/parser.ts +479 -0
  193. package/packages/db/src/select-query-parser/result.ts +553 -0
  194. package/packages/db/src/select-query-parser/types.ts +132 -0
  195. package/packages/db/src/select-query-parser/utils.ts +707 -0
  196. package/packages/db/tests/filters.test.ts +81 -0
  197. package/packages/db/tests/mutations-filtered.e2e.test.ts +132 -0
  198. package/packages/db/tests/mutations-filtered.test.ts +188 -0
  199. package/packages/db/tests/order.test.ts +45 -0
  200. package/packages/db/tests/rpc.test.ts +41 -0
  201. package/packages/db/tests/scale/build-large-schema.ts +94 -0
  202. package/packages/db/tests/scale/scale-compile.test.ts +167 -0
  203. package/packages/db/tests/schema.test.ts +77 -0
  204. package/packages/db/tests/single-accept.e2e.test.ts +123 -0
  205. package/packages/db/tests/single.test.ts +211 -0
  206. package/packages/db/tests/types/filters.test-d.ts +52 -0
  207. package/packages/db/tests/types/fixtures.ts +40 -0
  208. package/packages/db/tests/types/from-views.test-d.ts +18 -0
  209. package/packages/db/tests/types/mutations.test-d.ts +23 -0
  210. package/packages/db/tests/types/rpc.test-d.ts +60 -0
  211. package/packages/db/tests/types/schema-shape.test-d.ts +8 -0
  212. package/packages/db/tests/types/select-inference.test-d.ts +38 -0
  213. package/packages/db/tests/vendored/fixtures.ts +83 -0
  214. package/packages/db/tests/vendored/get-result.test-d.ts +75 -0
  215. package/packages/db/tsconfig.json +5 -0
  216. package/packages/db/tsconfig.test-d.json +5 -0
  217. package/packages/db/tsup.config.ts +11 -0
  218. package/packages/db/vitest.config.ts +8 -0
  219. package/packages/functions/LICENSE +21 -0
  220. package/packages/functions/README.md +96 -0
  221. package/packages/functions/dist/index.cjs +173 -0
  222. package/packages/functions/dist/index.d.cts +81 -0
  223. package/packages/functions/dist/index.d.ts +81 -0
  224. package/packages/functions/dist/index.js +168 -0
  225. package/packages/functions/node_modules/.bin/esbuild +19 -0
  226. package/packages/functions/node_modules/.bin/tsc +22 -0
  227. package/packages/functions/node_modules/.bin/tsserver +22 -0
  228. package/packages/functions/node_modules/.bin/tsup +22 -0
  229. package/packages/functions/node_modules/.bin/tsup-node +22 -0
  230. package/packages/functions/node_modules/.bin/vite +22 -0
  231. package/packages/functions/node_modules/.bin/vitest +22 -0
  232. package/packages/functions/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -0
  233. package/packages/functions/package.json +60 -0
  234. package/packages/functions/src/FunctionsService.ts +188 -0
  235. package/packages/functions/src/errors.ts +70 -0
  236. package/packages/functions/src/index.ts +6 -0
  237. package/packages/functions/tests/invoke.test.ts +97 -0
  238. package/packages/functions/tests/response-type-errors.test.ts +135 -0
  239. package/packages/functions/tsconfig.json +5 -0
  240. package/packages/functions/tsup.config.ts +11 -0
  241. package/packages/gen/LICENSE +21 -0
  242. package/packages/gen/README.md +84 -0
  243. package/packages/gen/dist/cli.cjs +334 -0
  244. package/packages/gen/dist/cli.d.cts +1 -0
  245. package/packages/gen/dist/cli.d.ts +1 -0
  246. package/packages/gen/dist/cli.js +332 -0
  247. package/packages/gen/dist/index.cjs +257 -0
  248. package/packages/gen/dist/index.d.cts +12 -0
  249. package/packages/gen/dist/index.d.ts +12 -0
  250. package/packages/gen/dist/index.js +255 -0
  251. package/packages/gen/node_modules/.bin/esbuild +19 -0
  252. package/packages/gen/node_modules/.bin/tsc +22 -0
  253. package/packages/gen/node_modules/.bin/tsserver +22 -0
  254. package/packages/gen/node_modules/.bin/tsup +22 -0
  255. package/packages/gen/node_modules/.bin/tsup-node +22 -0
  256. package/packages/gen/node_modules/.bin/vite +22 -0
  257. package/packages/gen/node_modules/.bin/vitest +22 -0
  258. package/packages/gen/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -0
  259. package/packages/gen/package.json +66 -0
  260. package/packages/gen/src/cli.ts +67 -0
  261. package/packages/gen/src/generate.ts +0 -0
  262. package/packages/gen/src/index.ts +2 -0
  263. package/packages/gen/tests/compile.test.ts +213 -0
  264. package/packages/gen/tests/e2e.test.ts +130 -0
  265. package/packages/gen/tests/generate.test.ts +396 -0
  266. package/packages/gen/tsconfig.json +5 -0
  267. package/packages/gen/tsup.config.ts +11 -0
  268. package/packages/notifications/LICENSE +21 -0
  269. package/packages/notifications/README.md +134 -0
  270. package/packages/notifications/dist/index.cjs +128 -0
  271. package/packages/notifications/dist/index.d.cts +81 -0
  272. package/packages/notifications/dist/index.d.ts +81 -0
  273. package/packages/notifications/dist/index.js +125 -0
  274. package/packages/notifications/node_modules/.bin/esbuild +19 -0
  275. package/packages/notifications/node_modules/.bin/tsc +22 -0
  276. package/packages/notifications/node_modules/.bin/tsserver +22 -0
  277. package/packages/notifications/node_modules/.bin/tsup +22 -0
  278. package/packages/notifications/node_modules/.bin/tsup-node +22 -0
  279. package/packages/notifications/node_modules/.bin/vite +22 -0
  280. package/packages/notifications/node_modules/.bin/vitest +22 -0
  281. package/packages/notifications/package.json +60 -0
  282. package/packages/notifications/src/NotificationsService.ts +230 -0
  283. package/packages/notifications/src/index.ts +19 -0
  284. package/packages/notifications/tsconfig.json +5 -0
  285. package/packages/notifications/tsup.config.ts +11 -0
  286. package/packages/realtime/LICENSE +21 -0
  287. package/packages/realtime/README.md +183 -0
  288. package/packages/realtime/dist/index.cjs +977 -0
  289. package/packages/realtime/dist/index.d.cts +259 -0
  290. package/packages/realtime/dist/index.d.ts +259 -0
  291. package/packages/realtime/dist/index.js +973 -0
  292. package/packages/realtime/node_modules/.bin/esbuild +19 -0
  293. package/packages/realtime/node_modules/.bin/tsc +22 -0
  294. package/packages/realtime/node_modules/.bin/tsserver +22 -0
  295. package/packages/realtime/node_modules/.bin/tsup +22 -0
  296. package/packages/realtime/node_modules/.bin/tsup-node +22 -0
  297. package/packages/realtime/node_modules/.bin/vite +22 -0
  298. package/packages/realtime/node_modules/.bin/vitest +22 -0
  299. package/packages/realtime/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -0
  300. package/packages/realtime/package.json +60 -0
  301. package/packages/realtime/src/AuraChannel.ts +485 -0
  302. package/packages/realtime/src/HeartbeatManager.ts +73 -0
  303. package/packages/realtime/src/RealtimeService.ts +505 -0
  304. package/packages/realtime/src/columnFilter.ts +301 -0
  305. package/packages/realtime/src/index.ts +4 -0
  306. package/packages/realtime/tests/ack-routing.test.ts +81 -0
  307. package/packages/realtime/tests/column-filter.test.ts +144 -0
  308. package/packages/realtime/tests/dispatch.test.ts +191 -0
  309. package/packages/realtime/tests/heartbeat.test.ts +67 -0
  310. package/packages/realtime/tests/http-broadcast.test.ts +68 -0
  311. package/packages/realtime/tests/realtime-token.test.ts +43 -0
  312. package/packages/realtime/tests/reconnect-race.test.ts +149 -0
  313. package/packages/realtime/tests/ref-id.test.ts +34 -0
  314. package/packages/realtime/tests/set-auth.test.ts +70 -0
  315. package/packages/realtime/tsconfig.json +5 -0
  316. package/packages/realtime/tsup.config.ts +11 -0
  317. package/packages/storage/LICENSE +21 -0
  318. package/packages/storage/README.md +141 -0
  319. package/packages/storage/dist/index.cjs +333 -0
  320. package/packages/storage/dist/index.d.cts +185 -0
  321. package/packages/storage/dist/index.d.ts +185 -0
  322. package/packages/storage/dist/index.js +329 -0
  323. package/packages/storage/node_modules/.bin/esbuild +19 -0
  324. package/packages/storage/node_modules/.bin/tsc +22 -0
  325. package/packages/storage/node_modules/.bin/tsserver +22 -0
  326. package/packages/storage/node_modules/.bin/tsup +22 -0
  327. package/packages/storage/node_modules/.bin/tsup-node +22 -0
  328. package/packages/storage/node_modules/.bin/vite +22 -0
  329. package/packages/storage/node_modules/.bin/vitest +22 -0
  330. package/packages/storage/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -0
  331. package/packages/storage/package.json +60 -0
  332. package/packages/storage/src/DownloadBuilder.ts +60 -0
  333. package/packages/storage/src/StorageService.ts +323 -0
  334. package/packages/storage/src/index.ts +2 -0
  335. package/packages/storage/src/path.ts +35 -0
  336. package/packages/storage/src/transform.ts +15 -0
  337. package/packages/storage/tests/download.test.ts +64 -0
  338. package/packages/storage/tests/parity.test.ts +154 -0
  339. package/packages/storage/tests/path.test.ts +87 -0
  340. package/packages/storage/tests/transform.test.ts +79 -0
  341. package/packages/storage/tsconfig.json +5 -0
  342. package/packages/storage/tsup.config.ts +11 -0
  343. package/pnpm-workspace.yaml +24 -0
  344. package/tsconfig.base.json +15 -0
@@ -0,0 +1,544 @@
1
+ 'use strict';
2
+
3
+ var core = require('@aurabase/core');
4
+
5
+ // src/DatabaseService.ts
6
+ function quoteReserved(v) {
7
+ const s = String(v);
8
+ return /[,()"]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s;
9
+ }
10
+ function encodeFilterValue(op, value) {
11
+ if (op === "in" || op === "not.in") {
12
+ const arr = Array.isArray(value) ? value : [value];
13
+ return `${op}.(${arr.map(quoteReserved).join(",")})`;
14
+ }
15
+ if (op === "cs" || op === "cd" || op === "ov") {
16
+ if (Array.isArray(value)) return `${op}.{${value.map(quoteReserved).join(",")}}`;
17
+ if (value !== null && typeof value === "object") return `${op}.${JSON.stringify(value)}`;
18
+ return `${op}.${value}`;
19
+ }
20
+ if (op.endsWith("(all)") || op.endsWith("(any)")) {
21
+ const arr = Array.isArray(value) ? value : [value];
22
+ return `${op}.{${arr.map(quoteReserved).join(",")}}`;
23
+ }
24
+ return `${op}.${value}`;
25
+ }
26
+ var PGRST_OBJECT_ACCEPT = "application/vnd.pgrst.object+json";
27
+ function mentionsZeroRows(err) {
28
+ if (!err) return false;
29
+ const details = typeof err.details === "string" ? err.details : JSON.stringify(err.details ?? "");
30
+ return /\b0 rows\b/.test(`${err.message ?? ""} ${details}`);
31
+ }
32
+ function pgrst116(zeroRows) {
33
+ return new core.AuraError(406, {
34
+ error: {
35
+ code: "PGRST116",
36
+ message: zeroRows ? "JSON object requested, 0 rows returned" : "JSON object requested, multiple rows returned"
37
+ }
38
+ });
39
+ }
40
+ var AuraPostgrestBuilder = class {
41
+ _client;
42
+ _basePath;
43
+ _filters = [];
44
+ _orGroups = [];
45
+ _selectColumns = null;
46
+ // `select()` a-t-il été appelé ? (distinct de `_selectColumns`, que `select('*')`
47
+ // remet à `null`) — sert à poser `Prefer: return=representation` sur une mutation
48
+ // même avec `select('*')`.
49
+ _selectInvoked = false;
50
+ _sortColumn = null;
51
+ _sortDirection = "asc";
52
+ _limit = null;
53
+ _offset = null;
54
+ _cursor = null;
55
+ _count = null;
56
+ _rawSelect = false;
57
+ _single = false;
58
+ _maybeSingle = false;
59
+ _throwOnError = false;
60
+ _mutation = null;
61
+ _rpc = null;
62
+ constructor(client, basePath) {
63
+ this._client = client;
64
+ this._basePath = basePath;
65
+ }
66
+ throwOnError() {
67
+ this._throwOnError = true;
68
+ return this;
69
+ }
70
+ /**
71
+ * Positionne l'état interne `rpc` (args + verbe HTTP) — méthode package-interne
72
+ * utilisée par `DatabaseService.rpc()` pour éviter un cast sur un champ `protected`.
73
+ *
74
+ * `count` accepte `'planned'` (parité stricte `RpcOptions`, cf. `DatabaseService.rpc`)
75
+ * en plus de `CountMode` (`@aurabase/core`, qui ne connaît que `exact`/`estimated` —
76
+ * package partagé, non modifié ici). Le cast vers `_count` reste sans risque runtime :
77
+ * `buildQueryString` sérialise la valeur telle quelle en query param `count=`.
78
+ */
79
+ _initRpc(args, verb, count) {
80
+ this._rpc = { args, verb };
81
+ if (count) this._count = count;
82
+ return this;
83
+ }
84
+ // -- Query string builder ---------------------------------------------------
85
+ buildQueryString() {
86
+ const params = new URLSearchParams();
87
+ if (this._selectColumns && this._selectColumns.length > 0) {
88
+ params.set("select", this._rawSelect ? this._selectColumns[0] : this._selectColumns.join(","));
89
+ }
90
+ for (const f of this._filters) {
91
+ params.append(f.column, encodeFilterValue(f.op, f.value));
92
+ }
93
+ for (const group of this._orGroups) {
94
+ const parts = group.filters.map((f) => `${f.column}.${encodeFilterValue(f.op, f.value)}`);
95
+ params.append("or", `(${parts.join(",")})`);
96
+ }
97
+ if (this._sortColumn) {
98
+ params.set("sort_by", this._sortColumn);
99
+ params.set("sort_order", this._sortDirection);
100
+ }
101
+ if (this._limit !== null) params.set("limit", String(this._limit));
102
+ if (this._offset !== null) params.set("offset", String(this._offset));
103
+ if (this._cursor !== null) params.set("cursor", this._cursor);
104
+ if (this._count) params.set("count", this._count);
105
+ if (this._rpc && (this._rpc.verb === "GET" || this._rpc.verb === "HEAD") && this._rpc.args && typeof this._rpc.args === "object") {
106
+ for (const [k, v] of Object.entries(this._rpc.args)) {
107
+ params.set(k, typeof v === "string" ? v : JSON.stringify(v));
108
+ }
109
+ }
110
+ return params.toString();
111
+ }
112
+ // -- Execution via PromiseLike ----------------------------------------------
113
+ then(onfulfilled, onrejected) {
114
+ return this._execute().then(onfulfilled, onrejected);
115
+ }
116
+ async _execute() {
117
+ let res;
118
+ const acceptSingle = this._single ? { Accept: PGRST_OBJECT_ACCEPT } : void 0;
119
+ if (this._rpc) {
120
+ const qs = this.buildQueryString();
121
+ const sep = qs ? "?" : "";
122
+ const url = `${this._client.baseUrl}${this._basePath}${sep}${qs}`;
123
+ const init = this._rpc.verb === "POST" ? { method: "POST", body: this._rpc.args, headers: acceptSingle } : { method: this._rpc.verb, headers: acceptSingle };
124
+ res = await core.fetchWithAuth(url, init, this._client);
125
+ } else if (this._mutation) {
126
+ const m = this._mutation;
127
+ const hasEffectiveFilter = this._filters.some((f) => f.column !== "") || this._orGroups.some((g) => g.filters.length > 0);
128
+ if ((m.type === "update" || m.type === "delete") && m.id === null && !hasEffectiveFilter) {
129
+ return {
130
+ data: null,
131
+ error: new core.AuraError(0, {
132
+ error: {
133
+ code: "unfiltered_mutation",
134
+ message: "update()/delete() sans filtre refus\xE9 (protection anti-wipe) \u2014 ajoutez un filtre .eq()/... ou utilisez une mutation id-based"
135
+ }
136
+ })
137
+ };
138
+ }
139
+ const prefer = [];
140
+ if (this._selectInvoked) prefer.push("return=representation");
141
+ if (this._count) prefer.push(`count=${this._count}`);
142
+ const merged = { ...acceptSingle };
143
+ if (prefer.length > 0) merged.Prefer = prefer.join(", ");
144
+ const headers = Object.keys(merged).length > 0 ? merged : void 0;
145
+ const qs = this.buildQueryString();
146
+ const sep = qs ? "?" : "";
147
+ switch (m.type) {
148
+ case "insert": {
149
+ res = await core.fetchWithAuth(
150
+ `${this._client.baseUrl}${this._basePath}${sep}${qs}`,
151
+ { method: "POST", body: m.body, headers },
152
+ this._client
153
+ );
154
+ break;
155
+ }
156
+ case "update": {
157
+ const path = m.id !== null ? `${this._basePath}/${m.id}` : this._basePath;
158
+ res = await core.fetchWithAuth(
159
+ `${this._client.baseUrl}${path}${sep}${qs}`,
160
+ { method: "PATCH", body: m.body, headers },
161
+ this._client
162
+ );
163
+ break;
164
+ }
165
+ case "delete": {
166
+ const path = m.id !== null ? `${this._basePath}/${m.id}` : this._basePath;
167
+ res = await core.fetchWithAuth(
168
+ `${this._client.baseUrl}${path}${sep}${qs}`,
169
+ { method: "DELETE", headers },
170
+ this._client
171
+ );
172
+ break;
173
+ }
174
+ case "upsert": {
175
+ const params = new URLSearchParams(qs);
176
+ params.set("on_conflict", m.onConflict);
177
+ res = await core.fetchWithAuth(
178
+ `${this._client.baseUrl}${this._basePath}?${params.toString()}`,
179
+ { method: "POST", body: m.body, headers },
180
+ this._client
181
+ );
182
+ break;
183
+ }
184
+ }
185
+ } else {
186
+ const qs = this.buildQueryString();
187
+ const sep = qs ? "?" : "";
188
+ res = await core.fetchWithAuth(
189
+ `${this._client.baseUrl}${this._basePath}${sep}${qs}`,
190
+ { method: "GET", headers: acceptSingle },
191
+ this._client
192
+ );
193
+ }
194
+ if (!this._single && (this._mutation?.type === "insert" || this._mutation?.type === "upsert") && res.data !== null && typeof res.data === "object" && !Array.isArray(res.data)) {
195
+ res = { ...res, data: [res.data] };
196
+ }
197
+ if (this._single) {
198
+ if (res.error) {
199
+ const zero = res.error.status === 406 ? mentionsZeroRows(res.error) : res.error.status === 404 && res.error.code === "RECORD_NOT_FOUND" ? true : null;
200
+ if (zero !== null) {
201
+ res = this._maybeSingle && zero ? { ...res, data: null, error: null } : { ...res, data: null, error: pgrst116(zero) };
202
+ }
203
+ } else if (Array.isArray(res.data)) {
204
+ const rows = res.data;
205
+ if (this._maybeSingle) {
206
+ res = rows.length > 1 ? { ...res, data: null, error: pgrst116(false) } : { ...res, data: rows[0] ?? null };
207
+ } else if (rows.length === 1) {
208
+ res = { ...res, data: rows[0] };
209
+ } else {
210
+ res = { ...res, data: null, error: pgrst116(rows.length === 0) };
211
+ }
212
+ }
213
+ }
214
+ if (this._throwOnError && res.error) {
215
+ const err = new Error(res.error.message);
216
+ err.code = res.error.code;
217
+ err.details = res.error.details;
218
+ throw err;
219
+ }
220
+ return res;
221
+ }
222
+ };
223
+ var AuraTransformBuilder = class extends AuraPostgrestBuilder {
224
+ // -- Column selection & resource embedding -----------------------------------
225
+ /**
226
+ * Select columns and optionally embed related resources (PostgREST parity).
227
+ *
228
+ * @example
229
+ * // Simple columns
230
+ * db.from('users').select('id, name, email')
231
+ *
232
+ * // All columns (default)
233
+ * db.from('users').select('*')
234
+ *
235
+ * // Embed related table (many-to-one)
236
+ * db.from('orders').select('*, customer:customers(name, email)')
237
+ *
238
+ * // Embed related table (one-to-many)
239
+ * db.from('directors').select('last_name, films(title, year)')
240
+ *
241
+ * // FK disambiguation
242
+ * db.from('orders').select('*, billing:addresses!billing_address_id(name)')
243
+ *
244
+ * // Nested embedding
245
+ * db.from('actors').select('first_name, roles(character, films(title))')
246
+ */
247
+ select(columns) {
248
+ this._selectInvoked = true;
249
+ const cols = columns;
250
+ if (cols === void 0 || cols === "*") {
251
+ this._selectColumns = null;
252
+ this._rawSelect = false;
253
+ } else if (typeof cols === "string") {
254
+ if (cols.includes("(")) {
255
+ this._selectColumns = [cols];
256
+ this._rawSelect = true;
257
+ } else {
258
+ this._selectColumns = cols.split(",").map((c) => c.trim());
259
+ this._rawSelect = false;
260
+ }
261
+ } else {
262
+ this._selectColumns = cols;
263
+ this._rawSelect = false;
264
+ }
265
+ return this;
266
+ }
267
+ // -- Sort / pagination ------------------------------------------------------
268
+ order(column, direction = "asc") {
269
+ this._sortColumn = column;
270
+ this._sortDirection = direction;
271
+ return this;
272
+ }
273
+ limit(n) {
274
+ this._limit = n;
275
+ return this;
276
+ }
277
+ offset(n) {
278
+ this._offset = n;
279
+ return this;
280
+ }
281
+ /**
282
+ * Pagination keyset : passer le `next_cursor` opaque de la page précédente (préférable à
283
+ * `offset` sur les grandes tables : stable, sans dup/skip). Renvoyer tel quel `meta.next_cursor` ;
284
+ * ne pas le construire/parser (format opaque).
285
+ */
286
+ cursor(cursor) {
287
+ this._cursor = cursor;
288
+ return this;
289
+ }
290
+ count(mode = "exact") {
291
+ this._count = mode;
292
+ return this;
293
+ }
294
+ // -- Modifiers --------------------------------------------------------------
295
+ single() {
296
+ this._single = true;
297
+ return this;
298
+ }
299
+ maybeSingle() {
300
+ this._single = true;
301
+ this._maybeSingle = true;
302
+ return this;
303
+ }
304
+ };
305
+ var AuraFilterBuilder = class extends AuraTransformBuilder {
306
+ // -- Filter methods (each returns this for chaining) ------------------------
307
+ //
308
+ // Typées sur `Row` : `column` est contraint à `keyof Row & string` (autocomplete +
309
+ // erreur de compilation sur colonne inconnue) et `value`/`values` à `Row[C]` (erreur
310
+ // sur mauvais type). Runtime inchangé — seules les signatures changent.
311
+ eq(column, value) {
312
+ this._filters.push({ column, op: "eq", value });
313
+ return this;
314
+ }
315
+ neq(column, value) {
316
+ this._filters.push({ column, op: "neq", value });
317
+ return this;
318
+ }
319
+ gt(column, value) {
320
+ this._filters.push({ column, op: "gt", value });
321
+ return this;
322
+ }
323
+ gte(column, value) {
324
+ this._filters.push({ column, op: "gte", value });
325
+ return this;
326
+ }
327
+ lt(column, value) {
328
+ this._filters.push({ column, op: "lt", value });
329
+ return this;
330
+ }
331
+ lte(column, value) {
332
+ this._filters.push({ column, op: "lte", value });
333
+ return this;
334
+ }
335
+ like(column, pattern) {
336
+ this._filters.push({ column, op: "like", value: pattern });
337
+ return this;
338
+ }
339
+ ilike(column, pattern) {
340
+ this._filters.push({ column, op: "ilike", value: pattern });
341
+ return this;
342
+ }
343
+ /**
344
+ * IS NULL or IS NOT NULL filter.
345
+ * PostgREST accepts: is.null, is.true, is.false (no "not_null" — use neq for that).
346
+ * For IS NOT NULL, use `neq(column, null)` or `is(column, 'not.null')`.
347
+ */
348
+ is(column, value) {
349
+ if (value === "not.null") {
350
+ this._filters.push({ column, op: "not.is", value: "null" });
351
+ } else {
352
+ this._filters.push({ column, op: "is", value });
353
+ }
354
+ return this;
355
+ }
356
+ in(column, values) {
357
+ this._filters.push({ column, op: "in", value: values });
358
+ return this;
359
+ }
360
+ /**
361
+ * Add an OR group: `or=(col1.eq.val1,col2.gt.val2)`
362
+ */
363
+ or(filters) {
364
+ this._orGroups.push({
365
+ filters: filters.map((f) => ({ column: f.column, op: f.op, value: f.value }))
366
+ });
367
+ return this;
368
+ }
369
+ // -- Filtres étendus (parité PostgREST) -------------------------------------
370
+ notIn(column, values) {
371
+ this._filters.push({ column, op: "not.in", value: values });
372
+ return this;
373
+ }
374
+ contains(column, value) {
375
+ this._filters.push({ column, op: "cs", value });
376
+ return this;
377
+ }
378
+ containedBy(column, value) {
379
+ this._filters.push({ column, op: "cd", value });
380
+ return this;
381
+ }
382
+ overlaps(column, value) {
383
+ this._filters.push({ column, op: "ov", value });
384
+ return this;
385
+ }
386
+ rangeGt(column, range) {
387
+ this._filters.push({ column, op: "sr", value: range });
388
+ return this;
389
+ }
390
+ rangeGte(column, range) {
391
+ this._filters.push({ column, op: "nxl", value: range });
392
+ return this;
393
+ }
394
+ rangeLt(column, range) {
395
+ this._filters.push({ column, op: "sl", value: range });
396
+ return this;
397
+ }
398
+ rangeLte(column, range) {
399
+ this._filters.push({ column, op: "nxr", value: range });
400
+ return this;
401
+ }
402
+ rangeAdjacent(column, range) {
403
+ this._filters.push({ column, op: "adj", value: range });
404
+ return this;
405
+ }
406
+ isDistinct(column, value) {
407
+ this._filters.push({ column, op: "isdistinct", value });
408
+ return this;
409
+ }
410
+ // Échappatoires (parité postgrest-js) : colonne `string` permissive, pas de contrainte sur `Row`.
411
+ not(column, op, value) {
412
+ this._filters.push({ column, op: `not.${op}`, value });
413
+ return this;
414
+ }
415
+ filter(column, op, value) {
416
+ this._filters.push({ column, op, value });
417
+ return this;
418
+ }
419
+ match(query) {
420
+ for (const [column, value] of Object.entries(query)) this._filters.push({ column, op: "eq", value });
421
+ return this;
422
+ }
423
+ textSearch(column, query, options) {
424
+ const t = options?.type;
425
+ const token = t === "plain" ? "plfts" : t === "phrase" ? "phfts" : t === "websearch" ? "wfts" : "fts";
426
+ const op = options?.config ? `${token}(${options.config})` : token;
427
+ this._filters.push({ column, op, value: query });
428
+ return this;
429
+ }
430
+ likeAllOf(column, patterns) {
431
+ this._filters.push({ column, op: "like(all)", value: patterns });
432
+ return this;
433
+ }
434
+ likeAnyOf(column, patterns) {
435
+ this._filters.push({ column, op: "like(any)", value: patterns });
436
+ return this;
437
+ }
438
+ ilikeAllOf(column, patterns) {
439
+ this._filters.push({ column, op: "ilike(all)", value: patterns });
440
+ return this;
441
+ }
442
+ ilikeAnyOf(column, patterns) {
443
+ this._filters.push({ column, op: "ilike(any)", value: patterns });
444
+ return this;
445
+ }
446
+ };
447
+ var AuraQueryBuilder = class extends AuraFilterBuilder {
448
+ // -- Mutations --------------------------------------------------------------
449
+ //
450
+ // Typées sur `Relation['Insert']`/`Relation['Update']` (parité postgrest-js) :
451
+ // un champ requis manquant côté insert/upsert, ou une colonne inconnue côté
452
+ // update, est une erreur de compilation. Runtime inchangé.
453
+ //
454
+ // Extraction par type conditionnel (plutôt qu'un accès indexé direct
455
+ // `Relation['Insert']`) : `Relation` est contraint à `GenericTable | GenericView`
456
+ // et `GenericNonUpdatableView` ne porte pas `Insert`/`Update`. `infer` distribue
457
+ // sur cette union et retombe sur `never` pour la branche non-updatable, ce qui
458
+ // évite l'erreur TS2536 tout en gardant le typage strict pour les tables.
459
+ insert(body) {
460
+ this._mutation = { type: "insert", body };
461
+ return this;
462
+ }
463
+ update(idOrBody, body) {
464
+ if (typeof idOrBody === "string") {
465
+ this._mutation = { type: "update", id: idOrBody, body };
466
+ } else {
467
+ this._mutation = { type: "update", id: null, body: idOrBody };
468
+ }
469
+ return this;
470
+ }
471
+ // COMPAT `delete(id)` → route id-based ; FILTRÉ `delete()` → route Table
472
+ // (exige un filtre, garde-fou anti-wipe).
473
+ delete(id) {
474
+ this._mutation = { type: "delete", id: typeof id === "string" ? id : null };
475
+ return this;
476
+ }
477
+ upsert(body, options) {
478
+ this._mutation = { type: "upsert", body, onConflict: options?.onConflict ?? "id" };
479
+ return this;
480
+ }
481
+ };
482
+
483
+ // src/DatabaseService.ts
484
+ var DatabaseService = class extends core.BaseService {
485
+ constructor(client) {
486
+ super(client);
487
+ }
488
+ get prefix() {
489
+ return `/v1/db/${this.client.projectId}`;
490
+ }
491
+ from(table) {
492
+ return new AuraQueryBuilder(this.client, `${this.prefix}/${table}`);
493
+ }
494
+ /**
495
+ * Exécute une requête SQL brute (passthrough direct vers Postgres).
496
+ *
497
+ * ⚠️ N'est PAS en lecture seule : SELECT, INSERT, UPDATE, DELETE et DDL
498
+ * s'exécutent réellement et persistent, dans la limite des GRANTs du rôle
499
+ * Postgres connecté pour ce projet (seule barrière de sécurité — il n'y a
500
+ * aucun filtre applicatif par type d'instruction côté serveur). Ne pas
501
+ * exposer cet endpoint sans restriction à des clients non fiables.
502
+ */
503
+ async sql(query, params) {
504
+ return this.request("POST", `${this.prefix}/raw`, { query, params });
505
+ }
506
+ /**
507
+ * Appelle une fonction Postgres exposée par PostgREST (`POST|GET|HEAD /rpc/{fn}`).
508
+ * Signature forte quand `Schema['Functions']` connaît `fn` (typage vendoré
509
+ * `GetRpcFunctionFilterBuilderByArgs`, parité postgrest-js) ; sinon repli permissif.
510
+ *
511
+ * @example
512
+ * const { data } = await db.rpc('add', { a: 1, b: 2 })
513
+ * const { data } = await db.rpc('list_things', {}).eq('id', 2).select('id,name').single()
514
+ */
515
+ rpc(fn, args, options) {
516
+ const verb = options?.head ? "HEAD" : options?.get ? "GET" : "POST";
517
+ const builder = new AuraFilterBuilder(
518
+ this.client,
519
+ `${this.prefix}/rpc/${fn}`
520
+ )._initRpc(args ?? {}, verb, options?.count);
521
+ return builder;
522
+ }
523
+ /**
524
+ * Fetch the schema (tables & colonnes, fonctions Postgres exposées via PostgREST,
525
+ * types enum et vues) pour le projet courant.
526
+ */
527
+ async schema() {
528
+ const res = await this.request("GET", `${this.prefix}/schema`);
529
+ if (res.error) return { data: null, error: res.error, meta: res.meta };
530
+ return {
531
+ data: {
532
+ tables: res.data?.tables ?? [],
533
+ functions: res.data?.functions ?? [],
534
+ enums: res.data?.enums ?? [],
535
+ views: res.data?.views ?? []
536
+ },
537
+ error: null,
538
+ meta: res.meta
539
+ };
540
+ }
541
+ };
542
+
543
+ exports.DatabaseService = DatabaseService;
544
+ exports.QueryBuilder = AuraQueryBuilder;