@kurotako/ir 0.1.0 → 0.2.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.
package/dist/index.d.ts CHANGED
@@ -19,16 +19,18 @@ declare const ScalarTypeSchema: v.PicklistSchema<["string", "boolean", "int", "b
19
19
  declare const StringFormatSchema: v.PicklistSchema<["email", "url", "uuid", "cuid", "cuid2", "ulid", "datetime", "date", "time", "duration", "ipv4", "ipv6"], undefined>;
20
20
  declare const ReferentialActionSchema: v.PicklistSchema<["cascade", "restrict", "setNull", "setDefault", "noAction"], undefined>;
21
21
  declare const IndexTypeSchema: v.PicklistSchema<["btree", "hash", "gin", "gist", "brin", "spgist"], undefined>;
22
- declare const FieldTypeSchema: v.VariantSchema<"kind", [v.ObjectSchema<{
23
- readonly kind: v.LiteralSchema<"scalar", undefined>;
24
- readonly scalar: v.PicklistSchema<["string", "boolean", "int", "bigint", "float", "decimal", "date", "datetime", "uuid", "bytes", "json"], undefined>;
25
- }, undefined>, v.ObjectSchema<{
26
- readonly kind: v.LiteralSchema<"enum", undefined>;
27
- readonly ref: v.StringSchema<undefined>;
28
- }, undefined>, v.ObjectSchema<{
29
- readonly kind: v.LiteralSchema<"unknown", undefined>;
30
- readonly hint: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
31
- }, undefined>], undefined>;
22
+ /**
23
+ * Recursive because of the `union` variant — wrapped in `v.lazy` with an explicit
24
+ * `v.GenericSchema<FieldType>` annotation, exactly like `JsonValueSchema`. The
25
+ * schema does **not** enforce `variants.length >= 2`: degenerate unions are
26
+ * tolerated on read and normalised in the cross-reference pass.
27
+ */
28
+ declare const FieldTypeSchema: v.GenericSchema<FieldType>;
29
+ declare const TypeAliasSchema: v.ObjectSchema<{
30
+ readonly name: v.StringSchema<undefined>;
31
+ readonly type: v.GenericSchema<FieldType>;
32
+ readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
33
+ }, undefined>;
32
34
  declare const ConstraintsSchema: v.ObjectSchema<{
33
35
  readonly min: v.OptionalSchema<v.NumberSchema<undefined>, undefined>;
34
36
  readonly max: v.OptionalSchema<v.NumberSchema<undefined>, undefined>;
@@ -48,16 +50,7 @@ declare const DefaultValueSchema: v.VariantSchema<"kind", [v.ObjectSchema<{
48
50
  }, undefined>], undefined>;
49
51
  declare const FieldSchema: v.ObjectSchema<{
50
52
  readonly name: v.StringSchema<undefined>;
51
- readonly type: v.VariantSchema<"kind", [v.ObjectSchema<{
52
- readonly kind: v.LiteralSchema<"scalar", undefined>;
53
- readonly scalar: v.PicklistSchema<["string", "boolean", "int", "bigint", "float", "decimal", "date", "datetime", "uuid", "bytes", "json"], undefined>;
54
- }, undefined>, v.ObjectSchema<{
55
- readonly kind: v.LiteralSchema<"enum", undefined>;
56
- readonly ref: v.StringSchema<undefined>;
57
- }, undefined>, v.ObjectSchema<{
58
- readonly kind: v.LiteralSchema<"unknown", undefined>;
59
- readonly hint: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
60
- }, undefined>], undefined>;
53
+ readonly type: v.GenericSchema<FieldType>;
61
54
  readonly list: v.BooleanSchema<undefined>;
62
55
  readonly optional: v.BooleanSchema<undefined>;
63
56
  readonly nullable: v.BooleanSchema<undefined>;
@@ -128,16 +121,7 @@ declare const EntitySchema: v.ObjectSchema<{
128
121
  readonly name: v.StringSchema<undefined>;
129
122
  readonly fields: v.ArraySchema<v.ObjectSchema<{
130
123
  readonly name: v.StringSchema<undefined>;
131
- readonly type: v.VariantSchema<"kind", [v.ObjectSchema<{
132
- readonly kind: v.LiteralSchema<"scalar", undefined>;
133
- readonly scalar: v.PicklistSchema<["string", "boolean", "int", "bigint", "float", "decimal", "date", "datetime", "uuid", "bytes", "json"], undefined>;
134
- }, undefined>, v.ObjectSchema<{
135
- readonly kind: v.LiteralSchema<"enum", undefined>;
136
- readonly ref: v.StringSchema<undefined>;
137
- }, undefined>, v.ObjectSchema<{
138
- readonly kind: v.LiteralSchema<"unknown", undefined>;
139
- readonly hint: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
140
- }, undefined>], undefined>;
124
+ readonly type: v.GenericSchema<FieldType>;
141
125
  readonly list: v.BooleanSchema<undefined>;
142
126
  readonly optional: v.BooleanSchema<undefined>;
143
127
  readonly nullable: v.BooleanSchema<undefined>;
@@ -207,16 +191,7 @@ declare const SourceIrSchema: v.ObjectSchema<{
207
191
  readonly name: v.StringSchema<undefined>;
208
192
  readonly fields: v.ArraySchema<v.ObjectSchema<{
209
193
  readonly name: v.StringSchema<undefined>;
210
- readonly type: v.VariantSchema<"kind", [v.ObjectSchema<{
211
- readonly kind: v.LiteralSchema<"scalar", undefined>;
212
- readonly scalar: v.PicklistSchema<["string", "boolean", "int", "bigint", "float", "decimal", "date", "datetime", "uuid", "bytes", "json"], undefined>;
213
- }, undefined>, v.ObjectSchema<{
214
- readonly kind: v.LiteralSchema<"enum", undefined>;
215
- readonly ref: v.StringSchema<undefined>;
216
- }, undefined>, v.ObjectSchema<{
217
- readonly kind: v.LiteralSchema<"unknown", undefined>;
218
- readonly hint: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
219
- }, undefined>], undefined>;
194
+ readonly type: v.GenericSchema<FieldType>;
220
195
  readonly list: v.BooleanSchema<undefined>;
221
196
  readonly optional: v.BooleanSchema<undefined>;
222
197
  readonly nullable: v.BooleanSchema<undefined>;
@@ -288,6 +263,11 @@ declare const SourceIrSchema: v.ObjectSchema<{
288
263
  readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
289
264
  readonly dbName: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
290
265
  }, undefined>, undefined>;
266
+ readonly typeAliases: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ObjectSchema<{
267
+ readonly name: v.StringSchema<undefined>;
268
+ readonly type: v.GenericSchema<FieldType>;
269
+ readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
270
+ }, undefined>, undefined>, undefined>;
291
271
  }, undefined>;
292
272
  declare const IrSchema: v.ObjectSchema<{
293
273
  readonly irVersion: v.StringSchema<undefined>;
@@ -299,16 +279,7 @@ declare const IrSchema: v.ObjectSchema<{
299
279
  readonly name: v.StringSchema<undefined>;
300
280
  readonly fields: v.ArraySchema<v.ObjectSchema<{
301
281
  readonly name: v.StringSchema<undefined>;
302
- readonly type: v.VariantSchema<"kind", [v.ObjectSchema<{
303
- readonly kind: v.LiteralSchema<"scalar", undefined>;
304
- readonly scalar: v.PicklistSchema<["string", "boolean", "int", "bigint", "float", "decimal", "date", "datetime", "uuid", "bytes", "json"], undefined>;
305
- }, undefined>, v.ObjectSchema<{
306
- readonly kind: v.LiteralSchema<"enum", undefined>;
307
- readonly ref: v.StringSchema<undefined>;
308
- }, undefined>, v.ObjectSchema<{
309
- readonly kind: v.LiteralSchema<"unknown", undefined>;
310
- readonly hint: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
311
- }, undefined>], undefined>;
282
+ readonly type: v.GenericSchema<FieldType>;
312
283
  readonly list: v.BooleanSchema<undefined>;
313
284
  readonly optional: v.BooleanSchema<undefined>;
314
285
  readonly nullable: v.BooleanSchema<undefined>;
@@ -380,6 +351,11 @@ declare const IrSchema: v.ObjectSchema<{
380
351
  readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
381
352
  readonly dbName: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
382
353
  }, undefined>, undefined>;
354
+ readonly typeAliases: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ObjectSchema<{
355
+ readonly name: v.StringSchema<undefined>;
356
+ readonly type: v.GenericSchema<FieldType>;
357
+ readonly doc: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
358
+ }, undefined>, undefined>, undefined>;
383
359
  }, undefined>, undefined>;
384
360
  }, undefined>;
385
361
 
@@ -389,12 +365,38 @@ declare const IrSchema: v.ObjectSchema<{
389
365
  * hand-written interface here.
390
366
  */
391
367
 
368
+ /**
369
+ * Hand-written because `FieldTypeSchema` is recursive (`union` variant) and
370
+ * `v.lazy` needs an explicit type — same pattern as `JsonValue`.
371
+ * `ref` is a bare name resolved against the same source (`entities` then
372
+ * `typeAliases`); there is no namespace qualifier in v1.
373
+ */
374
+ type FieldType = {
375
+ kind: 'scalar';
376
+ scalar: ScalarType;
377
+ } | {
378
+ kind: 'enum';
379
+ ref: string;
380
+ } | {
381
+ kind: 'unknown';
382
+ hint?: string;
383
+ } | {
384
+ kind: 'ref';
385
+ ref: string;
386
+ } | {
387
+ kind: 'union';
388
+ variants: FieldType[];
389
+ discriminator?: {
390
+ propertyName: string;
391
+ mapping?: Record<string, string>;
392
+ };
393
+ };
392
394
  type IR = v.InferOutput<typeof IrSchema>;
393
395
  type SourceIR = v.InferOutput<typeof SourceIrSchema>;
394
396
  type Entity = v.InferOutput<typeof EntitySchema>;
395
397
  type Field = v.InferOutput<typeof FieldSchema>;
396
- type FieldType = v.InferOutput<typeof FieldTypeSchema>;
397
398
  type ScalarType = v.InferOutput<typeof ScalarTypeSchema>;
399
+ type TypeAlias = v.InferOutput<typeof TypeAliasSchema>;
398
400
  type StringFormat = v.InferOutput<typeof StringFormatSchema>;
399
401
  type ReferentialAction = v.InferOutput<typeof ReferentialActionSchema>;
400
402
  type IndexType = v.InferOutput<typeof IndexTypeSchema>;
@@ -407,7 +409,7 @@ type EnumValue = v.InferOutput<typeof EnumValueSchema>;
407
409
  type IndexDef = v.InferOutput<typeof IndexDefSchema>;
408
410
  type CompositeUnique = v.InferOutput<typeof CompositeUniqueSchema>;
409
411
 
410
- type IrIssueCode = 'version_incompatible' | 'namespace_key_mismatch' | 'entity_key_mismatch' | 'duplicate_field' | 'duplicate_enum_value' | 'unresolved_enum_ref' | 'unresolved_field_ref' | 'unresolved_relation_target' | 'unresolved_back_relation' | 'invalid_constraint' | 'invalid_regex' | 'shape';
412
+ type IrIssueCode = 'version_incompatible' | 'namespace_key_mismatch' | 'entity_key_mismatch' | 'duplicate_field' | 'duplicate_enum_value' | 'unresolved_enum_ref' | 'unresolved_field_ref' | 'unresolved_relation_target' | 'unresolved_back_relation' | 'unresolved_ref' | 'unresolved_type_alias' | 'type_alias_key_mismatch' | 'degenerate_union' | 'union_cycle' | 'invalid_constraint' | 'invalid_regex' | 'shape';
411
413
  interface IrIssue {
412
414
  path: string;
413
415
  code: IrIssueCode;
@@ -416,6 +418,7 @@ interface IrIssue {
416
418
  type IrValidation<T> = {
417
419
  ok: true;
418
420
  value: T;
421
+ info?: IrIssue[];
419
422
  } | {
420
423
  ok: false;
421
424
  issues: IrIssue[];
@@ -443,9 +446,26 @@ interface EnumBuilder {
443
446
  doc(text: string): this;
444
447
  dbName(name: string): this;
445
448
  }
449
+ /** Type-setters shared by `UnionBuilder` and `TypeAliasBuilder`. */
450
+ interface TypeVariantBuilder {
451
+ scalar(t: ScalarType): this;
452
+ enum(ref: string): this;
453
+ ref(name: string): this;
454
+ union(build: (u: UnionBuilder) => void): this;
455
+ unknown(hint?: string): this;
456
+ }
457
+ interface UnionBuilder extends TypeVariantBuilder {
458
+ /** `mapping` values must name a `ref` variant of this union. */
459
+ discriminator(propertyName: string, mapping?: Record<string, string>): this;
460
+ }
461
+ interface TypeAliasBuilder extends TypeVariantBuilder {
462
+ doc(text: string): this;
463
+ }
446
464
  interface FieldBuilder {
447
465
  scalar(t: ScalarType): this;
448
466
  enum(ref: string): this;
467
+ ref(name: string): this;
468
+ union(build: (u: UnionBuilder) => void): this;
449
469
  unknown(hint?: string): this;
450
470
  list(): this;
451
471
  optional(): this;
@@ -492,6 +512,7 @@ interface EntityBuilder {
492
512
  interface SourceIrBuilder {
493
513
  addEnum(name: string, def: (e: EnumBuilder) => void): this;
494
514
  addEntity(name: string, def: (e: EntityBuilder) => void): this;
515
+ addTypeAlias(name: string, def: (t: TypeAliasBuilder) => void): this;
495
516
  build(): SourceIR;
496
517
  }
497
518
  declare function createSourceIR(init: {
@@ -510,6 +531,35 @@ declare function getSource(ir: IR, namespace: string): SourceIR | undefined;
510
531
  declare function resolveEntity(ir: IR, namespace: string, name: string): Entity | undefined;
511
532
  /** Entity-local enums shadow source-level enums of the same name. */
512
533
  declare function resolveEnum(source: SourceIR, entity: Entity | undefined, ref: string): EnumDef | undefined;
534
+ /**
535
+ * Resolve a `{ kind: 'ref' }` name against the same source: an entity first,
536
+ * then a type alias. Same-namespace only — v1 has no namespace qualifier.
537
+ */
538
+ declare function resolveRef(source: SourceIR, ref: string): Entity | TypeAlias | undefined;
539
+ declare function resolveTypeAlias(source: SourceIR, name: string): TypeAlias | undefined;
540
+ declare function iterTypeAliases(ir: IR): Iterable<{
541
+ namespace: string;
542
+ alias: TypeAlias;
543
+ }>;
544
+ /** Every `{ kind: 'ref' }` name reachable from a field type (through unions). */
545
+ declare function collectRefNames(type: FieldType, into?: Set<string>): Set<string>;
546
+ /**
547
+ * Names of entities / type aliases that take part in at least one `ref` cycle
548
+ * within `source` — following field-type `ref`, alias `type` `ref` and union
549
+ * variant `ref` edges. Mirrors the informational `union_cycle` pass in
550
+ * `validate.ts`; a generator consumes this (through `GenerateContext.cycles`)
551
+ * to decide which references must be `z.lazy`-wrapped / widened rather than
552
+ * emitted as a bare forward reference.
553
+ */
554
+ declare function refCycleMembers(source: SourceIR): Set<string>;
555
+ /**
556
+ * Flatten nested unions into a single variant list and drop
557
+ * structurally-identical duplicates, preserving first-seen order. The
558
+ * discriminator of a nested union is not carried up.
559
+ */
560
+ declare function flattenUnion(type: Extract<FieldType, {
561
+ kind: 'union';
562
+ }>): FieldType[];
513
563
  /**
514
564
  * A relation is cross-source when its target namespace is anything other than
515
565
  * the entity's own namespace — including the "absent" (empty) namespace, which
@@ -566,11 +616,11 @@ declare function scalarTsType(type: FieldType): string;
566
616
  * (independent versioning): a single string, bumped only on a breaking change
567
617
  * to the format itself.
568
618
  */
569
- declare const IR_VERSION = "1";
619
+ declare const IR_VERSION = "2";
570
620
  /**
571
621
  * Whether an IR produced against `irVersion` can be consumed by this build.
572
622
  * v1 rule: strict equality.
573
623
  */
574
624
  declare function isCompatible(irVersion: string): boolean;
575
625
 
576
- export { type CompositeUnique, CompositeUniqueSchema, type Constraints, ConstraintsSchema, type DefaultValue, DefaultValueSchema, type Entity, type EntityBuilder, EntitySchema, type EnumBuilder, type EnumDef, EnumDefSchema, type EnumValue, EnumValueSchema, type Field, type FieldBuilder, FieldSchema, type FieldType, FieldTypeSchema, type IR, IR_VERSION, type IndexDef, IndexDefSchema, type IndexType, IndexTypeSchema, IrBuildError, type IrIssue, type IrIssueCode, IrSchema, type IrValidation, IrValidationError, type JsonValue, JsonValueSchema, type ReferentialAction, ReferentialActionSchema, type Relation, type RelationBuilder, RelationSchema, type RelationTarget, RelationTargetSchema, type ScalarTsType, type ScalarType, ScalarTypeSchema, type SourceIR, type SourceIrBuilder, SourceIrSchema, type StringFormat, StringFormatSchema, assertIR, assertSourceIR, createFields, createSourceIR, getSource, isCompatible, isCreateOptional, isCrossSource, isDbAssigned, iterEntities, iterFields, parseIR, primaryKeyFields, resolveEntity, resolveEnum, resolveRelationTarget, scalarTsType, updateFields, validateIR, validateSourceIR };
626
+ export { type CompositeUnique, CompositeUniqueSchema, type Constraints, ConstraintsSchema, type DefaultValue, DefaultValueSchema, type Entity, type EntityBuilder, EntitySchema, type EnumBuilder, type EnumDef, EnumDefSchema, type EnumValue, EnumValueSchema, type Field, type FieldBuilder, FieldSchema, type FieldType, FieldTypeSchema, type IR, IR_VERSION, type IndexDef, IndexDefSchema, type IndexType, IndexTypeSchema, IrBuildError, type IrIssue, type IrIssueCode, IrSchema, type IrValidation, IrValidationError, type JsonValue, JsonValueSchema, type ReferentialAction, ReferentialActionSchema, type Relation, type RelationBuilder, RelationSchema, type RelationTarget, RelationTargetSchema, type ScalarTsType, type ScalarType, ScalarTypeSchema, type SourceIR, type SourceIrBuilder, SourceIrSchema, type StringFormat, StringFormatSchema, type TypeAlias, type TypeAliasBuilder, TypeAliasSchema, type TypeVariantBuilder, type UnionBuilder, assertIR, assertSourceIR, collectRefNames, createFields, createSourceIR, flattenUnion, getSource, isCompatible, isCreateOptional, isCrossSource, isDbAssigned, iterEntities, iterFields, iterTypeAliases, parseIR, primaryKeyFields, refCycleMembers, resolveEntity, resolveEnum, resolveRef, resolveRelationTarget, resolveTypeAlias, scalarTsType, updateFields, validateIR, validateSourceIR };