@nicia-ai/typegraph 0.33.0 → 0.34.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 (244) hide show
  1. package/README.md +20 -0
  2. package/dist/backend/postgres/index.cjs +26 -22
  3. package/dist/backend/postgres/index.d.cts +5 -5
  4. package/dist/backend/postgres/index.d.ts +5 -5
  5. package/dist/backend/postgres/index.js +13 -9
  6. package/dist/backend/postgres/pglite.cjs +17 -13
  7. package/dist/backend/postgres/pglite.cjs.map +1 -1
  8. package/dist/backend/postgres/pglite.d.cts +2 -2
  9. package/dist/backend/postgres/pglite.d.ts +2 -2
  10. package/dist/backend/postgres/pglite.js +13 -9
  11. package/dist/backend/postgres/pglite.js.map +1 -1
  12. package/dist/backend/sqlite/index.cjs +24 -20
  13. package/dist/backend/sqlite/index.d.cts +6 -6
  14. package/dist/backend/sqlite/index.d.ts +6 -6
  15. package/dist/backend/sqlite/index.js +12 -8
  16. package/dist/backend/sqlite/libsql.cjs +18 -14
  17. package/dist/backend/sqlite/libsql.cjs.map +1 -1
  18. package/dist/backend/sqlite/libsql.d.cts +11 -4
  19. package/dist/backend/sqlite/libsql.d.ts +11 -4
  20. package/dist/backend/sqlite/libsql.js +14 -10
  21. package/dist/backend/sqlite/libsql.js.map +1 -1
  22. package/dist/backend/sqlite/local.cjs +18 -14
  23. package/dist/backend/sqlite/local.cjs.map +1 -1
  24. package/dist/backend/sqlite/local.d.cts +2 -2
  25. package/dist/backend/sqlite/local.d.ts +2 -2
  26. package/dist/backend/sqlite/local.js +13 -9
  27. package/dist/backend/sqlite/local.js.map +1 -1
  28. package/dist/chunk-2EBMPJBO.js +16 -0
  29. package/dist/chunk-2EBMPJBO.js.map +1 -0
  30. package/dist/{chunk-CFNT3ITG.cjs → chunk-2JXB3NYJ.cjs} +15 -35
  31. package/dist/chunk-2JXB3NYJ.cjs.map +1 -0
  32. package/dist/{chunk-GYVBEZAO.js → chunk-2MRAX3UE.js} +3 -18
  33. package/dist/chunk-2MRAX3UE.js.map +1 -0
  34. package/dist/{chunk-D3EWZ67S.cjs → chunk-3BRVBQQD.cjs} +999 -2675
  35. package/dist/chunk-3BRVBQQD.cjs.map +1 -0
  36. package/dist/{chunk-FDN43KXV.cjs → chunk-3Z6TW3CT.cjs} +20 -19
  37. package/dist/chunk-3Z6TW3CT.cjs.map +1 -0
  38. package/dist/{chunk-6NRFUHMS.js → chunk-4DIBPQNJ.js} +5 -4
  39. package/dist/chunk-4DIBPQNJ.js.map +1 -0
  40. package/dist/chunk-4S2FXGBJ.js +9 -0
  41. package/dist/chunk-4S2FXGBJ.js.map +1 -0
  42. package/dist/chunk-5IQIX7LU.cjs +2070 -0
  43. package/dist/chunk-5IQIX7LU.cjs.map +1 -0
  44. package/dist/{chunk-CWFPJ3RC.js → chunk-6GNSY7AV.js} +3 -3
  45. package/dist/{chunk-CWFPJ3RC.js.map → chunk-6GNSY7AV.js.map} +1 -1
  46. package/dist/{chunk-NG5N7R45.cjs → chunk-6HVOHZHS.cjs} +184 -117
  47. package/dist/chunk-6HVOHZHS.cjs.map +1 -0
  48. package/dist/chunk-6SCVW4AS.js +20 -0
  49. package/dist/chunk-6SCVW4AS.js.map +1 -0
  50. package/dist/chunk-A7SZZFAG.js +2010 -0
  51. package/dist/chunk-A7SZZFAG.js.map +1 -0
  52. package/dist/chunk-ACSVZPK7.cjs +21 -0
  53. package/dist/chunk-ACSVZPK7.cjs.map +1 -0
  54. package/dist/{chunk-IZ4PCL47.js → chunk-BVTWD66D.js} +4 -66
  55. package/dist/chunk-BVTWD66D.js.map +1 -0
  56. package/dist/{chunk-QBYTJSAY.js → chunk-CHXTTSC6.js} +5 -39
  57. package/dist/chunk-CHXTTSC6.js.map +1 -0
  58. package/dist/{chunk-DJYRRDHA.js → chunk-CYX2JTDG.js} +3 -3
  59. package/dist/{chunk-DJYRRDHA.js.map → chunk-CYX2JTDG.js.map} +1 -1
  60. package/dist/{chunk-PRMKPFN5.js → chunk-DEQFL35G.js} +4 -3
  61. package/dist/chunk-DEQFL35G.js.map +1 -0
  62. package/dist/chunk-DI74SMXS.cjs +29 -0
  63. package/dist/chunk-DI74SMXS.cjs.map +1 -0
  64. package/dist/{chunk-WWVV44DY.cjs → chunk-G6SOJEDI.cjs} +11 -11
  65. package/dist/{chunk-WWVV44DY.cjs.map → chunk-G6SOJEDI.cjs.map} +1 -1
  66. package/dist/{chunk-LM2IC4ZJ.cjs → chunk-GQANNXXT.cjs} +15 -14
  67. package/dist/chunk-GQANNXXT.cjs.map +1 -0
  68. package/dist/chunk-GZCNZ7QW.js +24 -0
  69. package/dist/chunk-GZCNZ7QW.js.map +1 -0
  70. package/dist/{chunk-KTIEB7VH.cjs → chunk-HK5SATYM.cjs} +35 -58
  71. package/dist/chunk-HK5SATYM.cjs.map +1 -0
  72. package/dist/chunk-HMFK4BXR.cjs +12 -0
  73. package/dist/{chunk-6ZR4QC3O.cjs.map → chunk-HMFK4BXR.cjs.map} +1 -1
  74. package/dist/chunk-ITE4IXAK.js +25 -0
  75. package/dist/chunk-ITE4IXAK.js.map +1 -0
  76. package/dist/chunk-JJRCLAUN.cjs +155 -0
  77. package/dist/chunk-JJRCLAUN.cjs.map +1 -0
  78. package/dist/{chunk-ZFWNO3GE.cjs → chunk-JQGIPGVL.cjs} +66 -2
  79. package/dist/chunk-JQGIPGVL.cjs.map +1 -0
  80. package/dist/{chunk-XVUJY72M.js → chunk-K2TGS3EN.js} +105 -38
  81. package/dist/chunk-K2TGS3EN.js.map +1 -0
  82. package/dist/chunk-K34OPSJK.js +29 -0
  83. package/dist/chunk-K34OPSJK.js.map +1 -0
  84. package/dist/{chunk-YW7GHMF4.cjs → chunk-KGC4LY5T.cjs} +18 -17
  85. package/dist/chunk-KGC4LY5T.cjs.map +1 -0
  86. package/dist/{chunk-SST7XDY6.cjs → chunk-KNHYNQQ3.cjs} +111 -72
  87. package/dist/chunk-KNHYNQQ3.cjs.map +1 -0
  88. package/dist/{chunk-C4C4Q7A6.js → chunk-KUTNI6SI.js} +6 -21
  89. package/dist/chunk-KUTNI6SI.js.map +1 -0
  90. package/dist/{chunk-OS6KS3HV.js → chunk-LANHCOXB.js} +8 -7
  91. package/dist/chunk-LANHCOXB.js.map +1 -0
  92. package/dist/chunk-N6WTUSBV.cjs +23 -0
  93. package/dist/chunk-N6WTUSBV.cjs.map +1 -0
  94. package/dist/{chunk-73KASJGO.cjs → chunk-NAUA4PW5.cjs} +4 -28
  95. package/dist/chunk-NAUA4PW5.cjs.map +1 -0
  96. package/dist/chunk-QV245VMN.js +145 -0
  97. package/dist/chunk-QV245VMN.js.map +1 -0
  98. package/dist/{chunk-CEF2QOTO.js → chunk-RBV7YCQD.js} +4 -3
  99. package/dist/chunk-RBV7YCQD.js.map +1 -0
  100. package/dist/{chunk-OIE2UPVG.cjs → chunk-RHZWOJVA.cjs} +2 -19
  101. package/dist/chunk-RHZWOJVA.cjs.map +1 -0
  102. package/dist/{chunk-3IR2GEMJ.cjs → chunk-RRKQAKD6.cjs} +10 -9
  103. package/dist/chunk-RRKQAKD6.cjs.map +1 -0
  104. package/dist/{chunk-UPDMJEW6.js → chunk-RY2VIWAM.js} +11 -8
  105. package/dist/chunk-RY2VIWAM.js.map +1 -0
  106. package/dist/{chunk-PIHXFF73.js → chunk-SHLY4MCM.js} +3 -3
  107. package/dist/{chunk-PIHXFF73.js.map → chunk-SHLY4MCM.js.map} +1 -1
  108. package/dist/{chunk-SPT5JMME.js → chunk-T2DWPQNE.js} +103 -64
  109. package/dist/chunk-T2DWPQNE.js.map +1 -0
  110. package/dist/{chunk-WYTUF3QO.js → chunk-T46GR2VY.js} +3 -3
  111. package/dist/{chunk-WYTUF3QO.js.map → chunk-T46GR2VY.js.map} +1 -1
  112. package/dist/{chunk-MBLXCV2Y.cjs → chunk-T6SPUYLX.cjs} +5 -43
  113. package/dist/chunk-T6SPUYLX.cjs.map +1 -0
  114. package/dist/{chunk-3QRBXYT5.cjs → chunk-TGJE22RM.cjs} +6 -6
  115. package/dist/{chunk-3QRBXYT5.cjs.map → chunk-TGJE22RM.cjs.map} +1 -1
  116. package/dist/{chunk-BF3ZDEUE.cjs → chunk-TOTPVGOG.cjs} +70 -69
  117. package/dist/chunk-TOTPVGOG.cjs.map +1 -0
  118. package/dist/{chunk-KY5SDVA7.cjs → chunk-UJLOZYKT.cjs} +90 -87
  119. package/dist/chunk-UJLOZYKT.cjs.map +1 -0
  120. package/dist/{chunk-I4W7MKVG.cjs → chunk-UNNPKQUG.cjs} +19 -155
  121. package/dist/chunk-UNNPKQUG.cjs.map +1 -0
  122. package/dist/chunk-UR7W5YQK.js +18 -0
  123. package/dist/chunk-UR7W5YQK.js.map +1 -0
  124. package/dist/{chunk-PYD3UCDU.js → chunk-V3QNTB7Z.js} +19 -148
  125. package/dist/chunk-V3QNTB7Z.js.map +1 -0
  126. package/dist/{chunk-XKNAK27R.js → chunk-VCGO4V26.js} +4 -3
  127. package/dist/chunk-VCGO4V26.js.map +1 -0
  128. package/dist/{chunk-EF3Z645U.cjs → chunk-WL2SG7ZV.cjs} +6 -72
  129. package/dist/chunk-WL2SG7ZV.cjs.map +1 -0
  130. package/dist/{chunk-WLP6TKFB.js → chunk-WLRIWYHQ.js} +640 -2307
  131. package/dist/chunk-WLRIWYHQ.js.map +1 -0
  132. package/dist/{chunk-4RBAJVEC.cjs → chunk-WVC2Q2FD.cjs} +5 -5
  133. package/dist/{chunk-4RBAJVEC.cjs.map → chunk-WVC2Q2FD.cjs.map} +1 -1
  134. package/dist/chunk-XXVEULWX.js +39 -0
  135. package/dist/chunk-XXVEULWX.js.map +1 -0
  136. package/dist/chunk-YAZE7QZR.cjs +32 -0
  137. package/dist/chunk-YAZE7QZR.cjs.map +1 -0
  138. package/dist/chunk-YC6PWTHY.cjs +44 -0
  139. package/dist/chunk-YC6PWTHY.cjs.map +1 -0
  140. package/dist/chunk-Z5HBYM7U.cjs +11 -0
  141. package/dist/chunk-Z5HBYM7U.cjs.map +1 -0
  142. package/dist/{chunk-454I6GDF.js → chunk-Z5XBMDXU.js} +63 -3
  143. package/dist/chunk-Z5XBMDXU.js.map +1 -0
  144. package/dist/{chunk-3642VBVC.cjs → chunk-ZP5WHI4C.cjs} +106 -80
  145. package/dist/chunk-ZP5WHI4C.cjs.map +1 -0
  146. package/dist/{chunk-YCV72E6L.js → chunk-ZURTTSCM.js} +4 -25
  147. package/dist/chunk-ZURTTSCM.js.map +1 -0
  148. package/dist/{chunk-JHOBXD7O.js → chunk-ZV5N4DBG.js} +46 -20
  149. package/dist/chunk-ZV5N4DBG.js.map +1 -0
  150. package/dist/chunk-ZY4GXIVN.cjs +32 -0
  151. package/dist/chunk-ZY4GXIVN.cjs.map +1 -0
  152. package/dist/{ddl-CVnHJcar.d.ts → ddl-BrmPg61z.d.ts} +3 -3
  153. package/dist/{ddl-Dxt_fBHr.d.cts → ddl-j1-sH6lE.d.cts} +3 -3
  154. package/dist/graph-extension/index.cjs +18 -17
  155. package/dist/graph-extension/index.js +3 -2
  156. package/dist/graph-merge/index.cjs +294 -146
  157. package/dist/graph-merge/index.cjs.map +1 -1
  158. package/dist/graph-merge/index.d.cts +3 -3
  159. package/dist/graph-merge/index.d.ts +3 -3
  160. package/dist/graph-merge/index.js +172 -24
  161. package/dist/graph-merge/index.js.map +1 -1
  162. package/dist/index.cjs +181 -172
  163. package/dist/index.cjs.map +1 -1
  164. package/dist/index.d.cts +7 -7
  165. package/dist/index.d.ts +7 -7
  166. package/dist/index.js +30 -22
  167. package/dist/index.js.map +1 -1
  168. package/dist/indexes/index.cjs +27 -25
  169. package/dist/indexes/index.d.cts +4 -4
  170. package/dist/indexes/index.d.ts +4 -4
  171. package/dist/indexes/index.js +9 -7
  172. package/dist/interchange/index.cjs +25 -16
  173. package/dist/interchange/index.d.cts +3 -3
  174. package/dist/interchange/index.d.ts +3 -3
  175. package/dist/interchange/index.js +12 -3
  176. package/dist/{manager-C0_r0ALf.d.ts → manager-BZUeN_BU.d.ts} +1 -1
  177. package/dist/{manager-BYl_zyZR.d.cts → manager-ms9cKdc6.d.cts} +1 -1
  178. package/dist/{postgres-MzBPkYzq.d.ts → postgres-BhpxGMC5.d.cts} +1 -1
  179. package/dist/{postgres-_9psmVGm.d.cts → postgres-CWHLwNl1.d.ts} +1 -1
  180. package/dist/profiler/index.cjs +3 -3
  181. package/dist/profiler/index.d.cts +5 -5
  182. package/dist/profiler/index.d.ts +5 -5
  183. package/dist/profiler/index.js +2 -2
  184. package/dist/provenance/index.cjs +591 -0
  185. package/dist/provenance/index.cjs.map +1 -0
  186. package/dist/provenance/index.d.cts +73 -0
  187. package/dist/provenance/index.d.ts +73 -0
  188. package/dist/provenance/index.js +589 -0
  189. package/dist/provenance/index.js.map +1 -0
  190. package/dist/schema/index.cjs +36 -32
  191. package/dist/schema/index.cjs.map +1 -1
  192. package/dist/schema/index.d.cts +4 -4
  193. package/dist/schema/index.d.ts +4 -4
  194. package/dist/schema/index.js +13 -9
  195. package/dist/schema/index.js.map +1 -1
  196. package/dist/{sqlite-gm44dShb.d.ts → sqlite-CGu5SLsw.d.ts} +1 -1
  197. package/dist/{sqlite-BfksSb45.d.cts → sqlite-XrDxNaa7.d.cts} +1 -1
  198. package/dist/{store-view-FfE5_7eo.d.ts → store-view-BlgnhEYf.d.ts} +33 -5
  199. package/dist/{store-view-9jQ2rPum.d.cts → store-view-D09po4H6.d.cts} +33 -5
  200. package/dist/{types-CdWxK-nD.d.ts → types-B1TbZJYS.d.ts} +1 -1
  201. package/dist/{types-DGylBhWo.d.ts → types-D1Sqeyxy.d.ts} +7 -3
  202. package/dist/{types-B6mhrcLv.d.cts → types-DiE1AO7p.d.cts} +1 -1
  203. package/dist/{types-C_UnQOQp.d.cts → types-DkNnF-7K.d.cts} +7 -3
  204. package/dist/{where-Bpp2ul_z.d.cts → where-DKjuofPr.d.cts} +1 -1
  205. package/dist/{where-BsEXraKW.d.ts → where-DrUIIWHb.d.ts} +1 -1
  206. package/package.json +8 -2
  207. package/dist/chunk-2YMGNYXY.js +0 -40
  208. package/dist/chunk-2YMGNYXY.js.map +0 -1
  209. package/dist/chunk-3642VBVC.cjs.map +0 -1
  210. package/dist/chunk-3IR2GEMJ.cjs.map +0 -1
  211. package/dist/chunk-454I6GDF.js.map +0 -1
  212. package/dist/chunk-6NRFUHMS.js.map +0 -1
  213. package/dist/chunk-6ZR4QC3O.cjs +0 -12
  214. package/dist/chunk-73KASJGO.cjs.map +0 -1
  215. package/dist/chunk-BF3ZDEUE.cjs.map +0 -1
  216. package/dist/chunk-C4C4Q7A6.js.map +0 -1
  217. package/dist/chunk-CEF2QOTO.js.map +0 -1
  218. package/dist/chunk-CFNT3ITG.cjs.map +0 -1
  219. package/dist/chunk-D3EWZ67S.cjs.map +0 -1
  220. package/dist/chunk-EF3Z645U.cjs.map +0 -1
  221. package/dist/chunk-FDN43KXV.cjs.map +0 -1
  222. package/dist/chunk-GYVBEZAO.js.map +0 -1
  223. package/dist/chunk-I4W7MKVG.cjs.map +0 -1
  224. package/dist/chunk-IZ4PCL47.js.map +0 -1
  225. package/dist/chunk-JHOBXD7O.js.map +0 -1
  226. package/dist/chunk-KTIEB7VH.cjs.map +0 -1
  227. package/dist/chunk-KY5SDVA7.cjs.map +0 -1
  228. package/dist/chunk-LM2IC4ZJ.cjs.map +0 -1
  229. package/dist/chunk-MBLXCV2Y.cjs.map +0 -1
  230. package/dist/chunk-NG5N7R45.cjs.map +0 -1
  231. package/dist/chunk-OIE2UPVG.cjs.map +0 -1
  232. package/dist/chunk-OS6KS3HV.js.map +0 -1
  233. package/dist/chunk-PRMKPFN5.js.map +0 -1
  234. package/dist/chunk-PYD3UCDU.js.map +0 -1
  235. package/dist/chunk-QBYTJSAY.js.map +0 -1
  236. package/dist/chunk-SPT5JMME.js.map +0 -1
  237. package/dist/chunk-SST7XDY6.cjs.map +0 -1
  238. package/dist/chunk-UPDMJEW6.js.map +0 -1
  239. package/dist/chunk-WLP6TKFB.js.map +0 -1
  240. package/dist/chunk-XKNAK27R.js.map +0 -1
  241. package/dist/chunk-XVUJY72M.js.map +0 -1
  242. package/dist/chunk-YCV72E6L.js.map +0 -1
  243. package/dist/chunk-YW7GHMF4.cjs.map +0 -1
  244. package/dist/chunk-ZFWNO3GE.cjs.map +0 -1
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/utils/sql-errors.ts","../src/schema/canonical.ts"],"names":["ConfigurationError"],"mappings":";;;;;AAuBA,IAAM,4BAAA,GAA+B,eAAA;AACrC,IAAM,yBAAA,GAA4B,cAAA;AAClC,IAAM,0BAAA,GAA6B,eAAA;AACnC,IAAM,mCAAA,GACJ,oDAAA;AASF,IAAM,6BAAA,GAAgC,OAAA;AAiBtC,UAAU,WAAW,KAAA,EAAgD;AACnE,EAAA,MAAM,IAAA,uBAAW,GAAA,EAAa;AAC9B,EAAA,IAAI,OAAA,GAAmB,KAAA;AACvB,EAAA,OAAO,OAAA,KAAY,UAAa,OAAA,KAAY,IAAA,IAAQ,CAAC,IAAA,CAAK,GAAA,CAAI,OAAO,CAAA,EAAG;AACtE,IAAA,IAAA,CAAK,IAAI,OAAO,CAAA;AAChB,IAAA,MAAM,OAAA;AACN,IAAA,OAAA,GACE,gBAAgB,OAAO,CAAA,GAAI,QAAQ,GAAA,CAAI,OAAA,EAAS,OAAO,CAAA,GAAI,MAAA;AAAA,EAC/D;AACF;AAEA,SAAS,gBAAgB,KAAA,EAAiC;AACxD,EAAA,OACG,OAAO,KAAA,KAAU,QAAA,IAAY,KAAA,KAAU,IAAA,IAAS,OAAO,KAAA,KAAU,UAAA;AAEtE;AAMA,SAAS,yBAAyB,IAAA,EAAwB;AACxD,EAAA,OACE,gBAAgB,IAAI,CAAA,IACpB,QAAQ,GAAA,CAAI,IAAA,EAAM,MAAM,CAAA,KAAM,6BAAA;AAElC;AAEA,SAAS,gBAAgB,IAAA,EAAmC;AAC1D,EAAA,IAAI,OAAO,IAAA,KAAS,QAAA,EAAU,OAAO,IAAA;AACrC,EAAA,IAAI,IAAA,YAAgB,KAAA,EAAO,OAAO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AA,GAAU,aAAa,IAAI,CAAA;AACjC,IAAA,MAAM,KAAA,GAAQ,0BAAA,CAA2B,IAAA,CAAK,OAAO,CAAA;AACrD,IAAA,IAAI,KAAA,EAAO;AACT,MAAA,OAAO;AAAA,QACL,QAAA,EAAU,MAAA,CAAO,KAAA,CAAM,CAAC,CAAC,CAAA;AAAA,QACzB,MAAA,EAAQ,MAAM,CAAC,CAAA,KAAM,SAAY,MAAA,GAAY,MAAA,CAAO,KAAA,CAAM,CAAC,CAAC;AAAA,OAC9D;AAAA,IACF;AAAA,EACF;AACA,EAAA,OAAO,MAAA;AACT;;;ACtNO,SAAS,cAAA,CAAe,MAAc,KAAA,EAAyB;AACpE,EAAA,IAAI,KAAA,KAAU,QAAQ,OAAO,KAAA,KAAU,YAAY,CAAC,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,EAAG;AACxE,IAAA,MAAM,SAAkC,EAAC;AACzC,IAAA,KAAA,MAAW,OAAO,MAAA,CAAO,IAAA,CAAK,KAAK,CAAA,CAAE,UAAS,EAAG;AAC/C,MAAA,MAAA,CAAO,GAAG,CAAA,GAAK,KAAA,CAAkC,GAAG,CAAA;AAAA,IACtD;AACA,IAAA,OAAO,MAAA;AAAA,EACT;AACA,EAAA,OAAO,KAAA;AACT;AAaO,SAAS,cAAA,CAAe,GAAY,CAAA,EAAqB;AAC9D,EAAA,OACE,IAAA,CAAK,UAAU,CAAA,EAAG,cAAc,MAAM,IAAA,CAAK,SAAA,CAAU,GAAG,cAAc,CAAA;AAE1E","file":"chunk-OIE2UPVG.cjs","sourcesContent":["/**\n * Cross-dialect detection of \"this relation does not exist yet\" errors.\n *\n * Shared so the schema bootstrap (`loadActiveSchemaWithBootstrap`) and\n * the durable contribution-materialization gate (#135) agree on what a\n * missing-table failure looks like, and — critically — so neither one\n * swallows a genuine system fault (connection/permission/driver error)\n * as a benign \"not bootstrapped yet\".\n *\n * Detection walks the error's `cause` chain because Drizzle (>= the\n * `DrizzleQueryError` era, drizzle-orm ≥ 0.36) wraps every failure from a\n * query-builder call (`db.select()`, `db.insert()`, …): the wrapper's\n * `.message` becomes the failed query text and the real driver error —\n * which carries both the missing-relation text and the SQLSTATE — is\n * preserved on `.cause`. node-postgres / postgres-js nest the pg error\n * one link deep; better-sqlite3 throws it unwrapped; raw `client.query()`\n * fast paths surface it directly. Walking the chain makes the check\n * wrapper- and driver-agnostic instead of only matching the outermost\n * `.message`, which on Postgres is just the SQL string.\n */\n\nimport { ConfigurationError } from \"../errors\";\n\nconst SQLITE_MISSING_TABLE_PATTERN = \"no such table\";\nconst SQLITE_GENERIC_ERROR_CODE = \"SQLITE_ERROR\";\nconst DRIZZLE_QUERY_ERROR_PREFIX = \"Failed query:\";\nconst POSTGRES_UNDEFINED_RELATION_PATTERN =\n /\\b(?:relation|table)\\s+\"[^\"]+\"\\s+does not exist\\b/i;\n\n/**\n * SQLSTATE for PostgreSQL `undefined_table`. Preferred over the\n * human-readable message: it is locale-independent (the \"... does not\n * exist\" text is translated under a non-English `lc_messages`) and is\n * preserved on the underlying driver error even when Drizzle overwrites\n * `.message` with the query text.\n */\nconst POSTGRES_UNDEFINED_TABLE_CODE = \"42P01\";\n\n/**\n * Yields an error and each error reachable by following `.cause`,\n * outermost first. `seen` guards the pathological cyclic-cause case so a\n * self-referential chain can't spin forever.\n *\n * The walk intentionally follows `.cause` through *non-`Error`* links, not just\n * `Error` instances: postgres-js surfaces its driver error as a plain object\n * (message + SQLSTATE `code`) on a Drizzle wrapper's `.cause`, so stopping at\n * the first non-`Error` link would miss it (see the postgres-js test). A plain\n * object is classified by its locale-independent SQLSTATE alone\n * ({@link isPostgresUndefinedTable}); the looser SQLite message substring is\n * consulted only for `Error` instances and raw strings\n * ({@link missingTableMessage}), so an unrelated object in a cause chain that\n * merely mentions one of those phrases is not mistaken for a missing table.\n */\nfunction* errorChain(error: unknown): Generator<unknown, void, void> {\n const seen = new Set<unknown>();\n let current: unknown = error;\n while (current !== undefined && current !== null && !seen.has(current)) {\n seen.add(current);\n yield current;\n current =\n canReadProperty(current) ? Reflect.get(current, \"cause\") : undefined;\n }\n}\n\nfunction canReadProperty(value: unknown): value is object {\n return (\n (typeof value === \"object\" && value !== null) || typeof value === \"function\"\n );\n}\n\n/**\n * Whether a single chain link is a PostgreSQL `undefined_table` failure,\n * identified by its SQLSTATE rather than a message substring.\n */\nfunction isPostgresUndefinedTable(link: unknown): boolean {\n return (\n canReadProperty(link) &&\n Reflect.get(link, \"code\") === POSTGRES_UNDEFINED_TABLE_CODE\n );\n}\n\nfunction messageProperty(link: unknown): string | undefined {\n if (typeof link === \"string\") return link;\n if (link instanceof Error) return link.message;\n const message =\n canReadProperty(link) ?\n (Reflect.get(link, \"message\") as unknown)\n : undefined;\n return typeof message === \"string\" ? message : undefined;\n}\n\nfunction errorMessage(link: unknown): string {\n return messageProperty(link) ?? String(link);\n}\n\n/**\n * The message a SQLite missing-table substring match may be tested against — but\n * only for `Error` instances and raw `string` links, never an arbitrary plain\n * object.\n *\n * Generic \"does not exist\" is deliberately not substring-matched: PostgreSQL uses\n * that phrase for undefined columns, functions, types, and relations. PostgreSQL\n * missing tables are classified by SQLSTATE 42P01 when available, or by the\n * narrower driver-message shape `relation/table \"...\" does not exist` when a\n * bring-your-own driver omits SQLSTATE. SQLite does not expose a portable\n * SQLSTATE here, so the narrow \"no such table\" engine message is still accepted.\n */\nfunction missingTableMessage(link: unknown): string | undefined {\n return typeof link === \"string\" || link instanceof Error ?\n messageProperty(link)\n : undefined;\n}\n\nfunction sqliteErrorCode(link: unknown): unknown {\n if (!canReadProperty(link)) return undefined;\n return Reflect.get(link, \"code\");\n}\n\n/**\n * Cloudflare D1 / Durable Objects may surface a missing-table failure as the\n * generic SQLite code with no detail. Accept the bare marker, but do not\n * substring-match detailed `SQLITE_ERROR: ...` failures: those include syntax\n * errors and bind-limit faults that must stay loud.\n */\nfunction isBareSqliteErrorMarker(link: unknown): boolean {\n const message = messageProperty(link);\n if (message === SQLITE_GENERIC_ERROR_CODE) return true;\n if (sqliteErrorCode(link) !== SQLITE_GENERIC_ERROR_CODE) return false;\n return (\n message === undefined ||\n message === SQLITE_GENERIC_ERROR_CODE ||\n message.includes(SQLITE_MISSING_TABLE_PATTERN)\n );\n}\n\nfunction isPostgresUndefinedRelationMessage(link: unknown): boolean {\n const message = errorMessage(link);\n if (message.startsWith(DRIZZLE_QUERY_ERROR_PREFIX)) return false;\n return POSTGRES_UNDEFINED_RELATION_PATTERN.test(message);\n}\n\nexport function isMissingTableError(error: unknown): boolean {\n // SQLSTATE 42P01 is locale-independent and structural, so it is honored on\n // *every* link — including a plain driver-error object reached only by walking\n // through a non-`Error` `.cause` (postgres-js).\n //\n // The SQLite message substring is honored only while every prior link in the\n // chain was an `Error` (or the top-level string) — the reach of the\n // pre-broadening walk, which stopped at the first non-`Error` `.cause`.\n let everyPriorLinkWasError = true;\n for (const link of errorChain(error)) {\n if (isPostgresUndefinedTable(link)) return true;\n if (isPostgresUndefinedRelationMessage(link)) return true;\n if (isBareSqliteErrorMarker(link)) return true;\n if (everyPriorLinkWasError) {\n const message = missingTableMessage(link);\n if (message?.includes(SQLITE_MISSING_TABLE_PATTERN) === true) {\n return true;\n }\n }\n if (!(link instanceof Error)) everyPriorLinkWasError = false;\n }\n return false;\n}\n\nfunction historyMissingRecordedRelationsError(\n details: Record<string, unknown>,\n cause: unknown,\n): ConfigurationError {\n return new ConfigurationError(\n \"history: true requires the recorded-time relations to exist, but a recorded relation is missing.\",\n details,\n {\n cause,\n suggestion:\n \"Create the recorded-time relations (typegraph_recorded_nodes, typegraph_recorded_edges, typegraph_recorded_clock) — e.g. re-run the generated migration SQL — on this database before enabling history capture.\",\n },\n );\n}\n\n/**\n * Converts missing recorded-relation failures into the actionable precondition\n * error used by construction-time history checks. Capture paths use this after\n * the live write has already succeeded inside the same transaction; recorded\n * read paths use it when query/schema swapping reaches a recorded table that\n * has not been materialized yet.\n */\nexport async function withRecordedRelationsPrecondition<T>(\n promise: Promise<T>,\n details: Record<string, unknown>,\n): Promise<T> {\n try {\n return await promise;\n } catch (error) {\n if (!isMissingTableError(error)) throw error;\n throw historyMissingRecordedRelationsError(\n { ...details, code: \"RECORDED_RELATIONS_MISSING\" },\n error,\n );\n }\n}\n\n/**\n * Engine \"vector dimension mismatch\" message shapes. pgvector:\n * `expected 384 dimensions, not 512`; libSQL / sqlite-vec surface a similar\n * `expected N … got/not M`. The first capture is the dimension the *stored*\n * column expects; the optional second is the dimension that was *attempted*.\n */\nconst DIMENSION_MISMATCH_PATTERN =\n /expected (\\d+) dimensions(?:[,\\s]+(?:not|got|but got|but)\\s*(\\d+))?/i;\n\n/**\n * Parses an engine vector-dimension-mismatch error into `{ expected, actual }`\n * by walking the `.cause` chain (drivers wrap the real error). `expected` is\n * the stored column's dimension; `actual` (when the message includes it) is the\n * attempted vector's dimension. Returns `undefined` for unrelated errors.\n */\nexport function parseDimensionMismatch(\n error: unknown,\n): { expected: number; actual: number | undefined } | undefined {\n for (const link of errorChain(error)) {\n const message = errorMessage(link);\n const match = DIMENSION_MISMATCH_PATTERN.exec(message);\n if (match) {\n return {\n expected: Number(match[1]),\n actual: match[2] === undefined ? undefined : Number(match[2]),\n };\n }\n }\n return undefined;\n}\n","/**\n * Canonical-form helpers for deterministic schema serialization.\n *\n * Used by both content-hashing (`computeSchemaHash`) and structural diffing\n * (`computeSchemaDiff`) to ensure that semantically-equivalent objects with\n * differently-ordered keys produce identical canonical strings.\n */\n\n/**\n * `JSON.stringify` replacer that sorts object keys recursively.\n *\n * Apply via `JSON.stringify(value, sortedReplacer)` to obtain output in\n * which sibling keys at every depth appear in lexicographic order.\n *\n * Arrays are passed through unchanged — array order is semantically\n * meaningful and must not be normalized.\n */\nexport function sortedReplacer(_key: string, value: unknown): unknown {\n if (value !== null && typeof value === \"object\" && !Array.isArray(value)) {\n const sorted: Record<string, unknown> = {};\n for (const key of Object.keys(value).toSorted()) {\n sorted[key] = (value as Record<string, unknown>)[key];\n }\n return sorted;\n }\n return value;\n}\n\n/**\n * Compare two values for canonical-form equality.\n *\n * Returns `true` when both values produce identical JSON under `sortedReplacer`\n * — i.e., they have the same JSON structure regardless of object key order.\n * Used by diff machinery to detect semantic changes in JSON-shaped fields.\n *\n * Note: callers must handle `undefined` themselves — `JSON.stringify(undefined)`\n * returns `undefined`, so two `undefined` inputs would compare equal here, which\n * may or may not be the desired semantics depending on context.\n */\nexport function canonicalEqual(a: unknown, b: unknown): boolean {\n return (\n JSON.stringify(a, sortedReplacer) === JSON.stringify(b, sortedReplacer)\n );\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/backend/drizzle/execution/postgres-execution.ts","../src/backend/drizzle/postgres.ts"],"names":["sql","tables","options"],"mappings":";;;;;;;;;;;AAyEA,SAAS,mBAAA,CACP,OACA,QAAA,EACwE;AACxE,EAAA,IAAI,KAAA,KAAU,MAAA,IAAa,KAAA,KAAU,IAAA,EAAM;AACzC,IAAA,OAAO,KAAA;AAAA,EACT;AACA,EAAA,MAAM,SAAA,GAAY,KAAA;AAClB,EAAA,OAAO,OAAO,SAAA,CAAU,QAAQ,CAAA,KAAM,UAAA;AACxC;AAEA,SAAS,iBAAiB,SAAA,EAA+C;AAKvE,EAAA,OAAO,OAAO,SAAA,KAAc,QAAA,IAAY,mBAAA,CAAoB,WAAW,OAAO,CAAA;AAChF;AAEA,SAAS,eAAe,SAAA,EAA+C;AAUrE,EAAA,OACE,OAAO,cAAc,QAAA,IACrB,mBAAA,CAAoB,WAAW,OAAO,CAAA,IACtC,mBAAA,CAAoB,SAAA,EAAW,aAAa,CAAA;AAEhD;AAEA,SAAS,mBAAmB,SAAA,EAAmD;AAM7E,EAAA,OACE,OAAO,cAAc,UAAA,IACrB,mBAAA,CAAoB,WAAW,QAAQ,CAAA,IACvC,mBAAA,CAAoB,SAAA,EAAW,OAAO,CAAA;AAE1C;AAcO,SAAS,iBAAiB,EAAA,EAA4B;AAC3D,EAAA,MAAM,YAAa,EAAA,CAAuB,OAAA;AAC1C,EAAA,OACE,O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A,EAAW;AACjC,MAAA,IAAI;AACF,QAAA,MAAM,QAAQ,aAAa,CAAA;AAAA,MAC7B,SAAS,KAAA,EAAO;AAGd,QAAA,IAAI,CAAC,mBAAA,CAAoB,KAAK,CAAA,EAAG,MAAM,KAAA;AAAA,MACzC;AAAA,IACF,CAAA;AAAA;AAAA,IAIA,MAAM,QAAW,KAAA,EAAmC;AAClD,MAAA,OAAO,gBAAA,CAAiB,QAAW,KAAK,CAAA;AAAA,IAC1C,CAAA;AAAA,IAEA,WACE,KAAA,EACuD;AACvD,MAAA,OAAO,gBAAA,CAAiB,QAAQ,KAAK,CAAA;AAAA,IACvC;AAAA,GACF;AAEA,EAAA,OAAO,gBAAA;AACT;AAEA,SAAS,yBACP,OAAA,EAC0B;AAC1B,EAAA,MAAM,qBACJ,OAAA,CAAQ,gBAAA,IACR,+BAA+B,OAAA,CAAQ,EAAA,EAAI,QAAQ,cAAc,CAAA;AASnE,EAAA,OAAO,8BAAA,CAA+B;AAAA,IACpC,IAAI,OAAA,CAAQ,EAAA;AAAA,IACZ,gBAAA,EAAkB,kBAAA;AAAA,IAClB,gBAAgB,OAAA,CAAQ,cAAA;AAAA,IACxB,mBAAmB,OAAA,CAAQ,iBAAA;AAAA,IAC3B,YAAY,OAAA,CAAQ,UAAA;AAAA,IACpB,cAAc,OAAA,CAAQ,YAAA;AAAA,IACtB,kBAAkB,OAAA,CAAQ,gBAAA;AAAA,IAC1B,gBAAgB,OAAA,CAAQ,cAAA;AAAA,IACxB,eAAA,EACE,OAAA,CAAQ,cAAA,KAAmB,MAAA,GAAY,SAAY,qBAAA;AAAsB,GAC5E,CAAA;AACH","file":"chunk-OS6KS3HV.js","sourcesContent":["import { type SQL } from \"drizzle-orm\";\nimport { type PgDatabase, type PgQueryResultHKT } from \"drizzle-orm/pg-core\";\n\nimport { getOrCreateLru } from \"./lru\";\nimport {\n type CompiledSqlQuery,\n compileQueryWithDialect,\n type PreparedSqlStatement,\n type SqlExecutionAdapter,\n} from \"./types\";\n\n/**\n * Edge-safe: this module is loaded on Cloudflare Workers / Vercel Edge\n * via `@neondatabase/serverless`, so it must not statically import any\n * `node:*` module. Use only platform-neutral primitives.\n */\n\ntype PgQueryResult = Readonly<{\n rows: readonly unknown[];\n}>;\n\n/**\n * The wire-protocol-shaped client the fast path calls. Both the wrapped\n * node-postgres and postgres-js clients normalize to this signature; what\n * differs is whether the underlying driver gets a server-side prepared\n * statement (node-postgres path) or relies on its own internal preparation\n * (postgres-js).\n */\ntype PgQueryClient = Readonly<{\n query: (sqlText: string, params: readonly unknown[]) => Promise<PgQueryResult>;\n}>;\n\n/**\n * Minimal shape of the node-postgres / @neondatabase/serverless client\n * we rely on. Both accept either positional `query(text, values)`\n * (unnamed) or the configuration-object form `query({name, text, values})`\n * which uses a server-side prepared statement keyed by `name`.\n */\ntype NodePgQueryConfig = Readonly<{\n name: string;\n text: string;\n values: readonly unknown[];\n}>;\n\ntype NodePgClient = Readonly<{\n query: ((\n sqlText: string,\n params?: readonly unknown[],\n ) => Promise<PgQueryResult>) &\n ((config: NodePgQueryConfig) => Promise<PgQueryResult>);\n}>;\n\n/**\n * Minimal shape of the postgres-js tagged-template `Sql` client we rely on\n * for the raw fast path. `unsafe(sqlText, params)` is postgres-js's\n * parameterized-raw-SQL entry point and resolves to a row array.\n */\ntype PostgresJsClient = ((...args: readonly unknown[]) => unknown) &\n Readonly<{\n unsafe: (\n sqlText: string,\n params?: readonly unknown[],\n ) => PromiseLike<readonly unknown[]>;\n }>;\n\ntype PgClientCarrier = Readonly<{\n $client?: unknown;\n}>;\n\nexport type AnyPgDatabase = PgDatabase<PgQueryResultHKT, Record<string, unknown>>;\n\nexport type PostgresExecutionAdapter = SqlExecutionAdapter;\n\nfunction hasFunctionProperty<K extends string>(\n value: unknown,\n property: K,\n): value is Readonly<Record<K, (...args: readonly unknown[]) => unknown>> {\n if (value === undefined || value === null) {\n return false;\n }\n const candidate = value as Readonly<Record<K, unknown>>;\n return typeof candidate[property] === \"function\";\n}\n\nfunction isPgNativeClient(candidate: unknown): candidate is NodePgClient {\n // pg `Pool` / `Client` and @neondatabase/serverless `Pool` / `Client`\n // are object instances with a `.query` method. We require non-callable\n // here so we don't accidentally swallow the postgres-js or neon-http\n // tagged-template clients (both callable, both also expose `.query`).\n return typeof candidate === \"object\" && hasFunctionProperty(candidate, \"query\");\n}\n\nfunction isPgliteClient(candidate: unknown): candidate is NodePgClient {\n // PGlite (Postgres-in-WASM) exposes `.query(sql, params, options)` like\n // node-postgres and so also satisfies `isPgNativeClient` — but it does NOT\n // accept the `{name, text, values}` named-statement config form. Passing\n // that object doesn't merely fail the call: it desyncs PGlite's single\n // wire connection, so every subsequent query errors with `08P01`. Detect\n // PGlite structurally via `dumpDataDir` (a PGlite-specific method absent on\n // pg `Pool`/`Client`) so `resolvePgClient` can route it to the unnamed\n // positional wrapper regardless of `prepareStatements`. Duck-typed with no\n // static import, keeping this module dependency-free and edge-safe.\n return (\n typeof candidate === \"object\" &&\n hasFunctionProperty(candidate, \"query\") &&\n hasFunctionProperty(candidate, \"dumpDataDir\")\n );\n}\n\nfunction isPostgresJsClient(candidate: unknown): candidate is PostgresJsClient {\n // postgres-js's tagged-template Sql is callable, has `.unsafe` (raw\n // parameterized executor), and has `.begin` (transaction starter).\n // Neon HTTP is also callable + has `.unsafe`, but that `.unsafe` is a\n // fragment builder rather than a query executor, so we discriminate\n // on `.begin` (postgres-js only).\n return (\n typeof candidate === \"function\" &&\n hasFunctionProperty(candidate, \"unsafe\") &&\n hasFunctionProperty(candidate, \"begin\")\n );\n}\n\n/**\n * @neondatabase/serverless's `neon(url)` HTTP-only tagged-template\n * function. Distinguishing markers vs postgres-js: `.transaction`\n * (an HTTP-batch transaction submitter) instead of `.begin`. Its\n * `.query`/`.unsafe` methods have different signatures from pg's, so\n * the fast path can't drive them safely — Drizzle's neon-http session\n * handles them correctly via the `db.execute` slow path.\n *\n * Exported so the backend factory can auto-disable the `transactions`\n * capability when this driver is detected (HTTP can't hold a session,\n * so multi-statement transactions are not available regardless).\n */\nexport function isNeonHttpClient(db: AnyPgDatabase): boolean {\n const candidate = (db as PgClientCarrier).$client;\n return (\n typeof candidate === \"function\" &&\n hasFunctionProperty(candidate, \"transaction\") &&\n !hasFunctionProperty(candidate, \"begin\")\n );\n}\n\n/**\n * Default cap on the number of distinct SQL strings tracked for\n * server-side prepared statements. Mirrors the SQLite adapter's\n * `DEFAULT_PREPARED_STATEMENT_CACHE_MAX`. High-cardinality SQL text —\n * variable-length IN-list expansions, generated aliases, custom\n * `backend.execute()` calls — would otherwise grow this map (and the\n * matching per-session prepared-statement memory inside PostgreSQL)\n * without bound.\n */\nconst DEFAULT_POSTGRES_STATEMENT_CACHE_MAX = 256;\n\n/**\n * Module-scope cache mapping each unique SQL text to a stable\n * prepared-statement name. PostgreSQL only requires statement names to\n * be unique per session, so a monotonic counter is sufficient — and\n * avoids depending on a hash primitive (`node:crypto` doesn't exist on\n * Cloudflare Workers, Web Crypto's digest is async).\n *\n * The cache is module-scope on purpose: pg `Pool` reuses connections,\n * and the same SQL text needs the same statement name across every\n * adapter that might end up driving the same connection. Without\n * shared naming, two adapters could try to register two different\n * statements under the same name on a recycled connection.\n *\n * Bounded by `cacheMax` per call (LRU eviction). The counter is\n * deliberately independent of the cache size so eviction never\n * recycles a name — a recycled name could collide with a still-cached\n * statement on a long-lived pg connection (\"prepared statement\n * already exists\" error with different text).\n */\nconst STATEMENT_NAME_CACHE = new Map<string, string>();\nlet nextStatementId = 0;\n\nfunction statementNameFor(sqlText: string, cacheMax: number): string {\n return getOrCreateLru(STATEMENT_NAME_CACHE, sqlText, cacheMax, () => {\n const name = `tg_${nextStatementId}`;\n nextStatementId += 1;\n return name;\n });\n}\n\n/**\n * Test-only: reset the module-scope statement-name state. Used to\n * isolate tests that assert on counter values or eviction behavior.\n */\nexport function resetStatementNameCacheForTests(): void {\n STATEMENT_NAME_CACHE.clear();\n nextStatementId = 0;\n}\n\n/**\n * Walks a row and replaces any `Date` instance with its ISO-string\n * equivalent. Used to normalize node-postgres / neon-serverless output\n * when we bypass Drizzle's session: pg's default type parsers return\n * `Date` objects for `timestamptz` / `timestamp` / `date` columns, but\n * TypeGraph's row contract everywhere downstream is \"timestamps come\n * back as ISO strings.\" Drizzle's session installs a per-query type\n * override that does the same thing; this is the dependency-free,\n * edge-safe equivalent.\n *\n * O(columns × rows) — negligible for the row counts TypeGraph queries\n * return (typical: 1–100 rows × 5–30 columns).\n */\nfunction normalizeRow(row: unknown): unknown {\n if (row === null || typeof row !== \"object\") return row;\n let mutated: Record<string, unknown> | undefined;\n for (const key of Object.keys(row)) {\n const value = (row as Record<string, unknown>)[key];\n if (value instanceof Date) {\n mutated ??= { ...(row as Record<string, unknown>) };\n mutated[key] = value.toISOString();\n }\n }\n return mutated ?? row;\n}\n\nfunction normalizeRows(\n rows: readonly unknown[],\n): readonly unknown[] {\n // Single-pass scan; only allocate a new array if any row contained a\n // Date that needed normalizing. Most rows on most queries don't.\n let mutated: unknown[] | undefined;\n for (let index = 0; index < rows.length; index += 1) {\n const original = rows[index];\n const normalized = normalizeRow(original);\n if (normalized !== original) {\n mutated ??= [...rows] as unknown[];\n mutated[index] = normalized;\n }\n }\n return mutated ?? rows;\n}\n\n/**\n * Wraps a node-postgres / neon-serverless client so every call goes\n * through a server-side prepared statement keyed by a stable name. The\n * first call on each connection parses + plans + executes; subsequent\n * calls with the same statement name reuse the cached plan and skip\n * both parse and plan phases. Measurable on multi-CTE TypeGraph\n * queries: 3-hop drops from ~7-12ms (parse + plan + execute every call)\n * to ~0.8ms median (execute only).\n *\n * Returned rows are normalized so that `Date` objects from default pg\n * type parsers become ISO strings, matching the row shape Drizzle's\n * session would produce. Without this, `ctx.<alias>.meta.createdAt`\n * style accessors in the SELECT-result path return Date instances,\n * breaking user code that expects strings.\n */\nfunction wrapNodePgClient(\n client: NodePgClient,\n cacheMax: number,\n): PgQueryClient {\n return {\n async query(\n sqlText: string,\n params: readonly unknown[],\n ): Promise<PgQueryResult> {\n const result = await client.query({\n name: statementNameFor(sqlText, cacheMax),\n text: sqlText,\n values: params,\n });\n return { rows: normalizeRows(result.rows) };\n },\n };\n}\n\n/**\n * Variant that bypasses server-side prepared statements entirely.\n * Required for pgbouncer in transaction-pool mode (named statements\n * registered on one backend connection aren't visible on the next),\n * and useful when memory-sensitive workloads can't afford to keep\n * prepared statements warm across the pool.\n *\n * Same Date→ISO normalization as `wrapNodePgClient` so the row contract\n * downstream stays identical.\n */\nfunction wrapNodePgClientUnnamed(client: NodePgClient): PgQueryClient {\n return {\n async query(\n sqlText: string,\n params: readonly unknown[],\n ): Promise<PgQueryResult> {\n const result = await client.query(sqlText, params);\n return { rows: normalizeRows(result.rows) };\n },\n };\n}\n\nfunction adaptPostgresJsClient(sql: PostgresJsClient): PgQueryClient {\n return {\n async query(\n sqlText: string,\n params: readonly unknown[],\n ): Promise<PgQueryResult> {\n // postgres-js handles its own statement preparation internally\n // (controlled by the `prepare` connection option, default true),\n // so we don't need to name statements here ourselves.\n const rows = await sql.unsafe(sqlText, params);\n return { rows };\n },\n };\n}\n\n/**\n * Resolves a Drizzle-wrapped PostgreSQL client to a uniform `{query}` shape\n * that the fast path can call. Supports:\n *\n * - `drizzle-orm/node-postgres` (pg Pool / Client) — wrapped to use\n * server-side prepared statements with stable counter-derived names\n * keyed by the SQL text (see `statementNameFor`). When\n * `prepareStatements` is `false`, falls back to unnamed\n * `client.query(text, values)` for pgbouncer transaction-pool\n * compatibility.\n * - `drizzle-orm/neon-serverless` (@neondatabase/serverless Pool) —\n * pg-Pool-compatible, takes the same wrapper as node-postgres\n * - `drizzle-orm/postgres-js` (postgres-js tagged-template Sql) — adapted\n * via `.unsafe(sql, params)`. Safe because Drizzle installs transparent\n * parsers on the same client instance at `drizzle()` time, so direct\n * calls and Drizzle-routed calls produce identical row shapes.\n * - `drizzle-orm/pglite` (PGlite, Postgres-in-WASM) — routed to the unnamed\n * wrapper unconditionally. PGlite's `.query` has no named-statement config\n * form (and server-side prepared statements buy nothing for an in-process\n * engine), so `prepareStatements` is ignored for it.\n *\n * Returns `undefined` for `drizzle-orm/neon-http` and any other driver we\n * don't recognize; callers fall back to `db.execute(sql)`, which goes\n * through the Drizzle session and is always correct (just without the\n * server-side prepared-statement perf win).\n */\nfunction resolvePgClient(\n db: AnyPgDatabase,\n prepareStatements: boolean,\n cacheMax: number,\n): PgQueryClient | undefined {\n // Order matters: neon-http and postgres-js are both callable + have\n // `.unsafe`, but neon-http's `.unsafe` is a fragment builder (not a\n // query executor) and its `.query` doesn't accept the {name, text,\n // values} config form. Skip the fast path entirely for neon-http and\n // let Drizzle's session route the call.\n if (isNeonHttpClient(db)) {\n return undefined;\n }\n const client = (db as PgClientCarrier).$client;\n // Must precede `isPgNativeClient`: PGlite matches it too (object with\n // `.query`), but only the unnamed wrapper is safe for it (see\n // `isPgliteClient`).\n if (isPgliteClient(client)) {\n return wrapNodePgClientUnnamed(client);\n }\n if (isPgNativeClient(client)) {\n return prepareStatements\n ? wrapNodePgClient(client, cacheMax)\n : wrapNodePgClientUnnamed(client);\n }\n if (isPostgresJsClient(client)) {\n // postgres-js handles its own preparation internally (controlled\n // by the driver's own `prepare` connection option) — the\n // `prepareStatements` flag here only affects the node-postgres\n // path.\n return adaptPostgresJsClient(client);\n }\n return undefined;\n}\n\n/**\n * Normalizes the result shape of `db.execute(sql)` across drivers.\n *\n * - `drizzle-orm/node-postgres` / `drizzle-orm/neon-serverless` return a\n * pg-style `{ rows, rowCount, ... }` object.\n * - `drizzle-orm/postgres-js` returns the raw postgres-js result, which is\n * a plain array of rows (with extra non-enumerable properties like\n * `count` / `command`).\n *\n * The backend contract is \"a row array.\" Normalize here so every caller\n * downstream can assume the same shape.\n */\nasync function executeDrizzleQuery<TRow>(\n db: AnyPgDatabase,\n query: SQL,\n): Promise<readonly TRow[]> {\n const result = await db.execute(query);\n if (Array.isArray(result)) {\n return result as readonly TRow[];\n }\n return (result as Readonly<{ rows: readonly TRow[] }>).rows;\n}\n\nfunction createPgPreparedStatement(\n pgClient: PgQueryClient,\n sqlText: string,\n): PreparedSqlStatement {\n return {\n async execute<TRow>(params: readonly unknown[]): Promise<readonly TRow[]> {\n const result = await pgClient.query(sqlText, params);\n return result.rows as readonly TRow[];\n },\n };\n}\n\n/**\n * Tuning options for the PostgreSQL execution fast path.\n *\n * Defaults are correct for `drizzle-orm/node-postgres` against a\n * standalone PostgreSQL or session-pooled deployment. Override when:\n *\n * - **Pooling through pgbouncer in transaction-pool mode.** Set\n * `prepareStatements: false`. Named statements registered on one\n * pooled backend connection aren't visible to the next, so the\n * server-side prepared-statement cache must be off.\n * - **High-cardinality SQL text.** If the application emits many\n * distinct compiled SQL strings (variable-length IN-list expansions,\n * custom `backend.execute()` calls, generated aliases), tune\n * `preparedStatementCacheMax` down to bound per-session memory more\n * aggressively, or up if memory is plentiful and you want fewer\n * re-PREPAREs.\n */\nexport type PostgresExecutionAdapterOptions = Readonly<{\n /**\n * When `true` (default), node-postgres / neon-serverless calls go\n * through `client.query({name, text, values})` so PostgreSQL caches\n * the parsed/planned statement per session. Set to `false` to issue\n * unnamed `client.query(text, values)` calls — required for\n * pgbouncer transaction-pool mode.\n */\n prepareStatements?: boolean;\n /**\n * Maximum number of distinct SQL strings tracked for prepared-\n * statement naming. Defaults to `DEFAULT_POSTGRES_STATEMENT_CACHE_MAX`\n * (256). Ignored when `prepareStatements` is `false`.\n */\n preparedStatementCacheMax?: number;\n}>;\n\nexport function createPostgresExecutionAdapter(\n db: AnyPgDatabase,\n options: PostgresExecutionAdapterOptions = {},\n): PostgresExecutionAdapter {\n const prepareStatements = options.prepareStatements ?? true;\n const cacheMax =\n options.preparedStatementCacheMax ?? DEFAULT_POSTGRES_STATEMENT_CACHE_MAX;\n const pgClient = resolvePgClient(db, prepareStatements, cacheMax);\n\n function compile(query: SQL): CompiledSqlQuery {\n return compileQueryWithDialect(db, query, \"PostgreSQL\");\n }\n\n if (pgClient === undefined) {\n return {\n compile,\n async execute<TRow>(query: SQL): Promise<readonly TRow[]> {\n return executeDrizzleQuery<TRow>(db, query);\n },\n };\n }\n\n const pgQueryClient = pgClient;\n\n async function executeCompiled<TRow>(\n compiledQuery: CompiledSqlQuery,\n ): Promise<readonly TRow[]> {\n const result = await pgQueryClient.query(\n compiledQuery.sql,\n compiledQuery.params,\n );\n return result.rows as readonly TRow[];\n }\n\n return {\n compile,\n async execute<TRow>(query: SQL): Promise<readonly TRow[]> {\n // Fast path: compile via Drizzle's dialect, then execute through\n // the wrapped client directly. Bypasses Drizzle's session\n // overhead (logging spans, plan-cache bookkeeping for typed\n // queries we don't use), and on node-postgres this also enables\n // the server-side prepared-statement path because the wrapped\n // client assigns each unique SQL a stable statement name.\n const compiled = compile(query);\n return executeCompiled<TRow>(compiled);\n },\n executeCompiled,\n prepare(sqlText: string): PreparedSqlStatement {\n return createPgPreparedStatement(pgQueryClient, sqlText);\n },\n };\n}\n","/**\n * PostgreSQL backend adapter for TypeGraph.\n *\n * Works with any Drizzle PostgreSQL database instance. Tested against:\n * - `drizzle-orm/node-postgres` (pg Pool / Client)\n * - `drizzle-orm/postgres-js` (postgres-js tagged-template client)\n * - `drizzle-orm/neon-serverless` (@neondatabase/serverless Pool / Client)\n * - `drizzle-orm/neon-http` (@neondatabase/serverless `neon(url)`) —\n * transactions are auto-disabled because HTTP can't hold a session;\n * use `drizzle-orm/neon-serverless` if you need transactional writes.\n *\n * - `drizzle-orm/pglite` (PGlite, Postgres-in-WASM) — the execution\n * fast path detects PGlite and routes it correctly (its `.query` has no\n * named-statement form). For a batteries-included in-process setup, see\n * `createLocalPgliteBackend` in `@nicia-ai/typegraph/postgres/pglite`.\n *\n * Other pg-protocol Drizzle adapters (Vercel Postgres, Supabase via pg)\n * work unchanged because they all expose a compatible `db.execute()` /\n * `db.transaction()` surface.\n *\n * @example\n * ```typescript\n * import { drizzle } from \"drizzle-orm/node-postgres\";\n * import { Pool } from \"pg\";\n * import { createPostgresBackend, tables } from \"@nicia-ai/typegraph/postgres\";\n *\n * const pool = new Pool({ connectionString: process.env.DATABASE_URL });\n * const db = drizzle(pool);\n * const backend = createPostgresBackend(db, { tables });\n * ```\n */\nimport {\n and,\n eq,\n getTableName,\n inArray,\n isNull,\n type SQL,\n sql,\n} from \"drizzle-orm\";\nimport { PgDatabase, PgTransaction } from \"drizzle-orm/pg-core\";\n\nimport { ConfigurationError } from \"../../errors\";\nimport type { ResolvedSqlTableNames } from \"../../query/compiler/schema\";\nimport { quoteIdentifier } from \"../../query/compiler/utils\";\nimport {\n buildFulltextCapabilities,\n type FulltextStrategy,\n tsvectorStrategy,\n} from \"../../query/dialect/fulltext-strategy\";\nimport {\n assertPgvectorEfSearch,\n pgvectorStrategy,\n} from \"../../query/dialect/vector/pgvector-strategy\";\nimport {\n assertVectorSearchLimit,\n buildVectorCapabilities,\n type VectorSlot,\n type VectorStrategy,\n} from \"../../query/dialect/vector-strategy\";\nimport { isMissingTableError } from \"../../utils/sql-errors\";\nimport {\n type AdoptedTransaction,\n type BackendCapabilities,\n type CommitSchemaVersionParams,\n type ContributionMaterializationIdentity,\n type ContributionMaterializationRow,\n type CreateVectorIndexParams,\n type DeleteEmbeddingParams,\n type DeleteFulltextBatchParams,\n type DeleteFulltextParams,\n type DropVectorIndexParams,\n type FulltextSearchParams,\n type FulltextSearchResult,\n type GraphBackend,\n type IndexMaterializationRow,\n type KindRemovalRow,\n POSTGRES_CAPABILITIES,\n POSTGRES_MAX_BIND_PARAMETERS,\n type RecordContributionMaterializationParams,\n type RecordIndexMaterializationParams,\n type RecordKindRemovalParams,\n type SchemaVersionRow,\n type SetActiveVersionParams,\n type TransactionBackend,\n type TransactionOptions,\n type UpsertEmbeddingParams,\n type UpsertFulltextBatchParams,\n type UpsertFulltextParams,\n type VectorSearchParams,\n type VectorSearchResult,\n} from \"../types\";\nimport {\n buildContributionInsertValues,\n buildContributionOnConflictSet,\n createContributionMaterializer,\n gateFulltext,\n gateFulltextMethods,\n mapContributionMaterializationRow,\n POSTGRES_CONTRIBUTION_MAT_TIMESTAMPS,\n} from \"./contribution-materializations\";\nimport { generatePgCreateTableSQL, generatePostgresDDL } from \"./ddl\";\nimport {\n type AnyPgDatabase,\n createPostgresExecutionAdapter,\n isNeonHttpClient,\n type PostgresExecutionAdapter,\n type PostgresExecutionAdapterOptions,\n} from \"./execution/postgres-execution\";\nimport {\n buildMaterializationInsertValues,\n buildMaterializationOnConflictSet,\n mapMaterializationRow,\n POSTGRES_INDEX_MAT_TIMESTAMPS,\n} from \"./index-materializations\";\nimport {\n buildKindRemovalInsertValues,\n buildKindRemovalOnConflictSet,\n mapKindRemovalRow,\n POSTGRES_KIND_REMOVAL_TIMESTAMPS,\n} from \"./kind-removals\";\nimport {\n assertAdoptedDialect,\n type CommonOperationBackend,\n createCommonOperationBackend,\n type InternalOperationBackend,\n} from \"./operation-backend-core\";\nimport {\n createCachedTableExistence,\n createPostgresOperationStrategy,\n} from \"./operations/strategy\";\nimport {\n coerceNumericScore,\n createEdgeRowMapper,\n createNodeRowMapper,\n createSchemaVersionRowMapper,\n createUniqueRowMapper,\n nowIso,\n POSTGRES_ROW_MAPPER_CONFIG,\n} from \"./row-mappers\";\nimport {\n type PostgresTables,\n tables as defaultTables,\n} from \"./schema/postgres\";\nimport {\n createVectorSlotLatch,\n mapVectorWriteError,\n vectorSlotFromCreateIndexParams,\n vectorSlotFromDropIndexParams,\n vectorSlotFromParams,\n type VectorSlotLatch,\n} from \"./vector-runtime\";\n\n// ============================================================\n// Types\n// ============================================================\n\n/**\n * Options for creating a PostgreSQL backend.\n */\nexport type PostgresBackendOptions = Readonly<{\n /**\n * Custom table definitions. Use createPostgresTables() to customize table names.\n * Defaults to standard TypeGraph table names.\n */\n tables?: PostgresTables;\n /**\n * Fulltext strategy override. Defaults to `tsvectorStrategy`\n * (Postgres built-in `tsvector` + GIN). Pass a custom strategy here to\n * swap the entire fulltext stack — DDL, MATCH condition, rank\n * expression, and snippet generation — for alternate Postgres\n * backends like ParadeDB (`pg_search`), pg_trgm similarity, or\n * pgroonga without forking TypeGraph.\n */\n fulltext?: FulltextStrategy;\n /**\n * Vector strategy override. Defaults to `pgvectorStrategy` (pgvector's\n * `vector(N)` columns + HNSW/IVFFlat). The strategy owns per-`(kind,\n * field)` typed storage — DDL, upsert, delete, similarity search, and\n * ANN index lifecycle — and advertises `strategy.capabilities` as\n * `capabilities.vector`. Pass a custom strategy to swap the entire\n * vector stack for an alternate Postgres extension without forking\n * TypeGraph.\n *\n * Pass `false` to disable vector support entirely. The backend then\n * advertises no `capabilities.vector` and omits the embedding/search\n * methods, mirroring a SQLite connection without sqlite-vec. Required\n * for an in-process Postgres (e.g. PGlite) built without the pgvector\n * extension: the default `pgvectorStrategy` assumes `vector(N)` exists,\n * so any embedding write or `CREATE EXTENSION vector` would otherwise\n * hard-fail at runtime.\n */\n vector?: VectorStrategy | false;\n /**\n * Override specific backend capabilities. Useful when the underlying\n * driver doesn't support a feature TypeGraph would otherwise assume —\n * for example, an HTTP-only Postgres driver that can't hold a session\n * across statements would need `{ transactions: false }` so\n * TypeGraph falls through to non-transactional execution paths.\n *\n * `drizzle-orm/neon-http` is auto-detected and has `transactions`\n * disabled without an explicit override; this option exists for\n * other HTTP-style drivers and for tests that need to simulate a\n * capability gap.\n */\n capabilities?: Partial<BackendCapabilities>;\n /**\n * Use server-side prepared statements (named statements cached per\n * pg connection) on the node-postgres / neon-serverless fast path.\n * Defaults to `true`. Set to `false` when pooling through pgbouncer\n * in transaction-pool mode — pgbouncer routes successive statements\n * over different backend connections, and a `name` registered on one\n * is invisible on the next.\n *\n * No effect on `drizzle-orm/postgres-js` (handles preparation\n * internally) or `drizzle-orm/neon-http` (no fast path).\n */\n prepareStatements?: boolean;\n /**\n * Cap on the number of distinct SQL strings tracked for\n * prepared-statement naming. Defaults to 256. The cache is LRU-\n * bounded so high-cardinality SQL text (variable-length IN-lists,\n * generated aliases, `backend.execute()` calls with one-off SQL)\n * doesn't grow unbounded in either the Node process or in\n * PostgreSQL's per-session prepared-statement memory. Worst-case\n * server-side footprint is roughly `cap × pool size` statements\n * across all pooled connections. Ignored when\n * `prepareStatements` is `false`.\n */\n preparedStatementCacheMax?: number;\n}>;\n\nconst NODE_INSERT_PARAM_COUNT = 9;\nconst EDGE_INSERT_PARAM_COUNT = 12;\nconst POSTGRES_NODE_INSERT_BATCH_SIZE = Math.max(\n 1,\n Math.floor(POSTGRES_MAX_BIND_PARAMETERS / NODE_INSERT_PARAM_COUNT),\n);\nconst POSTGRES_EDGE_INSERT_BATCH_SIZE = Math.max(\n 1,\n Math.floor(POSTGRES_MAX_BIND_PARAMETERS / EDGE_INSERT_PARAM_COUNT),\n);\nconst POSTGRES_GET_NODES_ID_CHUNK_SIZE = Math.max(\n 1,\n POSTGRES_MAX_BIND_PARAMETERS - 2,\n);\nconst POSTGRES_GET_EDGES_ID_CHUNK_SIZE = Math.max(\n 1,\n POSTGRES_MAX_BIND_PARAMETERS - 1,\n);\nconst CHECK_UNIQUE_BATCH_FIXED_PARAM_COUNT = 3;\nconst POSTGRES_CHECK_UNIQUE_BATCH_CHUNK_SIZE = Math.max(\n 1,\n POSTGRES_MAX_BIND_PARAMETERS - CHECK_UNIQUE_BATCH_FIXED_PARAM_COUNT,\n);\n\n// ============================================================\n// Utilities\n// ============================================================\n\nconst toNodeRow = createNodeRowMapper(POSTGRES_ROW_MAPPER_CONFIG);\nconst toEdgeRow = createEdgeRowMapper(POSTGRES_ROW_MAPPER_CONFIG);\nconst toUniqueRow = createUniqueRowMapper(POSTGRES_ROW_MAPPER_CONFIG);\nconst toSchemaVersionRow = createSchemaVersionRowMapper(\n POSTGRES_ROW_MAPPER_CONFIG,\n);\n\nfunction buildPostgresCapabilities(\n fulltextStrategy: FulltextStrategy,\n vectorStrategy: VectorStrategy | undefined,\n): BackendCapabilities {\n return {\n ...POSTGRES_CAPABILITIES,\n ...(vectorStrategy === undefined ?\n {}\n : { vector: buildVectorCapabilities(vectorStrategy) }),\n fulltext: buildFulltextCapabilities(fulltextStrategy),\n };\n}\n\n// ============================================================\n// Backend Factory\n// ============================================================\n\n/**\n * Creates a TypeGraph backend for PostgreSQL databases.\n *\n * Works with any Drizzle PostgreSQL instance regardless of the underlying driver.\n *\n * @param db - A Drizzle PostgreSQL database instance\n * @param options - Backend configuration\n * @returns A GraphBackend implementation\n */\nexport function createPostgresBackend(\n db: AnyPgDatabase,\n options: PostgresBackendOptions = {},\n): GraphBackend {\n const tables = options.tables ?? defaultTables;\n const fulltextStrategy = options.fulltext ?? tsvectorStrategy;\n // pgvector is compiled into a standalone Postgres server, so it is wired\n // unconditionally by default (overridable for alternate Postgres vector\n // stacks). `vector: false` disables it — required for an in-process\n // Postgres (PGlite) built without the pgvector extension, where the\n // default strategy's `vector(N)` DDL would hard-fail.\n const vectorStrategy =\n options.vector === false ? undefined : (options.vector ?? pgvectorStrategy);\n // One latch per backend instance, shared with every transaction-scoped\n // backend so a slot's per-field table is created at most once per process.\n // Absent when vector support is disabled.\n const vectorSlotLatch =\n vectorStrategy === undefined ? undefined : createVectorSlotLatch();\n const baseCapabilities = buildPostgresCapabilities(\n fulltextStrategy,\n vectorStrategy,\n );\n // HTTP-only drivers (notably `drizzle-orm/neon-http`) can't hold a\n // session across statements, so multi-statement transactions are\n // unavailable regardless of what we declare. Auto-detect and downgrade\n // the capability so callers get correct fallback behavior without\n // having to remember to override it themselves.\n const httpOnlyOverrides = isNeonHttpClient(db) ? { transactions: false } : {};\n const capabilities: BackendCapabilities = {\n ...baseCapabilities,\n ...httpOnlyOverrides,\n ...options.capabilities,\n };\n const adapterOptions: PostgresExecutionAdapterOptions = {\n ...(options.prepareStatements === undefined ?\n {}\n : { prepareStatements: options.prepareStatements }),\n ...(options.preparedStatementCacheMax === undefined ?\n {}\n : { preparedStatementCacheMax: options.preparedStatementCacheMax }),\n };\n const executionAdapter = createPostgresExecutionAdapter(db, adapterOptions);\n const tableNames: ResolvedSqlTableNames = {\n nodes: getTableName(tables.nodes),\n edges: getTableName(tables.edges),\n recordedNodes: getTableName(tables.recordedNodes),\n recordedEdges: getTableName(tables.recordedEdges),\n recordedClock: getTableName(tables.recordedClock),\n fulltext: tables.fulltextTableName,\n uniques: getTableName(tables.uniques),\n };\n // Pre-quote identifiers so refreshStatistics() doesn't rebuild the\n // ANALYZE statement on every call. The recorded relations are ANALYZEd\n // separately under an existence guard (see refreshStatistics): a schema\n // created before recorded-time history landed (bring-your-own-pool, no DDL\n // re-run) has no recorded tables, and Postgres fails the whole ANALYZE if any\n // named relation is missing. Per-field vector tables are created lazily and\n // live outside this base set, so they are not ANALYZEd here.\n const coreAnalyzeStatement = sql`ANALYZE ${sql.join(\n [\n tableNames.nodes,\n tableNames.edges,\n getTableName(tables.uniques),\n tableNames.fulltext,\n ].map((name) => quoteIdentifier(name)),\n sql`, `,\n )}`;\n const recordedAnalyzeTables = [\n tableNames.recordedNodes,\n tableNames.recordedEdges,\n tableNames.recordedClock,\n ] as const;\n const operationStrategy = createPostgresOperationStrategy(\n tables,\n fulltextStrategy,\n );\n const operations = createPostgresOperationBackend({\n db,\n executionAdapter,\n adapterOptions,\n operationStrategy,\n tableNames,\n capabilities,\n fulltextStrategy,\n vectorStrategy,\n vectorSlotLatch,\n });\n\n // Whether `tableName` currently exists, via the same catalog probe `clear()`\n // uses — so refreshStatistics() never ANALYZEs a recorded relation that a\n // bring-your-own-pool schema has not yet created. The Postgres probe is\n // search_path-aware, so positive results are deliberately not cached by bare\n // table name.\n const recordedTableExists = createCachedTableExistence(\n async (tableName) => {\n const rows = await executionAdapter.execute<Record<string, unknown>>(\n operationStrategy.buildTableExists(tableName),\n );\n return rows[0];\n },\n { cacheExisting: false },\n );\n\n /**\n * Runs `fn` inside a Postgres transaction, holding an\n * `pg_advisory_xact_lock` keyed on the graph id. The advisory lock\n * serializes all schema commits per-graph: the read-then-write CAS in\n * `commitSchemaVersion` is safe even for the initial-commit case\n * where there is no row yet to `SELECT ... FOR UPDATE`.\n *\n * Refuses on backends that don't support transactions\n * (`drizzle-orm/neon-http`). The orphan-row crash window cannot be\n * eliminated without atomicity, so silent best-effort degradation is\n * worse than a typed error.\n */\n function runSchemaWriteTransaction<T>(\n graphId: string,\n fn: (tx: CommonOperationBackend) => Promise<T>,\n ): Promise<T> {\n if (!capabilities.transactions) {\n throw new ConfigurationError(\n \"commitSchemaVersion and setActiveVersion require atomic transactions, \" +\n \"but this Postgres backend does not provide them. The drizzle-orm/neon-http \" +\n \"driver communicates over HTTP and cannot hold a session across statements; \" +\n \"use drizzle-orm/neon-serverless (websocket) for transactional writes.\",\n {\n backend: \"postgres\",\n capability: \"transactions\",\n supportsTransactions: false,\n },\n );\n }\n\n return db.transaction(async (tx) => {\n // Advisory lock: hashtext($graphId) is collision-tolerant for the\n // size of an active graph set; collisions just serialize unrelated\n // graphs which is harmless. Held until the transaction commits.\n await tx.execute(sql`SELECT pg_advisory_xact_lock(hashtext(${graphId}))`);\n // Advisory lock is held here, so the schema-write-capable\n // InternalOperationBackend is used intentionally (see its type).\n const txBackend = createTransactionBackend({\n db: tx,\n adapterOptions,\n operationStrategy,\n tableNames,\n capabilities,\n fulltextStrategy,\n vectorStrategy,\n vectorSlotLatch,\n });\n return fn(txBackend);\n });\n }\n\n // Durable fulltext materialization (#135): the dialect-specific\n // marker-table primitives. Orchestration (materialize / assert /\n // per-instance cache) lives once in `createContributionMaterializer`.\n const matTable = tables.contributionMaterializations;\n\n async function ensureContributionMaterializationsTableImpl(): Promise<void> {\n await db.execute(sql.raw(generatePgCreateTableSQL(matTable)));\n }\n\n async function getContributionMaterializationRow(\n identity: ContributionMaterializationIdentity,\n ): Promise<ContributionMaterializationRow | undefined> {\n const rows = await db\n .select()\n .from(matTable)\n .where(\n and(\n eq(matTable.graphId, identity.graphId),\n eq(matTable.logicalName, identity.logicalName),\n eq(matTable.owner, identity.owner),\n eq(matTable.tableName, identity.tableName),\n ),\n );\n const row = rows[0];\n if (row === undefined) return undefined;\n return mapContributionMaterializationRow(\n row,\n POSTGRES_CONTRIBUTION_MAT_TIMESTAMPS.decode,\n );\n }\n\n async function recordContributionMaterializationRow(\n params: RecordContributionMaterializationParams,\n ): Promise<void> {\n await db\n .insert(matTable)\n .values(\n buildContributionInsertValues(\n params,\n POSTGRES_CONTRIBUTION_MAT_TIMESTAMPS.encode,\n ),\n )\n .onConflictDoUpdate({\n target: [\n matTable.graphId,\n matTable.logicalName,\n matTable.owner,\n matTable.tableName,\n ],\n set: buildContributionOnConflictSet(\n matTable.materializedAt,\n params.materializedAt,\n ),\n });\n }\n\n const contributionMaterializer = createContributionMaterializer({\n dialect: \"postgres\",\n fulltextStrategy,\n fulltextTableName: tables.fulltextTableName,\n execDdl: async (statement) => {\n await db.execute(sql.raw(statement));\n },\n ensureMarkerTable: ensureContributionMaterializationsTableImpl,\n getMarker: getContributionMaterializationRow,\n recordMarker: recordContributionMaterializationRow,\n });\n\n // Shared by `transaction()` (TypeGraph opens the tx) and\n // `adoptTransaction()` (#134 — the caller already opened it): bind a\n // tx-scoped backend to the *literal* `tx` client and gate fulltext on\n // the durable marker (a cached SELECT, never DDL).\n function bindTransactionBackend(tx: AnyPgDatabase): TransactionBackend {\n const txBackend = createTransactionBackend({\n db: tx,\n adapterOptions,\n operationStrategy,\n tableNames,\n capabilities,\n fulltextStrategy,\n vectorStrategy,\n vectorSlotLatch,\n });\n return gateFulltext(txBackend, contributionMaterializer.assertInitialized);\n }\n\n const backend: GraphBackend = {\n ...operations,\n\n async bootstrapTables(): Promise<void> {\n const statements = generatePostgresDDL(tables, fulltextStrategy);\n for (const statement of statements) {\n await db.execute(sql.raw(statement));\n }\n },\n\n // Every fulltext-touching method asserts the durable marker instead\n // of lazily emitting DDL. Steady state performs zero ensure; an\n // uninitialized database throws `StoreNotInitializedError` loudly\n // rather than self-healing on a read/write path (#135). Shared\n // verbatim with the tx-scoped gate via `gateFulltextMethods`.\n ...gateFulltextMethods(\n operations,\n contributionMaterializer.assertInitialized,\n ),\n\n async executeDdl(ddl: string): Promise<void> {\n await db.execute(sql.raw(ddl));\n },\n\n async ensureIndexMaterializationsTable(): Promise<void> {\n await db.execute(\n sql.raw(generatePgCreateTableSQL(tables.indexMaterializations)),\n );\n },\n\n async getIndexMaterialization(\n indexName: string,\n ): Promise<IndexMaterializationRow | undefined> {\n const t = tables.indexMaterializations;\n const rows = await db.select().from(t).where(eq(t.indexName, indexName));\n const row = rows[0];\n if (row === undefined) return undefined;\n return mapMaterializationRow(row, POSTGRES_INDEX_MAT_TIMESTAMPS.decode);\n },\n\n async getIndexMaterializations(\n statusKeys: readonly string[],\n ): Promise<readonly IndexMaterializationRow[]> {\n if (statusKeys.length === 0) return [];\n const t = tables.indexMaterializations;\n const rows = await db\n .select()\n .from(t)\n .where(inArray(t.indexName, [...statusKeys]));\n return rows.map((row) =>\n mapMaterializationRow(row, POSTGRES_INDEX_MAT_TIMESTAMPS.decode),\n );\n },\n\n async recordIndexMaterialization(\n params: RecordIndexMaterializationParams,\n ): Promise<void> {\n const t = tables.indexMaterializations;\n await db\n .insert(t)\n .values(\n buildMaterializationInsertValues(\n params,\n POSTGRES_INDEX_MAT_TIMESTAMPS.encode,\n ),\n )\n .onConflictDoUpdate({\n target: t.indexName,\n set: buildMaterializationOnConflictSet(\n t.materializedAt,\n params.materializedAt,\n ),\n });\n },\n\n async ensureContributionMaterializationsTable(): Promise<void> {\n await ensureContributionMaterializationsTableImpl();\n },\n\n async getContributionMaterialization(\n identity: ContributionMaterializationIdentity,\n ): Promise<ContributionMaterializationRow | undefined> {\n return getContributionMaterializationRow(identity);\n },\n\n async recordContributionMaterialization(\n params: RecordContributionMaterializationParams,\n ): Promise<void> {\n await recordContributionMaterializationRow(params);\n },\n\n async assertRuntimeContributionsInitialized(\n graphId: string,\n ): Promise<void> {\n await contributionMaterializer.assertInitialized(graphId);\n },\n\n async ensureKindRemovalsTable(): Promise<void> {\n await db.execute(sql.raw(generatePgCreateTableSQL(tables.kindRemovals)));\n },\n\n async getPendingKindRemovals(\n graphId: string,\n ): Promise<readonly KindRemovalRow[]> {\n const t = tables.kindRemovals;\n const rows = await db\n .select()\n .from(t)\n .where(and(eq(t.graphId, graphId), isNull(t.removedAt)));\n return rows.map((row) =>\n mapKindRemovalRow(row, POSTGRES_KIND_REMOVAL_TIMESTAMPS.decode),\n );\n },\n\n async getAllKindRemovals(\n graphId: string,\n ): Promise<readonly KindRemovalRow[]> {\n const t = tables.kindRemovals;\n const rows = await db.select().from(t).where(eq(t.graphId, graphId));\n return rows.map((row) =>\n mapKindRemovalRow(row, POSTGRES_KIND_REMOVAL_TIMESTAMPS.decode),\n );\n },\n\n async recordKindRemoval(params: RecordKindRemovalParams): Promise<void> {\n const t = tables.kindRemovals;\n await db\n .insert(t)\n .values(\n buildKindRemovalInsertValues(\n params,\n POSTGRES_KIND_REMOVAL_TIMESTAMPS.encode,\n ),\n )\n .onConflictDoUpdate({\n target: [t.graphId, t.kindName, t.entity, t.schemaVersion],\n set: buildKindRemovalOnConflictSet(t.removedAt, params.removedAt),\n });\n },\n\n async ensureReconciliationMarkersTable(): Promise<void> {\n await db.execute(\n sql.raw(generatePgCreateTableSQL(tables.reconciliationMarkers)),\n );\n },\n\n async ensureRuntimeContributions(graphId: string): Promise<void> {\n await contributionMaterializer.ensureRuntimeContributions(graphId);\n },\n\n /**\n * Superseded by `ensureRuntimeContributions(graphId)` (#129).\n * Retained as a thin back-compat wrapper for callers predating\n * #129; #135 routed it through the durable-marker writer.\n */\n async ensureFulltextTable(graphId: string): Promise<void> {\n await contributionMaterializer.ensureRuntimeContributions(graphId);\n },\n\n async getReconciliationMarker(\n graphId: string,\n ): Promise<number | undefined> {\n const t = tables.reconciliationMarkers;\n const rows = await db.select().from(t).where(eq(t.graphId, graphId));\n return rows[0]?.reconciledToVersion;\n },\n\n async setReconciliationMarker(\n graphId: string,\n version: number,\n ): Promise<void> {\n const t = tables.reconciliationMarkers;\n await db\n .insert(t)\n .values({ graphId, reconciledToVersion: version })\n .onConflictDoUpdate({\n target: t.graphId,\n set: { reconciledToVersion: version },\n });\n },\n\n async refreshStatistics(): Promise<void> {\n // Scoped to TypeGraph-managed tables only — we don't touch\n // unrelated tables in the same database. Without fresh stats\n // after a bulk load the planner can pick a reverse-index scan\n // with a filter (5ms forward traversal instead of 0.5ms) until\n // autovacuum catches up.\n await db.execute(coreAnalyzeStatement);\n // The recorded relations may be absent on a schema created before\n // recorded-time history landed (bring-your-own-pool, no DDL re-run).\n // Postgres fails the entire ANALYZE if any named relation is missing, so\n // ANALYZE only the recorded tables that exist.\n const tablePresence = await Promise.all(\n recordedAnalyzeTables.map(async (tableName) => ({\n tableName,\n exists: await recordedTableExists(tableName),\n })),\n );\n const presentRecordedTables = tablePresence\n .filter((entry) => entry.exists)\n .map((entry) => entry.tableName);\n if (presentRecordedTables.length > 0) {\n await db.execute(\n sql`ANALYZE ${sql.join(\n presentRecordedTables.map((name) => quoteIdentifier(name)),\n sql`, `,\n )}`,\n );\n }\n },\n\n async commitSchemaVersion(\n params: CommitSchemaVersionParams,\n ): Promise<SchemaVersionRow> {\n return runSchemaWriteTransaction(params.graphId, (target) =>\n target.commitSchemaVersion(params),\n );\n },\n\n async setActiveVersion(params: SetActiveVersionParams): Promise<void> {\n await runSchemaWriteTransaction(params.graphId, (target) =>\n target.setActiveVersion(params),\n );\n },\n\n async transaction<T>(\n fn: (tx: TransactionBackend, sql: AdoptedTransaction) => Promise<T>,\n options?: TransactionOptions,\n ): Promise<T> {\n // #134/#135: NO DDL or ensure here. The tx-scoped backend's\n // fulltext-touching methods assert the durable contribution\n // marker (one cached SELECT — never DDL) at point of use, exactly\n // like the non-tx wrappers. A transaction that never touches\n // fulltext never asserts; one that does runs pure DML against an\n // already-materialized table, with the \"no DDL in the business\n // transaction\" guarantee backed by the durable fact.\n const txConfig =\n options?.isolationLevel ?\n {\n isolationLevel: options.isolationLevel.replace(\"_\", \" \") as\n | \"read uncommitted\"\n | \"read committed\"\n | \"repeatable read\"\n | \"serializable\",\n }\n : undefined;\n\n return db.transaction(\n async (tx) => fn(bindTransactionBackend(tx), tx),\n txConfig,\n );\n },\n\n adoptTransaction(externalTx: AdoptedTransaction): TransactionBackend {\n // #134: cross-store atomicity is unsafe without real rollback —\n // the caller's relational write on `externalTx` *would* still\n // commit even though the graph write could not be undone. Refuse\n // loudly rather than silently degrade.\n if (!capabilities.transactions) {\n throw new ConfigurationError(\n \"Cross-store atomicity is unavailable on this Postgres backend: \" +\n \"its driver does not support transactions (drizzle-orm/neon-http, \" +\n \"Cloudflare D1). Adopting an external transaction here would let \" +\n \"the caller's relational write commit with no way to roll back \" +\n \"the graph write. Use a node-postgres or neon-serverless \" +\n \"(Pool/WebSocket) connection for cross-store transactions.\",\n {\n backend: \"postgres\",\n capability: \"transactions\",\n supportsTransactions: false,\n },\n );\n }\n assertAdoptedDialect<AnyPgDatabase>(externalTx, PgDatabase, \"postgres\");\n // The caller owns BEGIN/COMMIT/ROLLBACK via its own\n // `db.transaction(...)`. We adopt the literal `tx` client and run\n // pure DML on it — no transaction is opened or closed here, and no\n // DDL is emitted inside the caller's business transaction.\n return bindTransactionBackend(externalTx);\n },\n\n async close(): Promise<void> {\n // Drizzle doesn't expose a close method\n // Users manage connection lifecycle themselves\n },\n };\n\n return backend;\n}\n\ntype CreatePostgresOperationBackendOptions = Readonly<{\n db: AnyPgDatabase;\n executionAdapter: PostgresExecutionAdapter;\n /**\n * Adapter tuning (prepared-statement cache settings). Used to bind a\n * fresh, equivalently-configured adapter to a transaction client when\n * a per-search `efSearch` override opens its own transaction.\n */\n adapterOptions?: PostgresExecutionAdapterOptions | undefined;\n operationStrategy: ReturnType<typeof createPostgresOperationStrategy>;\n tableNames: ResolvedSqlTableNames;\n capabilities: BackendCapabilities;\n fulltextStrategy: FulltextStrategy;\n /**\n * Active vector strategy (`pgvectorStrategy` unless overridden), or\n * `undefined` when vector support is disabled (`vector: false`).\n */\n vectorStrategy: VectorStrategy | undefined;\n /**\n * Shared per-`(kind, field)` storage-ensure latch. Paired with\n * `vectorStrategy`: both present, or both `undefined`.\n */\n vectorSlotLatch: VectorSlotLatch | undefined;\n}>;\n\ntype CreatePostgresTransactionBackendOptions = Readonly<{\n db: AnyPgDatabase;\n executionAdapter?: PostgresExecutionAdapter;\n adapterOptions?: PostgresExecutionAdapterOptions;\n operationStrategy: ReturnType<typeof createPostgresOperationStrategy>;\n tableNames: ResolvedSqlTableNames;\n capabilities: BackendCapabilities;\n fulltextStrategy: FulltextStrategy;\n /** Active vector strategy. See {@link CreatePostgresOperationBackendOptions}. */\n vectorStrategy: VectorStrategy | undefined;\n /** Shared storage-ensure latch. See {@link CreatePostgresOperationBackendOptions}. */\n vectorSlotLatch: VectorSlotLatch | undefined;\n}>;\n\nfunction createPostgresOperationBackend(\n options: CreatePostgresOperationBackendOptions,\n): InternalOperationBackend {\n const {\n db,\n executionAdapter,\n adapterOptions,\n operationStrategy,\n tableNames,\n capabilities,\n fulltextStrategy,\n vectorStrategy,\n vectorSlotLatch,\n } = options;\n\n // Route through the execution adapter so driver-specific result shapes\n // (`{rows}` for node-postgres / neon-serverless; bare array for\n // postgres-js) are normalized in one place.\n async function execAll<T>(query: SQL): Promise<readonly T[]> {\n return executionAdapter.execute<T>(query);\n }\n\n async function execGet<T>(query: SQL): Promise<T | undefined> {\n const rows = await executionAdapter.execute<T>(query);\n return rows[0];\n }\n\n async function execRun(query: SQL): Promise<void> {\n await executionAdapter.execute(query);\n }\n\n type VectorSearchRow = Readonly<{ node_id: string; score: number }>;\n\n // One warning per backend instance when `efSearch` is supplied but the\n // driver can't hold a transaction to scope `SET LOCAL` to.\n let efSearchUnsupportedWarned = false;\n function warnEfSearchUnsupported(): void {\n if (efSearchUnsupportedWarned) return;\n efSearchUnsupportedWarned = true;\n if (typeof console === \"undefined\" || typeof console.warn !== \"function\") {\n return;\n }\n console.warn(\n \"[typegraph] efSearch (hnsw.ef_search override) was ignored: this \" +\n \"Postgres backend has transactions disabled (e.g. drizzle-orm/neon-http), \" +\n \"and SET LOCAL needs a transaction to scope the override. Use a \" +\n \"transactional driver (node-postgres / neon-serverless / postgres-js) \" +\n \"to apply efSearch.\",\n );\n }\n\n /**\n * Runs the vector SELECT, applying `hnsw.ef_search` transaction-locally\n * when requested. `SET LOCAL` only takes effect inside an explicit\n * transaction — issued in autocommit it rolls off with the statement\n * and the next pooled query (notably under transaction-mode pgbouncer)\n * sees the session default again. So when `efSearch` is set we bundle\n * `BEGIN; SET LOCAL …; SELECT …; COMMIT;` onto one connection. When\n * absent we take the unchanged single-statement fast path and open no\n * transaction.\n */\n async function runVectorSearch(\n efSearch: number | undefined,\n query: SQL,\n ): Promise<readonly VectorSearchRow[]> {\n if (efSearch === undefined) {\n return execAll<VectorSearchRow>(query);\n }\n if (!capabilities.transactions) {\n warnEfSearchUnsupported();\n return execAll<VectorSearchRow>(query);\n }\n // `SET` cannot be parameterized; `efSearch` is validated as an\n // integer in 1..MAX_HNSW_EF_SEARCH before we get here, so inlining\n // it is injection-safe.\n const setEfSearch = sql.raw(`SET LOCAL hnsw.ef_search = ${efSearch}`);\n if (db instanceof PgTransaction) {\n // Already inside the caller's transaction (low-level\n // backend.transaction / adoptTransaction). `executionAdapter` is\n // bound to this tx client, but SET LOCAL persists to the end of the\n // caller's transaction — leaking the override into their later\n // vector searches and breaking the per-search contract. Snapshot\n // the current frontier, apply the override, then restore it once\n // the SELECT has materialized so the override stays scoped to this\n // one search.\n const [setting] = await execAll<{ ef_search: string }>(\n sql`SELECT current_setting('hnsw.ef_search') AS ef_search`,\n );\n await execRun(setEfSearch);\n const rows = await execAll<VectorSearchRow>(query);\n // Restore only on success: a failed SELECT aborts the caller's\n // transaction, so its rollback discards the SET LOCAL anyway and a\n // restore here would just fail against the aborted tx, masking the\n // real error. `set_config(_, _, true)` is the parameterizable form\n // of SET LOCAL.\n if (setting !== undefined) {\n await execRun(\n sql`SELECT set_config('hnsw.ef_search', ${setting.ef_search}, true)`,\n );\n }\n return rows;\n }\n return db.transaction(async (tx) => {\n // Bind an equivalently-configured adapter to the tx client so the\n // SELECT keeps the server-side prepared-statement fast path and the\n // driver result-shape normalization, rather than a bespoke execute.\n const txAdapter = createPostgresExecutionAdapter(tx, adapterOptions);\n await txAdapter.execute(setEfSearch);\n return txAdapter.execute<VectorSearchRow>(query);\n });\n }\n\n // Runs the strategy's per-field DDL on this backend's connection (the tx\n // client for a transaction-scoped backend) so a slot's table exists\n // before the first write/search hits it.\n async function ensureVectorSlotStorage(slot: VectorSlot): Promise<void> {\n if (vectorStrategy === undefined || vectorSlotLatch === undefined) return;\n await vectorSlotLatch.ensure(vectorStrategy, slot, async (statement) => {\n await execRun(sql.raw(statement));\n });\n }\n\n const commonBackend = createCommonOperationBackend({\n batchConfig: {\n checkUniqueBatchChunkSize: POSTGRES_CHECK_UNIQUE_BATCH_CHUNK_SIZE,\n edgeInsertBatchSize: POSTGRES_EDGE_INSERT_BATCH_SIZE,\n getEdgesChunkSize: POSTGRES_GET_EDGES_ID_CHUNK_SIZE,\n getNodesChunkSize: POSTGRES_GET_NODES_ID_CHUNK_SIZE,\n nodeInsertBatchSize: POSTGRES_NODE_INSERT_BATCH_SIZE,\n },\n execution: {\n execAll,\n execGet,\n execRun,\n },\n nowIso,\n operationStrategy,\n rowMappers: {\n toEdgeRow,\n toNodeRow,\n toSchemaVersionRow,\n toUniqueRow,\n },\n tableExistenceCache: { cacheExisting: false },\n });\n\n const executeCompiled = executionAdapter.executeCompiled;\n const executeRawMethod: Pick<TransactionBackend, \"executeRaw\"> =\n executeCompiled === undefined ?\n {}\n : {\n async executeRaw<T>(\n sqlText: string,\n params: readonly unknown[],\n ): Promise<readonly T[]> {\n return executeCompiled<T>({ params, sql: sqlText });\n },\n };\n\n // Embedding write/search methods are present only when a vector strategy\n // is wired. With `vector: false` (e.g. PGlite without pgvector) they are\n // omitted, so `capabilities.vector` is absent and the store never routes\n // embedding work here — mirroring a SQLite connection without sqlite-vec.\n const vectorEmbeddingMethods =\n vectorStrategy === undefined ?\n {}\n : {\n async upsertEmbedding(params: UpsertEmbeddingParams): Promise<void> {\n const slot = vectorSlotFromParams(params);\n await ensureVectorSlotStorage(slot);\n const statements = vectorStrategy.buildUpsert(slot, params, nowIso());\n try {\n for (const statement of statements) {\n await execRun(statement);\n }\n } catch (error) {\n throw mapVectorWriteError(error, params);\n }\n },\n\n async deleteEmbedding(params: DeleteEmbeddingParams): Promise<void> {\n // Ensure the per-field table exists before deleting. A delete can\n // run before any embedding was ever written for the field (e.g. a\n // node hard-deleted having never carried one), and on Postgres a\n // DELETE against a missing relation INSIDE a transaction aborts the\n // whole transaction — swallowing the JS error can't un-abort it.\n // The idempotent ensure makes the DELETE target an existing\n // (possibly empty) table, so it's always a clean no-op.\n const slot = vectorSlotFromParams(params);\n await ensureVectorSlotStorage(slot);\n const statements = vectorStrategy.buildDelete(slot, params);\n for (const statement of statements) {\n await execRun(statement);\n }\n },\n\n async vectorSearch(\n params: VectorSearchParams,\n ): Promise<readonly VectorSearchResult[]> {\n assertVectorSearchLimit(params.limit);\n // Validate `efSearch` against pgvector's `hnsw.ef_search` ceiling\n // before `runVectorSearch` inlines it into `SET LOCAL`.\n assertPgvectorEfSearch(params.efSearch);\n const slot = vectorSlotFromParams(params);\n await ensureVectorSlotStorage(slot);\n const query = vectorStrategy.buildSearch(slot, params);\n let rows: readonly { node_id: string; score: number }[];\n try {\n rows = await runVectorSearch(params.efSearch, query);\n } catch (error) {\n // A query vector whose dimension no longer matches the stored\n // column surfaces the same typed error as the write path.\n throw mapVectorWriteError(error, params);\n }\n return rows.map((row) => ({\n nodeId: row.node_id,\n score: row.score,\n }));\n },\n };\n\n const operationBackend: InternalOperationBackend = {\n ...commonBackend,\n ...executeRawMethod,\n ...vectorEmbeddingMethods,\n capabilities,\n fulltextStrategy,\n ...(vectorStrategy === undefined ? {} : { vectorStrategy }),\n dialect: \"postgres\",\n tableNames,\n\n // === Vector Index Operations ===\n\n async createVectorIndex(params: CreateVectorIndexParams): Promise<void> {\n if (vectorStrategy === undefined) return;\n const slot = vectorSlotFromCreateIndexParams(params);\n // Ensure the per-field table exists first (idempotent), then create its\n // ANN index. pgvector's `ownedTables` builds the table only — the HNSW/\n // IVFFlat index is created here (and only here) so it picks up the\n // declared `m`/`ef_construction`/`lists` from `slot.indexParams` rather\n // than defaults.\n await ensureVectorSlotStorage(slot);\n // Honor the `concurrent` flag materializeIndexes passes on Postgres so the\n // ANN build doesn't take a write-blocking lock on a live table. execRun is\n // autocommit, which CONCURRENTLY requires.\n const indexStatement = vectorStrategy.buildCreateIndex?.(slot, {\n concurrent: params.concurrent === true,\n });\n if (indexStatement !== undefined) {\n await execRun(indexStatement);\n }\n },\n\n // === Fulltext Operations ===\n\n async upsertFulltext(params: UpsertFulltextParams): Promise<void> {\n const timestamp = nowIso();\n const statements = operationStrategy.buildUpsertFulltext(\n params,\n timestamp,\n );\n for (const stmt of statements) {\n await execRun(stmt);\n }\n },\n\n async deleteFulltext(params: DeleteFulltextParams): Promise<void> {\n const statements = operationStrategy.buildDeleteFulltext(params);\n for (const stmt of statements) {\n await execRun(stmt);\n }\n },\n\n async upsertFulltextBatch(\n params: UpsertFulltextBatchParams,\n ): Promise<void> {\n if (params.rows.length === 0) return;\n const timestamp = nowIso();\n const statements = operationStrategy.buildUpsertFulltextBatch(\n params,\n timestamp,\n );\n for (const stmt of statements) {\n await execRun(stmt);\n }\n },\n\n async deleteFulltextBatch(\n params: DeleteFulltextBatchParams,\n ): Promise<void> {\n if (params.nodeIds.length === 0) return;\n const statements = operationStrategy.buildDeleteFulltextBatch(params);\n for (const stmt of statements) {\n await execRun(stmt);\n }\n },\n\n async fulltextSearch(\n params: FulltextSearchParams,\n ): Promise<readonly FulltextSearchResult[]> {\n const query = operationStrategy.buildFulltextSearch(params);\n // pg returns `numeric` as a string to preserve precision; coerce at the\n // backend boundary so FulltextSearchResult.score is always `number`.\n const rows = await execAll<{\n node_id: string;\n score: number | string;\n snippet: string | null;\n }>(query);\n return rows.map((row, index) => ({\n nodeId: row.node_id,\n score: coerceNumericScore(row.score),\n rank: index + 1,\n ...(row.snippet === null ? {} : { snippet: row.snippet }),\n }));\n },\n\n async dropVectorIndex(params: DropVectorIndexParams): Promise<void> {\n if (vectorStrategy === undefined) return;\n const slot = vectorSlotFromDropIndexParams(params);\n const dropStatement = vectorStrategy.buildDropIndex?.(slot);\n if (dropStatement === undefined) return;\n try {\n await execRun(dropStatement);\n } catch (error) {\n // The per-field table (and thus its index) may never have been\n // materialized; treat a missing relation as already-dropped.\n if (!isMissingTableError(error)) throw error;\n }\n },\n\n // === Query Execution ===\n\n async execute<T>(query: SQL): Promise<readonly T[]> {\n return executionAdapter.execute<T>(query);\n },\n\n compileSql(\n query: SQL,\n ): Readonly<{ sql: string; params: readonly unknown[] }> {\n return executionAdapter.compile(query);\n },\n };\n\n return operationBackend;\n}\n\nfunction createTransactionBackend(\n options: CreatePostgresTransactionBackendOptions,\n): InternalOperationBackend {\n const txExecutionAdapter =\n options.executionAdapter ??\n createPostgresExecutionAdapter(options.db, options.adapterOptions);\n\n // A transaction-scoped backend gets its OWN per-field ensure-latch, never\n // the outer process-global one: a `CREATE TABLE/INDEX` that runs inside the\n // caller's transaction and then rolls back must not leave the shared latch\n // marking the slot \"ensured\" (which would skip the re-CREATE and make every\n // later write fail with \"relation does not exist\"). The fresh latch is\n // discarded with the transaction, so the next write re-ensures idempotently.\n // No latch when vector support is disabled (`vector: false`).\n return createPostgresOperationBackend({\n db: options.db,\n executionAdapter: txExecutionAdapter,\n adapterOptions: options.adapterOptions,\n operationStrategy: options.operationStrategy,\n tableNames: options.tableNames,\n capabilities: options.capabilities,\n fulltextStrategy: options.fulltextStrategy,\n vectorStrategy: options.vectorStrategy,\n vectorSlotLatch:\n options.vectorStrategy === undefined ? undefined : createVectorSlotLatch(),\n });\n}\n\n// Re-export schema utilities\nexport type { PostgresTableNames, PostgresTables } from \"./schema/postgres\";\nexport { createPostgresTables, tables } from \"./schema/postgres\";\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/backend/drizzle/columns/vector.ts","../src/query/dialect/vector/pgvector-strategy.ts"],"names":[],"mappings":";;;;AA0CsB,UAAA,CAInB;AAAA,E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A,CAAK,MAAM,CAAA;AAEzC,EAAA,MAAM,CAAA,GAAI,IAAA,CAAK,WAAA,EAAa,CAAA,IAAK,cAAA;AACjC,EAAA,MAAM,cAAA,GACJ,IAAA,CAAK,WAAA,EAAa,cAAA,IAAkB,4BAAA;AACtC,EAAA,MAAM,KAAA,GAAQ,IAAA,CAAK,WAAA,EAAa,KAAA,IAAS,qBAAA;AAGzC,EAAA,MAAM,MAAA,GAAS,CAAA,YAAA,EAAe,UAAA,GAAa,eAAA,GAAkB,EAAE,CAAA,cAAA,CAAA;AAE/D,EAAA,QAAQ,KAAK,SAAA;AAAW,IACtB,KAAK,MAAA,EAAQ;AACX,MAAA,OAAO,CAAA,EAAG,MAAM,CAAA,CAAA,EAAI,SAAS,CAAA,IAAA,EAAO,WAAW,CAAA,yBAAA,EAA4B,OAAO,CAAA,YAAA,EAAe,CAAC,CAAA,oBAAA,EAAuB,cAAc,CAAA,EAAA,CAAA;AAAA,IACzI;AAAA,IACA,KAAK,SAAA,EAAW;AACd,MAAA,OAAO,CAAA,EAAG,MAAM,CAAA,CAAA,EAAI,SAAS,OAAO,WAAW,CAAA,4BAAA,EAA+B,OAAO,CAAA,gBAAA,EAAmB,KAAK,CAAA,EAAA,CAAA;AAAA,IAC/G;AAAA,IACA,KAAK,MAAA,EAAQ;AACX,MAAA,MAAM,IAAI,KAAA;AAAA,QACR;AAAA,OACF;AAAA,IACF;AAAA,IACA,SAAS;AACP,MAAA,MAAM,cAAqB,IAAA,CAAK,SAAA;AAChC,MAAA,MAAM,IAAI,KAAA,CAAM,CAAA,+BAAA,EAAkC,MAAA,CAAO,WAAW,CAAC,CAAA,CAAE,CAAA;AAAA,IACzE;AAAA;AAEJ","file":"chunk-PRMKPFN5.js","sourcesContent":["/**\n * Custom Drizzle column type for pgvector's VECTOR type.\n *\n * Provides native support for PostgreSQL vector embeddings without\n * requiring runtime casts in queries.\n *\n * @example\n * ```typescript\n * import { pgTable, text, integer } from \"drizzle-orm/pg-core\";\n * import { vector } from \"./columns/vector\";\n *\n * const embeddings = pgTable(\"embeddings\", {\n * id: text(\"id\").primaryKey(),\n * embedding: vector(\"embedding\"),\n * dimensions: integer(\"dimensions\"),\n * });\n * ```\n */\nimport { customType } from \"drizzle-orm/pg-core\";\n\n/**\n * PostgreSQL vector column type for pgvector extension.\n *\n * Stores embeddings as native VECTOR type, enabling efficient\n * similarity search without runtime casts.\n *\n * The column type is unparameterized (`VECTOR` not `VECTOR(N)`)\n * to support multiple embedding dimensions in a single table.\n * Dimension validation is handled at the application level.\n *\n * @example\n * ```typescript\n * // In table definition\n * embedding: vector(\"embedding\").notNull(),\n *\n * // In queries - no cast needed\n * db.execute(sql`\n * SELECT * FROM embeddings\n * WHERE embedding <=> ${formatVector([0.1, 0.2, 0.3])} < 0.5\n * `);\n * ```\n */\nexport const vector = customType<{\n data: number[];\n driverData: string;\n config: { dimensions?: number };\n}>({\n dataType(config) {\n // Use parameterized type if dimensions specified, otherwise unparameterized\n return config?.dimensions ? `vector(${config.dimensions})` : \"vector\";\n },\n\n toDriver(value: number[]): string {\n // Format as pgvector literal: [1.0,2.0,3.0]\n return `[${value.join(\",\")}]`;\n },\n\n fromDriver(value: string): number[] {\n // pgvector returns '[1,2,3]' format\n // Handle both string and already-parsed array (some drivers differ)\n if (Array.isArray(value)) {\n return value as number[];\n }\n // Parse the string representation\n const content = value.slice(1, -1); // Remove [ and ]\n if (content === \"\") {\n return [];\n }\n return content.split(\",\").map((s) => Number.parseFloat(s.trim()));\n },\n});\n\n/**\n * Formats a number array as a pgvector literal string.\n *\n * Use this when building raw SQL queries with embeddings.\n *\n * @example\n * ```typescript\n * const embedding = [0.1, 0.2, 0.3];\n * db.execute(sql`\n * SELECT * FROM embeddings\n * WHERE embedding <=> ${formatVector(embedding)}::vector < 0.5\n * `);\n * ```\n */\nexport function formatVector(embedding: readonly number[]): string {\n return `[${embedding.join(\",\")}]`;\n}\n","/**\n * pgvector strategy.\n *\n * Postgres' pgvector extension is the one engine where the *same* SQL serves\n * brute-force and ANN: `ORDER BY embedding <=> q LIMIT k` scans sequentially\n * with no index and is rewritten to an HNSW/IVFFlat scan the moment a matching\n * index exists — the planner picks it up automatically. So `buildSearch` never\n * branches on `slot.indexType`; it emits one relevance scan and lets Postgres\n * decide. (Contrast libSQL/sqlite-vec, whose ANN needs distinct syntax.)\n *\n * Storage is per-`(nodeKind, fieldPath)`: a `vector(N)` column carrying the\n * field's fixed dimension `N`, with the operator-class-typed ANN index folded\n * into the same table contribution so materialization builds table + index\n * together. The typed column means the index does not need the legacy\n * `::vector(N)` cast-in-index escape hatch the old shared table relied on.\n *\n * Metric support is the full pgvector set — `cosine`, `l2`, `inner_product` —\n * advertised as data on `capabilities`, mirroring `vector_cosine_ops` /\n * `vector_l2_ops` / `vector_ip_ops` and the `<=>` / `<->` / `<#>` operators.\n *\n * Mirrors the `libsqlVectorStrategy` / `sqliteVecStrategy` structure while\n * letting Postgres' planner pick the ANN index when one exists.\n */\nimport { type SQL, sql } from \"drizzle-orm\";\n\nimport { formatVector } from \"../../../backend/drizzle/columns/vector\";\nimport { quotedTableName } from \"../../../backend/drizzle/operations/shared\";\nimport { type StrategyTableContribution } from \"../../../backend/table-contribution\";\nimport {\n type DeleteEmbeddingParams,\n type UpsertEmbeddingParams,\n type VectorCapabilities,\n type VectorMetric,\n type VectorSearchParams,\n} from \"../../../backend/types\";\nimport {\n assertFiniteEmbedding,\n quoteIdentifier,\n VECTOR_CONTRIBUTION_PREFIX,\n vectorMinScoreCondition,\n vectorPhysicalName,\n vectorScoreExpression,\n type VectorSlot,\n type VectorStrategy,\n} from \"../vector-strategy\";\n\n/** Physical-name prefixes for the per-field table and its ANN index. */\nconst TABLE_PREFIX = \"tg_vec\";\nconst INDEX_PREFIX = \"tg_vecidx\";\n\n/**\n * pgvector defines `hnsw.ef_search` as an integer GUC with a valid range\n * of 1..1000; `SET hnsw.ef_search = 1001` errors at the server. The\n * backend validates a per-search `efSearch` against this ceiling before\n * inlining it into `SET LOCAL`, surfacing a clear message instead of a raw\n * pgvector error.\n */\nexport const MAX_HNSW_EF_SEARCH = 1000;\n\n/**\n * Validates a per-search `efSearch` override against pgvector's\n * `hnsw.ef_search` valid range. `undefined` (no override) is accepted.\n */\nexport function assertPgvectorEfSearch(efSearch?: number): void {\n if (efSearch === undefined) return;\n if (!Number.isInteger(efSearch) || efSearch <= 0) {\n throw new Error(`efSearch must be a positive integer, got: ${efSearch}`);\n }\n if (efSearch > MAX_HNSW_EF_SEARCH) {\n throw new RangeError(\n `efSearch must be ≤ ${MAX_HNSW_EF_SEARCH} (pgvector's hnsw.ef_search ` +\n `valid range is 1..${MAX_HNSW_EF_SEARCH}), got: ${efSearch}`,\n );\n }\n}\n\n/**\n * pgvector's `vector` type caps at 16,000 dimensions for a stored column\n * (2,000 for an indexed column, but the column-type ceiling is what governs\n * storage; recall degradation beyond 2,000 is a tuning concern, not a hard\n * limit advertised here).\n */\nconst PGVECTOR_MAX_DIMENSIONS = 16_000;\n\n/** Default HNSW `m` (max connections per layer) — pgvector's own default. */\nconst DEFAULT_HNSW_M = 16;\n/** Default HNSW `ef_construction` (build candidate list) — pgvector's own default. */\nconst DEFAULT_HNSW_EF_CONSTRUCTION = 64;\n/** Default IVFFlat `lists` (inverted lists) — pgvector's own default. */\nconst DEFAULT_IVFFLAT_LISTS = 100;\n\nconst PGVECTOR_CAPABILITIES: VectorCapabilities = {\n supported: true,\n metrics: [\"cosine\", \"l2\", \"inner_product\"],\n indexTypes: [\"hnsw\", \"ivfflat\", \"none\"],\n maxDimensions: PGVECTOR_MAX_DIMENSIONS,\n};\n\n/** Whether a slot's declared index type materializes a real pgvector ANN index. */\nfunction usesAnnIndex(slot: VectorSlot): boolean {\n return slot.indexType === \"hnsw\" || slot.indexType === \"ivfflat\";\n}\n\n/**\n * pgvector operator class for a metric's ANN index — `vector_cosine_ops` /\n * `vector_l2_ops` / `vector_ip_ops`, mirroring the `<=>` / `<->` / `<#>`\n * distance operators this strategy emits.\n */\nfunction operatorClass(metric: VectorMetric): string {\n switch (metric) {\n case \"cosine\": {\n return \"vector_cosine_ops\";\n }\n case \"l2\": {\n return \"vector_l2_ops\";\n }\n case \"inner_product\": {\n return \"vector_ip_ops\";\n }\n default: {\n const _exhaustive: never = metric;\n throw new Error(`Unsupported vector metric: ${String(_exhaustive)}`);\n }\n }\n}\n\n/**\n * Formats an embedding as a `::vector` literal. The value is bound as a\n * parameter (not interpolated) — Postgres parses the `[a,b,c]` text and the\n * cast types it. Finiteness is validated first so a NaN names its index.\n */\nfunction vectorLiteral(embedding: readonly number[], name: string): SQL {\n assertFiniteEmbedding(embedding, name);\n return sql`${formatVector(embedding)}::vector`;\n}\n\nfunction distanceExpression(\n embeddingColumn: SQL,\n queryEmbedding: readonly number[],\n metric: VectorMetric,\n): SQL {\n const query = vectorLiteral(queryEmbedding, \"queryEmbedding\");\n switch (metric) {\n case \"cosine\": {\n return sql`(${embeddingColumn} <=> ${query})`;\n }\n case \"l2\": {\n return sql`(${embeddingColumn} <-> ${query})`;\n }\n case \"inner_product\": {\n return sql`(${embeddingColumn} <#> ${query})`;\n }\n default: {\n const _exhaustive: never = metric;\n throw new Error(`Unsupported vector metric: ${String(_exhaustive)}`);\n }\n }\n}\n\nexport const pgvectorStrategy: VectorStrategy = {\n name: \"pgvector\",\n capabilities: PGVECTOR_CAPABILITIES,\n\n tableName(graphId, nodeKind, fieldPath) {\n return vectorPhysicalName(TABLE_PREFIX, graphId, nodeKind, fieldPath);\n },\n\n ownedTables(slot): readonly StrategyTableContribution[] {\n const table = this.tableName(slot.graphId, slot.nodeKind, slot.fieldPath);\n const name = quoteIdentifier(table);\n\n // No standalone graph_id index: the PRIMARY KEY (graph_id, node_id) already\n // covers `WHERE graph_id = ?` via its leading column, so a separate index\n // would be pure write amplification.\n const createDdl = [\n `CREATE TABLE IF NOT EXISTS ${name} (\n \"graph_id\" TEXT NOT NULL,\n \"node_id\" TEXT NOT NULL,\n \"embedding\" vector(${slot.dimensions}) NOT NULL,\n \"created_at\" TIMESTAMPTZ NOT NULL,\n \"updated_at\" TIMESTAMPTZ NOT NULL,\n PRIMARY KEY (\"graph_id\", \"node_id\")\n);`,\n ];\n\n // The HNSW/IVFFlat index is intentionally NOT created here. pgvector\n // similarity SQL is planner-driven (`ORDER BY embedding <=> q LIMIT k`\n // uses the index when present, sequential-scans when not), so the index\n // is a pure materialization concern: `buildCreateIndex` builds it through\n // `materializeIndexes()` with the field's declared `m`/`ef_construction`/\n // `lists`. Building it eagerly here would bake in default tuning (the\n // write-ensure slot has no `indexParams`) and `CREATE INDEX IF NOT EXISTS`\n // would then mask the tuned index materialization would emit.\n\n return [\n {\n logicalName: `${VECTOR_CONTRIBUTION_PREFIX}:${slot.nodeKind}.${slot.fieldPath}`,\n owner: \"pgvector\",\n tableName: table,\n createDdl,\n runtimeEnsure: true,\n },\n ];\n },\n\n buildUpsert(slot, params: UpsertEmbeddingParams, timestamp): readonly SQL[] {\n const table = quotedTableName(\n this.tableName(slot.graphId, slot.nodeKind, slot.fieldPath),\n );\n const value = vectorLiteral(params.embedding, \"embedding\");\n return [\n sql`\n INSERT INTO ${table} (\"graph_id\", \"node_id\", \"embedding\", \"created_at\", \"updated_at\")\n VALUES (${params.graphId}, ${params.nodeId}, ${value}, ${timestamp}, ${timestamp})\n ON CONFLICT (\"graph_id\", \"node_id\")\n DO UPDATE SET \"embedding\" = EXCLUDED.\"embedding\", \"updated_at\" = EXCLUDED.\"updated_at\"\n `,\n ];\n },\n\n buildDelete(slot, params: DeleteEmbeddingParams): readonly SQL[] {\n const table = quotedTableName(\n this.tableName(slot.graphId, slot.nodeKind, slot.fieldPath),\n );\n return [\n sql`\n DELETE FROM ${table}\n WHERE \"graph_id\" = ${params.graphId} AND \"node_id\" = ${params.nodeId}\n `,\n ];\n },\n\n buildSearch(slot, params: VectorSearchParams): SQL {\n const table = quotedTableName(\n this.tableName(slot.graphId, slot.nodeKind, slot.fieldPath),\n );\n const embeddingColumn = sql`${table}.\"embedding\"`;\n const distance = distanceExpression(\n embeddingColumn,\n params.queryEmbedding,\n params.metric,\n );\n const score = vectorScoreExpression(distance, params.metric);\n\n // Same SQL for brute-force and ANN: with a matching HNSW/IVFFlat index the\n // Postgres planner rewrites this `ORDER BY distance LIMIT k` into an index\n // scan automatically, so the strategy never branches on `slot.indexType`.\n const conditions: SQL[] = [sql`${table}.\"graph_id\" = ${params.graphId}`];\n if (params.minScore !== undefined) {\n conditions.push(\n vectorMinScoreCondition(distance, params.metric, params.minScore),\n );\n }\n return sql`\n SELECT ${table}.\"node_id\" AS node_id, ${score} AS score\n FROM ${table}\n WHERE ${sql.join(conditions, sql` AND `)}\n ORDER BY ${distance} ASC\n LIMIT ${params.limit}\n `;\n },\n\n distanceExpression(embeddingColumn, queryEmbedding, metric) {\n return distanceExpression(embeddingColumn, queryEmbedding, metric);\n },\n\n buildCreateIndex(slot, options): SQL | undefined {\n if (!usesAnnIndex(slot)) return undefined;\n return sql.raw(\n pgvectorIndexDdl(\n this.tableName(slot.graphId, slot.nodeKind, slot.fieldPath),\n slot,\n options?.concurrent === true,\n ),\n );\n },\n\n buildDropIndex(slot): SQL | undefined {\n if (!usesAnnIndex(slot)) return undefined;\n const indexName = pgvectorIndexName(slot);\n return sql.raw(`DROP INDEX IF EXISTS ${quoteIdentifier(indexName)}`);\n },\n\n buildDropStorage(slot): readonly string[] {\n const table = quoteIdentifier(\n this.tableName(slot.graphId, slot.nodeKind, slot.fieldPath),\n );\n // CASCADE drops the ANN index along with the table.\n return [`DROP TABLE IF EXISTS ${table} CASCADE`];\n },\n};\n\nfunction pgvectorIndexName(slot: VectorSlot): string {\n return vectorPhysicalName(\n INDEX_PREFIX,\n slot.graphId,\n slot.nodeKind,\n slot.fieldPath,\n );\n}\n\nfunction pgvectorIndexDdl(\n table: string,\n slot: VectorSlot,\n concurrent: boolean,\n): string {\n const indexName = quoteIdentifier(pgvectorIndexName(slot));\n const quotedTable = quoteIdentifier(table);\n const opClass = operatorClass(slot.metric);\n // Honor the field's declared tuning; fall back to pgvector defaults.\n const m = slot.indexParams?.m ?? DEFAULT_HNSW_M;\n const efConstruction =\n slot.indexParams?.efConstruction ?? DEFAULT_HNSW_EF_CONSTRUCTION;\n const lists = slot.indexParams?.lists ?? DEFAULT_IVFFLAT_LISTS;\n // CONCURRENTLY builds without taking a write-blocking lock on the live table\n // (materializeIndexes passes concurrent on Postgres). Run outside a tx.\n const create = `CREATE INDEX${concurrent ? \" CONCURRENTLY\" : \"\"} IF NOT EXISTS`;\n\n switch (slot.indexType) {\n case \"hnsw\": {\n return `${create} ${indexName} ON ${quotedTable} USING hnsw (\"embedding\" ${opClass}) WITH (m = ${m}, ef_construction = ${efConstruction});`;\n }\n case \"ivfflat\": {\n return `${create} ${indexName} ON ${quotedTable} USING ivfflat (\"embedding\" ${opClass}) WITH (lists = ${lists});`;\n }\n case \"none\": {\n throw new Error(\n \"pgvectorIndexDdl called for a brute-force-only slot (indexType 'none').\",\n );\n }\n default: {\n const _exhaustive: never = slot.indexType;\n throw new Error(`Unsupported vector index type: ${String(_exhaustive)}`);\n }\n }\n}\n"]}