@prisma/orm-mongo 8.0.0-rc.9-dev.13 → 8.0.1-dev.10

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 (240) hide show
  1. package/README.md +48 -8
  2. package/dist/components-CWskH7lF-Dkc9ysZz.mjs +18 -0
  3. package/dist/components-CWskH7lF-Dkc9ysZz.mjs.map +1 -0
  4. package/dist/config.d.mts +14 -5
  5. package/dist/config.d.mts.map +1 -1
  6. package/dist/config.mjs +47 -25
  7. package/dist/config.mjs.map +1 -1
  8. package/dist/contract-builder.d.mts +33 -91
  9. package/dist/contract-builder.d.mts.map +1 -1
  10. package/dist/contract-builder.mjs +23 -7
  11. package/dist/contract-builder.mjs.map +1 -1
  12. package/dist/contract-types.d.mts +3 -0
  13. package/dist/contract-types.mjs +1 -0
  14. package/dist/control.d.mts +1 -1
  15. package/dist/control.d.mts.map +1 -1
  16. package/dist/control.mjs +3 -9
  17. package/dist/control.mjs.map +1 -1
  18. package/dist/init-quick-reference.md +116 -0
  19. package/dist/init-readme.md +35 -0
  20. package/dist/init.d.mts +7 -0
  21. package/dist/init.d.mts.map +1 -0
  22. package/dist/init.mjs +245 -0
  23. package/dist/init.mjs.map +1 -0
  24. package/dist/migration.d.mts +1 -0
  25. package/dist/migration.mjs +2 -0
  26. package/dist/module-base-B70DHaBE-k_c3Klu4.mjs +6 -0
  27. package/dist/module-base-B70DHaBE-k_c3Klu4.mjs.map +1 -0
  28. package/dist/mongo-static-Bti-jnlq-BGWHFTsS.mjs +74 -0
  29. package/dist/mongo-static-Bti-jnlq-BGWHFTsS.mjs.map +1 -0
  30. package/dist/{mongo-static-BdSGXYgQ-DSA-2DiK.d.mts → mongo-static-DabeBtmV-DILuhcbE.d.mts} +18 -5
  31. package/dist/mongo-static-DabeBtmV-DILuhcbE.d.mts.map +1 -0
  32. package/dist/runtime.d.mts +50 -44
  33. package/dist/runtime.d.mts.map +1 -1
  34. package/dist/runtime.mjs +106 -44
  35. package/dist/runtime.mjs.map +1 -1
  36. package/dist/static.d.mts +24 -2
  37. package/dist/static.d.mts.map +1 -0
  38. package/dist/static.mjs +2 -2
  39. package/dist/target__codec-ids.d.mts +1 -0
  40. package/dist/target__codec-ids.mjs +2 -0
  41. package/package.json +25 -70
  42. package/skills/prisma-8/SKILL.md +23 -18
  43. package/skills/prisma-8/references/build-a-target-package.md +117 -0
  44. package/skills/prisma-8/references/build.md +12 -16
  45. package/skills/prisma-8/references/contract.md +184 -78
  46. package/skills/prisma-8/references/debug.md +36 -25
  47. package/skills/prisma-8/references/feedback.md +8 -8
  48. package/skills/prisma-8/references/migration-model.md +23 -14
  49. package/skills/prisma-8/references/migration-review.md +23 -52
  50. package/skills/prisma-8/references/migrations.md +298 -82
  51. package/skills/prisma-8/references/queries-mongo.md +53 -17
  52. package/skills/prisma-8/references/queries-postgres.md +345 -34
  53. package/skills/prisma-8/references/queries.md +44 -25
  54. package/skills/prisma-8/references/quickstart.md +25 -22
  55. package/skills/prisma-8/references/runtime.md +172 -72
  56. package/skills/prisma-8/references/supabase.md +24 -21
  57. package/skills/prisma-8/references/upgrade-app.md +57 -22
  58. package/skills/prisma-8/references/upgrade-extension.md +72 -23
  59. package/skills/prisma-8/upgrading/app/upgrades/0.11-to-0.12/instructions.md +1 -1
  60. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.10-to-8.0.0-rc.11/instructions.md +34 -0
  61. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.11-to-8.0.0-rc.12/instructions.md +639 -0
  62. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.11-to-8.0.0-rc.12/scripts/multifile-psl/add-use-prisma-8-directive.mjs +60 -0
  63. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.11-to-8.0.0-rc.12/scripts/psl-verbatim-table-names/add-model-map.mjs +256 -0
  64. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.12-to-8.0.0-rc.13/instructions.md +332 -0
  65. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.12-to-8.0.0-rc.13/scripts/execution-ref-entry-field/rename-execution-ref-keys.ts +193 -0
  66. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.13-to-8.0.0-rc.14/instructions.md +489 -0
  67. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.14-to-8.0.0-rc.15/instructions.md +1368 -0
  68. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.14-to-8.0.0-rc.15/scripts/data-type-in-contract/data-type-in-contract.ts +1492 -0
  69. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.15-to-8.0.0-rc.16/instructions.md +108 -0
  70. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.16-to-8.0.0-rc.17/instructions.md +755 -0
  71. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.17-to-8.0.0-rc.18/instructions.md +973 -0
  72. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.17-to-8.0.0-rc.18/scripts/sql-expression-literals-psl/rewrite-sql-strings.mjs +271 -0
  73. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/instructions.md +1862 -0
  74. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/client-takes-contract-value/contract-from-json.ts +597 -0
  75. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/data-type-values/data-type-values.ts +1120 -0
  76. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/default-generators-to-on-create/rewrite-default-generators.mjs +86 -0
  77. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/mongo-field-storage-names/mongo-field-storage-names.ts +304 -0
  78. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/remove-nocheck/remove-nocheck.ts +2462 -0
  79. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.19-to-8.0.0-rc.20/instructions.md +513 -0
  80. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.19-to-8.0.0-rc.20/scripts/expand-type-aliases/expand-type-aliases.ts +1206 -0
  81. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.20-to-8.0.0-rc.21/instructions.md +803 -0
  82. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.21-to-8.0.0-rc.22/instructions.md +586 -0
  83. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.21-to-8.0.0-rc.22/scripts/value-objects-in-storage/value-objects-in-storage.ts +1184 -0
  84. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.22-to-8.0.0-rc.23/instructions.md +480 -0
  85. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.22-to-8.0.0-rc.23/scripts/mongo-index-names/mongo-index-names.ts +317 -0
  86. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/instructions.md +336 -0
  87. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/scripts/mongo-convenience-date-time-types/rewrite-mongo-date-types.ts +220 -0
  88. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/scripts/mongo-type-helpers/rewrite-mongo-field-types.ts +621 -0
  89. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.24-to-8.0.0-rc.25/instructions.md +1561 -0
  90. package/skills/prisma-8/upgrading/app/upgrades/8.0.0-rc.9-to-8.0.0-rc.10/instructions.md +13 -0
  91. package/skills/prisma-8/upgrading/extension/upgrades/0.11-to-0.12/instructions.md +1 -1
  92. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.10-to-8.0.0-rc.11/instructions.md +34 -0
  93. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.11-to-8.0.0-rc.12/instructions.md +857 -0
  94. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.11-to-8.0.0-rc.12/scripts/psl-verbatim-table-names/add-model-map.mjs +256 -0
  95. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.12-to-8.0.0-rc.13/instructions.md +504 -0
  96. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.12-to-8.0.0-rc.13/scripts/execution-ref-entry-field/rename-execution-ref-keys.ts +193 -0
  97. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.13-to-8.0.0-rc.14/instructions.md +143 -0
  98. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.14-to-8.0.0-rc.15/instructions.md +2168 -0
  99. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.14-to-8.0.0-rc.15/scripts/data-type-in-contract/data-type-in-contract.ts +1492 -0
  100. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.15-to-8.0.0-rc.16/instructions.md +223 -0
  101. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.16-to-8.0.0-rc.17/instructions.md +583 -0
  102. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.17-to-8.0.0-rc.18/instructions.md +1008 -0
  103. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/instructions.md +2186 -0
  104. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/client-takes-contract-value/contract-from-json.ts +597 -0
  105. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/data-type-values/data-type-values.ts +1120 -0
  106. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/default-generators-to-on-create/rewrite-default-generators.mjs +86 -0
  107. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/mongo-field-storage-names/mongo-field-storage-names.ts +304 -0
  108. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.18-to-8.0.0-rc.19/scripts/remove-nocheck/remove-nocheck.ts +2462 -0
  109. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.19-to-8.0.0-rc.20/instructions.md +381 -0
  110. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.19-to-8.0.0-rc.20/scripts/expand-type-aliases/expand-type-aliases.ts +1206 -0
  111. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.20-to-8.0.0-rc.21/instructions.md +570 -0
  112. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.21-to-8.0.0-rc.22/instructions.md +464 -0
  113. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.22-to-8.0.0-rc.23/instructions.md +410 -0
  114. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/instructions.md +303 -0
  115. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/scripts/mongo-convenience-date-time-types/rewrite-mongo-date-types.ts +220 -0
  116. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.23-to-8.0.0-rc.24/scripts/mongo-type-helpers/rewrite-mongo-field-types.ts +621 -0
  117. package/skills/prisma-8/upgrading/extension/upgrades/8.0.0-rc.24-to-8.0.0-rc.25/instructions.md +1593 -0
  118. package/dist/adapter.d.mts +0 -1
  119. package/dist/adapter.mjs +0 -2
  120. package/dist/adapter__codec-ids.d.mts +0 -1
  121. package/dist/adapter__codec-ids.mjs +0 -2
  122. package/dist/adapter__codec-types.d.mts +0 -1
  123. package/dist/adapter__codec-types.mjs +0 -2
  124. package/dist/adapter__codecs.d.mts +0 -1
  125. package/dist/adapter__codecs.mjs +0 -2
  126. package/dist/adapter__control.d.mts +0 -3
  127. package/dist/adapter__control.mjs +0 -3
  128. package/dist/adapter__runtime.d.mts +0 -3
  129. package/dist/adapter__runtime.mjs +0 -3
  130. package/dist/components.d.mts +0 -1
  131. package/dist/components.mjs +0 -2
  132. package/dist/components__authoring.d.mts +0 -1
  133. package/dist/components__authoring.mjs +0 -2
  134. package/dist/components__codec.d.mts +0 -1
  135. package/dist/components__codec.mjs +0 -2
  136. package/dist/components__components.d.mts +0 -1
  137. package/dist/components__components.mjs +0 -2
  138. package/dist/components__control.d.mts +0 -1
  139. package/dist/components__control.mjs +0 -2
  140. package/dist/components__emission.d.mts +0 -1
  141. package/dist/components__emission.mjs +0 -2
  142. package/dist/components__execution.d.mts +0 -1
  143. package/dist/components__execution.mjs +0 -2
  144. package/dist/components__ir.d.mts +0 -1
  145. package/dist/components__ir.mjs +0 -2
  146. package/dist/components__psl-ast.d.mts +0 -1
  147. package/dist/components__psl-ast.mjs +0 -2
  148. package/dist/components__utils.d.mts +0 -1
  149. package/dist/components__utils.mjs +0 -2
  150. package/dist/contract.d.mts +0 -1
  151. package/dist/contract.mjs +0 -2
  152. package/dist/contract__apply-specifier-default-control-policy.d.mts +0 -1
  153. package/dist/contract__apply-specifier-default-control-policy.mjs +0 -2
  154. package/dist/contract__contract-validation-error.d.mts +0 -1
  155. package/dist/contract__contract-validation-error.mjs +0 -2
  156. package/dist/contract__default-namespace.d.mts +0 -1
  157. package/dist/contract__default-namespace.mjs +0 -2
  158. package/dist/contract__enum-accessor.d.mts +0 -1
  159. package/dist/contract__enum-accessor.mjs +0 -2
  160. package/dist/contract__hashing-utils.d.mts +0 -1
  161. package/dist/contract__hashing-utils.mjs +0 -2
  162. package/dist/contract__hashing.d.mts +0 -1
  163. package/dist/contract__hashing.mjs +0 -2
  164. package/dist/contract__is-plain-record.d.mts +0 -1
  165. package/dist/contract__is-plain-record.mjs +0 -2
  166. package/dist/contract__resolve-domain-model.d.mts +0 -1
  167. package/dist/contract__resolve-domain-model.mjs +0 -2
  168. package/dist/contract__validate-domain.d.mts +0 -1
  169. package/dist/contract__validate-domain.mjs +0 -2
  170. package/dist/driver.d.mts +0 -1
  171. package/dist/driver.mjs +0 -2
  172. package/dist/driver__control.d.mts +0 -3
  173. package/dist/driver__control.mjs +0 -3
  174. package/dist/family-contract__canonicalization-hooks.d.mts +0 -1
  175. package/dist/family-contract__canonicalization-hooks.mjs +0 -2
  176. package/dist/family-contract__entity-kinds.d.mts +0 -1
  177. package/dist/family-contract__entity-kinds.mjs +0 -2
  178. package/dist/family__control-adapter.d.mts +0 -1
  179. package/dist/family__control-adapter.mjs +0 -2
  180. package/dist/family__ir.d.mts +0 -1
  181. package/dist/family__ir.mjs +0 -2
  182. package/dist/family__migration.d.mts +0 -1
  183. package/dist/family__migration.mjs +0 -2
  184. package/dist/family__pack.d.mts +0 -3
  185. package/dist/family__pack.mjs +0 -3
  186. package/dist/family__runtime.d.mts +0 -1
  187. package/dist/family__runtime.mjs +0 -2
  188. package/dist/family__schema-verify.d.mts +0 -1
  189. package/dist/family__schema-verify.mjs +0 -2
  190. package/dist/migration-tools__contract-snapshot-store.d.mts +0 -1
  191. package/dist/migration-tools__contract-snapshot-store.mjs +0 -2
  192. package/dist/mongo-static-BdSGXYgQ-DSA-2DiK.d.mts.map +0 -1
  193. package/dist/mongo-static-_wy-fC4P-mXlhePy0.mjs +0 -38
  194. package/dist/mongo-static-_wy-fC4P-mXlhePy0.mjs.map +0 -1
  195. package/dist/query-ast.d.mts +0 -1
  196. package/dist/query-ast.mjs +0 -2
  197. package/dist/query-ast__control.d.mts +0 -1
  198. package/dist/query-ast__control.mjs +0 -2
  199. package/dist/query-builder__contract-free.d.mts +0 -1
  200. package/dist/query-builder__contract-free.mjs +0 -2
  201. package/dist/target__control.d.mts +0 -1
  202. package/dist/target__control.mjs +0 -2
  203. package/dist/target__pack.d.mts +0 -3
  204. package/dist/target__pack.mjs +0 -3
  205. package/dist/utils.d.mts +0 -1
  206. package/dist/utils.mjs +0 -2
  207. package/dist/utils__abortable.d.mts +0 -1
  208. package/dist/utils__abortable.mjs +0 -2
  209. package/dist/utils__array-equal.d.mts +0 -1
  210. package/dist/utils__array-equal.mjs +0 -2
  211. package/dist/utils__assertions.d.mts +0 -1
  212. package/dist/utils__assertions.mjs +0 -2
  213. package/dist/utils__canonical-stringify.d.mts +0 -1
  214. package/dist/utils__canonical-stringify.mjs +0 -2
  215. package/dist/utils__casts.d.mts +0 -1
  216. package/dist/utils__casts.mjs +0 -2
  217. package/dist/utils__defined.d.mts +0 -1
  218. package/dist/utils__defined.mjs +0 -2
  219. package/dist/utils__hash-content.d.mts +0 -1
  220. package/dist/utils__hash-content.mjs +0 -2
  221. package/dist/utils__internal-error.d.mts +0 -1
  222. package/dist/utils__internal-error.mjs +0 -2
  223. package/dist/utils__json.d.mts +0 -1
  224. package/dist/utils__json.mjs +0 -2
  225. package/dist/utils__promise.d.mts +0 -1
  226. package/dist/utils__promise.mjs +0 -2
  227. package/dist/utils__redact-db-url.d.mts +0 -1
  228. package/dist/utils__redact-db-url.mjs +0 -2
  229. package/dist/utils__result.d.mts +0 -1
  230. package/dist/utils__result.mjs +0 -2
  231. package/dist/utils__simplify-deep.d.mts +0 -1
  232. package/dist/utils__simplify-deep.mjs +0 -2
  233. package/dist/utils__structured-error.d.mts +0 -1
  234. package/dist/utils__structured-error.mjs +0 -2
  235. package/dist/utils__suppress-idle-connection-errors.d.mts +0 -1
  236. package/dist/utils__suppress-idle-connection-errors.mjs +0 -2
  237. package/dist/utils__types.d.mts +0 -1
  238. package/dist/utils__types.mjs +0 -2
  239. package/dist/value__mongodb-types.d.mts +0 -1
  240. package/dist/value__mongodb-types.mjs +0 -2
