@revealui/contracts 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +160 -0
  3. package/dist/a2a/index.d.ts +383 -0
  4. package/dist/a2a/index.d.ts.map +1 -0
  5. package/dist/a2a/index.js +276 -0
  6. package/dist/a2a/index.js.map +1 -0
  7. package/dist/actions/action-validator.d.ts +48 -0
  8. package/dist/actions/action-validator.d.ts.map +1 -0
  9. package/dist/actions/action-validator.js +288 -0
  10. package/dist/actions/action-validator.js.map +1 -0
  11. package/dist/actions/index.d.ts +7 -0
  12. package/dist/actions/index.d.ts.map +1 -0
  13. package/dist/actions/index.js +7 -0
  14. package/dist/actions/index.js.map +1 -0
  15. package/dist/agents/index.d.ts +628 -0
  16. package/dist/agents/index.d.ts.map +1 -0
  17. package/dist/agents/index.js +511 -0
  18. package/dist/agents/index.js.map +1 -0
  19. package/dist/agents/types.d.ts +22 -0
  20. package/dist/agents/types.d.ts.map +1 -0
  21. package/dist/agents/types.js +2 -0
  22. package/dist/agents/types.js.map +1 -0
  23. package/dist/api/auth.d.ts +60 -0
  24. package/dist/api/auth.d.ts.map +1 -0
  25. package/dist/api/auth.js +84 -0
  26. package/dist/api/auth.js.map +1 -0
  27. package/dist/api/chat.d.ts +45 -0
  28. package/dist/api/chat.d.ts.map +1 -0
  29. package/dist/api/chat.js +55 -0
  30. package/dist/api/chat.js.map +1 -0
  31. package/dist/api/gdpr.d.ts +39 -0
  32. package/dist/api/gdpr.d.ts.map +1 -0
  33. package/dist/api/gdpr.js +53 -0
  34. package/dist/api/gdpr.js.map +1 -0
  35. package/dist/cms/collection.d.ts +108 -0
  36. package/dist/cms/collection.d.ts.map +1 -0
  37. package/dist/cms/collection.js +144 -0
  38. package/dist/cms/collection.js.map +1 -0
  39. package/dist/cms/compat.d.ts +128 -0
  40. package/dist/cms/compat.d.ts.map +1 -0
  41. package/dist/cms/compat.js +141 -0
  42. package/dist/cms/compat.js.map +1 -0
  43. package/dist/cms/config-contract.d.ts +278 -0
  44. package/dist/cms/config-contract.d.ts.map +1 -0
  45. package/dist/cms/config-contract.js +220 -0
  46. package/dist/cms/config-contract.js.map +1 -0
  47. package/dist/cms/config.d.ts +351 -0
  48. package/dist/cms/config.d.ts.map +1 -0
  49. package/dist/cms/config.js +50 -0
  50. package/dist/cms/config.js.map +1 -0
  51. package/dist/cms/errors.d.ts +122 -0
  52. package/dist/cms/errors.d.ts.map +1 -0
  53. package/dist/cms/errors.js +163 -0
  54. package/dist/cms/errors.js.map +1 -0
  55. package/dist/cms/extensibility.d.ts +211 -0
  56. package/dist/cms/extensibility.d.ts.map +1 -0
  57. package/dist/cms/extensibility.js +313 -0
  58. package/dist/cms/extensibility.js.map +1 -0
  59. package/dist/cms/field.d.ts +69 -0
  60. package/dist/cms/field.d.ts.map +1 -0
  61. package/dist/cms/field.js +94 -0
  62. package/dist/cms/field.js.map +1 -0
  63. package/dist/cms/functions.d.ts +466 -0
  64. package/dist/cms/functions.d.ts.map +1 -0
  65. package/dist/cms/functions.js +19 -0
  66. package/dist/cms/functions.js.map +1 -0
  67. package/dist/cms/global.d.ts +45 -0
  68. package/dist/cms/global.d.ts.map +1 -0
  69. package/dist/cms/global.js +62 -0
  70. package/dist/cms/global.js.map +1 -0
  71. package/dist/cms/index.d.ts +23 -0
  72. package/dist/cms/index.d.ts.map +1 -0
  73. package/dist/cms/index.js +42 -0
  74. package/dist/cms/index.js.map +1 -0
  75. package/dist/cms/structure.d.ts +1601 -0
  76. package/dist/cms/structure.d.ts.map +1 -0
  77. package/dist/cms/structure.js +757 -0
  78. package/dist/cms/structure.js.map +1 -0
  79. package/dist/content/index.d.ts +1542 -0
  80. package/dist/content/index.d.ts.map +1 -0
  81. package/dist/content/index.js +522 -0
  82. package/dist/content/index.js.map +1 -0
  83. package/dist/database/bridge.d.ts +177 -0
  84. package/dist/database/bridge.d.ts.map +1 -0
  85. package/dist/database/bridge.js +139 -0
  86. package/dist/database/bridge.js.map +1 -0
  87. package/dist/database/index.d.ts +8 -0
  88. package/dist/database/index.d.ts.map +1 -0
  89. package/dist/database/index.js +9 -0
  90. package/dist/database/index.js.map +1 -0
  91. package/dist/database/type-bridge.d.ts +178 -0
  92. package/dist/database/type-bridge.d.ts.map +1 -0
  93. package/dist/database/type-bridge.js +154 -0
  94. package/dist/database/type-bridge.js.map +1 -0
  95. package/dist/entities/agent-context.d.ts +280 -0
  96. package/dist/entities/agent-context.d.ts.map +1 -0
  97. package/dist/entities/agent-context.js +390 -0
  98. package/dist/entities/agent-context.js.map +1 -0
  99. package/dist/entities/agent-memory.d.ts +661 -0
  100. package/dist/entities/agent-memory.d.ts.map +1 -0
  101. package/dist/entities/agent-memory.js +544 -0
  102. package/dist/entities/agent-memory.js.map +1 -0
  103. package/dist/entities/board.d.ts +95 -0
  104. package/dist/entities/board.d.ts.map +1 -0
  105. package/dist/entities/board.js +99 -0
  106. package/dist/entities/board.js.map +1 -0
  107. package/dist/entities/code-provenance.d.ts +270 -0
  108. package/dist/entities/code-provenance.d.ts.map +1 -0
  109. package/dist/entities/code-provenance.js +339 -0
  110. package/dist/entities/code-provenance.js.map +1 -0
  111. package/dist/entities/index.d.ts +22 -0
  112. package/dist/entities/index.d.ts.map +1 -0
  113. package/dist/entities/index.js +22 -0
  114. package/dist/entities/index.js.map +1 -0
  115. package/dist/entities/media.d.ts +485 -0
  116. package/dist/entities/media.d.ts.map +1 -0
  117. package/dist/entities/media.js +606 -0
  118. package/dist/entities/media.js.map +1 -0
  119. package/dist/entities/page-revision.d.ts +390 -0
  120. package/dist/entities/page-revision.d.ts.map +1 -0
  121. package/dist/entities/page-revision.js +406 -0
  122. package/dist/entities/page-revision.js.map +1 -0
  123. package/dist/entities/page.d.ts +2349 -0
  124. package/dist/entities/page.d.ts.map +1 -0
  125. package/dist/entities/page.js +377 -0
  126. package/dist/entities/page.js.map +1 -0
  127. package/dist/entities/post.d.ts +619 -0
  128. package/dist/entities/post.d.ts.map +1 -0
  129. package/dist/entities/post.js +555 -0
  130. package/dist/entities/post.js.map +1 -0
  131. package/dist/entities/price.d.ts +772 -0
  132. package/dist/entities/price.d.ts.map +1 -0
  133. package/dist/entities/price.js +308 -0
  134. package/dist/entities/price.js.map +1 -0
  135. package/dist/entities/product.d.ts +753 -0
  136. package/dist/entities/product.d.ts.map +1 -0
  137. package/dist/entities/product.js +307 -0
  138. package/dist/entities/product.js.map +1 -0
  139. package/dist/entities/session.d.ts +222 -0
  140. package/dist/entities/session.d.ts.map +1 -0
  141. package/dist/entities/session.js +253 -0
  142. package/dist/entities/session.js.map +1 -0
  143. package/dist/entities/site.d.ts +387 -0
  144. package/dist/entities/site.d.ts.map +1 -0
  145. package/dist/entities/site.js +348 -0
  146. package/dist/entities/site.js.map +1 -0
  147. package/dist/entities/ticket-comment.d.ts +49 -0
  148. package/dist/entities/ticket-comment.d.ts.map +1 -0
  149. package/dist/entities/ticket-comment.js +58 -0
  150. package/dist/entities/ticket-comment.js.map +1 -0
  151. package/dist/entities/ticket-label.d.ts +64 -0
  152. package/dist/entities/ticket-label.d.ts.map +1 -0
  153. package/dist/entities/ticket-label.js +77 -0
  154. package/dist/entities/ticket-label.js.map +1 -0
  155. package/dist/entities/ticket.d.ts +204 -0
  156. package/dist/entities/ticket.d.ts.map +1 -0
  157. package/dist/entities/ticket.js +205 -0
  158. package/dist/entities/ticket.js.map +1 -0
  159. package/dist/entities/user.d.ts +336 -0
  160. package/dist/entities/user.d.ts.map +1 -0
  161. package/dist/entities/user.js +255 -0
  162. package/dist/entities/user.js.map +1 -0
  163. package/dist/foundation/contract.d.ts +221 -0
  164. package/dist/foundation/contract.d.ts.map +1 -0
  165. package/dist/foundation/contract.js +133 -0
  166. package/dist/foundation/contract.js.map +1 -0
  167. package/dist/foundation/index.d.ts +7 -0
  168. package/dist/foundation/index.d.ts.map +1 -0
  169. package/dist/foundation/index.js +7 -0
  170. package/dist/foundation/index.js.map +1 -0
  171. package/dist/generated/contracts.d.ts +1514 -0
  172. package/dist/generated/contracts.d.ts.map +1 -0
  173. package/dist/generated/contracts.js +959 -0
  174. package/dist/generated/contracts.js.map +1 -0
  175. package/dist/generated/database.d.ts +48 -0
  176. package/dist/generated/database.d.ts.map +1 -0
  177. package/dist/generated/database.js +17 -0
  178. package/dist/generated/database.js.map +1 -0
  179. package/dist/generated/zod-schemas.d.ts +14793 -0
  180. package/dist/generated/zod-schemas.d.ts.map +1 -0
  181. package/dist/generated/zod-schemas.js +547 -0
  182. package/dist/generated/zod-schemas.js.map +1 -0
  183. package/dist/index.d.ts +50 -0
  184. package/dist/index.d.ts.map +1 -0
  185. package/dist/index.js +81 -0
  186. package/dist/index.js.map +1 -0
  187. package/dist/representation/index.d.ts +304 -0
  188. package/dist/representation/index.d.ts.map +1 -0
  189. package/dist/representation/index.js +280 -0
  190. package/dist/representation/index.js.map +1 -0
  191. package/package.json +117 -0
