@db3.ai/app 0.1.0-beta.1

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 (1024) hide show
  1. package/LICENSE +31 -0
  2. package/README.md +241 -0
  3. package/agent-instructions.md +69 -0
  4. package/bin/db3-agents.mjs +140 -0
  5. package/bin/db3.mjs +11 -0
  6. package/dist/ai/AIErrors.d.ts +33 -0
  7. package/dist/ai/AIErrors.js +43 -0
  8. package/dist/ai/AIFailover.d.ts +25 -0
  9. package/dist/ai/AIFailover.js +96 -0
  10. package/dist/ai/AIProviders.d.ts +35 -0
  11. package/dist/ai/AIProviders.js +197 -0
  12. package/dist/ai/AIQuotaDeferredError.d.ts +19 -0
  13. package/dist/ai/AIQuotaDeferredError.js +25 -0
  14. package/dist/ai/AIRateLimiter.d.ts +103 -0
  15. package/dist/ai/AIRateLimiter.js +690 -0
  16. package/dist/ai/Agent.d.ts +633 -0
  17. package/dist/ai/Agent.js +2976 -0
  18. package/dist/ai/AgentHistory.d.ts +11 -0
  19. package/dist/ai/AgentHistory.js +495 -0
  20. package/dist/ai/AgentRunJob.d.ts +21 -0
  21. package/dist/ai/AgentRunJob.js +53 -0
  22. package/dist/ai/Ai.d.ts +375 -0
  23. package/dist/ai/Ai.js +1771 -0
  24. package/dist/ai/AiConversation.d.ts +17 -0
  25. package/dist/ai/AiConversation.js +38 -0
  26. package/dist/ai/AiExecutionContext.d.ts +7 -0
  27. package/dist/ai/AiExecutionContext.js +16 -0
  28. package/dist/ai/AiMessage.d.ts +43 -0
  29. package/dist/ai/AiMessage.js +105 -0
  30. package/dist/ai/AiRateLimitBucket.d.ts +69 -0
  31. package/dist/ai/AiRateLimitBucket.js +138 -0
  32. package/dist/ai/AiRateLimitReservation.d.ts +56 -0
  33. package/dist/ai/AiRateLimitReservation.js +101 -0
  34. package/dist/ai/AiRequest.d.ts +78 -0
  35. package/dist/ai/AiRequest.js +384 -0
  36. package/dist/ai/Estimates.d.ts +26 -0
  37. package/dist/ai/Estimates.js +34 -0
  38. package/dist/ai/Models.d.ts +88 -0
  39. package/dist/ai/Models.js +328 -0
  40. package/dist/ai/OpenAIProviderError.d.ts +30 -0
  41. package/dist/ai/OpenAIProviderError.js +77 -0
  42. package/dist/ai/OpenAIQuotaRetry.d.ts +27 -0
  43. package/dist/ai/OpenAIQuotaRetry.js +144 -0
  44. package/dist/ai/ProviderCosts.d.ts +55 -0
  45. package/dist/ai/ProviderCosts.js +108 -0
  46. package/dist/ai/ResponsesProviderCall.d.ts +13 -0
  47. package/dist/ai/ResponsesProviderCall.js +29 -0
  48. package/dist/ai/agentCitations.d.ts +27 -0
  49. package/dist/ai/agentCitations.js +153 -0
  50. package/dist/ai/contracts/AI.d.ts +347 -0
  51. package/dist/ai/contracts/AI.js +13 -0
  52. package/dist/ai/contracts/Agent.d.ts +476 -0
  53. package/dist/ai/contracts/Agent.js +1 -0
  54. package/dist/ai/contracts/AiRequestRunCostSummary.d.ts +31 -0
  55. package/dist/ai/contracts/AiRequestRunCostSummary.js +1 -0
  56. package/dist/ai/contracts/Pricing.d.ts +88 -0
  57. package/dist/ai/contracts/Pricing.js +1 -0
  58. package/dist/ai/contracts/Providers.d.ts +68 -0
  59. package/dist/ai/contracts/Providers.js +1 -0
  60. package/dist/ai/contracts/QuotaRetry.d.ts +38 -0
  61. package/dist/ai/contracts/QuotaRetry.js +1 -0
  62. package/dist/ai/contracts/RateLimits.d.ts +45 -0
  63. package/dist/ai/contracts/RateLimits.js +1 -0
  64. package/dist/ai/contracts/ResponsesProviderCall.d.ts +28 -0
  65. package/dist/ai/contracts/ResponsesProviderCall.js +1 -0
  66. package/dist/ai/contracts/index.d.ts +8 -0
  67. package/dist/ai/contracts/index.js +8 -0
  68. package/dist/ai/index.d.ts +27 -0
  69. package/dist/ai/index.js +27 -0
  70. package/dist/ai/registry.d.ts +28 -0
  71. package/dist/ai/registry.js +49 -0
  72. package/dist/ai/sdkNodeCompatibility.d.ts +76 -0
  73. package/dist/ai/toolCallRecovery.d.ts +16 -0
  74. package/dist/ai/toolCallRecovery.js +71 -0
  75. package/dist/ai/toolHelpers.d.ts +65 -0
  76. package/dist/ai/toolHelpers.js +106 -0
  77. package/dist/auth/Auth.d.ts +641 -0
  78. package/dist/auth/Auth.js +927 -0
  79. package/dist/auth/AuthProvider.d.ts +104 -0
  80. package/dist/auth/AuthProvider.js +185 -0
  81. package/dist/auth/AuthToken.d.ts +123 -0
  82. package/dist/auth/AuthToken.js +156 -0
  83. package/dist/auth/PasswordResetToken.d.ts +33 -0
  84. package/dist/auth/PasswordResetToken.js +64 -0
  85. package/dist/auth/UserIdentity.d.ts +46 -0
  86. package/dist/auth/UserIdentity.js +72 -0
  87. package/dist/auth/contracts/AuthProviderDriver.d.ts +94 -0
  88. package/dist/auth/contracts/AuthProviderDriver.js +1 -0
  89. package/dist/auth/contracts/index.d.ts +1 -0
  90. package/dist/auth/contracts/index.js +1 -0
  91. package/dist/auth/index.d.ts +8 -0
  92. package/dist/auth/index.js +8 -0
  93. package/dist/auth/passwordHash.d.ts +26 -0
  94. package/dist/auth/passwordHash.js +96 -0
  95. package/dist/auth/providers/GoogleAuthProvider.d.ts +135 -0
  96. package/dist/auth/providers/GoogleAuthProvider.js +305 -0
  97. package/dist/auth/providers/PasswordAuthProvider.d.ts +115 -0
  98. package/dist/auth/providers/PasswordAuthProvider.js +195 -0
  99. package/dist/auth/providers/index.d.ts +3 -0
  100. package/dist/auth/providers/index.js +3 -0
  101. package/dist/auth/providers/resolveAuthProviders.d.ts +29 -0
  102. package/dist/auth/providers/resolveAuthProviders.js +111 -0
  103. package/dist/cache/Cache.d.ts +67 -0
  104. package/dist/cache/Cache.js +115 -0
  105. package/dist/cache/contracts/CacheDriver.d.ts +10 -0
  106. package/dist/cache/contracts/CacheDriver.js +1 -0
  107. package/dist/cache/contracts/CacheOperations.d.ts +26 -0
  108. package/dist/cache/contracts/CacheOperations.js +1 -0
  109. package/dist/cache/contracts/CacheOptions.d.ts +51 -0
  110. package/dist/cache/contracts/CacheOptions.js +1 -0
  111. package/dist/cache/contracts/CacheRepository.d.ts +45 -0
  112. package/dist/cache/contracts/CacheRepository.js +1 -0
  113. package/dist/cache/contracts/index.d.ts +4 -0
  114. package/dist/cache/contracts/index.js +4 -0
  115. package/dist/cache/drivers/CacheManagerDriver.d.ts +55 -0
  116. package/dist/cache/drivers/CacheManagerDriver.js +68 -0
  117. package/dist/cache/drivers/createCacheDriver.d.ts +8 -0
  118. package/dist/cache/drivers/createCacheDriver.js +147 -0
  119. package/dist/cache/index.d.ts +2 -0
  120. package/dist/cache/index.js +2 -0
  121. package/dist/cli/Cli.d.ts +17 -0
  122. package/dist/cli/Cli.js +120 -0
  123. package/dist/cli/commands/repl.d.ts +9 -0
  124. package/dist/cli/commands/repl.js +27 -0
  125. package/dist/cli/contracts/CliCommand.d.ts +30 -0
  126. package/dist/cli/contracts/CliCommand.js +1 -0
  127. package/dist/cli/contracts/CliConfig.d.ts +9 -0
  128. package/dist/cli/contracts/CliConfig.js +1 -0
  129. package/dist/cli/contracts/CliOptions.d.ts +13 -0
  130. package/dist/cli/contracts/CliOptions.js +1 -0
  131. package/dist/cli/contracts/CommandDefinition.d.ts +18 -0
  132. package/dist/cli/contracts/CommandDefinition.js +1 -0
  133. package/dist/cli/contracts/ReplOptions.d.ts +11 -0
  134. package/dist/cli/contracts/ReplOptions.js +1 -0
  135. package/dist/cli/contracts/index.d.ts +5 -0
  136. package/dist/cli/contracts/index.js +1 -0
  137. package/dist/cli/defineCommand.d.ts +10 -0
  138. package/dist/cli/defineCommand.js +25 -0
  139. package/dist/cli/index.d.ts +5 -0
  140. package/dist/cli/index.js +5 -0
  141. package/dist/cli/runDb3.d.ts +11 -0
  142. package/dist/cli/runDb3.js +70 -0
  143. package/dist/cli/runRepl.d.ts +12 -0
  144. package/dist/cli/runRepl.js +54 -0
  145. package/dist/config/Config.d.ts +48 -0
  146. package/dist/config/Config.js +80 -0
  147. package/dist/config/env.d.ts +26 -0
  148. package/dist/config/env.js +117 -0
  149. package/dist/config/index.d.ts +2 -0
  150. package/dist/config/index.js +2 -0
  151. package/dist/db/ActiveProjection.d.ts +90 -0
  152. package/dist/db/ActiveProjection.js +134 -0
  153. package/dist/db/ActiveQueryBuilder.d.ts +268 -0
  154. package/dist/db/ActiveQueryBuilder.js +621 -0
  155. package/dist/db/ActiveRecord.d.ts +483 -0
  156. package/dist/db/ActiveRecord.js +765 -0
  157. package/dist/db/Database.d.ts +277 -0
  158. package/dist/db/Database.js +827 -0
  159. package/dist/db/FieldType.d.ts +540 -0
  160. package/dist/db/FieldType.js +706 -0
  161. package/dist/db/SoftDeletes.d.ts +30 -0
  162. package/dist/db/SoftDeletes.js +93 -0
  163. package/dist/db/commands/check.d.ts +6 -0
  164. package/dist/db/commands/check.js +8 -0
  165. package/dist/db/commands/index.d.ts +6 -0
  166. package/dist/db/commands/index.js +17 -0
  167. package/dist/db/commands/makeMigration.d.ts +7 -0
  168. package/dist/db/commands/makeMigration.js +9 -0
  169. package/dist/db/commands/migrate.d.ts +6 -0
  170. package/dist/db/commands/migrate.js +8 -0
  171. package/dist/db/connection.d.ts +10 -0
  172. package/dist/db/connection.js +55 -0
  173. package/dist/db/contracts/ActiveRecordDefinition.d.ts +64 -0
  174. package/dist/db/contracts/ActiveRecordDefinition.js +1 -0
  175. package/dist/db/contracts/DatabaseBackup.d.ts +7 -0
  176. package/dist/db/contracts/DatabaseBackup.js +1 -0
  177. package/dist/db/contracts/DatabaseMigrationsConfig.d.ts +9 -0
  178. package/dist/db/contracts/DatabaseMigrationsConfig.js +1 -0
  179. package/dist/db/contracts/index.d.ts +3 -0
  180. package/dist/db/contracts/index.js +3 -0
  181. package/dist/db/dialects/columnTypes.d.ts +20 -0
  182. package/dist/db/dialects/columnTypes.js +67 -0
  183. package/dist/db/dialects/index.d.ts +6 -0
  184. package/dist/db/dialects/index.js +6 -0
  185. package/dist/db/dialects/mariadb.d.ts +7 -0
  186. package/dist/db/dialects/mariadb.js +35 -0
  187. package/dist/db/dialects/mysql.d.ts +14 -0
  188. package/dist/db/dialects/mysql.js +203 -0
  189. package/dist/db/dialects/postgres.d.ts +5 -0
  190. package/dist/db/dialects/postgres.js +106 -0
  191. package/dist/db/dialects/resolveDialect.d.ts +14 -0
  192. package/dist/db/dialects/resolveDialect.js +57 -0
  193. package/dist/db/dialects/types.d.ts +44 -0
  194. package/dist/db/dialects/types.js +1 -0
  195. package/dist/db/errors.d.ts +15 -0
  196. package/dist/db/errors.js +20 -0
  197. package/dist/db/fields/BooleanField.d.ts +29 -0
  198. package/dist/db/fields/BooleanField.js +89 -0
  199. package/dist/db/fields/CharUuidField.d.ts +29 -0
  200. package/dist/db/fields/CharUuidField.js +63 -0
  201. package/dist/db/fields/ChoiceStringField.d.ts +25 -0
  202. package/dist/db/fields/ChoiceStringField.js +58 -0
  203. package/dist/db/fields/DecimalField.d.ts +20 -0
  204. package/dist/db/fields/DecimalField.js +105 -0
  205. package/dist/db/fields/EmailField.d.ts +31 -0
  206. package/dist/db/fields/EmailField.js +52 -0
  207. package/dist/db/fields/EncryptedJsonField.d.ts +82 -0
  208. package/dist/db/fields/EncryptedJsonField.js +169 -0
  209. package/dist/db/fields/IntegerField.d.ts +35 -0
  210. package/dist/db/fields/IntegerField.js +139 -0
  211. package/dist/db/fields/JsonField.d.ts +40 -0
  212. package/dist/db/fields/JsonField.js +79 -0
  213. package/dist/db/fields/JsonStringField.d.ts +21 -0
  214. package/dist/db/fields/JsonStringField.js +41 -0
  215. package/dist/db/fields/LinkField.d.ts +180 -0
  216. package/dist/db/fields/LinkField.js +359 -0
  217. package/dist/db/fields/PasswordField.d.ts +93 -0
  218. package/dist/db/fields/PasswordField.js +180 -0
  219. package/dist/db/fields/StringField.d.ts +43 -0
  220. package/dist/db/fields/StringField.js +102 -0
  221. package/dist/db/fields/StringListField.d.ts +54 -0
  222. package/dist/db/fields/StringListField.js +124 -0
  223. package/dist/db/fields/TextField.d.ts +25 -0
  224. package/dist/db/fields/TextField.js +44 -0
  225. package/dist/db/fields/TimestampField.d.ts +47 -0
  226. package/dist/db/fields/TimestampField.js +97 -0
  227. package/dist/db/fields/UlidField.d.ts +69 -0
  228. package/dist/db/fields/UlidField.js +114 -0
  229. package/dist/db/fields/UrlField.d.ts +30 -0
  230. package/dist/db/fields/UrlField.js +61 -0
  231. package/dist/db/fields/VectorField.d.ts +64 -0
  232. package/dist/db/fields/VectorField.js +180 -0
  233. package/dist/db/fields/createField.d.ts +2 -0
  234. package/dist/db/fields/createField.js +13 -0
  235. package/dist/db/fields/field.d.ts +36 -0
  236. package/dist/db/fields/field.js +101 -0
  237. package/dist/db/fields/index.d.ts +18 -0
  238. package/dist/db/fields/index.js +18 -0
  239. package/dist/db/fields/resolveFields.d.ts +17 -0
  240. package/dist/db/fields/resolveFields.js +38 -0
  241. package/dist/db/index.d.ts +13 -0
  242. package/dist/db/index.js +13 -0
  243. package/dist/db/migrations/DatabaseMigrationManager.d.ts +57 -0
  244. package/dist/db/migrations/DatabaseMigrationManager.js +589 -0
  245. package/dist/db/migrations/DatabaseSchemaInspector.d.ts +19 -0
  246. package/dist/db/migrations/DatabaseSchemaInspector.js +384 -0
  247. package/dist/db/migrations/KnexMigrationRenderer.d.ts +11 -0
  248. package/dist/db/migrations/KnexMigrationRenderer.js +390 -0
  249. package/dist/db/migrations/ModelSchemaCollector.d.ts +29 -0
  250. package/dist/db/migrations/ModelSchemaCollector.js +183 -0
  251. package/dist/db/migrations/SchemaDiffer.d.ts +13 -0
  252. package/dist/db/migrations/SchemaDiffer.js +427 -0
  253. package/dist/db/migrations/SchemaNames.d.ts +27 -0
  254. package/dist/db/migrations/SchemaNames.js +46 -0
  255. package/dist/db/migrations/SchemaSnapshotSerializer.d.ts +30 -0
  256. package/dist/db/migrations/SchemaSnapshotSerializer.js +49 -0
  257. package/dist/db/migrations/contracts/DatabaseMigration.d.ts +118 -0
  258. package/dist/db/migrations/contracts/DatabaseMigration.js +1 -0
  259. package/dist/db/migrations/contracts/SchemaChange.d.ts +83 -0
  260. package/dist/db/migrations/contracts/SchemaChange.js +1 -0
  261. package/dist/db/migrations/contracts/SchemaSnapshot.d.ts +94 -0
  262. package/dist/db/migrations/contracts/SchemaSnapshot.js +1 -0
  263. package/dist/db/migrations/contracts/index.d.ts +3 -0
  264. package/dist/db/migrations/contracts/index.js +3 -0
  265. package/dist/db/migrations/errors.d.ts +35 -0
  266. package/dist/db/migrations/errors.js +45 -0
  267. package/dist/db/migrations/index.d.ts +10 -0
  268. package/dist/db/migrations/index.js +10 -0
  269. package/dist/db/migrations/promoteDestructiveSchemaChanges.d.ts +19 -0
  270. package/dist/db/migrations/promoteDestructiveSchemaChanges.js +102 -0
  271. package/dist/db/queryMonitor.d.ts +142 -0
  272. package/dist/db/queryMonitor.js +357 -0
  273. package/dist/db/sqlErrors.d.ts +11 -0
  274. package/dist/db/sqlErrors.js +17 -0
  275. package/dist/db/test/db.d.ts +11 -0
  276. package/dist/db/test/db.js +109 -0
  277. package/dist/devtools/HttpEventDispatcher.d.ts +19 -0
  278. package/dist/devtools/HttpEventDispatcher.js +180 -0
  279. package/dist/devtools/contracts/DevtoolsEventDispatcher.d.ts +53 -0
  280. package/dist/devtools/contracts/DevtoolsEventDispatcher.js +1 -0
  281. package/dist/devtools/contracts/index.d.ts +1 -0
  282. package/dist/devtools/contracts/index.js +1 -0
  283. package/dist/devtools/environment.d.ts +10 -0
  284. package/dist/devtools/environment.js +14 -0
  285. package/dist/devtools/index.d.ts +3 -0
  286. package/dist/devtools/index.js +3 -0
  287. package/dist/events/Events.d.ts +70 -0
  288. package/dist/events/Events.js +126 -0
  289. package/dist/events/contracts/EventConstructor.d.ts +12 -0
  290. package/dist/events/contracts/EventConstructor.js +1 -0
  291. package/dist/events/contracts/EventListener.d.ts +6 -0
  292. package/dist/events/contracts/EventListener.js +1 -0
  293. package/dist/events/contracts/EventUnsubscribe.d.ts +4 -0
  294. package/dist/events/contracts/EventUnsubscribe.js +1 -0
  295. package/dist/events/contracts/index.d.ts +3 -0
  296. package/dist/events/contracts/index.js +3 -0
  297. package/dist/events/index.d.ts +2 -0
  298. package/dist/events/index.js +2 -0
  299. package/dist/flows/FlowBlockRegistry.d.ts +39 -0
  300. package/dist/flows/FlowBlockRegistry.js +66 -0
  301. package/dist/flows/FlowCompiler.d.ts +26 -0
  302. package/dist/flows/FlowCompiler.js +488 -0
  303. package/dist/flows/FlowDefinitionError.d.ts +21 -0
  304. package/dist/flows/FlowDefinitionError.js +16 -0
  305. package/dist/flows/FlowStepJob.d.ts +28 -0
  306. package/dist/flows/FlowStepJob.js +31 -0
  307. package/dist/flows/FlowValueValidator.d.ts +32 -0
  308. package/dist/flows/FlowValueValidator.js +105 -0
  309. package/dist/flows/Flows.d.ts +102 -0
  310. package/dist/flows/Flows.js +871 -0
  311. package/dist/flows/blocks/FlowInput.d.ts +10 -0
  312. package/dist/flows/blocks/FlowInput.js +29 -0
  313. package/dist/flows/blocks/FlowOutput.d.ts +10 -0
  314. package/dist/flows/blocks/FlowOutput.js +29 -0
  315. package/dist/flows/blocks/Subflow.d.ts +10 -0
  316. package/dist/flows/blocks/Subflow.js +21 -0
  317. package/dist/flows/blocks/index.d.ts +6 -0
  318. package/dist/flows/blocks/index.js +8 -0
  319. package/dist/flows/constants.d.ts +55 -0
  320. package/dist/flows/constants.js +47 -0
  321. package/dist/flows/contracts/FlowBlock.d.ts +64 -0
  322. package/dist/flows/contracts/FlowBlock.js +1 -0
  323. package/dist/flows/contracts/FlowCompiler.d.ts +30 -0
  324. package/dist/flows/contracts/FlowCompiler.js +1 -0
  325. package/dist/flows/contracts/FlowDefinition.d.ts +118 -0
  326. package/dist/flows/contracts/FlowDefinition.js +1 -0
  327. package/dist/flows/contracts/FlowDefinitionStore.d.ts +47 -0
  328. package/dist/flows/contracts/FlowDefinitionStore.js +1 -0
  329. package/dist/flows/contracts/FlowRun.d.ts +40 -0
  330. package/dist/flows/contracts/FlowRun.js +1 -0
  331. package/dist/flows/contracts/FlowValue.d.ts +49 -0
  332. package/dist/flows/contracts/FlowValue.js +1 -0
  333. package/dist/flows/contracts/FlowsService.d.ts +37 -0
  334. package/dist/flows/contracts/FlowsService.js +1 -0
  335. package/dist/flows/contracts/index.d.ts +7 -0
  336. package/dist/flows/contracts/index.js +7 -0
  337. package/dist/flows/defineBlock.d.ts +18 -0
  338. package/dist/flows/defineBlock.js +19 -0
  339. package/dist/flows/drivers/FileFlowDefinitionStore.d.ts +53 -0
  340. package/dist/flows/drivers/FileFlowDefinitionStore.js +313 -0
  341. package/dist/flows/drivers/index.d.ts +1 -0
  342. package/dist/flows/drivers/index.js +1 -0
  343. package/dist/flows/errors.d.ts +8 -0
  344. package/dist/flows/errors.js +19 -0
  345. package/dist/flows/index.d.ts +13 -0
  346. package/dist/flows/index.js +13 -0
  347. package/dist/flows/models/FlowRun.d.ts +55 -0
  348. package/dist/flows/models/FlowRun.js +126 -0
  349. package/dist/flows/models/FlowRunEvent.d.ts +41 -0
  350. package/dist/flows/models/FlowRunEvent.js +85 -0
  351. package/dist/flows/models/FlowStepRun.d.ts +56 -0
  352. package/dist/flows/models/FlowStepRun.js +137 -0
  353. package/dist/flows/models/index.d.ts +10 -0
  354. package/dist/flows/models/index.js +14 -0
  355. package/dist/index.d.ts +25 -0
  356. package/dist/index.js +16 -0
  357. package/dist/logging/Log.d.ts +66 -0
  358. package/dist/logging/Log.js +79 -0
  359. package/dist/logging/contracts/HttpExchange.d.ts +50 -0
  360. package/dist/logging/contracts/HttpExchange.js +1 -0
  361. package/dist/logging/contracts/Logger.d.ts +41 -0
  362. package/dist/logging/contracts/Logger.js +1 -0
  363. package/dist/logging/contracts/LoggerDriver.d.ts +16 -0
  364. package/dist/logging/contracts/LoggerDriver.js +1 -0
  365. package/dist/logging/contracts/LoggingOptions.d.ts +49 -0
  366. package/dist/logging/contracts/LoggingOptions.js +1 -0
  367. package/dist/logging/contracts/index.d.ts +4 -0
  368. package/dist/logging/contracts/index.js +4 -0
  369. package/dist/logging/drivers/PinoLoggerDriver.d.ts +24 -0
  370. package/dist/logging/drivers/PinoLoggerDriver.js +291 -0
  371. package/dist/logging/drivers/index.d.ts +1 -0
  372. package/dist/logging/drivers/index.js +1 -0
  373. package/dist/logging/index.d.ts +3 -0
  374. package/dist/logging/index.js +3 -0
  375. package/dist/logging/transports/DevtoolsLogTransport.d.ts +35 -0
  376. package/dist/logging/transports/DevtoolsLogTransport.js +96 -0
  377. package/dist/mail/Mail.d.ts +62 -0
  378. package/dist/mail/Mail.js +98 -0
  379. package/dist/mail/index.d.ts +4 -0
  380. package/dist/mail/index.js +4 -0
  381. package/dist/mail/transports/FileMailTransport.d.ts +12 -0
  382. package/dist/mail/transports/FileMailTransport.js +41 -0
  383. package/dist/mail/transports/MailgunTransport.d.ts +22 -0
  384. package/dist/mail/transports/MailgunTransport.js +57 -0
  385. package/dist/mail/transports/ResendTransport.d.ts +14 -0
  386. package/dist/mail/transports/ResendTransport.js +55 -0
  387. package/dist/media/MediaFile.d.ts +48 -0
  388. package/dist/media/MediaFile.js +104 -0
  389. package/dist/media/MediaItem.d.ts +47 -0
  390. package/dist/media/MediaItem.js +106 -0
  391. package/dist/media/MediaLibrary.d.ts +42 -0
  392. package/dist/media/MediaLibrary.js +94 -0
  393. package/dist/media/MediaManager.d.ts +159 -0
  394. package/dist/media/MediaManager.js +754 -0
  395. package/dist/media/constants.d.ts +27 -0
  396. package/dist/media/constants.js +19 -0
  397. package/dist/media/contracts/MediaOptions.d.ts +8 -0
  398. package/dist/media/contracts/MediaOptions.js +1 -0
  399. package/dist/media/contracts/index.d.ts +1 -0
  400. package/dist/media/contracts/index.js +1 -0
  401. package/dist/media/image/ImageReconstructionError.d.ts +15 -0
  402. package/dist/media/image/ImageReconstructionError.js +18 -0
  403. package/dist/media/image/ImageVariantRenderer.d.ts +44 -0
  404. package/dist/media/image/ImageVariantRenderer.js +337 -0
  405. package/dist/media/image/ImageVariantRequest.d.ts +28 -0
  406. package/dist/media/image/ImageVariantRequest.js +55 -0
  407. package/dist/media/image/contracts/ImageProcessor.d.ts +46 -0
  408. package/dist/media/image/contracts/ImageProcessor.js +1 -0
  409. package/dist/media/image/contracts/ImageVariant.d.ts +31 -0
  410. package/dist/media/image/contracts/ImageVariant.js +7 -0
  411. package/dist/media/image/contracts/ImageVariantCacheOptions.d.ts +20 -0
  412. package/dist/media/image/contracts/ImageVariantCacheOptions.js +1 -0
  413. package/dist/media/image/contracts/index.d.ts +3 -0
  414. package/dist/media/image/contracts/index.js +3 -0
  415. package/dist/media/image/index.d.ts +5 -0
  416. package/dist/media/image/index.js +5 -0
  417. package/dist/media/image/processors/SharpImageProcessor.d.ts +25 -0
  418. package/dist/media/image/processors/SharpImageProcessor.js +112 -0
  419. package/dist/media/image/processors/index.d.ts +1 -0
  420. package/dist/media/image/processors/index.js +1 -0
  421. package/dist/media/index.d.ts +8 -0
  422. package/dist/media/index.js +8 -0
  423. package/dist/media/types.d.ts +129 -0
  424. package/dist/media/types.js +1 -0
  425. package/dist/queue/FailedJob.d.ts +31 -0
  426. package/dist/queue/FailedJob.js +41 -0
  427. package/dist/queue/Queue.d.ts +208 -0
  428. package/dist/queue/Queue.js +685 -0
  429. package/dist/queue/QueueEvents.d.ts +31 -0
  430. package/dist/queue/QueueEvents.js +49 -0
  431. package/dist/queue/QueueRetryLaterError.d.ts +23 -0
  432. package/dist/queue/QueueRetryLaterError.js +34 -0
  433. package/dist/queue/QueueWorker.d.ts +72 -0
  434. package/dist/queue/QueueWorker.js +236 -0
  435. package/dist/queue/QueueableJob.d.ts +109 -0
  436. package/dist/queue/QueueableJob.js +137 -0
  437. package/dist/queue/QueuedJob.d.ts +31 -0
  438. package/dist/queue/QueuedJob.js +52 -0
  439. package/dist/queue/RetryPolicy.d.ts +51 -0
  440. package/dist/queue/RetryPolicy.js +84 -0
  441. package/dist/queue/commands/index.d.ts +4 -0
  442. package/dist/queue/commands/index.js +11 -0
  443. package/dist/queue/commands/makeJob.d.ts +7 -0
  444. package/dist/queue/commands/makeJob.js +9 -0
  445. package/dist/queue/console.d.ts +60 -0
  446. package/dist/queue/console.js +348 -0
  447. package/dist/queue/contracts/CreateJob.d.ts +12 -0
  448. package/dist/queue/contracts/CreateJob.js +1 -0
  449. package/dist/queue/contracts/QueueDriver.d.ts +122 -0
  450. package/dist/queue/contracts/QueueDriver.js +1 -0
  451. package/dist/queue/contracts/QueueEvents.d.ts +61 -0
  452. package/dist/queue/contracts/QueueEvents.js +1 -0
  453. package/dist/queue/contracts/QueuePayload.d.ts +136 -0
  454. package/dist/queue/contracts/QueuePayload.js +1 -0
  455. package/dist/queue/contracts/QueueRetry.d.ts +43 -0
  456. package/dist/queue/contracts/QueueRetry.js +1 -0
  457. package/dist/queue/contracts/QueueService.d.ts +199 -0
  458. package/dist/queue/contracts/QueueService.js +1 -0
  459. package/dist/queue/contracts/QueueWorkerLifecycle.d.ts +65 -0
  460. package/dist/queue/contracts/QueueWorkerLifecycle.js +1 -0
  461. package/dist/queue/contracts/QueueableJob.d.ts +50 -0
  462. package/dist/queue/contracts/QueueableJob.js +1 -0
  463. package/dist/queue/contracts/RedisQueueDriver.d.ts +37 -0
  464. package/dist/queue/contracts/RedisQueueDriver.js +1 -0
  465. package/dist/queue/contracts/index.d.ts +9 -0
  466. package/dist/queue/contracts/index.js +9 -0
  467. package/dist/queue/createJob.d.ts +18 -0
  468. package/dist/queue/createJob.js +52 -0
  469. package/dist/queue/drivers/DatabaseQueueDriver.d.ts +83 -0
  470. package/dist/queue/drivers/DatabaseQueueDriver.js +393 -0
  471. package/dist/queue/drivers/RedisQueueDriver.d.ts +102 -0
  472. package/dist/queue/drivers/RedisQueueDriver.js +641 -0
  473. package/dist/queue/index.d.ts +13 -0
  474. package/dist/queue/index.js +13 -0
  475. package/dist/queue/queueMonitor.d.ts +133 -0
  476. package/dist/queue/queueMonitor.js +238 -0
  477. package/dist/scheduler/ScheduledCall.d.ts +25 -0
  478. package/dist/scheduler/ScheduledCall.js +31 -0
  479. package/dist/scheduler/ScheduledEvent.d.ts +74 -0
  480. package/dist/scheduler/ScheduledEvent.js +115 -0
  481. package/dist/scheduler/ScheduledJob.d.ts +34 -0
  482. package/dist/scheduler/ScheduledJob.js +50 -0
  483. package/dist/scheduler/ScheduledOccurrence.d.ts +74 -0
  484. package/dist/scheduler/ScheduledOccurrence.js +194 -0
  485. package/dist/scheduler/ScheduledOccurrenceRecorder.d.ts +34 -0
  486. package/dist/scheduler/ScheduledOccurrenceRecorder.js +106 -0
  487. package/dist/scheduler/Scheduler.d.ts +76 -0
  488. package/dist/scheduler/Scheduler.js +186 -0
  489. package/dist/scheduler/SchedulerWorker.d.ts +37 -0
  490. package/dist/scheduler/SchedulerWorker.js +119 -0
  491. package/dist/scheduler/console.d.ts +18 -0
  492. package/dist/scheduler/console.js +288 -0
  493. package/dist/scheduler/constants.d.ts +13 -0
  494. package/dist/scheduler/constants.js +23 -0
  495. package/dist/scheduler/contracts/Scheduler.d.ts +104 -0
  496. package/dist/scheduler/contracts/Scheduler.js +1 -0
  497. package/dist/scheduler/contracts/SchedulerConsole.d.ts +37 -0
  498. package/dist/scheduler/contracts/SchedulerConsole.js +1 -0
  499. package/dist/scheduler/contracts/SchedulerWorker.d.ts +18 -0
  500. package/dist/scheduler/contracts/SchedulerWorker.js +1 -0
  501. package/dist/scheduler/contracts/index.d.ts +3 -0
  502. package/dist/scheduler/contracts/index.js +3 -0
  503. package/dist/scheduler/index.d.ts +9 -0
  504. package/dist/scheduler/index.js +9 -0
  505. package/dist/scheduler/scheduleTime.d.ts +30 -0
  506. package/dist/scheduler/scheduleTime.js +64 -0
  507. package/dist/security/Security.d.ts +60 -0
  508. package/dist/security/Security.js +252 -0
  509. package/dist/security/SecurityError.d.ts +13 -0
  510. package/dist/security/SecurityError.js +16 -0
  511. package/dist/security/contracts/SecurityOptions.d.ts +17 -0
  512. package/dist/security/contracts/SecurityOptions.js +1 -0
  513. package/dist/security/contracts/SecurityPayload.d.ts +13 -0
  514. package/dist/security/contracts/SecurityPayload.js +1 -0
  515. package/dist/security/contracts/index.d.ts +2 -0
  516. package/dist/security/contracts/index.js +2 -0
  517. package/dist/security/ensureAppKey.d.ts +16 -0
  518. package/dist/security/ensureAppKey.js +69 -0
  519. package/dist/security/index.d.ts +4 -0
  520. package/dist/security/index.js +4 -0
  521. package/dist/serialization/Serializable.d.ts +17 -0
  522. package/dist/serialization/Serializable.js +1 -0
  523. package/dist/serialization/SerializationError.d.ts +40 -0
  524. package/dist/serialization/SerializationError.js +42 -0
  525. package/dist/serialization/SerializationRegistry.d.ts +76 -0
  526. package/dist/serialization/SerializationRegistry.js +153 -0
  527. package/dist/serialization/SerializationRegistryError.d.ts +18 -0
  528. package/dist/serialization/SerializationRegistryError.js +18 -0
  529. package/dist/serialization/Serializer.d.ts +37 -0
  530. package/dist/serialization/Serializer.js +477 -0
  531. package/dist/serialization/contracts/SerializableClass.d.ts +20 -0
  532. package/dist/serialization/contracts/SerializableClass.js +1 -0
  533. package/dist/serialization/contracts/SerializedValue.d.ts +59 -0
  534. package/dist/serialization/contracts/SerializedValue.js +19 -0
  535. package/dist/serialization/contracts/SerializerOptions.d.ts +17 -0
  536. package/dist/serialization/contracts/SerializerOptions.js +1 -0
  537. package/dist/serialization/contracts/SerializerService.d.ts +26 -0
  538. package/dist/serialization/contracts/SerializerService.js +1 -0
  539. package/dist/serialization/contracts/index.d.ts +4 -0
  540. package/dist/serialization/contracts/index.js +4 -0
  541. package/dist/serialization/index.d.ts +6 -0
  542. package/dist/serialization/index.js +6 -0
  543. package/dist/server/App.d.ts +141 -0
  544. package/dist/server/App.js +225 -0
  545. package/dist/server/RequestContext.d.ts +42 -0
  546. package/dist/server/RequestContext.js +95 -0
  547. package/dist/server/appContext.d.ts +34 -0
  548. package/dist/server/appContext.js +42 -0
  549. package/dist/server/index.d.ts +3 -0
  550. package/dist/server/index.js +3 -0
  551. package/dist/ssr/constants.d.ts +6 -0
  552. package/dist/ssr/constants.js +6 -0
  553. package/dist/ssr/contracts/DocumentHead.d.ts +32 -0
  554. package/dist/ssr/contracts/DocumentHead.js +1 -0
  555. package/dist/ssr/contracts/SsrRenderContext.d.ts +23 -0
  556. package/dist/ssr/contracts/SsrRenderContext.js +1 -0
  557. package/dist/ssr/contracts/SsrRenderResult.d.ts +21 -0
  558. package/dist/ssr/contracts/SsrRenderResult.js +1 -0
  559. package/dist/ssr/contracts/SsrRenderer.d.ts +9 -0
  560. package/dist/ssr/contracts/SsrRenderer.js +1 -0
  561. package/dist/ssr/contracts/SsrRequest.d.ts +16 -0
  562. package/dist/ssr/contracts/SsrRequest.js +1 -0
  563. package/dist/ssr/contracts/index.d.ts +5 -0
  564. package/dist/ssr/contracts/index.js +1 -0
  565. package/dist/ssr/createSsrRenderContext.d.ts +9 -0
  566. package/dist/ssr/createSsrRenderContext.js +20 -0
  567. package/dist/ssr/fastify/contracts/FastifySsrOptions.d.ts +21 -0
  568. package/dist/ssr/fastify/contracts/FastifySsrOptions.js +1 -0
  569. package/dist/ssr/fastify/contracts/index.d.ts +1 -0
  570. package/dist/ssr/fastify/contracts/index.js +1 -0
  571. package/dist/ssr/fastify/fastifySsr.d.ts +25 -0
  572. package/dist/ssr/fastify/fastifySsr.js +89 -0
  573. package/dist/ssr/fastify/index.d.ts +2 -0
  574. package/dist/ssr/fastify/index.js +1 -0
  575. package/dist/ssr/index.d.ts +6 -0
  576. package/dist/ssr/index.js +5 -0
  577. package/dist/ssr/renderDocumentHead.d.ts +8 -0
  578. package/dist/ssr/renderDocumentHead.js +62 -0
  579. package/dist/ssr/renderSsrDocument.d.ts +14 -0
  580. package/dist/ssr/renderSsrDocument.js +28 -0
  581. package/dist/ssr/serializeSsrState.d.ts +10 -0
  582. package/dist/ssr/serializeSsrState.js +18 -0
  583. package/dist/ssr/vite/contracts/ViteSsrOptions.d.ts +45 -0
  584. package/dist/ssr/vite/contracts/ViteSsrOptions.js +1 -0
  585. package/dist/ssr/vite/contracts/index.d.ts +1 -0
  586. package/dist/ssr/vite/contracts/index.js +1 -0
  587. package/dist/ssr/vite/fastifyViteSsr.d.ts +24 -0
  588. package/dist/ssr/vite/fastifyViteSsr.js +213 -0
  589. package/dist/ssr/vite/index.d.ts +4 -0
  590. package/dist/ssr/vite/index.js +2 -0
  591. package/dist/ssr/vite/viteSsrManifest.d.ts +16 -0
  592. package/dist/ssr/vite/viteSsrManifest.js +63 -0
  593. package/dist/storage/Storage.d.ts +244 -0
  594. package/dist/storage/Storage.js +348 -0
  595. package/dist/storage/contents.d.ts +8 -0
  596. package/dist/storage/contents.js +18 -0
  597. package/dist/storage/contracts/StorageListing.d.ts +50 -0
  598. package/dist/storage/contracts/StorageListing.js +1 -0
  599. package/dist/storage/contracts/index.d.ts +1 -0
  600. package/dist/storage/contracts/index.js +1 -0
  601. package/dist/storage/drivers/FlystorageDisk.d.ts +185 -0
  602. package/dist/storage/drivers/FlystorageDisk.js +250 -0
  603. package/dist/storage/drivers/FlystorageListing.d.ts +27 -0
  604. package/dist/storage/drivers/FlystorageListing.js +69 -0
  605. package/dist/storage/drivers/LocalStorageDisk.d.ts +25 -0
  606. package/dist/storage/drivers/LocalStorageDisk.js +38 -0
  607. package/dist/storage/drivers/S3StorageDisk.d.ts +17 -0
  608. package/dist/storage/drivers/S3StorageDisk.js +102 -0
  609. package/dist/storage/drivers/index.d.ts +3 -0
  610. package/dist/storage/drivers/index.js +3 -0
  611. package/dist/storage/index.d.ts +7 -0
  612. package/dist/storage/index.js +7 -0
  613. package/dist/storage/mime.d.ts +7 -0
  614. package/dist/storage/mime.js +28 -0
  615. package/dist/storage/path.d.ts +22 -0
  616. package/dist/storage/path.js +61 -0
  617. package/dist/storage/types.d.ts +218 -0
  618. package/dist/storage/types.js +1 -0
  619. package/dist/url/UrlGenerator.d.ts +32 -0
  620. package/dist/url/UrlGenerator.js +61 -0
  621. package/dist/url/contracts/UrlGeneratorOptions.d.ts +13 -0
  622. package/dist/url/contracts/UrlGeneratorOptions.js +1 -0
  623. package/dist/url/contracts/index.d.ts +1 -0
  624. package/dist/url/contracts/index.js +1 -0
  625. package/dist/url/index.d.ts +2 -0
  626. package/dist/url/index.js +2 -0
  627. package/dist/validation/index.d.ts +28 -0
  628. package/dist/validation/index.js +260 -0
  629. package/package.json +290 -0
  630. package/src/ai/AIErrors.ts +46 -0
  631. package/src/ai/AIFailover.ts +101 -0
  632. package/src/ai/AIProviders.ts +228 -0
  633. package/src/ai/AIQuotaDeferredError.ts +26 -0
  634. package/src/ai/AIRateLimiter.ts +880 -0
  635. package/src/ai/Agent.ts +3630 -0
  636. package/src/ai/AgentHistory.ts +576 -0
  637. package/src/ai/AgentRunJob.ts +65 -0
  638. package/src/ai/Ai.ts +2176 -0
  639. package/src/ai/AiConversation.ts +49 -0
  640. package/src/ai/AiExecutionContext.ts +19 -0
  641. package/src/ai/AiMessage.ts +131 -0
  642. package/src/ai/AiRateLimitBucket.ts +163 -0
  643. package/src/ai/AiRateLimitReservation.ts +117 -0
  644. package/src/ai/AiRequest.ts +466 -0
  645. package/src/ai/Estimates.ts +36 -0
  646. package/src/ai/Models.ts +373 -0
  647. package/src/ai/OpenAIProviderError.ts +98 -0
  648. package/src/ai/OpenAIQuotaRetry.ts +183 -0
  649. package/src/ai/ProviderCosts.ts +156 -0
  650. package/src/ai/README.md +187 -0
  651. package/src/ai/ResponsesProviderCall.ts +34 -0
  652. package/src/ai/agentCitations.ts +186 -0
  653. package/src/ai/contracts/AI.ts +380 -0
  654. package/src/ai/contracts/Agent.ts +539 -0
  655. package/src/ai/contracts/AiRequestRunCostSummary.ts +31 -0
  656. package/src/ai/contracts/Pricing.ts +95 -0
  657. package/src/ai/contracts/Providers.ts +73 -0
  658. package/src/ai/contracts/QuotaRetry.ts +39 -0
  659. package/src/ai/contracts/RateLimits.ts +49 -0
  660. package/src/ai/contracts/ResponsesProviderCall.ts +30 -0
  661. package/src/ai/contracts/index.ts +8 -0
  662. package/src/ai/examples/Conversation.ts +12 -0
  663. package/src/ai/examples/HelpAgent.ts +27 -0
  664. package/src/ai/examples/createHelpImage.ts +13 -0
  665. package/src/ai/index.ts +27 -0
  666. package/src/ai/registry.ts +70 -0
  667. package/src/ai/sdkNodeCompatibility.d.ts +76 -0
  668. package/src/ai/toolCallRecovery.ts +82 -0
  669. package/src/ai/toolHelpers.ts +145 -0
  670. package/src/auth/Auth.ts +1422 -0
  671. package/src/auth/AuthProvider.ts +251 -0
  672. package/src/auth/AuthToken.ts +239 -0
  673. package/src/auth/PasswordResetToken.ts +93 -0
  674. package/src/auth/README.md +396 -0
  675. package/src/auth/UserIdentity.ts +112 -0
  676. package/src/auth/contracts/AuthProviderDriver.ts +114 -0
  677. package/src/auth/contracts/index.ts +1 -0
  678. package/src/auth/examples/runPasswordAuth.ts +45 -0
  679. package/src/auth/index.ts +8 -0
  680. package/src/auth/passwordHash.ts +160 -0
  681. package/src/auth/providers/GoogleAuthProvider.ts +470 -0
  682. package/src/auth/providers/PasswordAuthProvider.ts +256 -0
  683. package/src/auth/providers/index.ts +3 -0
  684. package/src/auth/providers/resolveAuthProviders.ts +166 -0
  685. package/src/cache/Cache.ts +151 -0
  686. package/src/cache/README.md +137 -0
  687. package/src/cache/contracts/CacheDriver.ts +11 -0
  688. package/src/cache/contracts/CacheOperations.ts +28 -0
  689. package/src/cache/contracts/CacheOptions.ts +56 -0
  690. package/src/cache/contracts/CacheRepository.ts +62 -0
  691. package/src/cache/contracts/index.ts +4 -0
  692. package/src/cache/drivers/CacheManagerDriver.ts +84 -0
  693. package/src/cache/drivers/createCacheDriver.ts +191 -0
  694. package/src/cache/examples/runNoteCache.ts +37 -0
  695. package/src/cache/index.ts +2 -0
  696. package/src/cli/Cli.ts +110 -0
  697. package/src/cli/README.md +164 -0
  698. package/src/cli/commands/repl.ts +29 -0
  699. package/src/cli/contracts/CliCommand.ts +33 -0
  700. package/src/cli/contracts/CliConfig.ts +10 -0
  701. package/src/cli/contracts/CliOptions.ts +13 -0
  702. package/src/cli/contracts/CommandDefinition.ts +19 -0
  703. package/src/cli/contracts/ReplOptions.ts +11 -0
  704. package/src/cli/contracts/index.ts +5 -0
  705. package/src/cli/defineCommand.ts +25 -0
  706. package/src/cli/index.ts +5 -0
  707. package/src/cli/runDb3.ts +63 -0
  708. package/src/cli/runRepl.ts +55 -0
  709. package/src/config/Config.ts +110 -0
  710. package/src/config/README.md +142 -0
  711. package/src/config/env.ts +164 -0
  712. package/src/config/examples/loadNotesConfig.ts +29 -0
  713. package/src/config/examples/runConfig.ts +40 -0
  714. package/src/config/index.ts +2 -0
  715. package/src/db/ActiveProjection.ts +311 -0
  716. package/src/db/ActiveQueryBuilder.ts +831 -0
  717. package/src/db/ActiveRecord.ts +1317 -0
  718. package/src/db/Database.ts +1283 -0
  719. package/src/db/FieldType.ts +1243 -0
  720. package/src/db/README.md +508 -0
  721. package/src/db/SoftDeletes.ts +158 -0
  722. package/src/db/commands/check.ts +10 -0
  723. package/src/db/commands/index.ts +19 -0
  724. package/src/db/commands/makeMigration.ts +11 -0
  725. package/src/db/commands/migrate.ts +10 -0
  726. package/src/db/connection.ts +68 -0
  727. package/src/db/contracts/ActiveRecordDefinition.ts +69 -0
  728. package/src/db/contracts/DatabaseBackup.ts +7 -0
  729. package/src/db/contracts/DatabaseMigrationsConfig.ts +10 -0
  730. package/src/db/contracts/index.ts +3 -0
  731. package/src/db/dialects/columnTypes.ts +103 -0
  732. package/src/db/dialects/index.ts +6 -0
  733. package/src/db/dialects/mariadb.ts +51 -0
  734. package/src/db/dialects/mysql.ts +277 -0
  735. package/src/db/dialects/postgres.ts +143 -0
  736. package/src/db/dialects/resolveDialect.ts +88 -0
  737. package/src/db/dialects/types.ts +59 -0
  738. package/src/db/errors.ts +22 -0
  739. package/src/db/examples/DefinedUser.ts +20 -0
  740. package/src/db/examples/FieldNote.ts +27 -0
  741. package/src/db/examples/KnowledgeNote.ts +29 -0
  742. package/src/db/examples/NoteCodeField.ts +9 -0
  743. package/src/db/examples/createNotesTogether.ts +24 -0
  744. package/src/db/examples/outputs/workspace-notes.json +9 -0
  745. package/src/db/examples/runFieldNotes.ts +51 -0
  746. package/src/db/examples/runNoteMigrations.ts +64 -0
  747. package/src/db/examples/runWorkspaceNotes.ts +86 -0
  748. package/src/db/examples/workspaceNotes.ts +57 -0
  749. package/src/db/fields/BooleanField.ts +128 -0
  750. package/src/db/fields/CharUuidField.ts +91 -0
  751. package/src/db/fields/ChoiceStringField.ts +97 -0
  752. package/src/db/fields/DecimalField.ts +157 -0
  753. package/src/db/fields/EmailField.ts +79 -0
  754. package/src/db/fields/EncryptedJsonField.ts +211 -0
  755. package/src/db/fields/IntegerField.ts +200 -0
  756. package/src/db/fields/JsonField.ts +108 -0
  757. package/src/db/fields/JsonStringField.ts +63 -0
  758. package/src/db/fields/LinkField.ts +502 -0
  759. package/src/db/fields/PasswordField.ts +294 -0
  760. package/src/db/fields/StringField.ts +154 -0
  761. package/src/db/fields/StringListField.ts +179 -0
  762. package/src/db/fields/TextField.ts +71 -0
  763. package/src/db/fields/TimestampField.ts +164 -0
  764. package/src/db/fields/UlidField.ts +189 -0
  765. package/src/db/fields/UrlField.ts +97 -0
  766. package/src/db/fields/VectorField.ts +266 -0
  767. package/src/db/fields/createField.ts +22 -0
  768. package/src/db/fields/field.ts +209 -0
  769. package/src/db/fields/index.ts +76 -0
  770. package/src/db/fields/resolveFields.ts +69 -0
  771. package/src/db/index.ts +13 -0
  772. package/src/db/migrations/DatabaseMigrationManager.ts +756 -0
  773. package/src/db/migrations/DatabaseSchemaInspector.ts +541 -0
  774. package/src/db/migrations/KnexMigrationRenderer.ts +490 -0
  775. package/src/db/migrations/ModelSchemaCollector.ts +240 -0
  776. package/src/db/migrations/SchemaDiffer.ts +541 -0
  777. package/src/db/migrations/SchemaNames.ts +62 -0
  778. package/src/db/migrations/SchemaSnapshotSerializer.ts +59 -0
  779. package/src/db/migrations/contracts/DatabaseMigration.ts +143 -0
  780. package/src/db/migrations/contracts/SchemaChange.ts +113 -0
  781. package/src/db/migrations/contracts/SchemaSnapshot.ts +122 -0
  782. package/src/db/migrations/contracts/index.ts +3 -0
  783. package/src/db/migrations/errors.ts +45 -0
  784. package/src/db/migrations/index.ts +10 -0
  785. package/src/db/migrations/promoteDestructiveSchemaChanges.ts +120 -0
  786. package/src/db/queryMonitor.ts +549 -0
  787. package/src/db/sqlErrors.ts +36 -0
  788. package/src/db/test/db.ts +159 -0
  789. package/src/devtools/HttpEventDispatcher.ts +214 -0
  790. package/src/devtools/contracts/DevtoolsEventDispatcher.ts +57 -0
  791. package/src/devtools/contracts/index.ts +1 -0
  792. package/src/devtools/environment.ts +18 -0
  793. package/src/devtools/index.ts +3 -0
  794. package/src/events/Events.ts +165 -0
  795. package/src/events/README.md +106 -0
  796. package/src/events/contracts/EventConstructor.ts +12 -0
  797. package/src/events/contracts/EventListener.ts +8 -0
  798. package/src/events/contracts/EventUnsubscribe.ts +4 -0
  799. package/src/events/contracts/index.ts +3 -0
  800. package/src/events/examples/NoteSaved.ts +9 -0
  801. package/src/events/examples/runNoteEvents.ts +31 -0
  802. package/src/events/index.ts +2 -0
  803. package/src/flows/FlowBlockRegistry.ts +79 -0
  804. package/src/flows/FlowCompiler.ts +624 -0
  805. package/src/flows/FlowDefinitionError.ts +24 -0
  806. package/src/flows/FlowStepJob.ts +54 -0
  807. package/src/flows/FlowValueValidator.ts +120 -0
  808. package/src/flows/Flows.ts +1118 -0
  809. package/src/flows/README.md +144 -0
  810. package/src/flows/blocks/FlowInput.ts +32 -0
  811. package/src/flows/blocks/FlowOutput.ts +32 -0
  812. package/src/flows/blocks/Subflow.ts +24 -0
  813. package/src/flows/blocks/index.ts +11 -0
  814. package/src/flows/constants.ts +62 -0
  815. package/src/flows/contracts/FlowBlock.ts +73 -0
  816. package/src/flows/contracts/FlowCompiler.ts +32 -0
  817. package/src/flows/contracts/FlowDefinition.ts +126 -0
  818. package/src/flows/contracts/FlowDefinitionStore.ts +52 -0
  819. package/src/flows/contracts/FlowRun.ts +44 -0
  820. package/src/flows/contracts/FlowValue.ts +54 -0
  821. package/src/flows/contracts/FlowsService.ts +39 -0
  822. package/src/flows/contracts/index.ts +7 -0
  823. package/src/flows/defineBlock.ts +25 -0
  824. package/src/flows/drivers/FileFlowDefinitionStore.ts +372 -0
  825. package/src/flows/drivers/index.ts +1 -0
  826. package/src/flows/errors.ts +22 -0
  827. package/src/flows/examples/FlowExampleApp.ts +15 -0
  828. package/src/flows/examples/createTextFlow.ts +14 -0
  829. package/src/flows/examples/normalizeTextBlock.ts +15 -0
  830. package/src/flows/examples/runTextFlow.ts +46 -0
  831. package/src/flows/examples/uppercaseTextBlock.ts +11 -0
  832. package/src/flows/index.ts +13 -0
  833. package/src/flows/models/FlowRun.ts +164 -0
  834. package/src/flows/models/FlowRunEvent.ts +108 -0
  835. package/src/flows/models/FlowStepRun.ts +176 -0
  836. package/src/flows/models/index.ts +16 -0
  837. package/src/index.ts +40 -0
  838. package/src/logging/Log.ts +100 -0
  839. package/src/logging/README.md +139 -0
  840. package/src/logging/contracts/HttpExchange.ts +53 -0
  841. package/src/logging/contracts/Logger.ts +45 -0
  842. package/src/logging/contracts/LoggerDriver.ts +19 -0
  843. package/src/logging/contracts/LoggingOptions.ts +51 -0
  844. package/src/logging/contracts/index.ts +4 -0
  845. package/src/logging/drivers/PinoLoggerDriver.ts +376 -0
  846. package/src/logging/drivers/index.ts +1 -0
  847. package/src/logging/examples/runNoteLogs.ts +38 -0
  848. package/src/logging/index.ts +3 -0
  849. package/src/logging/transports/DevtoolsLogTransport.ts +142 -0
  850. package/src/mail/Mail.ts +188 -0
  851. package/src/mail/README.md +206 -0
  852. package/src/mail/examples/runMailPreview.ts +44 -0
  853. package/src/mail/examples/welcomeMessage.ts +26 -0
  854. package/src/mail/index.ts +4 -0
  855. package/src/mail/transports/FileMailTransport.ts +58 -0
  856. package/src/mail/transports/MailgunTransport.ts +85 -0
  857. package/src/mail/transports/ResendTransport.ts +80 -0
  858. package/src/media/MediaFile.ts +132 -0
  859. package/src/media/MediaItem.ts +132 -0
  860. package/src/media/MediaLibrary.ts +117 -0
  861. package/src/media/MediaManager.ts +874 -0
  862. package/src/media/README.md +358 -0
  863. package/src/media/constants.ts +31 -0
  864. package/src/media/contracts/MediaOptions.ts +9 -0
  865. package/src/media/contracts/index.ts +1 -0
  866. package/src/media/examples/createPrivateFileServer.ts +55 -0
  867. package/src/media/examples/runPrivateFiles.ts +45 -0
  868. package/src/media/examples/runProjectMedia.ts +46 -0
  869. package/src/media/image/ImageReconstructionError.ts +18 -0
  870. package/src/media/image/ImageVariantRenderer.ts +386 -0
  871. package/src/media/image/ImageVariantRequest.ts +65 -0
  872. package/src/media/image/contracts/ImageProcessor.ts +53 -0
  873. package/src/media/image/contracts/ImageVariant.ts +37 -0
  874. package/src/media/image/contracts/ImageVariantCacheOptions.ts +21 -0
  875. package/src/media/image/contracts/index.ts +3 -0
  876. package/src/media/image/index.ts +5 -0
  877. package/src/media/image/processors/SharpImageProcessor.ts +121 -0
  878. package/src/media/image/processors/index.ts +1 -0
  879. package/src/media/index.ts +8 -0
  880. package/src/media/types.ts +168 -0
  881. package/src/queue/FailedJob.ts +58 -0
  882. package/src/queue/Queue.ts +938 -0
  883. package/src/queue/QueueEvents.ts +57 -0
  884. package/src/queue/QueueRetryLaterError.ts +40 -0
  885. package/src/queue/QueueWorker.ts +296 -0
  886. package/src/queue/QueueableJob.ts +167 -0
  887. package/src/queue/QueuedJob.ts +69 -0
  888. package/src/queue/README.md +368 -0
  889. package/src/queue/RetryPolicy.ts +129 -0
  890. package/src/queue/commands/index.ts +13 -0
  891. package/src/queue/commands/makeJob.ts +11 -0
  892. package/src/queue/console.ts +518 -0
  893. package/src/queue/contracts/CreateJob.ts +13 -0
  894. package/src/queue/contracts/QueueDriver.ts +135 -0
  895. package/src/queue/contracts/QueueEvents.ts +77 -0
  896. package/src/queue/contracts/QueuePayload.ts +150 -0
  897. package/src/queue/contracts/QueueRetry.ts +47 -0
  898. package/src/queue/contracts/QueueService.ts +219 -0
  899. package/src/queue/contracts/QueueWorkerLifecycle.ts +71 -0
  900. package/src/queue/contracts/QueueableJob.ts +56 -0
  901. package/src/queue/contracts/RedisQueueDriver.ts +37 -0
  902. package/src/queue/contracts/index.ts +9 -0
  903. package/src/queue/createJob.ts +54 -0
  904. package/src/queue/drivers/DatabaseQueueDriver.ts +449 -0
  905. package/src/queue/drivers/RedisQueueDriver.ts +733 -0
  906. package/src/queue/examples/DeferredReportJob.ts +25 -0
  907. package/src/queue/examples/GenerateReportJob.ts +37 -0
  908. package/src/queue/examples/WriteReportJob.ts +37 -0
  909. package/src/queue/examples/createAndProcessReportJob.ts +29 -0
  910. package/src/queue/examples/dispatchReportWorkflows.ts +40 -0
  911. package/src/queue/examples/manageReportRetries.ts +47 -0
  912. package/src/queue/examples/outputs/create-and-process-report-job.json +6 -0
  913. package/src/queue/examples/outputs/dispatch-report-workflows.json +14 -0
  914. package/src/queue/examples/outputs/manage-report-retries.json +24 -0
  915. package/src/queue/examples/outputs/queue-reports.json +11 -0
  916. package/src/queue/examples/reportConsole.ts +47 -0
  917. package/src/queue/examples/runBackpressure.ts +27 -0
  918. package/src/queue/examples/runQueueReports.ts +80 -0
  919. package/src/queue/examples/runQueueWorkflows.ts +43 -0
  920. package/src/queue/index.ts +13 -0
  921. package/src/queue/queueMonitor.ts +368 -0
  922. package/src/scheduler/README.md +139 -0
  923. package/src/scheduler/ScheduledCall.ts +36 -0
  924. package/src/scheduler/ScheduledEvent.ts +149 -0
  925. package/src/scheduler/ScheduledJob.ts +58 -0
  926. package/src/scheduler/ScheduledOccurrence.ts +250 -0
  927. package/src/scheduler/ScheduledOccurrenceRecorder.ts +125 -0
  928. package/src/scheduler/Scheduler.ts +225 -0
  929. package/src/scheduler/SchedulerWorker.ts +151 -0
  930. package/src/scheduler/console.ts +368 -0
  931. package/src/scheduler/constants.ts +27 -0
  932. package/src/scheduler/contracts/Scheduler.ts +123 -0
  933. package/src/scheduler/contracts/SchedulerConsole.ts +40 -0
  934. package/src/scheduler/contracts/SchedulerWorker.ts +23 -0
  935. package/src/scheduler/contracts/index.ts +3 -0
  936. package/src/scheduler/examples/RecoverableSummaryJob.ts +18 -0
  937. package/src/scheduler/examples/WriteDailySummaryJob.ts +17 -0
  938. package/src/scheduler/examples/registerDailySummary.ts +13 -0
  939. package/src/scheduler/examples/runDailySummary.ts +46 -0
  940. package/src/scheduler/examples/runScheduledReplay.ts +60 -0
  941. package/src/scheduler/index.ts +9 -0
  942. package/src/scheduler/scheduleTime.ts +78 -0
  943. package/src/security/README.md +130 -0
  944. package/src/security/Security.ts +346 -0
  945. package/src/security/SecurityError.ts +16 -0
  946. package/src/security/contracts/SecurityOptions.ts +20 -0
  947. package/src/security/contracts/SecurityPayload.ts +14 -0
  948. package/src/security/contracts/index.ts +2 -0
  949. package/src/security/ensureAppKey.ts +82 -0
  950. package/src/security/examples/runSecretRoundTrip.ts +42 -0
  951. package/src/security/index.ts +4 -0
  952. package/src/serialization/README.md +169 -0
  953. package/src/serialization/Serializable.ts +17 -0
  954. package/src/serialization/SerializationError.ts +91 -0
  955. package/src/serialization/SerializationRegistry.ts +208 -0
  956. package/src/serialization/SerializationRegistryError.ts +24 -0
  957. package/src/serialization/Serializer.ts +793 -0
  958. package/src/serialization/contracts/SerializableClass.ts +26 -0
  959. package/src/serialization/contracts/SerializedValue.ts +71 -0
  960. package/src/serialization/contracts/SerializerOptions.ts +19 -0
  961. package/src/serialization/contracts/SerializerService.ts +29 -0
  962. package/src/serialization/contracts/index.ts +4 -0
  963. package/src/serialization/examples/ExportRequest.ts +25 -0
  964. package/src/serialization/examples/runExportSerialization.ts +24 -0
  965. package/src/serialization/index.ts +6 -0
  966. package/src/server/App.ts +326 -0
  967. package/src/server/README.md +219 -0
  968. package/src/server/RequestContext.ts +122 -0
  969. package/src/server/appContext.ts +59 -0
  970. package/src/server/examples/createFirstServer.ts +25 -0
  971. package/src/server/examples/startFirstServer.ts +19 -0
  972. package/src/server/index.ts +16 -0
  973. package/src/ssr/README.md +158 -0
  974. package/src/ssr/constants.ts +8 -0
  975. package/src/ssr/contracts/DocumentHead.ts +35 -0
  976. package/src/ssr/contracts/SsrRenderContext.ts +25 -0
  977. package/src/ssr/contracts/SsrRenderResult.ts +23 -0
  978. package/src/ssr/contracts/SsrRenderer.ts +12 -0
  979. package/src/ssr/contracts/SsrRequest.ts +17 -0
  980. package/src/ssr/contracts/index.ts +19 -0
  981. package/src/ssr/createSsrRenderContext.ts +25 -0
  982. package/src/ssr/examples/createPublicPageServer.ts +32 -0
  983. package/src/ssr/examples/runPublicPages.ts +25 -0
  984. package/src/ssr/fastify/contracts/FastifySsrOptions.ts +24 -0
  985. package/src/ssr/fastify/contracts/index.ts +5 -0
  986. package/src/ssr/fastify/fastifySsr.ts +110 -0
  987. package/src/ssr/fastify/index.ts +2 -0
  988. package/src/ssr/index.ts +10 -0
  989. package/src/ssr/renderDocumentHead.ts +72 -0
  990. package/src/ssr/renderSsrDocument.ts +35 -0
  991. package/src/ssr/serializeSsrState.ts +19 -0
  992. package/src/ssr/vite/contracts/ViteSsrOptions.ts +52 -0
  993. package/src/ssr/vite/contracts/index.ts +6 -0
  994. package/src/ssr/vite/fastifyViteSsr.ts +279 -0
  995. package/src/ssr/vite/index.ts +4 -0
  996. package/src/ssr/vite/viteSsrManifest.ts +92 -0
  997. package/src/storage/README.md +310 -0
  998. package/src/storage/Storage.ts +392 -0
  999. package/src/storage/contents.ts +23 -0
  1000. package/src/storage/contracts/StorageListing.ts +55 -0
  1001. package/src/storage/contracts/index.ts +1 -0
  1002. package/src/storage/drivers/FlystorageDisk.ts +295 -0
  1003. package/src/storage/drivers/FlystorageListing.ts +78 -0
  1004. package/src/storage/drivers/LocalStorageDisk.ts +45 -0
  1005. package/src/storage/drivers/S3StorageDisk.ts +114 -0
  1006. package/src/storage/drivers/index.ts +3 -0
  1007. package/src/storage/examples/runStorage.ts +37 -0
  1008. package/src/storage/examples/runStreamExport.ts +41 -0
  1009. package/src/storage/examples/writeCsvExport.ts +45 -0
  1010. package/src/storage/index.ts +7 -0
  1011. package/src/storage/mime.ts +30 -0
  1012. package/src/storage/path.ts +76 -0
  1013. package/src/storage/types.ts +251 -0
  1014. package/src/url/README.md +41 -0
  1015. package/src/url/UrlGenerator.ts +72 -0
  1016. package/src/url/contracts/UrlGeneratorOptions.ts +14 -0
  1017. package/src/url/contracts/index.ts +1 -0
  1018. package/src/url/examples/runApplicationLinks.ts +33 -0
  1019. package/src/url/index.ts +2 -0
  1020. package/src/validation/README.md +433 -0
  1021. package/src/validation/examples/runNoteValidation.ts +18 -0
  1022. package/src/validation/examples/validateNoteInput.ts +31 -0
  1023. package/src/validation/index.ts +398 -0
  1024. package/templates/AGENTS.md +15 -0
