neoorm 0.7.3 → 0.8.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 (361) hide show
  1. package/README.md +4 -4
  2. package/dist/bin/neoorm.js +36 -27
  3. package/dist/bin/neoorm.js.map +1 -1
  4. package/dist/codegen/diff-manifest.d.ts.map +1 -1
  5. package/dist/codegen/diff-manifest.js +55 -30
  6. package/dist/codegen/diff-manifest.js.map +1 -1
  7. package/dist/codegen/emit-includes.d.ts.map +1 -1
  8. package/dist/codegen/emit-includes.js +6 -6
  9. package/dist/codegen/emit-includes.js.map +1 -1
  10. package/dist/codegen/emit-models.d.ts.map +1 -1
  11. package/dist/codegen/emit-models.js +14 -14
  12. package/dist/codegen/emit-models.js.map +1 -1
  13. package/dist/codegen/generate.d.ts +11 -1
  14. package/dist/codegen/generate.d.ts.map +1 -1
  15. package/dist/codegen/generate.js +30 -11
  16. package/dist/codegen/generate.js.map +1 -1
  17. package/dist/codegen/manifest-relations.d.ts +2 -1
  18. package/dist/codegen/manifest-relations.d.ts.map +1 -1
  19. package/dist/codegen/manifest-relations.js +4 -27
  20. package/dist/codegen/manifest-relations.js.map +1 -1
  21. package/dist/codegen/schema-to-manifest.d.ts +3 -2
  22. package/dist/codegen/schema-to-manifest.d.ts.map +1 -1
  23. package/dist/codegen/schema-to-manifest.js +9 -38
  24. package/dist/codegen/schema-to-manifest.js.map +1 -1
  25. package/dist/config.d.ts +6 -2
  26. package/dist/config.d.ts.map +1 -1
  27. package/dist/config.js +21 -13
  28. package/dist/config.js.map +1 -1
  29. package/dist/datasource-provider.d.ts +7 -0
  30. package/dist/datasource-provider.d.ts.map +1 -0
  31. package/dist/datasource-provider.js +11 -0
  32. package/dist/datasource-provider.js.map +1 -0
  33. package/dist/dialect/fk.d.ts.map +1 -1
  34. package/dist/dialect/fk.js +4 -2
  35. package/dist/dialect/fk.js.map +1 -1
  36. package/dist/dialect/postgres.d.ts.map +1 -1
  37. package/dist/dialect/postgres.js +11 -15
  38. package/dist/dialect/postgres.js.map +1 -1
  39. package/dist/dialect/shared.d.ts +3 -1
  40. package/dist/dialect/shared.d.ts.map +1 -1
  41. package/dist/dialect/shared.js +4 -0
  42. package/dist/dialect/shared.js.map +1 -1
  43. package/dist/dialect/sqlite.d.ts.map +1 -1
  44. package/dist/dialect/sqlite.js +15 -18
  45. package/dist/dialect/sqlite.js.map +1 -1
  46. package/dist/dialect/sqlite.test.d.ts +2 -0
  47. package/dist/dialect/sqlite.test.d.ts.map +1 -0
  48. package/dist/dialect/sqlite.test.js +122 -0
  49. package/dist/dialect/sqlite.test.js.map +1 -0
  50. package/dist/dialect/types.d.ts +2 -1
  51. package/dist/dialect/types.d.ts.map +1 -1
  52. package/dist/docs/render.d.ts.map +1 -1
  53. package/dist/docs/render.js.map +1 -1
  54. package/dist/docs/search.d.ts.map +1 -1
  55. package/dist/docs/search.js +4 -4
  56. package/dist/docs/search.js.map +1 -1
  57. package/dist/docs/server.d.ts.map +1 -1
  58. package/dist/docs/server.js +3 -3
  59. package/dist/docs/server.js.map +1 -1
  60. package/dist/index.d.ts +12 -10
  61. package/dist/index.d.ts.map +1 -1
  62. package/dist/index.js +5 -4
  63. package/dist/index.js.map +1 -1
  64. package/dist/init/scaffold.d.ts.map +1 -1
  65. package/dist/init/scaffold.js +3 -1
  66. package/dist/init/scaffold.js.map +1 -1
  67. package/dist/introspect/pull.d.ts.map +1 -1
  68. package/dist/introspect/pull.js +209 -154
  69. package/dist/introspect/pull.js.map +1 -1
  70. package/dist/introspect/queries.d.ts +6 -0
  71. package/dist/introspect/queries.d.ts.map +1 -1
  72. package/dist/introspect/queries.js +19 -0
  73. package/dist/introspect/queries.js.map +1 -1
  74. package/dist/introspect/sqlite/to-manifest.d.ts.map +1 -1
  75. package/dist/introspect/sqlite/to-manifest.js +6 -9
  76. package/dist/introspect/sqlite/to-manifest.js.map +1 -1
  77. package/dist/introspect/to-manifest.d.ts +8 -1
  78. package/dist/introspect/to-manifest.d.ts.map +1 -1
  79. package/dist/introspect/to-manifest.js +96 -21
  80. package/dist/introspect/to-manifest.js.map +1 -1
  81. package/dist/migrate/runner.d.ts +16 -0
  82. package/dist/migrate/runner.d.ts.map +1 -1
  83. package/dist/migrate/runner.js +147 -24
  84. package/dist/migrate/runner.js.map +1 -1
  85. package/dist/plugins/builtin.d.ts.map +1 -1
  86. package/dist/plugins/builtin.js +15 -16
  87. package/dist/plugins/builtin.js.map +1 -1
  88. package/dist/plugins/json/operators.d.ts.map +1 -1
  89. package/dist/plugins/json/operators.js +100 -27
  90. package/dist/plugins/json/operators.js.map +1 -1
  91. package/dist/plugins/postgis/columns.d.ts +18 -4
  92. package/dist/plugins/postgis/columns.d.ts.map +1 -1
  93. package/dist/plugins/postgis/columns.js +69 -10
  94. package/dist/plugins/postgis/columns.js.map +1 -1
  95. package/dist/plugins/postgis/columns.test.d.ts +2 -0
  96. package/dist/plugins/postgis/columns.test.d.ts.map +1 -0
  97. package/dist/plugins/postgis/columns.test.js +67 -0
  98. package/dist/plugins/postgis/columns.test.js.map +1 -0
  99. package/dist/plugins/postgis/index.d.ts +4 -2
  100. package/dist/plugins/postgis/index.d.ts.map +1 -1
  101. package/dist/plugins/postgis/index.js +2 -1
  102. package/dist/plugins/postgis/index.js.map +1 -1
  103. package/dist/plugins/postgis/where-types.d.ts +18 -0
  104. package/dist/plugins/postgis/where-types.d.ts.map +1 -0
  105. package/dist/plugins/postgis/where-types.js +2 -0
  106. package/dist/plugins/postgis/where-types.js.map +1 -0
  107. package/dist/plugins/registry.d.ts.map +1 -1
  108. package/dist/plugins/registry.js +20 -5
  109. package/dist/plugins/registry.js.map +1 -1
  110. package/dist/plugins/types.d.ts +1 -1
  111. package/dist/plugins/types.d.ts.map +1 -1
  112. package/dist/runtime/client.d.ts +60 -11
  113. package/dist/runtime/client.d.ts.map +1 -1
  114. package/dist/runtime/client.js +85 -39
  115. package/dist/runtime/client.js.map +1 -1
  116. package/dist/runtime/client.test.d.ts +2 -0
  117. package/dist/runtime/client.test.d.ts.map +1 -0
  118. package/dist/runtime/client.test.js +170 -0
  119. package/dist/runtime/client.test.js.map +1 -0
  120. package/dist/runtime/compile-error.d.ts +14 -0
  121. package/dist/runtime/compile-error.d.ts.map +1 -0
  122. package/dist/runtime/compile-error.js +14 -0
  123. package/dist/runtime/compile-error.js.map +1 -0
  124. package/dist/runtime/driver-sqlite.test.d.ts +2 -0
  125. package/dist/runtime/driver-sqlite.test.d.ts.map +1 -0
  126. package/dist/runtime/driver-sqlite.test.js +195 -0
  127. package/dist/runtime/driver-sqlite.test.js.map +1 -0
  128. package/dist/runtime/driver.d.ts +16 -1
  129. package/dist/runtime/driver.d.ts.map +1 -1
  130. package/dist/runtime/driver.js +118 -96
  131. package/dist/runtime/driver.js.map +1 -1
  132. package/dist/runtime/error-builders.d.ts +7 -6
  133. package/dist/runtime/error-builders.d.ts.map +1 -1
  134. package/dist/runtime/error-builders.js +2 -2
  135. package/dist/runtime/error-builders.js.map +1 -1
  136. package/dist/runtime/error-codes.d.ts +50 -0
  137. package/dist/runtime/error-codes.d.ts.map +1 -0
  138. package/dist/runtime/error-codes.js +54 -0
  139. package/dist/runtime/error-codes.js.map +1 -0
  140. package/dist/runtime/error-hints.d.ts +1 -0
  141. package/dist/runtime/error-hints.d.ts.map +1 -1
  142. package/dist/runtime/error-hints.js +13 -1
  143. package/dist/runtime/error-hints.js.map +1 -1
  144. package/dist/runtime/errors.d.ts +55 -8
  145. package/dist/runtime/errors.d.ts.map +1 -1
  146. package/dist/runtime/errors.js +143 -12
  147. package/dist/runtime/errors.js.map +1 -1
  148. package/dist/runtime/errors.test.d.ts +2 -0
  149. package/dist/runtime/errors.test.d.ts.map +1 -0
  150. package/dist/runtime/errors.test.js +115 -0
  151. package/dist/runtime/errors.test.js.map +1 -0
  152. package/dist/runtime/executor.d.ts +22 -3
  153. package/dist/runtime/executor.d.ts.map +1 -1
  154. package/dist/runtime/executor.js +93 -39
  155. package/dist/runtime/executor.js.map +1 -1
  156. package/dist/runtime/parse-db-json.d.ts +8 -0
  157. package/dist/runtime/parse-db-json.d.ts.map +1 -0
  158. package/dist/runtime/parse-db-json.js +33 -0
  159. package/dist/runtime/parse-db-json.js.map +1 -0
  160. package/dist/runtime/pg-error.d.ts.map +1 -1
  161. package/dist/runtime/pg-error.js +16 -16
  162. package/dist/runtime/pg-error.js.map +1 -1
  163. package/dist/runtime/pg-rollback.test.d.ts +2 -0
  164. package/dist/runtime/pg-rollback.test.d.ts.map +1 -0
  165. package/dist/runtime/pg-rollback.test.js +39 -0
  166. package/dist/runtime/pg-rollback.test.js.map +1 -0
  167. package/dist/runtime/query/aggregate.d.ts.map +1 -1
  168. package/dist/runtime/query/aggregate.js +15 -4
  169. package/dist/runtime/query/aggregate.js.map +1 -1
  170. package/dist/runtime/query/compile-aggregate.d.ts +39 -0
  171. package/dist/runtime/query/compile-aggregate.d.ts.map +1 -0
  172. package/dist/runtime/query/compile-aggregate.js +456 -0
  173. package/dist/runtime/query/compile-aggregate.js.map +1 -0
  174. package/dist/runtime/query/compile-where.d.ts +32 -0
  175. package/dist/runtime/query/compile-where.d.ts.map +1 -0
  176. package/dist/runtime/query/compile-where.js +708 -0
  177. package/dist/runtime/query/compile-where.js.map +1 -0
  178. package/dist/runtime/query/compile-write.d.ts +53 -0
  179. package/dist/runtime/query/compile-write.d.ts.map +1 -0
  180. package/dist/runtime/query/compile-write.js +467 -0
  181. package/dist/runtime/query/compile-write.js.map +1 -0
  182. package/dist/runtime/query/compile.d.ts +3 -114
  183. package/dist/runtime/query/compile.d.ts.map +1 -1
  184. package/dist/runtime/query/compile.js +3 -1536
  185. package/dist/runtime/query/compile.js.map +1 -1
  186. package/dist/runtime/query/count.d.ts +1 -0
  187. package/dist/runtime/query/count.d.ts.map +1 -1
  188. package/dist/runtime/query/count.js +12 -14
  189. package/dist/runtime/query/count.js.map +1 -1
  190. package/dist/runtime/query/create.d.ts.map +1 -1
  191. package/dist/runtime/query/create.js +20 -49
  192. package/dist/runtime/query/create.js.map +1 -1
  193. package/dist/runtime/query/cursor-codec.d.ts.map +1 -1
  194. package/dist/runtime/query/cursor-codec.js +8 -2
  195. package/dist/runtime/query/cursor-codec.js.map +1 -1
  196. package/dist/runtime/query/cursor.d.ts.map +1 -1
  197. package/dist/runtime/query/cursor.js +31 -7
  198. package/dist/runtime/query/cursor.js.map +1 -1
  199. package/dist/runtime/query/delete.d.ts.map +1 -1
  200. package/dist/runtime/query/delete.js +17 -17
  201. package/dist/runtime/query/delete.js.map +1 -1
  202. package/dist/runtime/query/execute.d.ts.map +1 -1
  203. package/dist/runtime/query/execute.js +11 -12
  204. package/dist/runtime/query/execute.js.map +1 -1
  205. package/dist/runtime/query/find-or-create.d.ts +4 -2
  206. package/dist/runtime/query/find-or-create.d.ts.map +1 -1
  207. package/dist/runtime/query/find-or-create.js +53 -43
  208. package/dist/runtime/query/find-or-create.js.map +1 -1
  209. package/dist/runtime/query/find-or-create.test.d.ts +2 -0
  210. package/dist/runtime/query/find-or-create.test.d.ts.map +1 -0
  211. package/dist/runtime/query/find-or-create.test.js +189 -0
  212. package/dist/runtime/query/find-or-create.test.js.map +1 -0
  213. package/dist/runtime/query/find.d.ts.map +1 -1
  214. package/dist/runtime/query/find.js +86 -58
  215. package/dist/runtime/query/find.js.map +1 -1
  216. package/dist/runtime/query/group-by.d.ts.map +1 -1
  217. package/dist/runtime/query/group-by.js +3 -2
  218. package/dist/runtime/query/group-by.js.map +1 -1
  219. package/dist/runtime/query/manifest-lookup.d.ts.map +1 -1
  220. package/dist/runtime/query/manifest-lookup.js +1 -1
  221. package/dist/runtime/query/manifest-lookup.js.map +1 -1
  222. package/dist/runtime/query/map-row.d.ts.map +1 -1
  223. package/dist/runtime/query/map-row.js +1 -1
  224. package/dist/runtime/query/map-row.js.map +1 -1
  225. package/dist/runtime/query/paginate.d.ts +3 -0
  226. package/dist/runtime/query/paginate.d.ts.map +1 -1
  227. package/dist/runtime/query/paginate.js +49 -11
  228. package/dist/runtime/query/paginate.js.map +1 -1
  229. package/dist/runtime/query/primary-key.d.ts +1 -1
  230. package/dist/runtime/query/primary-key.d.ts.map +1 -1
  231. package/dist/runtime/query/primary-key.js +45 -8
  232. package/dist/runtime/query/primary-key.js.map +1 -1
  233. package/dist/runtime/query/projection.d.ts +1 -0
  234. package/dist/runtime/query/projection.d.ts.map +1 -1
  235. package/dist/runtime/query/projection.js +23 -7
  236. package/dist/runtime/query/projection.js.map +1 -1
  237. package/dist/runtime/query/relation-planner.d.ts.map +1 -1
  238. package/dist/runtime/query/relation-planner.js +82 -53
  239. package/dist/runtime/query/relation-planner.js.map +1 -1
  240. package/dist/runtime/query/relation-writes.d.ts +3 -1
  241. package/dist/runtime/query/relation-writes.d.ts.map +1 -1
  242. package/dist/runtime/query/relation-writes.js +145 -54
  243. package/dist/runtime/query/relation-writes.js.map +1 -1
  244. package/dist/runtime/query/serialize-dialect.test.d.ts +2 -0
  245. package/dist/runtime/query/serialize-dialect.test.d.ts.map +1 -0
  246. package/dist/runtime/query/serialize-dialect.test.js +79 -0
  247. package/dist/runtime/query/serialize-dialect.test.js.map +1 -0
  248. package/dist/runtime/query/sqlite-constraint-errors.test.d.ts +2 -0
  249. package/dist/runtime/query/sqlite-constraint-errors.test.d.ts.map +1 -0
  250. package/dist/runtime/query/sqlite-constraint-errors.test.js +60 -0
  251. package/dist/runtime/query/sqlite-constraint-errors.test.js.map +1 -0
  252. package/dist/runtime/query/strip.d.ts.map +1 -1
  253. package/dist/runtime/query/strip.js.map +1 -1
  254. package/dist/runtime/query/table-index.d.ts.map +1 -1
  255. package/dist/runtime/query/table-index.js +4 -3
  256. package/dist/runtime/query/table-index.js.map +1 -1
  257. package/dist/runtime/query/unique.d.ts +8 -1
  258. package/dist/runtime/query/unique.d.ts.map +1 -1
  259. package/dist/runtime/query/unique.js +89 -4
  260. package/dist/runtime/query/unique.js.map +1 -1
  261. package/dist/runtime/query/unique.test.d.ts +2 -0
  262. package/dist/runtime/query/unique.test.d.ts.map +1 -0
  263. package/dist/runtime/query/unique.test.js +231 -0
  264. package/dist/runtime/query/unique.test.js.map +1 -0
  265. package/dist/runtime/query/update.d.ts.map +1 -1
  266. package/dist/runtime/query/update.js +30 -35
  267. package/dist/runtime/query/update.js.map +1 -1
  268. package/dist/runtime/query/update.test.d.ts +2 -0
  269. package/dist/runtime/query/update.test.d.ts.map +1 -0
  270. package/dist/runtime/query/update.test.js +186 -0
  271. package/dist/runtime/query/update.test.js.map +1 -0
  272. package/dist/runtime/query/updated-at.d.ts.map +1 -1
  273. package/dist/runtime/query/updated-at.js.map +1 -1
  274. package/dist/runtime/query/upsert.d.ts.map +1 -1
  275. package/dist/runtime/query/upsert.js +5 -7
  276. package/dist/runtime/query/upsert.js.map +1 -1
  277. package/dist/runtime/schema-error.d.ts.map +1 -1
  278. package/dist/runtime/schema-error.js +6 -2
  279. package/dist/runtime/schema-error.js.map +1 -1
  280. package/dist/runtime/sqlite-error.d.ts.map +1 -1
  281. package/dist/runtime/sqlite-error.js +11 -12
  282. package/dist/runtime/sqlite-error.js.map +1 -1
  283. package/dist/runtime/sqlite-open.d.ts +2 -0
  284. package/dist/runtime/sqlite-open.d.ts.map +1 -1
  285. package/dist/runtime/sqlite-open.js +18 -1
  286. package/dist/runtime/sqlite-open.js.map +1 -1
  287. package/dist/runtime/transaction.d.ts +4 -0
  288. package/dist/runtime/transaction.d.ts.map +1 -1
  289. package/dist/runtime/transaction.js +31 -1
  290. package/dist/runtime/transaction.js.map +1 -1
  291. package/dist/runtime/transaction.test.d.ts +2 -0
  292. package/dist/runtime/transaction.test.d.ts.map +1 -0
  293. package/dist/runtime/transaction.test.js +43 -0
  294. package/dist/runtime/transaction.test.js.map +1 -0
  295. package/dist/runtime/types.d.ts +50 -17
  296. package/dist/runtime/types.d.ts.map +1 -1
  297. package/dist/schema/column-where.d.ts +11 -1
  298. package/dist/schema/column-where.d.ts.map +1 -1
  299. package/dist/schema/column.d.ts +1 -1
  300. package/dist/schema/column.d.ts.map +1 -1
  301. package/dist/schema/column.js +1 -1
  302. package/dist/schema/column.js.map +1 -1
  303. package/dist/schema/define-schema.d.ts +1 -1
  304. package/dist/schema/define-schema.d.ts.map +1 -1
  305. package/dist/schema/define-schema.js +5 -23
  306. package/dist/schema/define-schema.js.map +1 -1
  307. package/dist/schema/index.d.ts +2 -2
  308. package/dist/schema/index.d.ts.map +1 -1
  309. package/dist/schema/index.js +1 -1
  310. package/dist/schema/index.js.map +1 -1
  311. package/dist/schema/relation-types.d.ts +29 -22
  312. package/dist/schema/relation-types.d.ts.map +1 -1
  313. package/dist/schema/relation.d.ts.map +1 -1
  314. package/dist/schema/relation.js +6 -4
  315. package/dist/schema/relation.js.map +1 -1
  316. package/dist/schema/table-registry.d.ts.map +1 -1
  317. package/dist/schema/table-registry.js +8 -2
  318. package/dist/schema/table-registry.js.map +1 -1
  319. package/dist/schema/table.d.ts +6 -3
  320. package/dist/schema/table.d.ts.map +1 -1
  321. package/dist/schema/table.js +30 -31
  322. package/dist/schema/table.js.map +1 -1
  323. package/dist/schema/types.d.ts +33 -25
  324. package/dist/schema/types.d.ts.map +1 -1
  325. package/dist/sql/builder.d.ts +5 -1
  326. package/dist/sql/builder.d.ts.map +1 -1
  327. package/dist/sql/builder.js +23 -3
  328. package/dist/sql/builder.js.map +1 -1
  329. package/dist/sql/index.d.ts +5 -2
  330. package/dist/sql/index.d.ts.map +1 -1
  331. package/dist/sql/index.js +5 -2
  332. package/dist/sql/index.js.map +1 -1
  333. package/dist/utils/case.d.ts +4 -0
  334. package/dist/utils/case.d.ts.map +1 -1
  335. package/dist/utils/case.js +11 -0
  336. package/dist/utils/case.js.map +1 -1
  337. package/dist/utils/dotenv.d.ts +8 -0
  338. package/dist/utils/dotenv.d.ts.map +1 -0
  339. package/dist/utils/dotenv.js +70 -0
  340. package/dist/utils/dotenv.js.map +1 -0
  341. package/dist/utils/dotenv.test.d.ts +2 -0
  342. package/dist/utils/dotenv.test.d.ts.map +1 -0
  343. package/dist/utils/dotenv.test.js +32 -0
  344. package/dist/utils/dotenv.test.js.map +1 -0
  345. package/dist/utils/inflect.d.ts +6 -0
  346. package/dist/utils/inflect.d.ts.map +1 -0
  347. package/dist/utils/inflect.js +80 -0
  348. package/dist/utils/inflect.js.map +1 -0
  349. package/docs/cli.md +6 -2
  350. package/docs/configuration.md +3 -1
  351. package/docs/errors.md +61 -6
  352. package/docs/examples.md +63 -13
  353. package/docs/getting-started.md +24 -1
  354. package/docs/migrations.md +15 -2
  355. package/docs/plugins.md +13 -1
  356. package/docs/queries.md +109 -10
  357. package/docs/relations.md +5 -1
  358. package/docs/schema.md +12 -5
  359. package/docs/sqlite.md +24 -10
  360. package/docs/transactions.md +13 -2
  361. package/package.json +4 -2