@@ -0,0 +1,177 @@
1
+ /**
2
+ * Database Contract Bridge
3
+ *
4
+ * Bridges Database types (from @revealui/db) with Contracts (from @revealui/contracts).
5
+ * Provides type-safe conversion utilities between database entities and contract-validated entities.
6
+ *
7
+ * This enables:
8
+ * - Type-safe conversion from Database types to Contract types
9
+ * - Runtime validation of database data using Contracts
10
+ * - Ensuring type safety across the DB -> ORM -> Contracts -> RevealUI layers
11
+ *
12
+ * @module @revealui/contracts/database
13
+ */
14
+ import type { Contract } from '../foundation/contract.js';
15
+ /**
16
+ * Generic Database type structure
17
+ * Matches the structure of @revealui/db/types Database type
18
+ * Used to avoid circular dependency between @revealui/contracts and @revealui/db
19
+ *
20
+ * @template T - The database tables structure
21
+ */
22
+ export type Database<T extends {
23
+ public: {
24
+ Tables: Record<string, {
25
+ Row: unknown;
26
+ Insert: unknown;
27
+ Update: unknown;
28
+ }>;
29
+ };
30
+ } = {
31
+ public: {
32
+ Tables: Record<string, {
33
+ Row: unknown;
34
+ Insert: unknown;
35
+ Update: unknown;
36
+ }>;
37
+ };
38
+ }> = T;
39
+ /**
40
+ * Convert a database row to a contract-validated entity
41
+ *
42
+ * @template T - The contract type
43
+ * @param contract - The contract to validate against
44
+ * @param dbRow - The database row data
45
+ * @returns Validated entity matching the contract
46
+ * @throws If validation fails
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * import { UserSchema } from '@revealui/contracts'
51
+ * import type { Database } from '@revealui/db/types' // Or use Database from this module
52
+ *
53
+ * const dbUser: Database['public']['Tables']['users']['Row'] = await db.query.users.findFirst()
54
+ * const validatedUser = dbRowToContract(UserSchema, dbUser)
55
+ * // validatedUser is now validated and typed according to UserSchema
56
+ * ```
57
+ */
58
+ export declare function dbRowToContract<T>(contract: Contract<T>, dbRow: unknown): T;
59
+ /**
60
+ * Safely convert a database row to a contract-validated entity
61
+ *
62
+ * Returns a result object instead of throwing on validation failure.
63
+ *
64
+ * @template T - The contract type
65
+ * @param contract - The contract to validate against
66
+ * @param dbRow - The database row data
67
+ * @returns Validation result with typed data or errors
68
+ *
69
+ * @example
70
+ * ```typescript
71
+ * const result = safeDbRowToContract(UserSchema, dbUser)
72
+ * if (result.success) {
73
+ * // result.data is fully typed and validated
74
+ * console.log(result.data.email)
75
+ * } else {
76
+ * // Handle validation errors
77
+ * console.error(result.errors)
78
+ * }
79
+ * ```
80
+ */
81
+ export declare function safeDbRowToContract<T>(contract: Contract<T>, dbRow: unknown): ReturnType<typeof contract.validate>;
82
+ /**
83
+ * Convert a contract-validated entity to a database insert type
84
+ *
85
+ * This ensures that data validated by contracts can be safely inserted into the database.
86
+ *
87
+ * @template TInsert - The database insert type
88
+ * @template TContract - The contract type
89
+ * @param contractData - The contract-validated data
90
+ * @returns Data ready for database insertion
91
+ *
92
+ * @example
93
+ * ```typescript
94
+ * const newUser = createUser({ email: 'user@example.com', name: 'User' })
95
+ * const dbInsert: Database['public']['Tables']['users']['Insert'] = contractToDbInsert(newUser)
96
+ * await db.insert(users).values(dbInsert)
97
+ * ```
98
+ */
99
+ export declare function contractToDbInsert<TContract, TInsert>(contractData: TContract): TInsert;
100
+ /**
101
+ * Type guard to check if database row matches a contract
102
+ *
103
+ * @template T - The contract type
104
+ * @param contract - The contract to check against
105
+ * @param dbRow - The database row to check
106
+ * @returns True if the row matches the contract
107
+ */
108
+ export declare function isDbRowMatchingContract<T>(contract: Contract<T>, dbRow: unknown): dbRow is T;
109
+ /**
110
+ * Extract table name from Database type
111
+ *
112
+ * Utility type to extract table names from the Database type structure.
113
+ */
114
+ export type TableName<T extends Database> = keyof T['public']['Tables'];
115
+ /**
116
+ * Extract row type for a specific table
117
+ *
118
+ * @template T - The Database type
119
+ * @template N - The table name
120
+ */
121
+ export type TableRowType<T extends Database, N extends TableName<T>> = T['public']['Tables'][N] extends {
122
+ Row: infer R;
123
+ } ? R : never;
124
+ /**
125
+ * Extract insert type for a specific table
126
+ *
127
+ * @template T - The Database type
128
+ * @template N - The table name
129
+ */
130
+ export type TableInsertType<T extends Database, N extends TableName<T>> = T['public']['Tables'][N] extends {
131
+ Insert: infer I;
132
+ } ? I : never;
133
+ /**
134
+ * Extract update type for a specific table
135
+ *
136
+ * @template T - The Database type
137
+ * @template N - The table name
138
+ */
139
+ export type TableUpdateType<T extends Database, N extends TableName<T>> = T['public']['Tables'][N] extends {
140
+ Update: infer U;
141
+ } ? U : never;
142
+ /**
143
+ * Database Contract Registry
144
+ *
145
+ * Maps table names to their corresponding contracts.
146
+ * This enables automatic validation when converting database rows to contract types.
147
+ */
148
+ export declare class DatabaseContractRegistry {
149
+ private contracts;
150
+ /**
151
+ * Register a contract for a table
152
+ *
153
+ * @param tableName - The database table name
154
+ * @param contract - The contract to use for validation
155
+ */
156
+ register<T>(tableName: string, contract: Contract<T>): void;
157
+ /**
158
+ * Get a contract for a table
159
+ *
160
+ * @param tableName - The database table name
161
+ * @returns The contract if registered, undefined otherwise
162
+ */
163
+ get<T>(tableName: string): Contract<T> | undefined;
164
+ /**
165
+ * Validate a database row using its registered contract
166
+ *
167
+ * @param tableName - The database table name
168
+ * @param dbRow - The database row to validate
169
+ * @returns Validation result
170
+ */
171
+ validateRow(tableName: string, dbRow: unknown): ReturnType<Contract<unknown>['validate']> | null;
172
+ }
173
+ /**
174
+ * Global database contract registry instance
175
+ */
176
+ export declare const databaseContractRegistry: DatabaseContractRegistry;
177
+ //# sourceMappingURL=bridge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bridge.d.ts","sourceRoot":"","sources":["../../src/database/bridge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAA;AAEzD;;;;;;GAMG;AACH,MAAM,MAAM,QAAQ,CAClB,CAAC,SAAS;IACR,MAAM,EAAE;QAEN,MAAM,EAAE,MAAM,CACZ,MAAM,EACN;YAEE,GAAG,EAAE,OAAO,CAAA;YAEZ,MAAM,EAAE,OAAO,CAAA;YAEf,MAAM,EAAE,OAAO,CAAA;SAChB,CACF,CAAA;KACF,CAAA;CACF,GAAG;IACF,MAAM,EAAE;QAEN,MAAM,EAAE,MAAM,CACZ,MAAM,EACN;YAEE,GAAG,EAAE,OAAO,CAAA;YAEZ,MAAM,EAAE,OAAO,CAAA;YAEf,MAAM,EAAE,OAAO,CAAA;SAChB,CACF,CAAA;KACF,CAAA;CACF,IACC,CAAC,CAAA;AAEL;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,GAAG,CAAC,CAE3E;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EACnC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EACrB,KAAK,EAAE,OAAO,GACb,UAAU,CAAC,OAAO,QAAQ,CAAC,QAAQ,CAAC,CAEtC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,kBAAkB,CAAC,S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AAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,IAAI;IAI3D;;;;;OAKG;IACH,GAAG,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,SAAS;IAIlD;;;;;;OAMG;IACH,WAAW,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,UAAU,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,UAAU,CAAC,CAAC,GAAG,IAAI;CAOjG;AAED;;GAEG;AACH,eAAO,MAAM,wBAAwB,0BAAiC,CAAA"}
@@ -0,0 +1,139 @@
1
+ /**
2
+ * Database Contract Bridge
3
+ *
4
+ * Bridges Database types (from @revealui/db) with Contracts (from @revealui/contracts).
5
+ * Provides type-safe conversion utilities between database entities and contract-validated entities.
6
+ *
7
+ * This enables:
8
+ * - Type-safe conversion from Database types to Contract types
9
+ * - Runtime validation of database data using Contracts
10
+ * - Ensuring type safety across the DB -> ORM -> Contracts -> RevealUI layers
11
+ *
12
+ * @module @revealui/contracts/database
13
+ */
14
+ /**
15
+ * Convert a database row to a contract-validated entity
16
+ *
17
+ * @template T - The contract type
18
+ * @param contract - The contract to validate against
19
+ * @param dbRow - The database row data
20
+ * @returns Validated entity matching the contract
21
+ * @throws If validation fails
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * import { UserSchema } from '@revealui/contracts'
26
+ * import type { Database } from '@revealui/db/types' // Or use Database from this module
27
+ *
28
+ * const dbUser: Database['public']['Tables']['users']['Row'] = await db.query.users.findFirst()
29
+ * const validatedUser = dbRowToContract(UserSchema, dbUser)
30
+ * // validatedUser is now validated and typed according to UserSchema
31
+ * ```
32
+ */
33
+ export function dbRowToContract(contract, dbRow) {
34
+ return contract.parse(dbRow);
35
+ }
36
+ /**
37
+ * Safely convert a database row to a contract-validated entity
38
+ *
39
+ * Returns a result object instead of throwing on validation failure.
40
+ *
41
+ * @template T - The contract type
42
+ * @param contract - The contract to validate against
43
+ * @param dbRow - The database row data
44
+ * @returns Validation result with typed data or errors
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * const result = safeDbRowToContract(UserSchema, dbUser)
49
+ * if (result.success) {
50
+ * // result.data is fully typed and validated
51
+ * console.log(result.data.email)
52
+ * } else {
53
+ * // Handle validation errors
54
+ * console.error(result.errors)
55
+ * }
56
+ * ```
57
+ */
58
+ export function safeDbRowToContract(contract, dbRow) {
59
+ return contract.validate(dbRow);
60
+ }
61
+ /**
62
+ * Convert a contract-validated entity to a database insert type
63
+ *
64
+ * This ensures that data validated by contracts can be safely inserted into the database.
65
+ *
66
+ * @template TInsert - The database insert type
67
+ * @template TContract - The contract type
68
+ * @param contractData - The contract-validated data
69
+ * @returns Data ready for database insertion
70
+ *
71
+ * @example
72
+ * ```typescript
73
+ * const newUser = createUser({ email: 'user@example.com', name: 'User' })
74
+ * const dbInsert: Database['public']['Tables']['users']['Insert'] = contractToDbInsert(newUser)
75
+ * await db.insert(users).values(dbInsert)
76
+ * ```
77
+ */
78
+ export function contractToDbInsert(contractData) {
79
+ // Type assertion - the contract should match the database structure
80
+ // In practice, contracts and database schemas should be kept in sync
81
+ return contractData;
82
+ }
83
+ /**
84
+ * Type guard to check if database row matches a contract
85
+ *
86
+ * @template T - The contract type
87
+ * @param contract - The contract to check against
88
+ * @param dbRow - The database row to check
89
+ * @returns True if the row matches the contract
90
+ */
91
+ export function isDbRowMatchingContract(contract, dbRow) {
92
+ return contract.isType(dbRow);
93
+ }
94
+ /**
95
+ * Database Contract Registry
96
+ *
97
+ * Maps table names to their corresponding contracts.
98
+ * This enables automatic validation when converting database rows to contract types.
99
+ */
100
+ export class DatabaseContractRegistry {
101
+ contracts = new Map();
102
+ /**
103
+ * Register a contract for a table
104
+ *
105
+ * @param tableName - The database table name
106
+ * @param contract - The contract to use for validation
107
+ */
108
+ register(tableName, contract) {
109
+ this.contracts.set(tableName, contract);
110
+ }
111
+ /**
112
+ * Get a contract for a table
113
+ *
114
+ * @param tableName - The database table name
115
+ * @returns The contract if registered, undefined otherwise
116
+ */
117
+ get(tableName) {
118
+ return this.contracts.get(tableName);
119
+ }
120
+ /**
121
+ * Validate a database row using its registered contract
122
+ *
123
+ * @param tableName - The database table name
124
+ * @param dbRow - The database row to validate
125
+ * @returns Validation result
126
+ */
127
+ validateRow(tableName, dbRow) {
128
+ const contract = this.contracts.get(tableName);
129
+ if (!contract) {
130
+ return null;
131
+ }
132
+ return contract.validate(dbRow);
133
+ }
134
+ }
135
+ /**
136
+ * Global database contract registry instance
137
+ */
138
+ export const databaseContractRegistry = new DatabaseContractRegistry();
139
+ //# sourceMappingURL=bridge.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bridge.js","sourceRoot":"","sources":["../../src/database/bridge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AA6CH;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,eAAe,CAAI,QAAqB,EAAE,KAAc;IACtE,OAAO,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;AAC9B,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,mBAAmB,CACjC,QAAqB,EACrB,KAAc;IAEd,OAAO,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;AACjC,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,kBAAkB,CAAqB,YAAuB;IAC5E,oEAAoE;IACpE,qEAAqE;IACrE,OAAO,YAAkC,CAAA;AAC3C,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,uBAAuB,CAAI,QAAqB,EAAE,KAAc;IAC9E,OAAO,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AAC/B,CAAC;AAyDD;;;;;GAKG;AACH,MAAM,OAAO,wBAAwB;IAC3B,SAAS,GAAG,IAAI,GAAG,EAA6B,CAAA;IAExD;;;;;OAKG;IACH,QAAQ,CAAI,SAAiB,EAAE,QAAqB;QAClD,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,SAAS,EAAE,QAA6B,CAAC,CAAA;IAC9D,CAAC;IAED;;;;;OAKG;IACH,GAAG,CAAI,SAAiB;QACtB,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,SAAS,CAA4B,CAAA;IACjE,CAAC;IAED;;;;;;OAMG;IACH,WAAW,CAAC,SAAiB,EAAE,KAAc;QAC3C,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,CAAA;QAC9C,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,OAAO,IAAI,CAAA;QACb,CAAC;QACD,OAAO,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;IACjC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,IAAI,wBAAwB,EAAE,CAAA"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @revealui/contracts/database
3
+ *
4
+ * Database contract bridges - type-safe conversion between database types and contracts
5
+ */
6
+ export { contractToDbInsert, DatabaseContractRegistry, databaseContractRegistry, dbRowToContract, isDbRowMatchingContract, safeDbRowToContract, type TableInsertType, type TableName, type TableRowType, type TableUpdateType, } from './bridge.js';
7
+ export { batchContractToDbInsert, batchDbRowsToContract, type ContractToDrizzleInsert, createContractToDbMapper, createDbRowMapper, createTableContractRegistry, type DrizzleToContract, isDbRowAndContract, type TableContractMap, } from './type-bridge.js';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/database/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EACL,kBAAkB,EAClB,wBAAwB,EACxB,wBAAwB,EACxB,eAAe,EACf,uBAAuB,EACvB,mBAAmB,EACnB,KAAK,eAAe,EACpB,KAAK,SAAS,EACd,KAAK,YAAY,EACjB,KAAK,eAAe,GACrB,MAAM,aAAa,CAAA;AACpB,OAAO,EACL,uBAAuB,EACvB,qBAAqB,EACrB,KAAK,uBAAuB,EAC5B,wBAAwB,EACxB,iBAAiB,EACjB,2BAA2B,EAC3B,KAAK,iBAAiB,EACtB,kBAAkB,EAClB,KAAK,gBAAgB,GACtB,MAAM,kBAAkB,CAAA"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @revealui/contracts/database
3
+ *
4
+ * Database contract bridges - type-safe conversion between database types and contracts
5
+ */
6
+ // Re-export contractToDbInsert for convenience
7
+ export { contractToDbInsert, DatabaseContractRegistry, databaseContractRegistry, dbRowToContract, isDbRowMatchingContract, safeDbRowToContract, } from './bridge.js';
8
+ export { batchContractToDbInsert, batchDbRowsToContract, createContractToDbMapper, createDbRowMapper, createTableContractRegistry, isDbRowAndContract, } from './type-bridge.js';
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/database/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,+CAA+C;AAC/C,OAAO,EACL,kBAAkB,EAClB,wBAAwB,EACxB,wBAAwB,EACxB,eAAe,EACf,uBAAuB,EACvB,mBAAmB,GAKpB,MAAM,aAAa,CAAA;AACpB,OAAO,EACL,uBAAuB,EACvB,qBAAqB,EAErB,wBAAwB,EACxB,iBAAiB,EACjB,2BAA2B,EAE3B,kBAAkB,GAEnB,MAAM,kBAAkB,CAAA"}
@@ -0,0 +1,178 @@
1
+ /**
2
+ * Type Bridge Utilities
3
+ *
4
+ * Provides comprehensive type-safe conversion utilities between:
5
+ * - Drizzle ORM types (from @revealui/db)
6
+ * - Contract types (from @revealui/contracts)
7
+ * - RevealUI types (from @revealui/core)
8
+ *
9
+ * This ensures type safety across all layers of the application.
10
+ *
11
+ * @module @revealui/contracts/core/contracts/type-bridge
12
+ */
13
+ import type { Contract, ContractValidationResult } from '../foundation/contract.js';
14
+ /**
15
+ * Generic Database type structure
16
+ * Matches the structure of @revealui/db/types Database type
17
+ * Used to avoid circular dependency between @revealui/contracts and @revealui/db
18
+ *
19
+ * @template T - The database tables structure
20
+ */
21
+ export type Database<T extends {
22
+ public: {
23
+ Tables: Record<string, {
24
+ Row: unknown;
25
+ Insert: unknown;
26
+ Update: unknown;
27
+ }>;
28
+ };
29
+ } = {
30
+ public: {
31
+ Tables: Record<string, {
32
+ Row: unknown;
33
+ Insert: unknown;
34
+ Update: unknown;
35
+ }>;
36
+ };
37
+ }> = T;
38
+ /**
39
+ * Convert Drizzle table type to Contract type
40
+ *
41
+ * This is a type-level utility that helps ensure Drizzle types
42
+ * can be safely converted to Contract types.
43
+ *
44
+ * @template TTable - The Drizzle table type
45
+ * @template TContract - The Contract type
46
+ */
47
+ export type DrizzleToContract<TTable, TContract> = TTable extends TContract ? TContract : never;
48
+ /**
49
+ * Convert Contract type to Drizzle insert type
50
+ *
51
+ * Ensures contract-validated data can be safely inserted into database.
52
+ *
53
+ * @template TContract - The Contract type
54
+ * @template TInsert - The Drizzle insert type
55
+ */
56
+ export type ContractToDrizzleInsert<TContract, TInsert> = TContract extends TInsert ? TInsert : never;
57
+ /**
58
+ * Type-safe mapper function for converting database rows to contract types
59
+ *
60
+ * @template TContract - The contract type
61
+ * @template TDbRow - The database row type
62
+ * @param contract - The contract to validate against
63
+ * @param mapper - Optional mapper function to transform the row before validation
64
+ * @returns A function that converts database rows to contract-validated entities
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * const userMapper = createDbRowMapper(UserSchema, (row) => ({
69
+ * ...row,
70
+ * // Transform if needed
71
+ * }))
72
+ *
73
+ * const dbUser = await db.query.users.findFirst()
74
+ * const validatedUser = userMapper(dbUser)
75
+ * ```
76
+ */
77
+ export declare function createDbRowMapper<TContract, TDbRow = unknown>(contract: Contract<TContract>, mapper?: (row: TDbRow) => unknown): (row: TDbRow) => TContract;
78
+ /**
79
+ * Type-safe mapper function for converting contract types to database insert types
80
+ *
81
+ * @template TContract - The contract type
82
+ * @template TInsert - The database insert type
83
+ * @param mapper - Optional mapper function to transform the contract data before insertion
84
+ * @returns A function that converts contract-validated entities to database insert types
85
+ *
86
+ * @example
87
+ * ```typescript
88
+ * const insertMapper = createContractToDbMapper<User, Database['public']['Tables']['users']['Insert']>(
89
+ * (user) => ({
90
+ * ...user,
91
+ * // Transform if needed
92
+ * })
93
+ * )
94
+ *
95
+ * const newUser = createUser({ email: 'user@example.com', name: 'User' })
96
+ * const dbInsert = insertMapper(newUser)
97
+ * await db.insert(users).values(dbInsert)
98
+ * ```
99
+ */
100
+ export declare function createContractToDbMapper<TContract, TInsert = TContract>(mapper?: (data: TContract) => TInsert): (data: TContract) => TInsert;
101
+ /**
102
+ * Batch convert database rows to contract-validated entities
103
+ *
104
+ * @template TContract - The contract type
105
+ * @template TDbRow - The database row type
106
+ * @param contract - The contract to validate against
107
+ * @param rows - Array of database rows
108
+ * @param mapper - Optional mapper function
109
+ * @returns Array of validated entities
110
+ */
111
+ export declare function batchDbRowsToContract<TContract, TDbRow = unknown>(contract: Contract<TContract>, rows: TDbRow[], mapper?: (row: TDbRow) => unknown): TContract[];
112
+ /**
113
+ * Batch convert contract-validated entities to database insert types
114
+ *
115
+ * @template TContract - The contract type
116
+ * @template TInsert - The database insert type
117
+ * @param entities - Array of contract-validated entities
118
+ * @param mapper - Optional mapper function
119
+ * @returns Array of database insert types
120
+ */
121
+ export declare function batchContractToDbInsert<TContract, TInsert = TContract>(entities: TContract[], mapper?: (data: TContract) => TInsert): TInsert[];
122
+ /**
123
+ * Type guard to check if a value matches both Drizzle and Contract types
124
+ *
125
+ * @template TContract - The contract type
126
+ * @template TDbRow - The database row type
127
+ * @param contract - The contract to check against
128
+ * @param value - The value to check
129
+ * @returns True if value matches both types
130
+ */
131
+ export declare function isDbRowAndContract<TContract, TDbRow>(contract: Contract<TContract>, value: unknown): value is TContract & TDbRow;
132
+ /**
133
+ * Extract contract type from a table name
134
+ *
135
+ * Utility type helper for type-safe table-to-contract mapping.
136
+ *
137
+ * @template T - The Database type (from @revealui/db/types or compatible structure)
138
+ */
139
+ export type TableContractMap<T extends Database> = {
140
+ [K in keyof T['public']['Tables']]?: T['public']['Tables'][K] extends {
141
+ Row: infer R;
142
+ } ? Contract<R> : never;
143
+ };
144
+ /**
145
+ * Create a type-safe table-to-contract registry
146
+ *
147
+ * @template T - The Database type
148
+ * @param map - Map of table names to contracts
149
+ * @returns Registry with type-safe access
150
+ *
151
+ * @example
152
+ * ```typescript
153
+ * const registry = createTableContractRegistry<Database>({
154
+ * users: UserSchema,
155
+ * sites: SiteSchema,
156
+ * })
157
+ *
158
+ * const user = await db.query.users.findFirst()
159
+ * const validated = registry.validate('users', user)
160
+ * ```
161
+ */
162
+ export declare function createTableContractRegistry<T extends Database>(map: TableContractMap<T>): {
163
+ /**
164
+ * Validate a database row using its table's contract
165
+ */
166
+ validate<K extends keyof T["public"]["Tables"]>(tableName: K, row: T["public"]["Tables"][K] extends {
167
+ Row: infer R;
168
+ } ? R : never): T["public"]["Tables"][K] extends {
169
+ Row: infer R;
170
+ } ? ContractValidationResult<R> | null : null;
171
+ /**
172
+ * Get contract for a table
173
+ */
174
+ getContract<K extends keyof T["public"]["Tables"]>(tableName: K): T["public"]["Tables"][K] extends {
175
+ Row: infer R;
176
+ } ? Contract<R> | undefined : undefined;
177
+ };
178
+ //# sourceMappingURL=type-bridge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type-bridge.d.ts","sourceRoot":"","sources":["../../src/database/type-bridge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAA;AAEnF;;;;;;GAMG;AACH,MAAM,MAAM,QAAQ,CAClB,CAAC,SAAS;IACR,MAAM,EAAE;QAEN,MAAM,EAAE,MAAM,CACZ,MAAM,EACN;YAEE,GAAG,EAAE,OAAO,CAAA;YAEZ,MAAM,EAAE,OAAO,CAAA;YAEf,MAAM,EAAE,OAAO,CA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