@@ -0,0 +1,1243 @@
1
+ import {
2
+ isFinallyPromise,
3
+ normalizeDatabaseComment,
4
+ startCase,
5
+ } from '@db3.ai/pure';
6
+ import type { Knex } from 'knex';
7
+ import { isDeepStrictEqual } from 'node:util';
8
+ import type { ValidationRule } from '../validation';
9
+ import type {
10
+ DatabaseDialect,
11
+ DatabaseValueOptions,
12
+ } from './dialects';
13
+ import { resolveDatabaseDialect } from './dialects';
14
+
15
+ /**
16
+ * Generic database row shape returned by the SQL driver.
17
+ */
18
+ export type DbRow = Record<string, unknown>;
19
+
20
+ /**
21
+ * Partial database row used for inserts and updates.
22
+ *
23
+ * A field returns an object instead of a scalar because some fields may map to
24
+ * multiple database columns. For example, a map/location field could return
25
+ * `{ location_lat, location_lng }`.
26
+ */
27
+ export type DbWriteData = Record<string, unknown>;
28
+
29
+ /**
30
+ * A structured validation error attached to a specific model field.
31
+ */
32
+ export interface FieldError {
33
+ /** Logical field name on the model, for example `email` or `password`. */
34
+ field: string;
35
+
36
+ /** Human-readable error message. */
37
+ message: string;
38
+
39
+ /** Stable machine-readable error code, for example `required`. */
40
+ code?: string;
41
+
42
+ /** Offending value, where safe to expose. Avoid storing secrets here. */
43
+ value?: unknown;
44
+
45
+ /** Extra structured data for UI/API consumers. */
46
+ details?: unknown;
47
+ }
48
+
49
+ /**
50
+ * Error thrown by `ActiveRecord.save()` when one or more fields are invalid.
51
+ */
52
+ export class RecordValidationError extends Error {
53
+ constructor(public readonly errors: FieldError[]) {
54
+ super('Record validation failed');
55
+ this.name = 'RecordValidationError';
56
+ }
57
+ }
58
+
59
+ export type FieldDefault =
60
+ | null
61
+ | string
62
+ | number
63
+ | boolean
64
+ | Record<string, unknown>
65
+ | unknown[];
66
+
67
+ export type FieldDefaultFactory = () => unknown;
68
+
69
+ export type GeneratedValue =
70
+ | boolean
71
+ | 'db'
72
+ | 'ulid'
73
+ | 'uuid-v4'
74
+ | 'now';
75
+
76
+ export interface FrontendComponentSpec {
77
+ component: string;
78
+ props: Record<string, unknown>;
79
+ }
80
+
81
+ export interface DomFormRenderSpec {
82
+ component: 'DomForm';
83
+ props: Record<string, unknown>;
84
+ children: FrontendComponentSpec[];
85
+ }
86
+
87
+ export interface FieldFrontendConfig {
88
+ component?: string;
89
+ props?: Record<string, unknown>;
90
+ searchComponent?: string;
91
+ searchProps?: Record<string, unknown>;
92
+ }
93
+
94
+ export interface FieldRenderOptions {
95
+ mode?: 'create' | 'edit' | 'search';
96
+ }
97
+
98
+ /**
99
+ * Core configuration shared by all fields.
100
+ *
101
+ * Concrete field types should extend this with their own options.
102
+ */
103
+ export interface FieldConfig {
104
+ /** Database column name. Defaults to the model field name. */
105
+ column?: string;
106
+
107
+ /** Developer-facing note for generated models and schema tooling. */
108
+ comment?: string;
109
+
110
+ /** Whether the field must have a non-empty value. */
111
+ required?: boolean;
112
+
113
+ /** Whether the field is the model primary key. */
114
+ primary?: boolean;
115
+
116
+ /** Whether the value should be omitted from `toJSON()`. */
117
+ hidden?: boolean;
118
+
119
+ /** Whether normal ActiveRecord fetches should include this field's columns. */
120
+ selectedByDefault?: boolean;
121
+
122
+ /** Whether the generated database schema should include a unique constraint. */
123
+ unique?: boolean;
124
+
125
+ /** Whether the generated database schema should include an index on this field's primary column. */
126
+ index?: boolean;
127
+
128
+ /** Optional name for the generated single-column index. */
129
+ indexName?: string;
130
+
131
+ /** Additional generated database schema indexes for this field. */
132
+ indexes?: DbIndexSpec[];
133
+
134
+ /** Optional human-friendly label for UI/admin/form generation. */
135
+ label?: string;
136
+
137
+ /** Static default value or runtime factory. Use static values for serializable config. */
138
+ default?: FieldDefault | FieldDefaultFactory;
139
+
140
+ /** Named generation strategy. This is serializable and lets the field/framework decide how to generate the value. */
141
+ generated?: GeneratedValue;
142
+
143
+ /** Frontend rendering hints composed by FieldType render helpers. */
144
+ frontend?: FieldFrontendConfig;
145
+ }
146
+
147
+ export type FieldClass<TField extends FieldType<any, any, any, any, any> = FieldType<any, any, any, any, any>> =
148
+ new (config?: any) => TField;
149
+
150
+ export interface FieldDefinition<TField extends FieldType<any, any, any, any, any> = FieldType<any, any, any, any, any>> {
151
+ type: FieldClass<TField>;
152
+ config: ConstructorParameters<FieldClass<TField>>[0];
153
+ }
154
+
155
+ export type FieldInput<TField extends FieldType<any, any, any, any, any> = FieldType<any, any, any, any, any>> =
156
+ | FieldClass<TField>
157
+ | FieldDefinition<TField>
158
+ | TField;
159
+
160
+ export type FieldInputMap = Record<string, FieldInput<any>>;
161
+
162
+ export type FieldMap = Record<string, FieldType<any, any, any, any, any>>;
163
+
164
+ export type FieldMapFactory = (field: any) => FieldInputMap;
165
+
166
+ /**
167
+ * Context passed into low-level field methods.
168
+ *
169
+ * Record instances normally use the higher-level field instance API. This
170
+ * context remains for schema/query work and for concrete field internals.
171
+ */
172
+ export interface FieldContext {
173
+ /** The model class metadata. */
174
+ model: ModelMetadata;
175
+
176
+ /** Database dialect used for schema/value conversion when available. */
177
+ dialect?: DatabaseDialect;
178
+
179
+ /** Knex table builder when schema is being applied to the database. */
180
+ table?: Knex.TableBuilder;
181
+
182
+ /** Options used when writing columns through `table`. */
183
+ columnWriteOptions?: DbColumnWriteOptions;
184
+
185
+ /** The active record instance when available. Query code may omit this. */
186
+ record?: unknown;
187
+
188
+ /** Logical field name on the model, for example `email`. */
189
+ fieldName: string;
190
+ }
191
+
192
+ /**
193
+ * Minimal model metadata required by fields.
194
+ *
195
+ * This avoids importing `ActiveRecord` into the field base class and keeps the
196
+ * dependency direction simple.
197
+ */
198
+ export interface ModelMetadata {
199
+ /** Database table name. */
200
+ table: string;
201
+
202
+ /** Logical primary-key field name. */
203
+ primaryKey: string;
204
+
205
+ /** Static field definitions on the model class. */
206
+ fields: FieldMapFactory;
207
+ }
208
+
209
+ /**
210
+ * Options passed when collecting database write data from fields.
211
+ */
212
+ export interface DbWriteOptions {
213
+ /** True for insert, false for update. */
214
+ isInsert: boolean;
215
+
216
+ /** Whether clean fields should be excluded from the generated row. */
217
+ onlyDirty: boolean;
218
+
219
+ /** Database value conversion context for driver-specific field storage. */
220
+ valueOptions?: DatabaseValueOptions;
221
+ }
222
+
223
+ /**
224
+ * Basic state used by most one-column value fields.
225
+ */
226
+ export interface BasicFieldState<TValue = unknown> {
227
+ /** Current app-memory value. */
228
+ value: TValue;
229
+
230
+ /** Value originally loaded from the database or initial default. */
231
+ originalValue: TValue;
232
+
233
+ /** Whether the current value differs from the original value. */
234
+ dirty: boolean;
235
+
236
+ /** Validation errors for this field on this record. */
237
+ errors: FieldError[];
238
+ }
239
+
240
+ /**
241
+ * Captures a clean comparison snapshot for mutable field values.
242
+ *
243
+ * Ordinary JSON objects, arrays, dates, maps, and sets are cloned so in-place
244
+ * mutations remain visible to dirty tracking. Values that cannot be cloned
245
+ * without changing their initial semantics retain their existing identity and
246
+ * continue to rely on explicit assignment.
247
+ *
248
+ * @param value - Current field value to retain as the clean baseline.
249
+ * @returns Independent comparison snapshot when the value supports it.
250
+ */
251
+ function snapshotFieldValue<TValue>(value: TValue): TValue {
252
+ if (typeof value !== 'object' || value === null) {
253
+ return value;
254
+ }
255
+
256
+ try {
257
+ const snapshot = structuredClone(value);
258
+
259
+ return isDeepStrictEqual(value, snapshot)
260
+ ? snapshot
261
+ : value;
262
+ } catch {
263
+ return value;
264
+ }
265
+ }
266
+
267
+ /**
268
+ * Database column description produced by a field's `getDbSchema()`.
269
+ *
270
+ * The `type` string comes from the active dialect and is applied directly by
271
+ * `Database` via Knex `specificType`.
272
+ */
273
+ export interface DbColumnSpec {
274
+ /** Database column name. */
275
+ name: string;
276
+
277
+ /** Database column type, e.g. `varchar(255)`, `char(26)`, `json`, or `longtext`. */
278
+ type: string;
279
+
280
+ /** Whether null is allowed. */
281
+ nullable?: boolean;
282
+
283
+ /** Whether this column is the primary key. */
284
+ primary?: boolean;
285
+
286
+ /** Whether this column has a unique constraint. */
287
+ unique?: boolean;
288
+
289
+ /** Database default value. */
290
+ default?: unknown;
291
+
292
+ /** Database-native column comment where supported, such as MySQL. */
293
+ comment?: string;
294
+ }
295
+
296
+ /**
297
+ * Options for writing a column onto a Knex table builder.
298
+ */
299
+ export interface DbColumnWriteOptions {
300
+ alter?: boolean;
301
+ alterNullable?: boolean;
302
+ alterType?: boolean;
303
+ applyDefault?: boolean;
304
+ applyComment?: boolean;
305
+ }
306
+
307
+ /**
308
+ * Writes a column spec onto a Knex table builder.
309
+ */
310
+ export function writeDbColumn(
311
+ table: Knex.TableBuilder,
312
+ column: DbColumnSpec,
313
+ options: DbColumnWriteOptions = {},
314
+ ): void {
315
+ const columnBuilder = table.specificType(column.name, column.type);
316
+
317
+ if (column.primary && !options.alter) {
318
+ columnBuilder.primary();
319
+ }
320
+
321
+ if (!options.alter || options.alterNullable) {
322
+ if (column.nullable === false || column.primary) {
323
+ columnBuilder.notNullable();
324
+ } else {
325
+ columnBuilder.nullable();
326
+ }
327
+ }
328
+
329
+ if (column.unique && !options.alter) {
330
+ columnBuilder.unique();
331
+ }
332
+
333
+ if (column.default !== undefined && options.applyDefault !== false) {
334
+ columnBuilder.defaultTo(column.default as any);
335
+ }
336
+
337
+ const comment = normalizeDatabaseComment(column.comment);
338
+
339
+ if (comment && options.applyComment !== false) {
340
+ columnBuilder.comment(comment);
341
+ }
342
+
343
+ if (options.alter) {
344
+ columnBuilder.alter({
345
+ alterNullable: options.alterNullable ?? false,
346
+ alterType: options.alterType ?? false,
347
+ });
348
+ }
349
+ }
350
+
351
+ /**
352
+ * Neutral database index description.
353
+ */
354
+ export interface DbIndexSpec {
355
+ /** Columns included in the index. */
356
+ columns: DbIndexColumnSpec[];
357
+
358
+ /** Optional index name. */
359
+ name?: string;
360
+
361
+ /** Whether the index is unique. */
362
+ unique?: boolean;
363
+
364
+ /** Database index kind. Defaults to a normal scalar index. */
365
+ type?: 'normal' | 'vector';
366
+ }
367
+
368
+ export type DbIndexColumnSpec =
369
+ | string
370
+ | {
371
+ name: string;
372
+ order?: 'asc' | 'desc';
373
+ };
374
+
375
+ export function fieldSchemaIndexes(
376
+ column: string,
377
+ config: FieldConfig,
378
+ ): DbIndexSpec[] | undefined {
379
+ const indexes = [...(config.indexes ?? [])];
380
+
381
+ if (config.index) {
382
+ indexes.push({
383
+ columns: [column],
384
+ name: config.indexName,
385
+ });
386
+ }
387
+
388
+ return indexes.length > 0 ? indexes : undefined;
389
+ }
390
+
391
+ /**
392
+ * Neutral foreign-key description.
393
+ */
394
+ export interface DbForeignKeySpec {
395
+ /** Local database column. */
396
+ column: string;
397
+
398
+ /** Referenced database table. */
399
+ referencesTable: string;
400
+
401
+ /** Referenced database column. */
402
+ referencesColumn: string;
403
+
404
+ /** Optional ON DELETE action. */
405
+ onDelete?: 'CASCADE' | 'RESTRICT' | 'SET NULL' | 'NO ACTION';
406
+
407
+ /** Optional ON UPDATE action. */
408
+ onUpdate?: 'CASCADE' | 'RESTRICT' | 'SET NULL' | 'NO ACTION';
409
+ }
410
+
411
+ /**
412
+ * Database schema contribution produced by a single field.
413
+ */
414
+ export interface DbSchemaPart {
415
+ /** Columns required by this field. */
416
+ columns?: DbColumnSpec[];
417
+
418
+ /** Indexes required by this field. */
419
+ indexes?: DbIndexSpec[];
420
+
421
+ /** Foreign keys required by this field. */
422
+ foreignKeys?: DbForeignKeySpec[];
423
+ }
424
+
425
+ /**
426
+ * Base class for reusable field behaviour.
427
+ *
428
+ * Static model fields are schema definitions. Each ActiveRecord instance creates
429
+ * private live field objects that own their value, dirty state, errors, field
430
+ * name, and parent record reference.
431
+ *
432
+ * The low-level lifecycle remains:
433
+ *
434
+ * - `createState()` creates per-record storage.
435
+ * - `setValue()` handles input assignment and stores the hydrated backend value.
436
+ * - `getValue()` handles normal property reads.
437
+ * - `validate()` stores field errors.
438
+ * - `getDataForDb()` returns database write data.
439
+ * - `setFromDb()` hydrates state from a database row.
440
+ * - `getDisplayValue()` serializes for frontend/form/API-safe output.
441
+ */
442
+ export abstract class FieldType<
443
+ TValue = unknown,
444
+ TDbValue = TValue,
445
+ TDisplayValue = TValue,
446
+ TInput = TValue,
447
+ TState extends { errors: FieldError[] } = BasicFieldState<TValue>,
448
+ > {
449
+ private $fieldName?: string;
450
+ private $model?: ModelMetadata;
451
+ private $record?: unknown;
452
+ private $state?: TState;
453
+ private $activeContext?: FieldContext;
454
+
455
+ constructor(public readonly config: FieldConfig = {}) { }
456
+
457
+ /**
458
+ * Logical field name on the parent record.
459
+ */
460
+ get name(): string {
461
+ return this.fieldName;
462
+ }
463
+
464
+ /**
465
+ * Logical field name on the parent record/model.
466
+ */
467
+ get fieldName(): string {
468
+ return this.context.fieldName;
469
+ }
470
+
471
+ /**
472
+ * Parent model metadata.
473
+ */
474
+ get model(): ModelMetadata {
475
+ return this.context.model;
476
+ }
477
+
478
+ /**
479
+ * Parent ActiveRecord instance.
480
+ */
481
+ get record(): unknown {
482
+ const record = this.context.record;
483
+
484
+ if (record === undefined) {
485
+ throw new Error('Field is not bound to a record.');
486
+ }
487
+
488
+ return record;
489
+ }
490
+
491
+ public required(value: boolean = true): this {
492
+ this.config.required = value;
493
+ return this;
494
+ }
495
+
496
+ /**
497
+ * Primary database column for this bound field.
498
+ */
499
+ get column(): string {
500
+ return this.getColumn();
501
+ }
502
+
503
+ /**
504
+ * Current app-memory value for this bound field.
505
+ */
506
+ get value(): TValue {
507
+ return this.getValue(this.state);
508
+ }
509
+
510
+ set value(input: TInput) {
511
+ this.setValue(this.state, input);
512
+ }
513
+
514
+ /**
515
+ * Whether this bound field has changed since hydration/default creation.
516
+ */
517
+ get dirty(): boolean {
518
+ return this.isDirty(this.state);
519
+ }
520
+
521
+ /**
522
+ * Current validation errors for this bound field.
523
+ */
524
+ get errors(): FieldError[] {
525
+ return this.getErrors(this.state);
526
+ }
527
+
528
+ /**
529
+ * Clears validation errors for this bound field.
530
+ */
531
+ clearErrors(): void {
532
+ this.state.errors = [];
533
+ }
534
+
535
+ /**
536
+ * Hydrates this bound field from a database row.
537
+ */
538
+ hydrate(row: DbRow): void {
539
+ this.setFromDb(this.state, row);
540
+ }
541
+
542
+ /**
543
+ * Validates this bound field.
544
+ */
545
+ async validateField(): Promise<FieldError[]> {
546
+ return this.validate(this.state);
547
+ }
548
+
549
+ /**
550
+ * Returns database write data for this bound field.
551
+ */
552
+ async getBoundDataForDb(options: DbWriteOptions): Promise<DbWriteData> {
553
+ return this.getDataForDb(this.state, options);
554
+ }
555
+
556
+ /**
557
+ * Returns a frontend/form/API-safe value for this bound field.
558
+ */
559
+ getBoundDisplayValue(): TDisplayValue | undefined {
560
+ return this.getDisplayValue(this.state);
561
+ }
562
+
563
+ /**
564
+ * Converts a query value for this bound field.
565
+ */
566
+ getBoundQueryValue(input: TInput): unknown {
567
+ return this.getQueryValue(input);
568
+ }
569
+
570
+ /**
571
+ * Marks this bound field as clean.
572
+ */
573
+ markBoundClean(): void {
574
+ this.markClean(this.state);
575
+ }
576
+
577
+ /**
578
+ * Returns the column for this field blueprint on a model.
579
+ */
580
+ getColumnFor(model: ModelMetadata, fieldName: string): string {
581
+ return this.getColumn({
582
+ model,
583
+ fieldName,
584
+ });
585
+ }
586
+
587
+ /**
588
+ * Converts a query value for this field blueprint on a model.
589
+ */
590
+ getQueryValueFor(
591
+ model: ModelMetadata,
592
+ fieldName: string,
593
+ input: TInput,
594
+ ): unknown {
595
+ return this.getQueryValue(input, {
596
+ model,
597
+ fieldName,
598
+ });
599
+ }
600
+
601
+ /**
602
+ * Returns schema metadata for this field blueprint on a model.
603
+ */
604
+ getDbSchemaFor(model: ModelMetadata, fieldName: string): DbSchemaPart {
605
+ return this.getDbSchema({
606
+ model,
607
+ fieldName,
608
+ });
609
+ }
610
+
611
+ /**
612
+ * Returns the DomStudio component spec for rendering this field in a form.
613
+ */
614
+ getFormComponent(
615
+ options: FieldRenderOptions = {},
616
+ ctx?: FieldContext,
617
+ ): FrontendComponentSpec {
618
+ return this.withContext(ctx, () => ({
619
+ component: this.config.frontend?.component || this.defaultFormComponent(),
620
+ props: {
621
+ ...this.defaultFormComponentProps(options),
622
+ ...(this.config.frontend?.props ?? {}),
623
+ },
624
+ }));
625
+ }
626
+
627
+ /**
628
+ * Returns the DomStudio component spec for rendering this field as a search filter.
629
+ */
630
+ getSearchFilterComponent(
631
+ options: FieldRenderOptions = {},
632
+ ctx?: FieldContext,
633
+ ): FrontendComponentSpec | null {
634
+ return this.withContext(ctx, () => {
635
+ const component = this.config.frontend?.searchComponent || this.defaultSearchFilterComponent();
636
+
637
+ if (!component) return null;
638
+
639
+ return {
640
+ component,
641
+ props: {
642
+ ...this.defaultSearchFilterComponentProps(options),
643
+ ...(this.config.frontend?.searchProps ?? {}),
644
+ },
645
+ };
646
+ });
647
+ }
648
+
649
+ /**
650
+ * Returns a form component spec for an unbound model field.
651
+ */
652
+ getFormComponentFor(
653
+ model: ModelMetadata,
654
+ fieldName: string,
655
+ options: FieldRenderOptions = {},
656
+ ): FrontendComponentSpec {
657
+ return this.getFormComponent(options, {
658
+ model,
659
+ fieldName,
660
+ });
661
+ }
662
+
663
+ /**
664
+ * Returns a search component spec for an unbound model field.
665
+ */
666
+ getSearchFilterComponentFor(
667
+ model: ModelMetadata,
668
+ fieldName: string,
669
+ options: FieldRenderOptions = {},
670
+ ): FrontendComponentSpec | null {
671
+ return this.getSearchFilterComponent(options, {
672
+ model,
673
+ fieldName,
674
+ });
675
+ }
676
+
677
+ /**
678
+ * Returns the primary database column for this field.
679
+ *
680
+ * Compound fields may still return additional columns from `getDataForDb()`.
681
+ */
682
+ getColumn(ctx?: FieldContext): string {
683
+ return this.withContext(ctx, () => this.config.column ?? this.fieldName);
684
+ }
685
+
686
+ /**
687
+ * Creates per-record runtime state for this field.
688
+ */
689
+ createState(ctx?: FieldContext): TState {
690
+ return this.withContext(ctx, () => {
691
+ const value = this.getDefaultValue();
692
+
693
+ return {
694
+ value,
695
+ originalValue: snapshotFieldValue(value),
696
+ dirty: false,
697
+ errors: [],
698
+ } as unknown as TState;
699
+ });
700
+ }
701
+
702
+ /**
703
+ * Handles app-level assignment, e.g. `user.email = 'x@example.com'`.
704
+ *
705
+ * Keep this mostly synchronous. Async work such as hashing should usually
706
+ * happen in `getDataForDb()` during save.
707
+ */
708
+ setValue(state: TState, input: TInput, ctx?: FieldContext): void {
709
+ return this.withContext(ctx, () => {
710
+ const value = this.parse(input);
711
+
712
+ (state as any).value = value;
713
+ (state as any).dirty = true;
714
+ });
715
+ }
716
+
717
+ /**
718
+ * Handles app-level property reads, e.g. `user.email`.
719
+ */
720
+ getValue(state: TState, ctx?: FieldContext): TValue {
721
+ return (state as any).value;
722
+ }
723
+
724
+ /**
725
+ * Hydrates field state from a raw database row.
726
+ */
727
+ setFromDb(state: TState, row: DbRow, ctx?: FieldContext): void {
728
+ return this.withContext(ctx, () => {
729
+ const hasColumn = Object.prototype.hasOwnProperty.call(row, this.column);
730
+ const dbValue = hasColumn ? row[this.column] as TDbValue : undefined;
731
+ const value = hasColumn
732
+ ? dbValue === null || dbValue === undefined
733
+ ? this.config.default === undefined
734
+ ? this.fromDbValue(dbValue as TDbValue)
735
+ : this.getDefaultValue()
736
+ : this.fromDbValue(dbValue)
737
+ : this.getDefaultValue();
738
+
739
+ (state as any).value = value;
740
+ (state as any).originalValue = snapshotFieldValue(value);
741
+ (state as any).dirty = false;
742
+ state.errors = [];
743
+ });
744
+ }
745
+
746
+ /**
747
+ * Validates the field and stores the resulting errors on the field state.
748
+ */
749
+ async validate(state: TState, ctx?: FieldContext): Promise<FieldError[]> {
750
+ return this.withContext(ctx, async () => {
751
+ state.errors = [];
752
+
753
+ const errors = await this.collectErrors(state);
754
+ state.errors.push(...errors);
755
+
756
+ return state.errors;
757
+ });
758
+ }
759
+
760
+ /**
761
+ * Returns current validation errors for this field state.
762
+ */
763
+ getErrors(state: TState): FieldError[] {
764
+ return state.errors;
765
+ }
766
+
767
+ /**
768
+ * Returns true if this field state currently has validation errors.
769
+ */
770
+ hasErrors(state: TState): boolean {
771
+ return state.errors.length > 0;
772
+ }
773
+
774
+ /**
775
+ * Returns true if the field has changed since hydration/default creation.
776
+ */
777
+ isDirty(state: TState): boolean {
778
+ return (
779
+ Boolean((state as any).dirty)
780
+ || !isDeepStrictEqual(
781
+ (state as any).value,
782
+ (state as any).originalValue,
783
+ )
784
+ );
785
+ }
786
+
787
+ /**
788
+ * Marks the state as clean after a successful save or hydration.
789
+ */
790
+ markClean(state: TState): void {
791
+ (state as any).originalValue = snapshotFieldValue((state as any).value);
792
+ (state as any).dirty = false;
793
+ state.errors = [];
794
+ }
795
+
796
+ /**
797
+ * Returns database write data for this field.
798
+ *
799
+ * The default implementation maps one field to one column.
800
+ */
801
+ getDataForDb(
802
+ state: TState,
803
+ options: DbWriteOptions,
804
+ ): Promise<DbWriteData>;
805
+ getDataForDb(
806
+ state: TState,
807
+ ctx: FieldContext,
808
+ options: DbWriteOptions,
809
+ ): Promise<DbWriteData>;
810
+ async getDataForDb(
811
+ state: TState,
812
+ ctxOrOptions: FieldContext | DbWriteOptions,
813
+ options?: DbWriteOptions,
814
+ ): Promise<DbWriteData> {
815
+ const args = this.resolveDbWriteArgs(ctxOrOptions, options);
816
+
817
+ return this.withContext(args.ctx, async () => {
818
+ if (this.config.generated && this.isEmpty((state as any).value)) {
819
+ return {};
820
+ }
821
+
822
+ if (args.options.onlyDirty && !this.isDirty(state)) {
823
+ return {};
824
+ }
825
+
826
+ const value = this.toDbValue((state as any).value, args.options.valueOptions);
827
+
828
+ return {
829
+ [this.column]: value,
830
+ };
831
+ });
832
+ }
833
+
834
+ /**
835
+ * Returns a frontend/form/API-safe value for this field.
836
+ */
837
+ getDisplayValue(state: TState, ctx?: FieldContext): TDisplayValue | undefined {
838
+ return this.withContext(ctx, () => {
839
+ if (this.config.hidden) {
840
+ return undefined;
841
+ }
842
+
843
+ const value = this.getValue(state);
844
+
845
+ if (this.isEmpty(value) && this.config.default !== undefined) {
846
+ return this.toDisplayValue(this.getDefaultValue());
847
+ }
848
+
849
+ return this.toDisplayValue(value);
850
+ });
851
+ }
852
+
853
+ /**
854
+ * Alias used by JSON transport. Framework docs call this display data.
855
+ */
856
+ getJsonValue(state: TState, ctx?: FieldContext): TDisplayValue | undefined {
857
+ return this.getDisplayValue(state, ctx);
858
+ }
859
+
860
+ /**
861
+ * Converts an input query value into its database representation.
862
+ *
863
+ * Query builder methods call this so `where('email', 'STEVE@EXAMPLE.COM')`
864
+ * can compare against the normalized database value.
865
+ */
866
+ getQueryValue(input: TInput, ctx?: FieldContext): unknown {
867
+ return this.withContext(ctx, () => this.toDbValue(this.parse(input)));
868
+ }
869
+
870
+ /**
871
+ * Returns database schema metadata for dev sync and diff tooling.
872
+ *
873
+ * When `ctx.table` is present the field also writes its column(s) onto that
874
+ * Knex builder using the dialect-owned SQL type string.
875
+ */
876
+ getDbSchema(ctx?: FieldContext): DbSchemaPart {
877
+ return this.withContext(ctx, () => {
878
+ return this.completeDbSchema({
879
+ columns: [
880
+ {
881
+ name: this.column,
882
+ type: this.schemaDialect().columnTypes.text(),
883
+ nullable: !this.config.required && !this.config.primary,
884
+ primary: this.config.primary,
885
+ unique: this.config.unique,
886
+ },
887
+ ],
888
+ indexes: fieldSchemaIndexes(this.column, this.config),
889
+ }, ctx);
890
+ });
891
+ }
892
+
893
+ /**
894
+ * Returns request/input validation rules for this field.
895
+ */
896
+ getValidationRules(ctx?: FieldContext): ValidationRule[] {
897
+ return this.withContext(ctx, () => [
898
+ this.config.required || this.config.primary ? 'required' : 'nullable',
899
+ ]);
900
+ }
901
+
902
+ /**
903
+ * Parses app/input assignment into the field's app-memory value.
904
+ */
905
+ protected parse(input: TInput): TValue {
906
+ return input as unknown as TValue;
907
+ }
908
+
909
+ /**
910
+ * Default form component for this field type.
911
+ */
912
+ protected defaultFormComponent(): string {
913
+ return 'DomTextInput';
914
+ }
915
+
916
+ /**
917
+ * Default form props derived from FieldType config.
918
+ */
919
+ protected defaultFormComponentProps(_options: FieldRenderOptions): Record<string, unknown> {
920
+ return {
921
+ name: this.fieldName,
922
+ label: this.fieldLabel(),
923
+ required: Boolean(this.config.required),
924
+ readOnly: Boolean(this.config.primary || this.config.generated),
925
+ };
926
+ }
927
+
928
+ /**
929
+ * Default search component for this field type.
930
+ */
931
+ protected defaultSearchFilterComponent(): string | null {
932
+ return 'DomTextInput';
933
+ }
934
+
935
+ /**
936
+ * Default search props derived from FieldType config.
937
+ */
938
+ protected defaultSearchFilterComponentProps(_options: FieldRenderOptions): Record<string, unknown> {
939
+ return {
940
+ name: this.fieldName,
941
+ label: this.fieldLabel(),
942
+ };
943
+ }
944
+
945
+ protected fieldLabel(): string {
946
+ return typeof this.config.label === 'string' && this.config.label.trim()
947
+ ? this.config.label.trim()
948
+ : startCase(this.fieldName);
949
+ }
950
+
951
+ /**
952
+ * Converts a raw database value into app memory.
953
+ */
954
+ protected fromDbValue(input: TDbValue): TValue {
955
+ return input as unknown as TValue;
956
+ }
957
+
958
+ /**
959
+ * Converts an app-memory value into database storage form.
960
+ */
961
+ protected toDbValue(input: TValue, _options?: DatabaseValueOptions): TDbValue {
962
+ return input as unknown as TDbValue;
963
+ }
964
+
965
+ /**
966
+ * Converts a hydrated backend value into frontend/form display data.
967
+ */
968
+ protected toDisplayValue(input: TValue): TDisplayValue {
969
+ return input as unknown as TDisplayValue;
970
+ }
971
+
972
+ /**
973
+ * Collects validation errors for this field state.
974
+ *
975
+ * Concrete fields should call `super.collectErrors()` then append their own
976
+ * errors.
977
+ */
978
+ protected async collectErrors(state: TState): Promise<FieldError[]> {
979
+ const value = (state as any).value;
980
+
981
+ if (this.config.required && this.isEmpty(value)) {
982
+ return [
983
+ {
984
+ field: this.fieldName,
985
+ message: `${this.fieldName} is required`,
986
+ code: 'required',
987
+ value,
988
+ },
989
+ ];
990
+ }
991
+
992
+ return [];
993
+ }
994
+
995
+ /**
996
+ * Tests whether a value should count as empty for required validation.
997
+ */
998
+ protected isEmpty(value: unknown): boolean {
999
+ return value === null || value === undefined || value === '';
1000
+ }
1001
+
1002
+ /**
1003
+ * Returns the database schema default implied by the runtime field default.
1004
+ */
1005
+ protected getDbDefaultValue(): unknown {
1006
+ if (this.config.default === undefined) {
1007
+ return undefined;
1008
+ }
1009
+
1010
+ if (typeof this.config.default === 'function') {
1011
+ return undefined;
1012
+ }
1013
+
1014
+ return this.toDbValue(this.config.default as TValue);
1015
+ }
1016
+
1017
+ /**
1018
+ * Context for this bound field.
1019
+ */
1020
+ protected get context(): FieldContext {
1021
+ if (this.$activeContext) {
1022
+ return {
1023
+ ...this.$activeContext,
1024
+ record: this.$activeContext.record ?? this.$record,
1025
+ };
1026
+ }
1027
+
1028
+ return {
1029
+ model: this.requireModel(),
1030
+ record: this.$record,
1031
+ fieldName: this.requireFieldName(),
1032
+ };
1033
+ }
1034
+
1035
+ /**
1036
+ * Dialect for schema generation, falling back to the configured connection.
1037
+ */
1038
+ protected schemaDialect(): DatabaseDialect {
1039
+ return this.context.dialect ?? resolveDatabaseDialect();
1040
+ }
1041
+
1042
+ /**
1043
+ * Returns schema metadata and optionally writes columns onto `ctx.table`.
1044
+ */
1045
+ protected completeDbSchema(part: DbSchemaPart, ctx?: FieldContext): DbSchemaPart {
1046
+ if (ctx?.table) {
1047
+ for (const column of part.columns ?? []) {
1048
+ writeDbColumn(ctx.table, column, ctx.columnWriteOptions);
1049
+ }
1050
+ }
1051
+
1052
+ return part;
1053
+ }
1054
+
1055
+ /**
1056
+ * State for this bound field.
1057
+ */
1058
+ protected get state(): TState {
1059
+ if (!this.$state) {
1060
+ throw new Error('Field is not bound to a record.');
1061
+ }
1062
+
1063
+ return this.$state;
1064
+ }
1065
+
1066
+ /**
1067
+ * Attaches this field instance to a model without creating record state.
1068
+ */
1069
+ bindToModel(
1070
+ model: ModelMetadata,
1071
+ fieldName: string,
1072
+ ): this {
1073
+ this.$model = model;
1074
+ this.$fieldName = fieldName;
1075
+
1076
+ return this;
1077
+ }
1078
+
1079
+ /**
1080
+ * Returns an unbound copy of this field definition.
1081
+ */
1082
+ clone(): this {
1083
+ const Field = this.constructor as FieldClass<this>;
1084
+
1085
+ return new Field(this.config);
1086
+ }
1087
+
1088
+ /**
1089
+ * Returns a cloned field definition with config overrides applied.
1090
+ */
1091
+ withConfig(config: Partial<FieldConfig>): this {
1092
+ const Field = this.constructor as FieldClass<this>;
1093
+
1094
+ return new Field({
1095
+ ...this.config,
1096
+ ...config,
1097
+ });
1098
+ }
1099
+
1100
+ /**
1101
+ * Attaches this field instance to a concrete ActiveRecord instance.
1102
+ */
1103
+ bindToRecord(
1104
+ record: unknown,
1105
+ fieldName: string,
1106
+ ): this;
1107
+ bindToRecord(
1108
+ model: ModelMetadata,
1109
+ record: unknown,
1110
+ fieldName: string,
1111
+ ): this;
1112
+ bindToRecord(
1113
+ modelOrRecord: ModelMetadata | unknown,
1114
+ recordOrFieldName: unknown,
1115
+ fieldName?: string,
1116
+ ): this {
1117
+ const hasExplicitModel = typeof fieldName === 'string';
1118
+ const resolvedModel = hasExplicitModel
1119
+ ? modelOrRecord as ModelMetadata
1120
+ : this.modelFromRecord(modelOrRecord);
1121
+ const resolvedRecord = hasExplicitModel ? recordOrFieldName : modelOrRecord;
1122
+ const resolvedFieldName = hasExplicitModel
1123
+ ? fieldName
1124
+ : recordOrFieldName as string;
1125
+
1126
+ this.bindToModel(resolvedModel, resolvedFieldName);
1127
+ this.$record = resolvedRecord;
1128
+ this.$state = this.createState();
1129
+
1130
+ return this;
1131
+ }
1132
+
1133
+ protected resolveDbWriteArgs(
1134
+ ctxOrOptions: FieldContext | DbWriteOptions,
1135
+ options?: DbWriteOptions,
1136
+ ): { ctx?: FieldContext; options: DbWriteOptions } {
1137
+ if (options) {
1138
+ return {
1139
+ ctx: ctxOrOptions as FieldContext,
1140
+ options,
1141
+ };
1142
+ }
1143
+
1144
+ return {
1145
+ options: ctxOrOptions as DbWriteOptions,
1146
+ };
1147
+ }
1148
+
1149
+ /**
1150
+ * We are trying to deprecate this function in favour of using object functions like this.get
1151
+ * @param ctx @deprecated this.schemaDialect()
1152
+ * @returns
1153
+ */
1154
+ protected withContext<TResult>(
1155
+ ctx: FieldContext | undefined,
1156
+ callback: () => TResult,
1157
+ ): TResult {
1158
+ if (!ctx) {
1159
+ return callback();
1160
+ }
1161
+
1162
+ const previousContext = this.$activeContext;
1163
+
1164
+ this.$activeContext = ctx;
1165
+
1166
+ try {
1167
+ const result = callback();
1168
+
1169
+ if (isFinallyPromise(result)) {
1170
+ return result.finally(() => {
1171
+ this.$activeContext = previousContext;
1172
+ }) as TResult;
1173
+ }
1174
+
1175
+ this.$activeContext = previousContext;
1176
+ return result;
1177
+ } catch (error) {
1178
+ this.$activeContext = previousContext;
1179
+ throw error;
1180
+ }
1181
+ }
1182
+
1183
+ private modelFromRecord(record: unknown): ModelMetadata {
1184
+ const Model = (record as { constructor?: unknown })?.constructor as
1185
+ | ModelMetadata
1186
+ | undefined;
1187
+
1188
+ if (!Model?.table || !Model.primaryKey) {
1189
+ throw new Error('Field record is not backed by an ActiveRecord model.');
1190
+ }
1191
+
1192
+ return Model;
1193
+ }
1194
+
1195
+ private requireModel(): ModelMetadata {
1196
+ if (!this.$model) {
1197
+ throw new Error('Field is not bound to a model.');
1198
+ }
1199
+
1200
+ return this.$model;
1201
+ }
1202
+
1203
+ private requireFieldName(): string {
1204
+ if (!this.$fieldName) {
1205
+ throw new Error('Field is not bound to a record.');
1206
+ }
1207
+
1208
+ return this.$fieldName;
1209
+ }
1210
+
1211
+ /**
1212
+ * Resolves a per-record default, copying cloneable literal values.
1213
+ *
1214
+ * Use a default factory for custom objects that cannot be copied safely.
1215
+ */
1216
+ private getDefaultValue(): TValue {
1217
+ if (typeof this.config.default === 'function') {
1218
+ return (this.config.default as () => unknown)() as TValue;
1219
+ }
1220
+
1221
+ return snapshotFieldValue((this.config.default ?? null) as TValue);
1222
+ }
1223
+ }
1224
+
1225
+ export type FieldValue<TField> =
1226
+ TField extends FieldType<infer TValue, any, any, any, any>
1227
+ ? TValue
1228
+ : never;
1229
+
1230
+ export type FieldDbValue<TField> =
1231
+ TField extends FieldType<any, infer TDbValue, any, any, any>
1232
+ ? TDbValue
1233
+ : never;
1234
+
1235
+ export type FieldDisplayValue<TField> =
1236
+ TField extends FieldType<any, any, infer TDisplayValue, any, any>
1237
+ ? TDisplayValue
1238
+ : never;
1239
+
1240
+ export type FieldInputValue<TField> =
1241
+ TField extends FieldType<any, any, any, infer TInput, any>
1242
+ ? TInput
1243
+ : never;