@nest-yalc-2/omnikernel-module 1.1.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 (230) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +193 -0
  3. package/package.json +52 -0
  4. package/src/__tests__/omni-collection.entity.spec.ts +49 -0
  5. package/src/__tests__/omni-collection.service.spec.ts +83 -0
  6. package/src/__tests__/omni-document.entity.spec.ts +55 -0
  7. package/src/__tests__/omni-document.service.spec.ts +83 -0
  8. package/src/__tests__/omni-extension-projection-constraints.spec.ts +518 -0
  9. package/src/__tests__/omni-extension-projection.registration.spec.ts +946 -0
  10. package/src/__tests__/omni-extension-projection.service.spec.ts +806 -0
  11. package/src/__tests__/omni-external-ref.entity.spec.ts +36 -0
  12. package/src/__tests__/omni-external-ref.service.spec.ts +252 -0
  13. package/src/__tests__/omni-named.entity.spec.ts +33 -0
  14. package/src/__tests__/omni-record.entity.spec.ts +32 -0
  15. package/src/__tests__/omni-record.service.spec.ts +107 -0
  16. package/src/__tests__/omni-relation-semantics.spec.ts +102 -0
  17. package/src/__tests__/omni-relation.entity.spec.ts +38 -0
  18. package/src/__tests__/omni-scope-substrate.spec.ts +81 -0
  19. package/src/__tests__/omni-scoped.backend.spec.ts +115 -0
  20. package/src/__tests__/omni-scoped.service.spec.ts +354 -0
  21. package/src/__tests__/omnikernel.diagnostics.spec.ts +94 -0
  22. package/src/__tests__/omnikernel.module.spec.ts +40 -0
  23. package/src/__tests__/omnikernel.persistence.spec.ts +154 -0
  24. package/src/__tests__/omnikernel.public-api.spec.ts +238 -0
  25. package/src/__tests__/omnikernel.query.service.spec.ts +155 -0
  26. package/src/__tests__/tsconfig.json +6 -0
  27. package/src/base/omni-base.entity.d.ts +9 -0
  28. package/src/base/omni-base.entity.js +53 -0
  29. package/src/base/omni-base.entity.js.map +1 -0
  30. package/src/base/omni-base.entity.ts +41 -0
  31. package/src/base/omni-external-ref.entity.d.ts +13 -0
  32. package/src/base/omni-external-ref.entity.js +72 -0
  33. package/src/base/omni-external-ref.entity.js.map +1 -0
  34. package/src/base/omni-external-ref.entity.ts +53 -0
  35. package/src/base/omni-named.entity.d.ts +6 -0
  36. package/src/base/omni-named.entity.js +35 -0
  37. package/src/base/omni-named.entity.js.map +1 -0
  38. package/src/base/omni-named.entity.ts +16 -0
  39. package/src/base/omni-record.entity.d.ts +13 -0
  40. package/src/base/omni-record.entity.js +71 -0
  41. package/src/base/omni-record.entity.js.map +1 -0
  42. package/src/base/omni-record.entity.ts +51 -0
  43. package/src/base/omni-relation.entity.d.ts +15 -0
  44. package/src/base/omni-relation.entity.js +91 -0
  45. package/src/base/omni-relation.entity.js.map +1 -0
  46. package/src/base/omni-relation.entity.ts +72 -0
  47. package/src/index.d.ts +49 -0
  48. package/src/index.js +66 -0
  49. package/src/index.js.map +1 -0
  50. package/src/index.ts +49 -0
  51. package/src/omni-collection-kind.enum.d.ts +4 -0
  52. package/src/omni-collection-kind.enum.js +13 -0
  53. package/src/omni-collection-kind.enum.js.map +1 -0
  54. package/src/omni-collection-kind.enum.ts +10 -0
  55. package/src/omni-collection.backend.d.ts +2 -0
  56. package/src/omni-collection.backend.js +15 -0
  57. package/src/omni-collection.backend.js.map +1 -0
  58. package/src/omni-collection.backend.ts +16 -0
  59. package/src/omni-collection.dto.d.ts +32 -0
  60. package/src/omni-collection.dto.js +184 -0
  61. package/src/omni-collection.dto.js.map +1 -0
  62. package/src/omni-collection.dto.ts +170 -0
  63. package/src/omni-collection.entity.d.ts +7 -0
  64. package/src/omni-collection.entity.js +41 -0
  65. package/src/omni-collection.entity.js.map +1 -0
  66. package/src/omni-collection.entity.ts +21 -0
  67. package/src/omni-collection.service.d.ts +14 -0
  68. package/src/omni-collection.service.js +24 -0
  69. package/src/omni-collection.service.js.map +1 -0
  70. package/src/omni-collection.service.ts +75 -0
  71. package/src/omni-document-kind.enum.d.ts +6 -0
  72. package/src/omni-document-kind.enum.js +15 -0
  73. package/src/omni-document-kind.enum.js.map +1 -0
  74. package/src/omni-document-kind.enum.ts +12 -0
  75. package/src/omni-document.backend.d.ts +2 -0
  76. package/src/omni-document.backend.js +15 -0
  77. package/src/omni-document.backend.js.map +1 -0
  78. package/src/omni-document.backend.ts +16 -0
  79. package/src/omni-document.dto.d.ts +35 -0
  80. package/src/omni-document.dto.js +213 -0
  81. package/src/omni-document.dto.js.map +1 -0
  82. package/src/omni-document.dto.ts +198 -0
  83. package/src/omni-document.entity.d.ts +10 -0
  84. package/src/omni-document.entity.js +53 -0
  85. package/src/omni-document.entity.js.map +1 -0
  86. package/src/omni-document.entity.ts +30 -0
  87. package/src/omni-document.service.d.ts +14 -0
  88. package/src/omni-document.service.js +24 -0
  89. package/src/omni-document.service.js.map +1 -0
  90. package/src/omni-document.service.ts +75 -0
  91. package/src/omni-dto.helpers.d.ts +1 -0
  92. package/src/omni-dto.helpers.js +10 -0
  93. package/src/omni-dto.helpers.js.map +1 -0
  94. package/src/omni-dto.helpers.ts +21 -0
  95. package/src/omni-extension-projection.definition.d.ts +17 -0
  96. package/src/omni-extension-projection.definition.js +160 -0
  97. package/src/omni-extension-projection.definition.js.map +1 -0
  98. package/src/omni-extension-projection.definition.ts +294 -0
  99. package/src/omni-extension-projection.resource.d.ts +42 -0
  100. package/src/omni-extension-projection.resource.js +127 -0
  101. package/src/omni-extension-projection.resource.js.map +1 -0
  102. package/src/omni-extension-projection.resource.ts +274 -0
  103. package/src/omni-extension-projection.service.d.ts +33 -0
  104. package/src/omni-extension-projection.service.js +332 -0
  105. package/src/omni-extension-projection.service.js.map +1 -0
  106. package/src/omni-extension-projection.service.ts +452 -0
  107. package/src/omni-external-ref-binding.validator.d.ts +14 -0
  108. package/src/omni-external-ref-binding.validator.js +34 -0
  109. package/src/omni-external-ref-binding.validator.js.map +1 -0
  110. package/src/omni-external-ref-binding.validator.ts +53 -0
  111. package/src/omni-external-ref-internal-type.enum.d.ts +5 -0
  112. package/src/omni-external-ref-internal-type.enum.js +14 -0
  113. package/src/omni-external-ref-internal-type.enum.js.map +1 -0
  114. package/src/omni-external-ref-internal-type.enum.ts +11 -0
  115. package/src/omni-external-ref.backend.d.ts +2 -0
  116. package/src/omni-external-ref.backend.js +19 -0
  117. package/src/omni-external-ref.backend.js.map +1 -0
  118. package/src/omni-external-ref.backend.ts +25 -0
  119. package/src/omni-external-ref.dto.d.ts +26 -0
  120. package/src/omni-external-ref.dto.js +140 -0
  121. package/src/omni-external-ref.dto.js.map +1 -0
  122. package/src/omni-external-ref.dto.ts +128 -0
  123. package/src/omni-external-ref.service.d.ts +33 -0
  124. package/src/omni-external-ref.service.js +133 -0
  125. package/src/omni-external-ref.service.js.map +1 -0
  126. package/src/omni-external-ref.service.ts +255 -0
  127. package/src/omni-migration.d.ts +28 -0
  128. package/src/omni-migration.js +239 -0
  129. package/src/omni-migration.js.map +1 -0
  130. package/src/omni-migration.ts +365 -0
  131. package/src/omni-named.backend.d.ts +2 -0
  132. package/src/omni-named.backend.js +13 -0
  133. package/src/omni-named.backend.js.map +1 -0
  134. package/src/omni-named.backend.ts +11 -0
  135. package/src/omni-named.dto.d.ts +19 -0
  136. package/src/omni-named.dto.js +94 -0
  137. package/src/omni-named.dto.js.map +1 -0
  138. package/src/omni-named.dto.ts +86 -0
  139. package/src/omni-projection.catalog.d.ts +49 -0
  140. package/src/omni-projection.catalog.js +177 -0
  141. package/src/omni-projection.catalog.js.map +1 -0
  142. package/src/omni-projection.catalog.ts +345 -0
  143. package/src/omni-projection.lifecycle.d.ts +20 -0
  144. package/src/omni-projection.lifecycle.js +3 -0
  145. package/src/omni-projection.lifecycle.js.map +1 -0
  146. package/src/omni-projection.lifecycle.ts +53 -0
  147. package/src/omni-record-status.enum.d.ts +5 -0
  148. package/src/omni-record-status.enum.js +14 -0
  149. package/src/omni-record-status.enum.js.map +1 -0
  150. package/src/omni-record-status.enum.ts +11 -0
  151. package/src/omni-record.backend.d.ts +2 -0
  152. package/src/omni-record.backend.js +13 -0
  153. package/src/omni-record.backend.js.map +1 -0
  154. package/src/omni-record.backend.ts +19 -0
  155. package/src/omni-record.dto.d.ts +29 -0
  156. package/src/omni-record.dto.js +169 -0
  157. package/src/omni-record.dto.js.map +1 -0
  158. package/src/omni-record.dto.ts +157 -0
  159. package/src/omni-record.service.d.ts +18 -0
  160. package/src/omni-record.service.js +45 -0
  161. package/src/omni-record.service.js.map +1 -0
  162. package/src/omni-record.service.ts +113 -0
  163. package/src/omni-relation-kind.contract.d.ts +9 -0
  164. package/src/omni-relation-kind.contract.js +33 -0
  165. package/src/omni-relation-kind.contract.js.map +1 -0
  166. package/src/omni-relation-kind.contract.ts +49 -0
  167. package/src/omni-relation-kind.enum.d.ts +6 -0
  168. package/src/omni-relation-kind.enum.js +15 -0
  169. package/src/omni-relation-kind.enum.js.map +1 -0
  170. package/src/omni-relation-kind.enum.ts +12 -0
  171. package/src/omni-relation-projection.definition.d.ts +25 -0
  172. package/src/omni-relation-projection.definition.js +83 -0
  173. package/src/omni-relation-projection.definition.js.map +1 -0
  174. package/src/omni-relation-projection.definition.ts +166 -0
  175. package/src/omni-relation-projection.resource.d.ts +51 -0
  176. package/src/omni-relation-projection.resource.js +203 -0
  177. package/src/omni-relation-projection.resource.js.map +1 -0
  178. package/src/omni-relation-projection.resource.ts +360 -0
  179. package/src/omni-relation-projection.service.d.ts +47 -0
  180. package/src/omni-relation-projection.service.js +385 -0
  181. package/src/omni-relation-projection.service.js.map +1 -0
  182. package/src/omni-relation-projection.service.ts +610 -0
  183. package/src/omni-relation-semantics.d.ts +11 -0
  184. package/src/omni-relation-semantics.js +34 -0
  185. package/src/omni-relation-semantics.js.map +1 -0
  186. package/src/omni-relation-semantics.ts +52 -0
  187. package/src/omni-relation-status.enum.d.ts +5 -0
  188. package/src/omni-relation-status.enum.js +14 -0
  189. package/src/omni-relation-status.enum.js.map +1 -0
  190. package/src/omni-relation-status.enum.ts +11 -0
  191. package/src/omni-relation.backend.d.ts +2 -0
  192. package/src/omni-relation.backend.js +17 -0
  193. package/src/omni-relation.backend.js.map +1 -0
  194. package/src/omni-relation.backend.ts +21 -0
  195. package/src/omni-relation.dto.d.ts +28 -0
  196. package/src/omni-relation.dto.js +153 -0
  197. package/src/omni-relation.dto.js.map +1 -0
  198. package/src/omni-relation.dto.ts +142 -0
  199. package/src/omni-relation.service.d.ts +19 -0
  200. package/src/omni-relation.service.js +76 -0
  201. package/src/omni-relation.service.js.map +1 -0
  202. package/src/omni-relation.service.ts +159 -0
  203. package/src/omni-scope.d.ts +33 -0
  204. package/src/omni-scope.js +91 -0
  205. package/src/omni-scope.js.map +1 -0
  206. package/src/omni-scope.ts +161 -0
  207. package/src/omni-scoped.backend.d.ts +21 -0
  208. package/src/omni-scoped.backend.js +47 -0
  209. package/src/omni-scoped.backend.js.map +1 -0
  210. package/src/omni-scoped.backend.ts +110 -0
  211. package/src/omni-scoped.repository.d.ts +10 -0
  212. package/src/omni-scoped.repository.js +26 -0
  213. package/src/omni-scoped.repository.js.map +1 -0
  214. package/src/omni-scoped.repository.ts +34 -0
  215. package/src/omni-scoped.service.d.ts +36 -0
  216. package/src/omni-scoped.service.js +171 -0
  217. package/src/omni-scoped.service.js.map +1 -0
  218. package/src/omni-scoped.service.ts +338 -0
  219. package/src/omnikernel.diagnostics.d.ts +16 -0
  220. package/src/omnikernel.diagnostics.js +50 -0
  221. package/src/omnikernel.diagnostics.js.map +1 -0
  222. package/src/omnikernel.diagnostics.ts +83 -0
  223. package/src/omnikernel.module.d.ts +5 -0
  224. package/src/omnikernel.module.js +87 -0
  225. package/src/omnikernel.module.js.map +1 -0
  226. package/src/omnikernel.module.ts +97 -0
  227. package/src/omnikernel.query.service.d.ts +17 -0
  228. package/src/omnikernel.query.service.js +85 -0
  229. package/src/omnikernel.query.service.js.map +1 -0
  230. package/src/omnikernel.query.service.ts +108 -0