@@ -0,0 +1,708 @@
1
+ import { effectiveRelations } from "../../codegen/manifest-relations.js";
2
+ import { postgresDialect, quoteIdentifier, tableRef, } from "../../dialect/postgres.js";
3
+ import { getColumnType } from "../../plugins/registry.js";
4
+ import { compileError } from "../compile-error.js";
5
+ import { QueryErrorCode } from "../error-codes.js";
6
+ import { didYouMean, suggestWhereOperator } from "../error-hints.js";
7
+ import { findM2M } from "./manifest-lookup.js";
8
+ import { primaryKeySqlName, requireScalarPrimaryKey, targetRelationPkSql, } from "./primary-key.js";
9
+ import { columnByTsName, columnsByTsNames, getOrSetSqlCache, getTableIndex, requireTsColumn, } from "./table-index.js";
10
+ export function colByTs(table, tsName, manifestIndex) {
11
+ return columnByTsName(getTableIndex(manifestIndex, table.accessor), table, tsName);
12
+ }
13
+ function compiledResult(sql, params, nextParamIndex, impossible = false) {
14
+ return {
15
+ sql,
16
+ params,
17
+ nextParamIndex,
18
+ ...(impossible ? { impossible: true } : {}),
19
+ };
20
+ }
21
+ const PARAMLESS_OPERATORS = new Set(["isNull", "isNotNull"]);
22
+ export function isOperatorObject(value) {
23
+ return (typeof value === "object" &&
24
+ value !== null &&
25
+ !Array.isArray(value) &&
26
+ !(value instanceof Date));
27
+ }
28
+ const operatorParamTransform = {
29
+ contains: (v) => `%${String(v)}%`,
30
+ startsWith: (v) => `${String(v)}%`,
31
+ endsWith: (v) => `%${String(v)}`,
32
+ };
33
+ function parseQueryMode(value) {
34
+ if (value === undefined || value === "default")
35
+ return "default";
36
+ if (value === "insensitive")
37
+ return "insensitive";
38
+ compileError(`unsupported query mode: ${String(value)}`);
39
+ }
40
+ function isStringPatternOp(op) {
41
+ return (op === "equals" ||
42
+ op === "contains" ||
43
+ op === "startsWith" ||
44
+ op === "endsWith" ||
45
+ op === "search");
46
+ }
47
+ function stringFilterSql(op, sqlCol, paramIndex, mode, dialect) {
48
+ switch (op) {
49
+ case "equals":
50
+ return mode === "insensitive"
51
+ ? dialect.ilike(sqlCol, paramIndex)
52
+ : dialect.whereOperators.equals(sqlCol, paramIndex);
53
+ case "contains":
54
+ case "startsWith":
55
+ case "endsWith":
56
+ return mode === "insensitive"
57
+ ? dialect.ilike(sqlCol, paramIndex)
58
+ : dialect.whereOperators[op](sqlCol, paramIndex);
59
+ case "search":
60
+ return dialect.regex(sqlCol, paramIndex, mode === "insensitive");
61
+ default: {
62
+ const _never = op;
63
+ compileError(`unsupported string filter: ${_never}`);
64
+ }
65
+ }
66
+ }
67
+ function pluginWhereOperators(col) {
68
+ if (col.kind === "fk")
69
+ return {};
70
+ return getColumnType(col.kind)?.whereOperators ?? {};
71
+ }
72
+ export function serializeColumnValue(col, value, dialect = postgresDialect) {
73
+ if (col.kind === "fk")
74
+ return value;
75
+ const plugin = getColumnType(col.kind);
76
+ if (plugin?.serializeValue) {
77
+ return plugin.serializeValue(col, value, dialect);
78
+ }
79
+ return value;
80
+ }
81
+ function defaultColumnRef(col) {
82
+ return quoteIdentifier(col.sqlName);
83
+ }
84
+ function qualifiedColumnRefForTable(table) {
85
+ return (col) => `${tableRef(table)}.${quoteIdentifier(col.sqlName)}`;
86
+ }
87
+ function parentPkRef(table) {
88
+ const pkSql = primaryKeySqlName(table);
89
+ return `${tableRef(table)}.${quoteIdentifier(pkSql)}`;
90
+ }
91
+ function knownWhereOperatorNames(col, dialect) {
92
+ return [
93
+ ...Object.keys(dialect.whereOperators),
94
+ ...Object.keys(pluginWhereOperators(col)),
95
+ ];
96
+ }
97
+ function throwUnsupportedWhereOperator(op, col, operators) {
98
+ compileError(`unsupported where operator "${op}" on column "${col.tsName}"`, {
99
+ code: QueryErrorCode.invalid_args,
100
+ columnTsName: col.tsName,
101
+ suggestions: suggestWhereOperator(op, operators),
102
+ });
103
+ }
104
+ function compileColumnCondition(col, rawValue, dialect, paramIndex, columnRef) {
105
+ const sqlCol = columnRef(col);
106
+ const spatialOps = pluginWhereOperators(col);
107
+ const conditions = [];
108
+ const params = [];
109
+ let nextParamIndex = paramIndex;
110
+ let impossible = false;
111
+ if (rawValue === null) {
112
+ conditions.push(dialect.whereOperators.isNull(sqlCol, nextParamIndex));
113
+ return { sql: conditions.join(" AND "), params, nextParamIndex };
114
+ }
115
+ if (!isOperatorObject(rawValue) || Array.isArray(rawValue)) {
116
+ conditions.push(dialect.whereOperators.equals(sqlCol, nextParamIndex));
117
+ params.push(serializeColumnValue(col, rawValue, dialect));
118
+ nextParamIndex++;
119
+ return { sql: conditions.join(" AND "), params, nextParamIndex };
120
+ }
121
+ const queryMode = parseQueryMode(rawValue.mode);
122
+ const knownOps = knownWhereOperatorNames(col, dialect);
123
+ const knownOpSet = new Set(knownOps);
124
+ const operatorKeys = Object.keys(rawValue).filter((key) => key !== "mode");
125
+ const knownKeys = operatorKeys.filter((key) => knownOpSet.has(key));
126
+ const unknownKeys = operatorKeys.filter((key) => !knownOpSet.has(key));
127
+ if (knownKeys.length === 0) {
128
+ if (operatorKeys.length > 0) {
129
+ const jsonColumn = col.kind === "json" || col.kind === "jsonb";
130
+ const typo = unknownKeys.find((key) => didYouMean(key, knownOps).length > 0);
131
+ if (typo || !jsonColumn) {
132
+ const badOp = typo ?? unknownKeys[0];
133
+ if (badOp) {
134
+ throwUnsupportedWhereOperator(badOp, col, knownOps);
135
+ }
136
+ }
137
+ }
138
+ conditions.push(dialect.whereOperators.equals(sqlCol, nextParamIndex));
139
+ params.push(rawValue);
140
+ nextParamIndex++;
141
+ return { sql: conditions.join(" AND "), params, nextParamIndex };
142
+ }
143
+ if (unknownKeys[0]) {
144
+ throwUnsupportedWhereOperator(unknownKeys[0], col, knownOps);
145
+ }
146
+ for (const [op, value] of Object.entries(rawValue)) {
147
+ if (op === "mode")
148
+ continue;
149
+ if (op in spatialOps) {
150
+ const operator = spatialOps[op];
151
+ if (!operator) {
152
+ throwUnsupportedWhereOperator(op, col, knownOps);
153
+ }
154
+ const compiled = operator.compile(sqlCol, value, col, nextParamIndex, dialect);
155
+ conditions.push(compiled.sql);
156
+ params.push(...compiled.params);
157
+ nextParamIndex += compiled.params.length;
158
+ continue;
159
+ }
160
+ if (!(op in dialect.whereOperators)) {
161
+ throwUnsupportedWhereOperator(op, col, knownOps);
162
+ }
163
+ const operator = op;
164
+ if (PARAMLESS_OPERATORS.has(operator)) {
165
+ conditions.push(dialect.whereOperators[operator](sqlCol, nextParamIndex));
166
+ continue;
167
+ }
168
+ if (Array.isArray(value) && value.length === 0) {
169
+ if (operator === "in") {
170
+ conditions.push("1=0");
171
+ impossible = true;
172
+ }
173
+ else if (operator === "notIn") {
174
+ conditions.push("1=1");
175
+ }
176
+ continue;
177
+ }
178
+ const transform = operatorParamTransform[operator];
179
+ const paramValue = operator === "in" || operator === "notIn"
180
+ ? Array.isArray(value)
181
+ ? value.map((item) => serializeColumnValue(col, item, dialect))
182
+ : value
183
+ : transform
184
+ ? transform(value)
185
+ : serializeColumnValue(col, value, dialect);
186
+ conditions.push(isStringPatternOp(operator)
187
+ ? stringFilterSql(operator, sqlCol, nextParamIndex, queryMode, dialect)
188
+ : dialect.whereOperators[operator](sqlCol, nextParamIndex));
189
+ params.push(paramValue);
190
+ nextParamIndex++;
191
+ }
192
+ return compiledResult(conditions.join(" AND "), params, nextParamIndex, impossible);
193
+ }
194
+ function compileExistsSubquery(existsSql, negate) {
195
+ return negate ? `NOT EXISTS (${existsSql})` : `EXISTS (${existsSql})`;
196
+ }
197
+ function compileRelationCondition(manifest, parentTable, relation, rawValue, dialect, paramIndex, manifestIndex) {
198
+ const m2m = findM2M(manifest, parentTable.accessor, relation.name);
199
+ const targetTable = manifest.tables[relation.targetAccessor];
200
+ if (!targetTable) {
201
+ return { sql: "", params: [], nextParamIndex: paramIndex };
202
+ }
203
+ const parentTableIndex = getTableIndex(manifestIndex, parentTable.accessor);
204
+ if (relation.cardinality === "one") {
205
+ if (!isOperatorObject(rawValue) || Array.isArray(rawValue)) {
206
+ compileError(`Relation filter "${relation.name}" must be a where object`, {
207
+ tableAccessor: parentTable.accessor,
208
+ tableSqlName: parentTable.sqlName,
209
+ });
210
+ }
211
+ const relAlias = "_rel";
212
+ const columnRef = (col) => `${quoteIdentifier(relAlias)}.${quoteIdentifier(col.sqlName)}`;
213
+ const nested = compileWhereNode(manifest, targetTable, rawValue, dialect, paramIndex, columnRef, manifestIndex);
214
+ const parentFkCol = columnByTsName(parentTableIndex, parentTable, relation.fkColumn);
215
+ const parentFkRef = parentFkCol
216
+ ? `${tableRef(parentTable)}.${quoteIdentifier(parentFkCol.sqlName)}`
217
+ : `${tableRef(parentTable)}.${quoteIdentifier(relation.fkSqlColumn)}`;
218
+ const targetPkSql = targetRelationPkSql(targetTable, relation);
219
+ const joinCond = `${quoteIdentifier(relAlias)}.${quoteIdentifier(targetPkSql)} = ${parentFkRef}`;
220
+ const whereParts = [joinCond];
221
+ if (nested.sql)
222
+ whereParts.push(nested.sql);
223
+ const existsSql = `SELECT 1 FROM ${tableRef(targetTable)} AS ${quoteIdentifier(relAlias)} WHERE ${whereParts.join(" AND ")}`;
224
+ return compiledResult(compileExistsSubquery(existsSql, false), nested.params, nested.nextParamIndex, nested.impossible);
225
+ }
226
+ if (!isOperatorObject(rawValue) || Array.isArray(rawValue)) {
227
+ compileError(`Relation filter "${relation.name}" must be an object with some, every, or none`, {
228
+ tableAccessor: parentTable.accessor,
229
+ tableSqlName: parentTable.sqlName,
230
+ });
231
+ }
232
+ const relationModes = ["some", "every", "none"];
233
+ const modes = relationModes.filter((key) => key in rawValue);
234
+ if (modes.length !== 1) {
235
+ const hintKey = Object.keys(rawValue)[0] ?? "some";
236
+ compileError(`Relation filter "${relation.name}" requires exactly one of some, every, or none`, {
237
+ tableAccessor: parentTable.accessor,
238
+ tableSqlName: parentTable.sqlName,
239
+ suggestions: suggestWhereOperator(hintKey, relationModes),
240
+ });
241
+ }
242
+ const mode = modes[0];
243
+ if (!mode) {
244
+ compileError(`Relation filter "${relation.name}" requires exactly one of some, every, or none`, {
245
+ tableAccessor: parentTable.accessor,
246
+ tableSqlName: parentTable.sqlName,
247
+ });
248
+ }
249
+ const extraKeys = Object.keys(rawValue).filter((key) => key !== mode);
250
+ if (extraKeys[0]) {
251
+ compileError(`unsupported relation filter "${extraKeys[0]}" on "${relation.name}"`, {
252
+ tableAccessor: parentTable.accessor,
253
+ tableSqlName: parentTable.sqlName,
254
+ suggestions: suggestWhereOperator(extraKeys[0], relationModes),
255
+ });
256
+ }
257
+ const nestedWhere = rawValue[mode];
258
+ if (nestedWhere !== undefined && !isOperatorObject(nestedWhere)) {
259
+ compileError(`Relation filter "${relation.name}.${mode}" must be a where object`, {
260
+ tableAccessor: parentTable.accessor,
261
+ tableSqlName: parentTable.sqlName,
262
+ });
263
+ }
264
+ const relAlias = "_rel";
265
+ const columnRef = (col) => `${quoteIdentifier(relAlias)}.${quoteIdentifier(col.sqlName)}`;
266
+ const nested = compileWhereNode(manifest, targetTable, (nestedWhere ?? {}), dialect, paramIndex, columnRef, manifestIndex);
267
+ let fromClause;
268
+ const joinParts = [];
269
+ if (m2m) {
270
+ const isLeft = m2m.leftAccessor === parentTable.accessor;
271
+ const throughTable = manifest.tables[m2m.throughAccessor];
272
+ if (!throughTable) {
273
+ return { sql: "", params: [], nextParamIndex: paramIndex };
274
+ }
275
+ const junctionAlias = "_jt";
276
+ const parentFkCol = isLeft ? m2m.leftFkColumn : m2m.rightFkColumn;
277
+ const targetFkCol = isLeft ? m2m.rightFkColumn : m2m.leftFkColumn;
278
+ const targetPkSql = targetRelationPkSql(targetTable);
279
+ fromClause = `${tableRef(throughTable)} AS ${quoteIdentifier(junctionAlias)} INNER JOIN ${tableRef(targetTable)} AS ${quoteIdentifier(relAlias)} ON ${quoteIdentifier(relAlias)}.${quoteIdentifier(targetPkSql)} = ${quoteIdentifier(junctionAlias)}.${quoteIdentifier(targetFkCol)}`;
280
+ joinParts.push(`${quoteIdentifier(junctionAlias)}.${quoteIdentifier(parentFkCol)} = ${parentPkRef(parentTable)}`);
281
+ }
282
+ else {
283
+ fromClause = `${tableRef(targetTable)} AS ${quoteIdentifier(relAlias)}`;
284
+ joinParts.push(`${quoteIdentifier(relAlias)}.${quoteIdentifier(relation.fkSqlColumn)} = ${parentPkRef(parentTable)}`);
285
+ }
286
+ switch (mode) {
287
+ case "some":
288
+ case "none": {
289
+ const whereParts = nested.sql
290
+ ? [...joinParts, nested.sql]
291
+ : joinParts;
292
+ const existsSql = `SELECT 1 FROM ${fromClause} WHERE ${whereParts.join(" AND ")}`;
293
+ return compiledResult(compileExistsSubquery(existsSql, mode === "none"), nested.params, nested.nextParamIndex, mode === "some" && nested.impossible);
294
+ }
295
+ case "every": {
296
+ const everyWhereParts = nested.sql
297
+ ? [...joinParts, `NOT (${nested.sql})`]
298
+ : [...joinParts, "FALSE"];
299
+ const everySql = `SELECT 1 FROM ${fromClause} WHERE ${everyWhereParts.join(" AND ")}`;
300
+ return {
301
+ sql: compileExistsSubquery(everySql, true),
302
+ params: nested.params,
303
+ nextParamIndex: nested.nextParamIndex,
304
+ };
305
+ }
306
+ default: {
307
+ const _never = mode;
308
+ compileError(`unsupported relation filter "${String(_never)}" on "${relation.name}"`, {
309
+ tableAccessor: parentTable.accessor,
310
+ tableSqlName: parentTable.sqlName,
311
+ });
312
+ }
313
+ }
314
+ }
315
+ function combinatorTableContext(table) {
316
+ return {
317
+ tableAccessor: table.accessor,
318
+ tableSqlName: table.sqlName,
319
+ };
320
+ }
321
+ function compileLogicalCombinator(combinator, value, manifest, table, dialect, startParamIndex, columnRef, manifestIndex) {
322
+ if (!Array.isArray(value)) {
323
+ compileError(`${combinator} must be an array of where objects`, {
324
+ ...combinatorTableContext(table),
325
+ });
326
+ }
327
+ if (value.length === 0) {
328
+ return compiledResult(combinator === "OR" ? "1=0" : "1=1", [], startParamIndex, combinator === "OR");
329
+ }
330
+ const parts = [];
331
+ const params = [];
332
+ let paramIndex = startParamIndex;
333
+ const childImpossible = [];
334
+ for (const item of value) {
335
+ if (!isOperatorObject(item)) {
336
+ compileError(`${combinator} items must be where objects`, {
337
+ ...combinatorTableContext(table),
338
+ });
339
+ }
340
+ const compiled = compileWhereNode(manifest, table, item, dialect, paramIndex, columnRef, manifestIndex);
341
+ parts.push(`(${compiled.sql || "1=1"})`);
342
+ params.push(...compiled.params);
343
+ paramIndex = compiled.nextParamIndex;
344
+ childImpossible.push(Boolean(compiled.impossible));
345
+ }
346
+ const joiner = combinator === "OR" ? " OR " : " AND ";
347
+ const impossible = combinator === "OR"
348
+ ? childImpossible.every(Boolean)
349
+ : childImpossible.some(Boolean);
350
+ return compiledResult(`(${parts.join(joiner)})`, params, paramIndex, impossible);
351
+ }
352
+ function compileWhereNode(manifest, table, where, dialect, startParamIndex, columnRef = defaultColumnRef, manifestIndex) {
353
+ const conditions = [];
354
+ const params = [];
355
+ let paramIndex = startParamIndex;
356
+ let impossible = false;
357
+ const tableIndex = getTableIndex(manifestIndex, table.accessor);
358
+ const relations = tableIndex?.effectiveRelationsByName ??
359
+ new Map(effectiveRelations(manifest, table).map((rel) => [rel.name, rel]));
360
+ for (const [key, value] of Object.entries(where)) {
361
+ if (value === undefined)
362
+ continue;
363
+ if (key === "AND" || key === "OR") {
364
+ const compiled = compileLogicalCombinator(key, value, manifest, table, dialect, paramIndex, columnRef, manifestIndex);
365
+ conditions.push(compiled.sql);
366
+ params.push(...compiled.params);
367
+ paramIndex = compiled.nextParamIndex;
368
+ if (compiled.impossible)
369
+ impossible = true;
370
+ continue;
371
+ }
372
+ if (key === "NOT") {
373
+ if (!isOperatorObject(value)) {
374
+ compileError("NOT must be a where object", {
375
+ ...combinatorTableContext(table),
376
+ });
377
+ }
378
+ const compiled = compileWhereNode(manifest, table, value, dialect, paramIndex, columnRef, manifestIndex);
379
+ conditions.push(`NOT (${compiled.sql || "1=1"})`);
380
+ params.push(...compiled.params);
381
+ paramIndex = compiled.nextParamIndex;
382
+ continue;
383
+ }
384
+ const relation = relations.get(key);
385
+ if (relation) {
386
+ const compiled = compileRelationCondition(manifest, table, relation, value, dialect, paramIndex, manifestIndex);
387
+ if (compiled.sql)
388
+ conditions.push(compiled.sql);
389
+ params.push(...compiled.params);
390
+ paramIndex = compiled.nextParamIndex;
391
+ if (compiled.impossible)
392
+ impossible = true;
393
+ continue;
394
+ }
395
+ const col = requireTsColumn(tableIndex, table, key, "where", "select");
396
+ const compiled = compileColumnCondition(col, value, dialect, paramIndex, columnRef);
397
+ if (compiled.sql)
398
+ conditions.push(compiled.sql);
399
+ params.push(...compiled.params);
400
+ paramIndex = compiled.nextParamIndex;
401
+ if (compiled.impossible)
402
+ impossible = true;
403
+ }
404
+ return compiledResult(conditions.join(" AND "), params, paramIndex, impossible);
405
+ }
406
+ export function compileWhere(manifest, table, where, dialect, startParamIndex = 1, manifestIndex, qualifyColumns = false, tableAlias) {
407
+ if (!where || Object.keys(where).length === 0) {
408
+ return { sql: "", params: [] };
409
+ }
410
+ const columnRef = tableAlias
411
+ ? (col) => `${quoteIdentifier(tableAlias)}.${quoteIdentifier(col.sqlName)}`
412
+ : qualifyColumns
413
+ ? qualifiedColumnRefForTable(table)
414
+ : defaultColumnRef;
415
+ const result = compileWhereNode(manifest, table, where, dialect, startParamIndex, columnRef, manifestIndex);
416
+ return {
417
+ sql: result.sql ? `WHERE ${result.sql}` : "",
418
+ params: result.params,
419
+ ...(result.impossible ? { impossible: true } : {}),
420
+ };
421
+ }
422
+ function logicalShapeKey(combinator, value) {
423
+ if (value.length === 0)
424
+ return `${combinator}:empty`;
425
+ return `${combinator}:${value
426
+ .filter((item) => !!item && typeof item === "object" && !Array.isArray(item))
427
+ .map((item) => whereShapeKey(item) || "{}")
428
+ .join(",")}`;
429
+ }
430
+ export function whereShapeKey(where) {
431
+ const parts = [];
432
+ for (const [key, value] of Object.entries(where)) {
433
+ if (key === "AND" && Array.isArray(value)) {
434
+ parts.push(logicalShapeKey("AND", value));
435
+ continue;
436
+ }
437
+ if (key === "OR" && Array.isArray(value)) {
438
+ parts.push(logicalShapeKey("OR", value));
439
+ continue;
440
+ }
441
+ if (key === "NOT" && isOperatorObject(value)) {
442
+ parts.push(`NOT:${whereShapeKey(value)}`);
443
+ continue;
444
+ }
445
+ if (isOperatorObject(value) && !(value instanceof Date)) {
446
+ const ops = Object.keys(value).sort();
447
+ if (ops.some((op) => op === "some" || op === "every" || op === "none")) {
448
+ const mode = ops.find((op) => op === "some" || op === "every" || op === "none");
449
+ const nested = value[mode ?? ""];
450
+ parts.push(`${key}:rel:${mode}:${isOperatorObject(nested) ? whereShapeKey(nested) : "{}"}`);
451
+ }
452
+ else if (ops.includes("in") || ops.includes("notIn")) {
453
+ const arr = value.in ?? value.notIn;
454
+ const len = Array.isArray(arr) ? arr.length : 0;
455
+ const op = ops.includes("in") ? "in" : "notIn";
456
+ parts.push(`${key}:${op}:${len}`);
457
+ }
458
+ else {
459
+ const modePart = typeof value.mode === "string" ? `:mode:${value.mode}` : "";
460
+ const opsForKey = ops.filter((op) => op !== "mode");
461
+ parts.push(`${key}:${opsForKey.join(",")}${modePart}`);
462
+ }
463
+ continue;
464
+ }
465
+ parts.push(`${key}:eq`);
466
+ }
467
+ return parts.join("&");
468
+ }
469
+ function collectWhereParams(manifest, table, where, dialect, manifestIndex) {
470
+ const params = [];
471
+ function walk(node, columnRef) {
472
+ const tableIndex = getTableIndex(manifestIndex, table.accessor);
473
+ const relations = tableIndex?.effectiveRelationsByName ??
474
+ new Map(effectiveRelations(manifest, table).map((rel) => [
475
+ rel.name,
476
+ rel,
477
+ ]));
478
+ for (const [key, value] of Object.entries(node)) {
479
+ if (key === "AND" || key === "OR") {
480
+ if (Array.isArray(value)) {
481
+ for (const item of value) {
482
+ if (item &&
483
+ typeof item === "object" &&
484
+ !Array.isArray(item)) {
485
+ walk(item, columnRef);
486
+ }
487
+ }
488
+ }
489
+ continue;
490
+ }
491
+ if (key === "NOT" && isOperatorObject(value)) {
492
+ walk(value, columnRef);
493
+ continue;
494
+ }
495
+ if (relations.get(key)) {
496
+ const compiled = compileRelationCondition(manifest, table, relations.get(key), value, dialect, 1, manifestIndex);
497
+ params.push(...compiled.params);
498
+ continue;
499
+ }
500
+ const col = columnByTsName(tableIndex, table, key);
501
+ if (!col)
502
+ continue;
503
+ const compiled = compileColumnCondition(col, value, dialect, 1, columnRef);
504
+ params.push(...compiled.params);
505
+ }
506
+ }
507
+ walk(where, defaultColumnRef);
508
+ return params;
509
+ }
510
+ function whereValuesFingerprint(manifest, table, where, dialect, manifestIndex) {
511
+ return JSON.stringify(collectWhereParams(manifest, table, where, dialect, manifestIndex));
512
+ }
513
+ export function getCachedWhereClause(manifest, table, where, dialect, startParamIndex = 1, manifestIndex, qualifyColumns = false) {
514
+ if (!where || Object.keys(where).length === 0) {
515
+ return { sql: "", params: [] };
516
+ }
517
+ const tableIndex = getTableIndex(manifestIndex, table.accessor);
518
+ const shape = qualifyColumns
519
+ ? `${whereShapeKey(where)}|qualified`
520
+ : whereShapeKey(where);
521
+ const shellCached = tableIndex?.whereClauseByShape.get(shape);
522
+ if (shellCached) {
523
+ return {
524
+ sql: shellCached.sql,
525
+ params: collectWhereParams(manifest, table, where, dialect, manifestIndex),
526
+ ...(shellCached.impossible ? { impossible: true } : {}),
527
+ };
528
+ }
529
+ const fingerprint = whereValuesFingerprint(manifest, table, where, dialect, manifestIndex);
530
+ const cacheKey = `${shape}\0${fingerprint}`;
531
+ const cached = tableIndex?.whereClauseByFingerprint.get(cacheKey);
532
+ if (cached)
533
+ return cached;
534
+ const compiled = compileWhere(manifest, table, where, dialect, startParamIndex, manifestIndex, qualifyColumns);
535
+ tableIndex?.whereClauseByShape.set(shape, {
536
+ sql: compiled.sql,
537
+ ...(compiled.impossible ? { impossible: true } : {}),
538
+ });
539
+ tableIndex?.whereClauseByFingerprint.set(cacheKey, compiled);
540
+ return compiled;
541
+ }
542
+ export function orderByShapeKey(orderBy, tableAlias) {
543
+ if (!orderBy || Object.keys(orderBy).length === 0)
544
+ return "";
545
+ const entries = Object.entries(orderBy)
546
+ .filter(([key]) => key !== "_count")
547
+ .map(([key, direction]) => typeof direction === "string"
548
+ ? `${key}:${direction.toUpperCase()}`
549
+ : "")
550
+ .filter(Boolean);
551
+ if (entries.length === 0)
552
+ return "";
553
+ const base = entries.join("|");
554
+ return tableAlias ? `${base}|@${tableAlias}` : base;
555
+ }
556
+ export function getCachedOrderByClause(table, orderBy, tableAlias, manifestIndex) {
557
+ if (!orderBy || Object.keys(orderBy).length === 0)
558
+ return "";
559
+ const tableIndex = getTableIndex(manifestIndex, table.accessor);
560
+ const shape = orderByShapeKey(orderBy, tableAlias);
561
+ if (!shape)
562
+ return "";
563
+ if (!tableIndex) {
564
+ return compileOrderBy(table, orderBy, tableAlias, manifestIndex);
565
+ }
566
+ return getOrSetSqlCache(tableIndex.orderBySqlByShape, shape, () => compileOrderBy(table, orderBy, tableAlias, manifestIndex));
567
+ }
568
+ export function isImpossibleWhereSql(sql) {
569
+ if (!sql)
570
+ return false;
571
+ return /^1\s*=\s*0$/.test(sql.trim());
572
+ }
573
+ export function isImpossibleWhere(whereSql) {
574
+ if (!whereSql)
575
+ return false;
576
+ return isImpossibleWhereSql(whereSql.replace(/^WHERE\s+/i, ""));
577
+ }
578
+ export function compileOrderBy(table, orderBy, tableAlias, manifestIndex) {
579
+ if (!orderBy || Object.keys(orderBy).length === 0)
580
+ return "";
581
+ const tableIndex = getTableIndex(manifestIndex, table.accessor);
582
+ const prefix = tableAlias ? `${quoteIdentifier(tableAlias)}.` : "";
583
+ const parts = [];
584
+ for (const [tsKey, direction] of Object.entries(orderBy)) {
585
+ if (tsKey === "_count" || typeof direction !== "string")
586
+ continue;
587
+ const col = columnByTsName(tableIndex, table, tsKey);
588
+ if (!col)
589
+ continue;
590
+ const dir = direction.toUpperCase() === "DESC" ? "DESC" : "ASC";
591
+ parts.push(`${prefix}${quoteIdentifier(col.sqlName)} ${dir}`);
592
+ }
593
+ return parts.length > 0 ? `ORDER BY ${parts.join(", ")}` : "";
594
+ }
595
+ export function normalizeSelectColumns(select) {
596
+ if (!select)
597
+ return undefined;
598
+ if (Array.isArray(select))
599
+ return select;
600
+ return Object.entries(select)
601
+ .filter(([, enabled]) => enabled === true)
602
+ .map(([key]) => key);
603
+ }
604
+ /** Columns returned by default SELECT (omits `.hidden()` unless `includeHidden` or explicitly selected). */
605
+ export function columnsForOutput(tableIndex, table, select, includeHidden) {
606
+ if (select && select.length > 0) {
607
+ return columnsByTsNames(tableIndex, table, select);
608
+ }
609
+ if (includeHidden) {
610
+ return table.columns;
611
+ }
612
+ return table.columns.filter((col) => col.hidden !== true);
613
+ }
614
+ function aliasToTsName(expression, col) {
615
+ if (col.sqlName === col.tsName)
616
+ return expression;
617
+ return `${expression} AS ${quoteIdentifier(col.tsName)}`;
618
+ }
619
+ function selectExpression(col) {
620
+ if (col.kind === "fk") {
621
+ return aliasToTsName(quoteIdentifier(col.sqlName), col);
622
+ }
623
+ const plugin = getColumnType(col.kind);
624
+ if (plugin?.selectExpression) {
625
+ return aliasToTsName(plugin.selectExpression(col), col);
626
+ }
627
+ return aliasToTsName(quoteIdentifier(col.sqlName), col);
628
+ }
629
+ export function buildSelectColumns(table, select, manifestIndex, includeHidden, tableAlias) {
630
+ const tableIndex = getTableIndex(manifestIndex, table.accessor);
631
+ const cols = columnsForOutput(tableIndex, table, select, includeHidden);
632
+ const prefix = tableAlias ? `${quoteIdentifier(tableAlias)}.` : "";
633
+ return cols.map((c) => `${prefix}${selectExpression(c)}`).join(", ");
634
+ }
635
+ export function buildQualifiedSelectColumns(table, select, manifestIndex, includeHidden) {
636
+ const ref = tableRef(table);
637
+ const tableIndex = getTableIndex(manifestIndex, table.accessor);
638
+ const cols = columnsForOutput(tableIndex, table, select, includeHidden);
639
+ return cols.map((c) => `${ref}.${selectExpression(c)}`).join(", ");
640
+ }
641
+ export function buildFindByIdQuery(table, select, manifestIndex, includeHidden) {
642
+ const { sqlName } = requireScalarPrimaryKey(table);
643
+ const sqlCol = quoteIdentifier(sqlName);
644
+ const selectCols = buildSelectColumns(table, select, manifestIndex, includeHidden);
645
+ return `SELECT ${selectCols} FROM ${tableRef(table)} WHERE ${sqlCol} = $1`;
646
+ }
647
+ export function buildFindAllQuery(table) {
648
+ return `SELECT ${buildSelectColumns(table)} FROM ${tableRef(table)}`;
649
+ }
650
+ export function normalizeLimitOffset(value, label) {
651
+ if (typeof value !== "number" || !Number.isInteger(value) || value < 0) {
652
+ compileError(`${label} must be a non-negative integer, got ${JSON.stringify(value)}`);
653
+ }
654
+ return value;
655
+ }
656
+ export function buildFindManyQuery(table, whereSql, orderSql, take, skip, distinctOn, extraSelectCols, joinClauses, manifestIndex, groupBySql, select, includeHidden) {
657
+ const hasJoins = Boolean(joinClauses && joinClauses.length > 0);
658
+ const tableIndex = getTableIndex(manifestIndex, table.accessor);
659
+ const selectCols = hasJoins
660
+ ? buildQualifiedSelectColumns(table, select, manifestIndex, includeHidden)
661
+ : buildSelectColumns(table, select, manifestIndex, includeHidden);
662
+ let sql = "SELECT ";
663
+ if (distinctOn && distinctOn.length > 0) {
664
+ const distinctCols = columnsByTsNames(tableIndex, table, distinctOn)
665
+ .map((col) => hasJoins
666
+ ? `${tableRef(table)}.${quoteIdentifier(col.sqlName)}`
667
+ : quoteIdentifier(col.sqlName))
668
+ .join(", ");
669
+ sql += `DISTINCT ON (${distinctCols}) `;
670
+ }
671
+ sql += selectCols;
672
+ if (extraSelectCols && extraSelectCols.length > 0) {
673
+ sql += `, ${extraSelectCols.join(", ")}`;
674
+ }
675
+ sql += ` FROM ${tableRef(table)}`;
676
+ if (joinClauses && joinClauses.length > 0) {
677
+ sql += ` ${joinClauses.join(" ")}`;
678
+ }
679
+ if (whereSql)
680
+ sql += ` ${whereSql}`;
681
+ if (groupBySql)
682
+ sql += ` ${groupBySql}`;
683
+ if (orderSql)
684
+ sql += ` ${orderSql}`;
685
+ if (take !== undefined) {
686
+ sql += ` LIMIT ${normalizeLimitOffset(take, "take")}`;
687
+ }
688
+ if (skip !== undefined) {
689
+ sql += ` OFFSET ${normalizeLimitOffset(skip, "skip")}`;
690
+ }
691
+ return sql;
692
+ }
693
+ export function buildPaginateQuery(table, whereSql, orderSql, take, extraSelectCols, joinClauses, manifestIndex, select, includeHidden) {
694
+ return buildFindManyQuery(table, whereSql, orderSql, take + 1, undefined, undefined, extraSelectCols, joinClauses, manifestIndex, undefined, select, includeHidden);
695
+ }
696
+ export function buildExistsQuery(table, whereSql) {
697
+ let sql = `SELECT 1 FROM ${tableRef(table)}`;
698
+ if (whereSql)
699
+ sql += ` ${whereSql}`;
700
+ sql += " LIMIT 1";
701
+ return sql;
702
+ }
703
+ export function getCachedFindManyQuery(tableIndex, signature, build) {
704
+ if (!tableIndex)
705
+ return build();
706
+ return getOrSetSqlCache(tableIndex.findManySqlBySignature, signature, build);
707
+ }
708
+ //# sourceMappingURL=compile-where.js.map