@@ -0,0 +1,271 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Rewrites raw SQL written as a quoted string in PSL into a `sql` literal with the same text:
4
+ * the `where:` and `expression:` arguments of `@@index`, `@@fullTextIndex` and `@@check`, and
5
+ * `using =` and `withCheck =` in `policy_*` blocks.
6
+ *
7
+ * Usage:
8
+ * node scripts/codemods/rewrite-sql-strings.mjs <file-or-glob> [...more]
9
+ *
10
+ * Globs never descend into `node_modules` or `dist`. Files are rewritten in place, and each
11
+ * changed file is printed with its number of rewrites. Running it twice is a no-op.
12
+ */
13
+
14
+ import { readFileSync, writeFileSync } from 'node:fs';
15
+ import { glob } from 'node:fs/promises';
16
+ import { argv, exit, stderr, stdout } from 'node:process';
17
+ import { fileURLToPath } from 'node:url';
18
+
19
+ const ATTRIBUTE_START = /@@(index|fullTextIndex|check)\s*\(/g;
20
+ const ATTRIBUTE_ARGUMENT = /^(where|expression)\s*:\s*$/;
21
+ const POLICY_START = /^[ \t]*policy_[A-Za-z]+\s+[A-Za-z_][A-Za-z0-9_]*\s*\{/gm;
22
+ const POLICY_ENTRY = /(^|\n)([ \t]*(using|withCheck)\s*=\s*)$/;
23
+ const TAG = 'sql';
24
+
25
+ /** The end (exclusive) of the quoted string that starts at `start`, or -1 when it is not closed. */
26
+ function stringEnd(source, start) {
27
+ const quote = source[start];
28
+ for (let i = start + 1; i < source.length; i += 1) {
29
+ if (source[i] === '\\') {
30
+ i += 1;
31
+ continue;
32
+ }
33
+ if (source[i] === quote) return i + 1;
34
+ if (source[i] === '\n') return -1;
35
+ }
36
+ return -1;
37
+ }
38
+
39
+ /** The index of the line break that ends the `//` comment starting at `start`, or the source length. */
40
+ function commentEnd(source, start) {
41
+ const newline = source.indexOf('\n', start);
42
+ return newline === -1 ? source.length : newline;
43
+ }
44
+
45
+ /** The `[start, end)` ranges of `//` and `///` comments, quoted strings and backtick literals: text in which nothing declares an attribute or a policy. */
46
+ function inertRanges(source) {
47
+ const ranges = [];
48
+ for (let i = 0; i < source.length; i += 1) {
49
+ const ch = source[i];
50
+ if (ch === '"' || ch === "'") {
51
+ const end = stringEnd(source, i);
52
+ if (end !== -1) ranges.push([i, end]);
53
+ i = end === -1 ? commentEnd(source, i) : end - 1;
54
+ continue;
55
+ }
56
+ if (ch === '`') {
57
+ const end = source.indexOf('`', i + 1);
58
+ if (end === -1) continue;
59
+ ranges.push([i, end + 1]);
60
+ i = end;
61
+ continue;
62
+ }
63
+ if (ch === '/' && source[i + 1] === '/') {
64
+ const end = commentEnd(source, i);
65
+ ranges.push([i, end]);
66
+ i = end;
67
+ }
68
+ }
69
+ return ranges;
70
+ }
71
+
72
+ /** The end (exclusive) of the bracketed region opening at `open`, skipping strings and tagged literals. */
73
+ function closingEnd(source, open, openChar, closeChar) {
74
+ let depth = 0;
75
+ for (let i = open; i < source.length; i += 1) {
76
+ const ch = source[i];
77
+ if (ch === '"' || ch === "'" || ch === '`') {
78
+ const end = ch === '`' ? source.indexOf('`', i + 1) + 1 : stringEnd(source, i);
79
+ if (end <= 0) return -1;
80
+ i = end - 1;
81
+ continue;
82
+ }
83
+ if (ch === '/' && source[i + 1] === '/') {
84
+ const newline = source.indexOf('\n', i);
85
+ if (newline === -1) return -1;
86
+ i = newline;
87
+ continue;
88
+ }
89
+ if (ch === openChar) depth += 1;
90
+ if (ch === closeChar) {
91
+ depth -= 1;
92
+ if (depth === 0) return i + 1;
93
+ }
94
+ }
95
+ return -1;
96
+ }
97
+
98
+ function hexAt(raw, start, length) {
99
+ const digits = raw.slice(start, start + length);
100
+ return digits.length === length && /^[0-9A-Fa-f]+$/.test(digits)
101
+ ? String.fromCharCode(Number.parseInt(digits, 16))
102
+ : undefined;
103
+ }
104
+
105
+ const SIMPLE_ESCAPES = {
106
+ '/': '/',
107
+ b: '\b',
108
+ f: '\f',
109
+ n: '\n',
110
+ r: '\r',
111
+ t: '\t',
112
+ '"': '"',
113
+ "'": "'",
114
+ '\\': '\\',
115
+ };
116
+
117
+ /** The text of a PSL string literal body, decoded as the PSL parser decodes it. */
118
+ export function decodePslString(raw) {
119
+ let out = '';
120
+ for (let i = 0; i < raw.length; i += 1) {
121
+ const ch = raw[i];
122
+ if (ch !== '\\' || i + 1 >= raw.length) {
123
+ out += ch;
124
+ continue;
125
+ }
126
+ const next = raw[i + 1];
127
+ if (Object.hasOwn(SIMPLE_ESCAPES, next)) {
128
+ out += SIMPLE_ESCAPES[next];
129
+ i += 1;
130
+ continue;
131
+ }
132
+ const length = next === 'x' ? 2 : next === 'u' ? 4 : 0;
133
+ const decoded = length === 0 ? undefined : hexAt(raw, i + 2, length);
134
+ if (decoded === undefined) {
135
+ out += `\\${next}`;
136
+ i += 1;
137
+ continue;
138
+ }
139
+ out += decoded;
140
+ i += 1 + length;
141
+ }
142
+ return out;
143
+ }
144
+
145
+ /** A `sql` literal holding `text`: the backtick form, or the double-quote form when the text holds a backtick. A backtick body doubles a backslash only before another backslash or at its end, where it would be read as an escape. */
146
+ export function printSqlLiteral(text) {
147
+ if (text.includes('`')) {
148
+ const escaped = text
149
+ .replace(/\\/g, '\\\\')
150
+ .replace(/"/g, '\\"')
151
+ .replace(/\n/g, '\\n')
152
+ .replace(/\r/g, '\\r');
153
+ return `${TAG}"${escaped}"`;
154
+ }
155
+ const fenced = text.replace(/\\(?=\\|$)/g, '\\\\');
156
+ return text.includes('\n') ? `${TAG}\`\n${fenced}\n\`` : `${TAG}\`${fenced}\``;
157
+ }
158
+
159
+ /** The quoted strings to rewrite, as `[start, end)` ranges of `source`. */
160
+ function stringRanges(source) {
161
+ const inert = inertRanges(source);
162
+ const isInert = (index) => inert.some(([start, end]) => index >= start && index < end);
163
+ const ranges = [];
164
+ for (const match of source.matchAll(ATTRIBUTE_START)) {
165
+ if (isInert(match.index)) continue;
166
+ const open = match.index + match[0].length - 1;
167
+ const close = closingEnd(source, open, '(', ')');
168
+ if (close === -1) continue;
169
+ let depth = 0;
170
+ let argumentStart = open + 1;
171
+ for (let i = open + 1; i < close - 1; i += 1) {
172
+ const ch = source[i];
173
+ if (ch === '/' && source[i + 1] === '/') {
174
+ i = commentEnd(source, i);
175
+ continue;
176
+ }
177
+ if (ch === '"' || ch === "'") {
178
+ const end = stringEnd(source, i);
179
+ if (end === -1) break;
180
+ const before = source.slice(argumentStart, i).trim();
181
+ if (depth === 0 && ATTRIBUTE_ARGUMENT.test(before)) ranges.push([i, end]);
182
+ i = end - 1;
183
+ continue;
184
+ }
185
+ if (ch === '`') {
186
+ i = source.indexOf('`', i + 1);
187
+ if (i === -1) break;
188
+ continue;
189
+ }
190
+ if (ch === '(' || ch === '[' || ch === '{') depth += 1;
191
+ if (ch === ')' || ch === ']' || ch === '}') depth -= 1;
192
+ if (ch === ',' && depth === 0) argumentStart = i + 1;
193
+ }
194
+ }
195
+ for (const match of source.matchAll(POLICY_START)) {
196
+ if (isInert(match.index + match[0].search(/\S/))) continue;
197
+ const open = match.index + match[0].length - 1;
198
+ const close = closingEnd(source, open, '{', '}');
199
+ if (close === -1) continue;
200
+ for (let i = open + 1; i < close - 1; i += 1) {
201
+ const ch = source[i];
202
+ if (ch === '/' && source[i + 1] === '/') {
203
+ i = commentEnd(source, i);
204
+ continue;
205
+ }
206
+ if (ch === '"' || ch === "'") {
207
+ const end = stringEnd(source, i);
208
+ if (end === -1) break;
209
+ if (POLICY_ENTRY.test(source.slice(open + 1, i))) ranges.push([i, end]);
210
+ i = end - 1;
211
+ continue;
212
+ }
213
+ if (ch === '`') {
214
+ i = source.indexOf('`', i + 1);
215
+ if (i === -1) break;
216
+ }
217
+ }
218
+ }
219
+ return ranges.sort((a, b) => a[0] - b[0]);
220
+ }
221
+
222
+ /** Rewrites `source` and returns it with the number of strings rewritten. */
223
+ export function rewriteSqlStringsWithCount(source) {
224
+ const ranges = stringRanges(source);
225
+ let out = '';
226
+ let position = 0;
227
+ for (const [start, end] of ranges) {
228
+ out += source.slice(position, start);
229
+ out += printSqlLiteral(decodePslString(source.slice(start + 1, end - 1)));
230
+ position = end;
231
+ }
232
+ out += source.slice(position);
233
+ return { source: out, count: ranges.length };
234
+ }
235
+
236
+ export function rewriteSqlStrings(source) {
237
+ return rewriteSqlStringsWithCount(source).source;
238
+ }
239
+
240
+ const SKIPPED_DIRECTORIES = /(^|\/)(node_modules|dist)\//;
241
+
242
+ async function filesMatching(patterns) {
243
+ const files = new Set();
244
+ for (const pattern of patterns) {
245
+ for await (const match of glob(pattern, {
246
+ exclude: (path) => /(^|\/)(node_modules|dist)$/.test(path),
247
+ })) {
248
+ if (!SKIPPED_DIRECTORIES.test(match)) files.add(match);
249
+ }
250
+ }
251
+ return [...files].sort();
252
+ }
253
+
254
+ async function main() {
255
+ const patterns = argv.slice(2);
256
+ if (patterns.length === 0) {
257
+ stderr.write('usage: node rewrite-sql-strings.mjs <file-or-glob> [...more]\n');
258
+ exit(2);
259
+ }
260
+ for (const file of await filesMatching(patterns)) {
261
+ const before = readFileSync(file, 'utf8');
262
+ const { source, count } = rewriteSqlStringsWithCount(before);
263
+ if (count === 0) continue;
264
+ writeFileSync(file, source);
265
+ stdout.write(`${file}: ${count} rewritten\n`);
266
+ }
267
+ }
268
+
269
+ if (argv[1] && fileURLToPath(import.meta.url) === argv[1]) {
270
+ await main();
271
+ }