@@ -0,0 +1,294 @@
1
+ import {
2
+ compileProjectionUniqueConstraintPredicate,
3
+ createProjectionSchemaOptions,
4
+ defineProjectionResource,
5
+ getProjectionReferenceColumnNames,
6
+ getProjectionReferenceIndexName,
7
+ getProjectionReferenceTargetColumnNames,
8
+ getProjectionUniqueConstraintColumnNames,
9
+ type ProjectionDialect,
10
+ type ProjectionResourceDefinition,
11
+ } from '@nest-yalc-2/crud-gen';
12
+ import {
13
+ Column,
14
+ Entity,
15
+ ForeignKey,
16
+ Index,
17
+ PrimaryColumn,
18
+ Table,
19
+ type EntityTarget,
20
+ type EntitySchemaColumnOptions,
21
+ type ObjectLiteral,
22
+ } from 'typeorm';
23
+ import { OmniRecordEntity } from './base/omni-record.entity.js';
24
+ import { OmniRecordStatus } from './omni-record-status.enum.js';
25
+
26
+ export interface OmniExtensionProjectionDefinition extends ProjectionResourceDefinition {
27
+ /**
28
+ * Immutable metadata written only to the Omni record that owns every
29
+ * extension row. The extension table never mirrors these values.
30
+ */
31
+ owner: {
32
+ kind: string;
33
+ title: string;
34
+ status: OmniRecordStatus;
35
+ schema: {
36
+ id: string;
37
+ version: number;
38
+ };
39
+ };
40
+ }
41
+
42
+ function assertNonEmptyString(
43
+ value: unknown,
44
+ label: string,
45
+ max: number,
46
+ ): void {
47
+ if (
48
+ typeof value !== 'string' ||
49
+ value.trim().length === 0 ||
50
+ value.length > max
51
+ ) {
52
+ throw new TypeError(
53
+ `${label} must be a non-empty string up to ${max} characters.`,
54
+ );
55
+ }
56
+ }
57
+
58
+ /**
59
+ * Defines an owner-extension projection. The inherited projection metadata
60
+ * remains the single source for public fields, codecs, JSON paths, and query
61
+ * capabilities; the extra owner block fixes Omni record metadata server-side.
62
+ */
63
+ export function defineOmniExtensionProjection<
64
+ TDefinition extends OmniExtensionProjectionDefinition,
65
+ >(definition: TDefinition): Readonly<TDefinition> {
66
+ assertNonEmptyString(definition.owner?.kind, 'Omni extension owner kind', 64);
67
+ assertNonEmptyString(
68
+ definition.owner?.title,
69
+ 'Omni extension owner title',
70
+ 255,
71
+ );
72
+ assertNonEmptyString(
73
+ definition.owner?.schema?.id,
74
+ 'Omni extension owner schema id',
75
+ 128,
76
+ );
77
+ if (
78
+ !Number.isInteger(definition.owner?.schema?.version) ||
79
+ definition.owner.schema.version < 1 ||
80
+ definition.owner.schema.version > 2_147_483_647
81
+ ) {
82
+ throw new TypeError(
83
+ 'Omni extension owner schema version must be a positive signed 32-bit integer.',
84
+ );
85
+ }
86
+ if (!Object.values(OmniRecordStatus).includes(definition.owner.status)) {
87
+ throw new TypeError(
88
+ 'Omni extension owner status must be an Omni record status.',
89
+ );
90
+ }
91
+
92
+ return defineProjectionResource(definition);
93
+ }
94
+
95
+ function extensionSchema(
96
+ definition: OmniExtensionProjectionDefinition,
97
+ dialect: ProjectionDialect,
98
+ ): {
99
+ columns: Record<string, EntitySchemaColumnOptions>;
100
+ indices: ReturnType<typeof createProjectionSchemaOptions>['indices'];
101
+ foreignKeys: Array<{
102
+ name: string;
103
+ columnNames: string[];
104
+ referencedTableName: string;
105
+ referencedColumnNames: string[];
106
+ onDelete: 'RESTRICT' | 'NO ACTION';
107
+ }>;
108
+ } {
109
+ const projectionSchema = createProjectionSchemaOptions(definition, dialect);
110
+ const { [definition.revision.column]: _ownerRevision, ...columns } =
111
+ projectionSchema.columns;
112
+ const scopeColumn = definition.scope.column;
113
+ const identityColumn = definition.identity.column;
114
+
115
+ columns[scopeColumn] = {
116
+ ...columns[scopeColumn],
117
+ primary: true,
118
+ nullable: false,
119
+ };
120
+ columns[identityColumn] = {
121
+ ...columns[identityColumn],
122
+ primary: true,
123
+ nullable: false,
124
+ length: 36,
125
+ };
126
+
127
+ const references = definition.references ?? [];
128
+ const uniqueConstraints = definition.uniqueConstraints ?? [];
129
+ return {
130
+ columns,
131
+ indices: [
132
+ ...projectionSchema.indices,
133
+ ...references.map((reference) => ({
134
+ name: getProjectionReferenceIndexName(reference),
135
+ columns: getProjectionReferenceColumnNames(definition, reference),
136
+ unique: false,
137
+ })),
138
+ ...uniqueConstraints.map((constraint) => ({
139
+ name: constraint.name,
140
+ columns: getProjectionUniqueConstraintColumnNames(
141
+ definition,
142
+ constraint,
143
+ ),
144
+ unique: true,
145
+ where: compileProjectionUniqueConstraintPredicate(
146
+ definition,
147
+ constraint,
148
+ dialect.name,
149
+ ),
150
+ })),
151
+ ],
152
+ foreignKeys: references.map((reference) => ({
153
+ name: reference.name,
154
+ columnNames: getProjectionReferenceColumnNames(definition, reference),
155
+ referencedTableName: reference.target.tableName,
156
+ referencedColumnNames: getProjectionReferenceTargetColumnNames(reference),
157
+ onDelete: reference.onDelete,
158
+ })),
159
+ };
160
+ }
161
+
162
+ function migrationColumnType(type: EntitySchemaColumnOptions['type']): string {
163
+ if (type === String) return 'varchar';
164
+ if (type === Number) return 'integer';
165
+ if (type === Boolean) return 'boolean';
166
+ if (type === 'simple-json') return 'text';
167
+ if (typeof type === 'string') return type;
168
+ throw new TypeError('Omni extension projection column type is unsupported.');
169
+ }
170
+
171
+ /**
172
+ * Builds a TypeORM entity target for one extension table. This is deliberately
173
+ * a decorated class, rather than an EntitySchema: TypeORM builds EntitySchema
174
+ * metadata separately and cannot resolve an EntitySchema foreign key to the
175
+ * decorated OmniRecordEntity. The generated class keeps both sides in the
176
+ * same metadata pass, allowing the composite FK to be synchronized normally.
177
+ */
178
+ export function createOmniExtensionProjectionEntity<
179
+ Extension extends ObjectLiteral,
180
+ >(
181
+ definition: OmniExtensionProjectionDefinition,
182
+ dialect: ProjectionDialect,
183
+ ): EntityTarget<Extension> {
184
+ const { columns, indices, foreignKeys } = extensionSchema(
185
+ definition,
186
+ dialect,
187
+ );
188
+ const scopeColumn = definition.scope.column;
189
+ const identityColumn = definition.identity.column;
190
+
191
+ class OmniExtensionProjectionEntity {}
192
+
193
+ Entity(definition.tableName)(OmniExtensionProjectionEntity);
194
+ for (const [columnName, declared] of Object.entries(columns)) {
195
+ const options = { ...declared };
196
+ const primary = options.primary === true;
197
+ delete options.primary;
198
+ if (primary) {
199
+ PrimaryColumn(options.type as never, options as never)(
200
+ OmniExtensionProjectionEntity.prototype,
201
+ columnName,
202
+ );
203
+ } else if (typeof options.type === 'function') {
204
+ Column(options as never)(
205
+ OmniExtensionProjectionEntity.prototype,
206
+ columnName,
207
+ );
208
+ } else {
209
+ Column(options.type as never, options as never)(
210
+ OmniExtensionProjectionEntity.prototype,
211
+ columnName,
212
+ );
213
+ }
214
+ }
215
+ for (const index of indices) {
216
+ if (!index.name || !Array.isArray(index.columns)) {
217
+ throw new TypeError(
218
+ 'Omni extension projection indexes require a name and column list.',
219
+ );
220
+ }
221
+ Index(index.name, index.columns, {
222
+ unique: index.unique,
223
+ ...(index.where ? { where: index.where } : {}),
224
+ })(OmniExtensionProjectionEntity);
225
+ }
226
+ for (const foreignKey of foreignKeys) {
227
+ ForeignKey(
228
+ foreignKey.referencedTableName,
229
+ foreignKey.columnNames as never,
230
+ foreignKey.referencedColumnNames as never,
231
+ { name: foreignKey.name, onDelete: foreignKey.onDelete },
232
+ )(OmniExtensionProjectionEntity);
233
+ }
234
+ ForeignKey(
235
+ () => OmniRecordEntity,
236
+ [scopeColumn, identityColumn],
237
+ ['scopeId', 'guid'],
238
+ { onDelete: 'CASCADE' },
239
+ )(OmniExtensionProjectionEntity);
240
+
241
+ return OmniExtensionProjectionEntity as unknown as EntityTarget<Extension>;
242
+ }
243
+
244
+ /**
245
+ * Creates reviewed-migration DDL for an extension table. Applications pass
246
+ * the returned Table to QueryRunner.createTable; runtime CRUD never creates
247
+ * schema objects. JSON storage remains a ProjectionDialect concern.
248
+ */
249
+ export function createOmniExtensionProjectionTable(
250
+ definition: OmniExtensionProjectionDefinition,
251
+ dialect: ProjectionDialect,
252
+ ): Table {
253
+ const { columns, indices, foreignKeys } = extensionSchema(
254
+ definition,
255
+ dialect,
256
+ );
257
+ const scopeColumn = definition.scope.column;
258
+ const identityColumn = definition.identity.column;
259
+
260
+ return new Table({
261
+ name: definition.tableName,
262
+ columns: Object.entries(columns).map(([name, options]) => ({
263
+ name,
264
+ type: migrationColumnType(options.type),
265
+ isPrimary: options.primary === true,
266
+ isNullable: options.nullable === true,
267
+ ...(options.length !== undefined
268
+ ? { length: String(options.length) }
269
+ : {}),
270
+ })),
271
+ indices: indices.map((index) => {
272
+ if (!index.name || !Array.isArray(index.columns)) {
273
+ throw new TypeError(
274
+ 'Omni extension projection indexes require a name and column list.',
275
+ );
276
+ }
277
+ return {
278
+ name: index.name,
279
+ columnNames: index.columns,
280
+ isUnique: index.unique === true,
281
+ ...(index.where ? { where: index.where } : {}),
282
+ };
283
+ }),
284
+ foreignKeys: [
285
+ {
286
+ columnNames: [scopeColumn, identityColumn],
287
+ referencedTableName: 'omni-record',
288
+ referencedColumnNames: ['scopeId', 'guid'],
289
+ onDelete: 'CASCADE',
290
+ },
291
+ ...foreignKeys,
292
+ ],
293
+ });
294
+ }
@@ -0,0 +1,42 @@
1
+ import { type InjectionToken, type Provider } from '@nestjs/common';
2
+ import { type ICrudGenResourceFactoryResult, type ProjectionGraphqlTypes } from '@nest-yalc-2/crud-gen';
3
+ import type { ClassType } from '@nest-yalc-2/types/globals.d.js';
4
+ import type { EntityTarget, ObjectLiteral } from 'typeorm';
5
+ import type { OmniExtensionProjectionDefinition } from './omni-extension-projection.definition.js';
6
+ import type { OmniExtensionProjectionReaderRegistration, OmniProjectionReaderCatalogProvider } from './omni-projection.catalog.js';
7
+ import type { OmniProjectionLifecycleProvider } from './omni-projection.lifecycle.js';
8
+ export interface OmniExtensionProjectionGraphqlNames {
9
+ object: string;
10
+ create: string;
11
+ patch: string;
12
+ conditions: string;
13
+ }
14
+ export interface OmniExtensionProjectionRegistrationOptions<Extension extends ObjectLiteral, Api extends ObjectLiteral> {
15
+ entity: EntityTarget<Extension>;
16
+ apiModel: ClassType<Api>;
17
+ definition: OmniExtensionProjectionDefinition;
18
+ catalog?: OmniProjectionReaderCatalogProvider;
19
+ lifecycle?: OmniProjectionLifecycleProvider<OmniExtensionProjectionDefinition, Extension>;
20
+ dbConnection: string;
21
+ rest?: {
22
+ path?: string;
23
+ };
24
+ graphql?: {
25
+ prefix?: string;
26
+ names?: OmniExtensionProjectionGraphqlNames;
27
+ };
28
+ moduleRefToken?: InjectionToken;
29
+ }
30
+ export interface OmniExtensionProjectionRegistration<Extension extends ObjectLiteral, Api extends ObjectLiteral> {
31
+ definition: OmniExtensionProjectionDefinition;
32
+ entities: readonly EntityTarget<Extension>[];
33
+ reservedRecordKinds: readonly string[];
34
+ reader: OmniExtensionProjectionReaderRegistration<Extension>;
35
+ graphqlTypes: ProjectionGraphqlTypes;
36
+ resource: ICrudGenResourceFactoryResult<Api>;
37
+ controllers: readonly ClassType[];
38
+ providers: readonly Provider[];
39
+ serviceToken: string;
40
+ dataLoaderToken: string;
41
+ }
42
+ export declare function createOmniExtensionProjectionRegistration<Extension extends ObjectLiteral, Api extends ObjectLiteral>(options: OmniExtensionProjectionRegistrationOptions<Extension, Api>): OmniExtensionProjectionRegistration<Extension, Api>;
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createOmniExtensionProjectionRegistration = createOmniExtensionProjectionRegistration;
4
+ const common_1 = require("@nestjs/common");
5
+ const typeorm_1 = require("@nestjs/typeorm");
6
+ const crud_gen_1 = require("@nest-yalc-2/crud-gen");
7
+ const data_loader_1 = require("@nest-yalc-2/data-loader");
8
+ const event_manager_1 = require("@nest-yalc-2/event-manager");
9
+ const omni_record_entity_js_1 = require("./base/omni-record.entity.js");
10
+ const omni_extension_projection_service_js_1 = require("./omni-extension-projection.service.js");
11
+ const omni_scope_js_1 = require("./omni-scope.js");
12
+ function dialectFor(dataSource) {
13
+ if (dataSource.options.type !== 'sqlite' &&
14
+ dataSource.options.type !== 'postgres') {
15
+ throw new TypeError('Omni extension projections require a SQLite or PostgreSQL data source.');
16
+ }
17
+ return (0, crud_gen_1.createProjectionDialect)(dataSource.options.type);
18
+ }
19
+ function defaultGraphqlNames(apiModel) {
20
+ return {
21
+ object: apiModel.name,
22
+ create: `${apiModel.name}CreateInput`,
23
+ patch: `${apiModel.name}UpdateInput`,
24
+ conditions: `${apiModel.name}Condition`,
25
+ };
26
+ }
27
+ function createOmniExtensionProjectionRegistration(options) {
28
+ const graphqlTypes = (0, crud_gen_1.createProjectionGraphqlTypes)(options.definition, options.graphql?.names ?? defaultGraphqlNames(options.apiModel));
29
+ const serviceToken = (0, crud_gen_1.getServiceToken)(options.apiModel);
30
+ const dataLoaderToken = (0, data_loader_1.getDataloaderToken)(options.apiModel);
31
+ const resource = (0, crud_gen_1.CrudGenResourceFactory)({
32
+ entityModel: options.apiModel,
33
+ backend: false,
34
+ graphql: {
35
+ resolver: {
36
+ dto: graphqlTypes.object,
37
+ input: {
38
+ create: graphqlTypes.create,
39
+ update: graphqlTypes.patch,
40
+ conditions: graphqlTypes.conditions,
41
+ },
42
+ ...(options.graphql?.prefix ? { prefix: options.graphql.prefix } : {}),
43
+ ...(options.moduleRefToken !== undefined
44
+ ? { moduleRefToken: options.moduleRefToken }
45
+ : {}),
46
+ queries: {
47
+ getResource: { idName: options.definition.identity.column },
48
+ },
49
+ },
50
+ serviceToken,
51
+ dataLoaderToken,
52
+ },
53
+ rest: {
54
+ dto: graphqlTypes.object,
55
+ serialize: true,
56
+ ...(options.rest?.path ? { path: options.rest.path } : {}),
57
+ idField: options.definition.identity.column,
58
+ serviceToken,
59
+ },
60
+ });
61
+ const reader = Object.freeze({
62
+ type: 'extension',
63
+ id: options.definition.id,
64
+ entity: options.entity,
65
+ definition: options.definition,
66
+ });
67
+ const serviceProvider = options.lifecycle && options.catalog
68
+ ? {
69
+ provide: serviceToken,
70
+ scope: common_1.Scope.REQUEST,
71
+ useFactory: (dataSource, scope, events, lifecycle, catalog) => new omni_extension_projection_service_js_1.OmniExtensionProjectionService(dataSource.getRepository(options.entity), dataSource.getRepository(omni_record_entity_js_1.OmniRecordEntity), dataSource, scope, dialectFor(dataSource), events, options.definition, lifecycle, catalog),
72
+ inject: [
73
+ (0, typeorm_1.getDataSourceToken)(options.dbConnection),
74
+ omni_scope_js_1.OmniScopeContext,
75
+ event_manager_1.YalcEventService,
76
+ options.lifecycle.token,
77
+ options.catalog.token,
78
+ ],
79
+ }
80
+ : options.lifecycle
81
+ ? {
82
+ provide: serviceToken,
83
+ scope: common_1.Scope.REQUEST,
84
+ useFactory: (dataSource, scope, events, lifecycle) => new omni_extension_projection_service_js_1.OmniExtensionProjectionService(dataSource.getRepository(options.entity), dataSource.getRepository(omni_record_entity_js_1.OmniRecordEntity), dataSource, scope, dialectFor(dataSource), events, options.definition, lifecycle),
85
+ inject: [
86
+ (0, typeorm_1.getDataSourceToken)(options.dbConnection),
87
+ omni_scope_js_1.OmniScopeContext,
88
+ event_manager_1.YalcEventService,
89
+ options.lifecycle.token,
90
+ ],
91
+ }
92
+ : {
93
+ provide: serviceToken,
94
+ scope: common_1.Scope.REQUEST,
95
+ useFactory: (dataSource, scope, events) => new omni_extension_projection_service_js_1.OmniExtensionProjectionService(dataSource.getRepository(options.entity), dataSource.getRepository(omni_record_entity_js_1.OmniRecordEntity), dataSource, scope, dialectFor(dataSource), events, options.definition, undefined),
96
+ inject: [
97
+ (0, typeorm_1.getDataSourceToken)(options.dbConnection),
98
+ omni_scope_js_1.OmniScopeContext,
99
+ event_manager_1.YalcEventService,
100
+ ],
101
+ };
102
+ const providers = [
103
+ serviceProvider,
104
+ {
105
+ provide: dataLoaderToken,
106
+ scope: common_1.Scope.REQUEST,
107
+ useFactory: (service, scope) => new data_loader_1.GQLDataLoader((0, data_loader_1.getFn)(service), 'guid', undefined, {
108
+ cacheKeyFn: (key) => scope.cacheKey(key),
109
+ }),
110
+ inject: [serviceToken, omni_scope_js_1.OmniScopeContext],
111
+ },
112
+ ...resource.providers,
113
+ ];
114
+ return Object.freeze({
115
+ definition: options.definition,
116
+ entities: Object.freeze([options.entity]),
117
+ reservedRecordKinds: Object.freeze([options.definition.owner.kind]),
118
+ reader,
119
+ graphqlTypes: Object.freeze(graphqlTypes),
120
+ resource,
121
+ controllers: Object.freeze([...resource.controllers]),
122
+ providers: Object.freeze(providers),
123
+ serviceToken,
124
+ dataLoaderToken,
125
+ });
126
+ }
127
+ //# sourceMappingURL=omni-extension-projection.resource.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"omni-extension-projection.resource.js","sourceRoot":"","sources":["omni-extension-projection.resource.ts"],"names":[],"mappings":";;AAiHA,8FAgKC;AAjRD,2CAA2E;AAC3E,6CAAqD;AACrD,oDAO+B;AAC/B,0DAIkC;AAClC,8DAA8D;AAG9D,wEAAgE;AAEhE,iGAAwF;AAMxF,mDAAmD;AA0DnD,SAAS,UAAU,CAAC,UAAsB;IACxC,IACE,UAAU,CAAC,OAAO,CAAC,IAAI,KAAK,QAAQ;QACpC,UAAU,CAAC,OAAO,CAAC,IAAI,KAAK,UAAU,EACtC,CAAC;QACD,MAAM,IAAI,SAAS,CACjB,wEAAwE,CACzE,CAAC;IACJ,CAAC;IACD,OAAO,IAAA,kCAAuB,EAAC,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;AAC1D,CAAC;AAED,SAAS,mBAAmB,CAC1B,QAAmB;IAEnB,OAAO;QACL,MAAM,EAAE,QAAQ,CAAC,IAAI;QACrB,MAAM,EAAE,GAAG,QAAQ,CAAC,IAAI,aAAa;QACrC,KAAK,EAAE,GAAG,QAAQ,CAAC,IAAI,aAAa;QACpC,UAAU,EAAE,GAAG,QAAQ,CAAC,IAAI,WAAW;KACxC,CAAC;AACJ,CAAC;AAQD,SAAgB,yCAAyC,CAIvD,OAAmE;IAEnE,MAAM,YAAY,GAAG,IAAA,uCAA4B,EAC/C,OAAO,CAAC,UAAU,EAClB,OAAO,CAAC,OAAO,EAAE,KAAK,IAAI,mBAAmB,CAAC,OAAO,CAAC,QAAQ,CAAC,CAChE,CAAC;IACF,MAAM,YAAY,GAAG,IAAA,0BAAe,EAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IACvD,MAAM,eAAe,GAAG,IAAA,gCAAkB,EAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC7D,MAAM,QAAQ,GAAG,IAAA,iCAAsB,EAAM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