@danceroutine/tango-schema 1.1.3 → 1.3.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 (38) hide show
  1. package/dist/domain/Model.d.ts +17 -4
  2. package/dist/domain/index.d.ts +1 -1
  3. package/dist/domain-Cufz6y1q.js.map +1 -1
  4. package/dist/index.d.ts +2 -2
  5. package/dist/index.js +1 -1
  6. package/dist/model/Model.d.ts +5 -2
  7. package/dist/model/ModelAugmentorRegistry.d.ts +2 -2
  8. package/dist/model/ModelDefinition.d.ts +5 -3
  9. package/dist/model/decorators/Decorators.d.ts +54 -6
  10. package/dist/model/decorators/domain/DecoratedFieldKind.d.ts +6 -0
  11. package/dist/model/decorators/domain/ModelRef.d.ts +11 -0
  12. package/dist/model/decorators/domain/RelationDecoratedSchema.d.ts +24 -0
  13. package/dist/model/decorators/domain/RelationDecoratorConfig.d.ts +35 -0
  14. package/dist/model/decorators/{types.d.ts → domain/TangoFieldMeta.d.ts} +6 -5
  15. package/dist/model/decorators/domain/ZodTypeAny.d.ts +2 -0
  16. package/dist/model/decorators/index.d.ts +9 -2
  17. package/dist/model/{internal → fields}/FieldMetadataStore.d.ts +2 -1
  18. package/dist/model/fields/FinalizedStorageArtifacts.d.ts +11 -0
  19. package/dist/model/{inferFields.d.ts → fields/inferFieldsFromSchema.d.ts} +7 -2
  20. package/dist/model/index.d.ts +13 -3
  21. package/dist/model/index.js +2 -2
  22. package/dist/model/internal/InternalSchemaModel.d.ts +31 -0
  23. package/dist/model/registry/ModelRegistry.d.ts +35 -2
  24. package/dist/model/registry/index.d.ts +4 -0
  25. package/dist/model/relations/NormalizedRelationStorageDescriptor.d.ts +36 -0
  26. package/dist/model/relations/RelationBuilder.d.ts +19 -0
  27. package/dist/model/relations/RelationDescriptorNormalizer.d.ts +30 -0
  28. package/dist/model/relations/RelationSpec.d.ts +48 -0
  29. package/dist/model/relations/ResolvedRelationGraph.d.ts +43 -0
  30. package/dist/model/relations/ResolvedRelationGraphBuilder.d.ts +48 -0
  31. package/dist/model/relations/SchemaNaming.d.ts +11 -0
  32. package/dist/model/relations/index.d.ts +13 -0
  33. package/dist/model-YLW1ydkV.js +1144 -0
  34. package/dist/model-YLW1ydkV.js.map +1 -0
  35. package/package.json +3 -2
  36. package/dist/model/RelationBuilder.d.ts +0 -12
  37. package/dist/model-DI8lQH1W.js +0 -585
  38. package/dist/model-DI8lQH1W.js.map +0 -1
@@ -1,22 +1,35 @@
1
1
  import type { z } from 'zod';
2
2
  import type { ModelMetadata } from './ModelMetadata';
3
3
  import type { ModelWriteHooks } from './ModelWriteHooks';
4
+ import { InternalDecoratedFieldKind } from '../model/decorators/domain/DecoratedFieldKind';
5
+ import type { RelationDecoratedSchemaKind } from '../model/decorators/domain/RelationDecoratedSchema';
4
6
  declare const MODEL_AUGMENTATION_SCHEMA: unique symbol;
7
+ declare const MODEL_KEY: unique symbol;
5
8
  type ModelAugmentationCarrier<TSchema extends z.ZodObject<z.ZodRawShape>> = {
6
9
  readonly [MODEL_AUGMENTATION_SCHEMA]?: TSchema;
7
10
  };
11
+ type ModelKeyCarrier<TKey extends string> = {
12
+ readonly [MODEL_KEY]?: TKey;
13
+ };
14
+ type IsAny<TValue> = 0 extends 1 & TValue ? true : false;
8
15
  declare global {
9
- interface TangoSchemaModelAugmentations<TSchema extends z.ZodObject<z.ZodRawShape> = z.ZodObject<z.ZodRawShape>> {
16
+ interface TangoSchemaModelAugmentations<TSchema extends z.ZodObject<z.ZodRawShape> = z.ZodObject<z.ZodRawShape>, TKey extends string = string> {
10
17
  }
11
18
  }
12
- export interface ModelAugmentations<TSchema extends z.ZodObject<z.ZodRawShape> = z.ZodObject<z.ZodRawShape>> extends ModelAugmentationCarrier<TSchema>, TangoSchemaModelAugmentations<TSchema> {
19
+ type ManyToManyFieldKeys<TShape extends z.ZodRawShape> = {
20
+ [K in keyof TShape]: IsAny<TShape[K]> extends true ? never : [RelationDecoratedSchemaKind<TShape[K]>] extends [never] ? never : [RelationDecoratedSchemaKind<TShape[K]>] extends [typeof InternalDecoratedFieldKind.MANY_TO_MANY] ? K : never;
21
+ }[keyof TShape];
22
+ type PersistedShape<TShape extends z.ZodRawShape> = Omit<z.output<z.ZodObject<TShape>>, ManyToManyFieldKeys<TShape>>;
23
+ export type PersistedModelOutput<TSchema extends z.ZodObject<z.ZodRawShape>> = TSchema extends z.ZodObject<infer TShape> ? PersistedShape<TShape> : z.output<TSchema>;
24
+ export interface ModelAugmentations<TSchema extends z.ZodObject<z.ZodRawShape> = z.ZodObject<z.ZodRawShape>, TKey extends string = string> extends ModelAugmentationCarrier<TSchema>, ModelKeyCarrier<TKey>, TangoSchemaModelAugmentations<TSchema, TKey> {
13
25
  }
14
- export interface Model<TSchema extends z.ZodObject<z.ZodRawShape> = z.ZodObject<z.ZodRawShape>> extends ModelAugmentations<TSchema> {
26
+ export interface Model<TSchema extends z.ZodObject<z.ZodRawShape> = z.ZodObject<z.ZodRawShape>, TKey extends string = string> extends ModelAugmentations<TSchema, TKey> {
15
27
  metadata: ModelMetadata;
16
28
  schema: TSchema;
17
29
  /**
18
30
  * Model-owned write lifecycle hooks preserved from the definition.
19
31
  */
20
- hooks?: ModelWriteHooks<z.output<TSchema>>;
32
+ hooks?: ModelWriteHooks<PersistedModelOutput<TSchema>>;
21
33
  }
34
+ export type ModelKeyOf<TModel> = TModel extends Model<z.ZodObject<z.ZodRawShape>, infer TKey> ? TKey : never;
22
35
  export {};
@@ -9,5 +9,5 @@ export type { Field } from './Field';
9
9
  export type { IndexDef } from './IndexDef';
10
10
  export type { RelationDef } from './RelationDef';
11
11
  export type { ModelMetadata } from './ModelMetadata';
12
- export type { Model, ModelAugmentations } from './Model';
12
+ export type { Model, ModelAugmentations, ModelKeyOf, PersistedModelOutput } from './Model';
13
13
  export type { ModelHookModel, ModelWriteHookManager, ModelWriteHooks, BeforeCreateHookArgs, AfterCreateHookArgs, BeforeUpdateHookArgs, AfterUpdateHookArgs, BeforeDeleteHookArgs, AfterDeleteHookArgs, BeforeBulkCreateHookArgs, AfterBulkCreateHookArgs, } from './ModelWriteHooks';
@@ -1 +1 @@
1
- {"version":3,"file":"domain-Cufz6y1q.js","names":[],"sources":["../src/domain/index.ts"],"sourcesContent":["/**\n * Domain boundary barrel: centralizes this subdomain's public contract.\n */\n\nexport type { FieldType } from './FieldType';\nexport type { DeleteReferentialAction } from './DeleteReferentialAction';\nexport type { UpdateReferentialAction } from './UpdateReferentialAction';\nexport type { RelationType } from './RelationType';\nexport type { Field } from './Field';\nexport type { IndexDef } from './IndexDef';\nexport type { RelationDef } from './RelationDef';\nexport type { ModelMetadata } from './ModelMetadata';\nexport type { Model, ModelAugmentations } from './Model';\nexport type {\n ModelHookModel,\n ModelWriteHookManager,\n ModelWriteHooks,\n BeforeCreateHookArgs,\n AfterCreateHookArgs,\n BeforeUpdateHookArgs,\n AfterUpdateHookArgs,\n BeforeDeleteHookArgs,\n AfterDeleteHookArgs,\n BeforeBulkCreateHookArgs,\n AfterBulkCreateHookArgs,\n} from './ModelWriteHooks';\n"],"mappings":""}
1
+ {"version":3,"file":"domain-Cufz6y1q.js","names":[],"sources":["../src/domain/index.ts"],"sourcesContent":["/**\n * Domain boundary barrel: centralizes this subdomain's public contract.\n */\n\nexport type { FieldType } from './FieldType';\nexport type { DeleteReferentialAction } from './DeleteReferentialAction';\nexport type { UpdateReferentialAction } from './UpdateReferentialAction';\nexport type { RelationType } from './RelationType';\nexport type { Field } from './Field';\nexport type { IndexDef } from './IndexDef';\nexport type { RelationDef } from './RelationDef';\nexport type { ModelMetadata } from './ModelMetadata';\nexport type { Model, ModelAugmentations, ModelKeyOf, PersistedModelOutput } from './Model';\nexport type {\n ModelHookModel,\n ModelWriteHookManager,\n ModelWriteHooks,\n BeforeCreateHookArgs,\n AfterCreateHookArgs,\n BeforeUpdateHookArgs,\n AfterUpdateHookArgs,\n BeforeDeleteHookArgs,\n AfterDeleteHookArgs,\n BeforeBulkCreateHookArgs,\n AfterBulkCreateHookArgs,\n} from './ModelWriteHooks';\n"],"mappings":""}
package/dist/index.d.ts CHANGED
@@ -4,5 +4,5 @@
4
4
  */
5
5
  export * as domain from './domain/index';
6
6
  export * as model from './model/index';
7
- export type { DeleteReferentialAction, Field, FieldType, IndexDef, ModelHookModel, ModelAugmentations, ModelMetadata, ModelWriteHookManager, ModelWriteHooks, BeforeCreateHookArgs, AfterCreateHookArgs, BeforeUpdateHookArgs, AfterUpdateHookArgs, BeforeDeleteHookArgs, AfterDeleteHookArgs, BeforeBulkCreateHookArgs, AfterBulkCreateHookArgs, RelationDef, RelationType, UpdateReferentialAction, } from './domain/index';
8
- export { Model, RelationBuilder, ModelRegistry, registerModelAugmentor, Constraints, Decorators, Indexes, Meta, c, i, m, t, type ModelDefinition, } from './model/index';
7
+ export type { DeleteReferentialAction, Field, FieldType, IndexDef, ModelHookModel, ModelKeyOf, ModelAugmentations, ModelMetadata, PersistedModelOutput, ModelWriteHookManager, ModelWriteHooks, BeforeCreateHookArgs, AfterCreateHookArgs, BeforeUpdateHookArgs, AfterUpdateHookArgs, BeforeDeleteHookArgs, AfterDeleteHookArgs, BeforeBulkCreateHookArgs, AfterBulkCreateHookArgs, RelationDef, RelationType, UpdateReferentialAction, } from './domain/index';
8
+ export { Model, RelationBuilder, ModelRegistry, registerModelAugmentor, Constraints, Decorators, Indexes, Meta, c, i, m, t, type ModelDefinition, type ForeignKeyDecoratorConfig, type FieldDecoratorBuilder, type DecoratedFieldKind, type ManyToManyDecoratorConfig, type ModelRef, type ModelRefTarget, type OneToOneDecoratorConfig, type RelationDecoratedSchema, type TypedModelRef, } from './model/index';
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Constraints, Decorators, Indexes, Meta, Model, ModelRegistry, RelationBuilder, model_exports, registerModelAugmentor } from "./model-DI8lQH1W.js";
1
+ import { Constraints, Decorators, Indexes, Meta, Model, ModelRegistry, RelationBuilder, model_exports, registerModelAugmentor } from "./model-YLW1ydkV.js";
2
2
  import { domain_exports } from "./domain-Cufz6y1q.js";
3
3
 
4
4
  export { Constraints, Decorators, Indexes, Meta, Model, ModelRegistry, RelationBuilder, Constraints as c, domain_exports as domain, Indexes as i, Meta as m, model_exports as model, registerModelAugmentor, Decorators as t };
@@ -3,6 +3,9 @@ import type { Model as SchemaModel } from '../domain/Model';
3
3
  import type { ModelDefinition } from './ModelDefinition';
4
4
  /**
5
5
  * Creates a model definition with metadata and schema validation.
6
- * Automatically infers field types from Zod schema if fields are not explicitly provided.
6
+ * Automatically finalizes field types through the owning model registry.
7
7
  */
8
- export declare function Model<TSchema extends z.ZodObject<z.ZodRawShape>>(definition: ModelDefinition<TSchema>): SchemaModel<TSchema>;
8
+ export declare function Model<const TNamespace extends string, const TName extends string, TSchema extends z.ZodObject<z.ZodRawShape>>(definition: ModelDefinition<TSchema> & {
9
+ namespace: TNamespace;
10
+ name: TName;
11
+ }): SchemaModel<TSchema, `${TNamespace}/${TName}`>;
@@ -1,6 +1,6 @@
1
1
  import type { z } from 'zod';
2
2
  import type { Model } from '../domain/Model';
3
- export type ModelAugmentor = <TSchema extends z.ZodObject<z.ZodRawShape>>(model: Model<TSchema>) => void;
3
+ export type ModelAugmentor = <TSchema extends z.ZodObject<z.ZodRawShape>, TKey extends string>(model: Model<TSchema, TKey>) => void;
4
4
  /**
5
5
  * Register a model augmentor that runs for existing and future models.
6
6
  */
@@ -8,4 +8,4 @@ export declare function registerModelAugmentor(augmentor: ModelAugmentor): () =>
8
8
  /**
9
9
  * Apply all registered augmentors to a model before it is returned publicly.
10
10
  */
11
- export declare function applyModelAugmentors<TSchema extends z.ZodObject<z.ZodRawShape>>(model: Model<TSchema>): Model<TSchema>;
11
+ export declare function applyModelAugmentors<TSchema extends z.ZodObject<z.ZodRawShape>, TKey extends string>(model: Model<TSchema, TKey>): Model<TSchema, TKey>;
@@ -1,12 +1,14 @@
1
1
  import { z } from 'zod';
2
- import type { Field, IndexDef, RelationDef } from '../domain/index';
2
+ import type { Field, IndexDef, PersistedModelOutput, RelationDef } from '../domain/index';
3
3
  import type { ModelWriteHooks } from '../domain/index';
4
- import type { RelationBuilder } from './RelationBuilder';
4
+ import type { RelationBuilder } from './relations/RelationBuilder';
5
+ import type { ModelRegistry } from './registry/ModelRegistry';
5
6
  export interface ModelDefinition<TSchema extends z.ZodObject<z.ZodRawShape>> {
6
7
  namespace: string;
7
8
  name: string;
8
9
  table?: string;
9
10
  schema: TSchema;
11
+ registry?: ModelRegistry;
10
12
  fields?: Field[];
11
13
  indexes?: IndexDef[];
12
14
  relations?: (builder: RelationBuilder) => Record<string, RelationDef>;
@@ -17,5 +19,5 @@ export interface ModelDefinition<TSchema extends z.ZodObject<z.ZodRawShape>> {
17
19
  /**
18
20
  * Model-owned write lifecycle hooks that run inside `Model.objects`.
19
21
  */
20
- hooks?: ModelWriteHooks<z.output<TSchema>>;
22
+ hooks?: ModelWriteHooks<PersistedModelOutput<TSchema>>;
21
23
  }
@@ -1,18 +1,66 @@
1
1
  import { z } from 'zod';
2
- import type { ModelRef, ReferentialOptions, ZodTypeAny } from './types';
2
+ import type { ZodTypeAny } from './domain/ZodTypeAny';
3
+ import { type ModelRef, type ModelRefTarget, type TypedModelRef } from './domain/ModelRef';
4
+ import type { ReferentialOptions } from './domain/TangoFieldMeta';
5
+ import type { Model, ModelKeyOf } from '../../domain';
6
+ import type { ForeignKeyDecoratorConfig, ManyToManyDecoratorConfig, OneToOneDecoratorConfig } from './domain/RelationDecoratorConfig';
7
+ import type { RelationDecoratedSchema } from './domain/RelationDecoratedSchema';
8
+ export interface FieldDecoratorBuilder<TField extends ZodTypeAny> {
9
+ defaultValue(value: string | {
10
+ now: true;
11
+ } | null): FieldDecoratorBuilder<TField>;
12
+ dbDefault(value: string): FieldDecoratorBuilder<TField>;
13
+ dbColumn(name: string): FieldDecoratorBuilder<TField>;
14
+ dbIndex(): FieldDecoratorBuilder<TField>;
15
+ choices(values: readonly unknown[]): FieldDecoratorBuilder<TField>;
16
+ validators(...values: readonly ((value: unknown) => unknown)[]): FieldDecoratorBuilder<TField>;
17
+ helpText(text: string): FieldDecoratorBuilder<TField>;
18
+ errorMessages(map: Record<string, string>): FieldDecoratorBuilder<TField>;
19
+ build(): TField;
20
+ }
21
+ type ConfigName<TConfig> = TConfig extends {
22
+ name: infer TName extends string;
23
+ } ? TName : undefined;
24
+ type ConfigRelatedName<TConfig> = TConfig extends {
25
+ relatedName: infer TRelatedName extends string;
26
+ } ? TRelatedName : undefined;
3
27
  type UnaryFieldDecorator = {
4
28
  <T extends ZodTypeAny>(schema: T): T;
5
29
  <T extends ZodTypeAny>(): (input: T) => T;
6
30
  };
7
31
  type RelationshipDecorator = {
8
- <T extends ZodTypeAny>(target: ModelRef, schema: T, options?: ReferentialOptions): T;
9
- (target: ModelRef, options?: ReferentialOptions): z.ZodNumber;
32
+ <TRef extends ModelRef, T extends ZodTypeAny, const TConfig extends ForeignKeyDecoratorConfig<T> & {
33
+ field: T;
34
+ }>(target: TRef, config: TConfig): RelationDecoratedSchema<T, 'foreignKey', ModelRefTarget<TRef>, ConfigName<TConfig>, ConfigRelatedName<TConfig>>;
35
+ <TRef extends ModelRef, const TConfig extends ForeignKeyDecoratorConfig<z.ZodNumber> | undefined>(target: TRef, config?: TConfig): RelationDecoratedSchema<z.ZodNumber, 'foreignKey', ModelRefTarget<TRef>, ConfigName<TConfig>, ConfigRelatedName<TConfig>>;
36
+ /**
37
+ * @deprecated Use `t.foreignKey(target, { field: schema, ...options })` instead.
38
+ */
39
+ <T extends ZodTypeAny>(target: ModelRef, schema: T, options?: ReferentialOptions): RelationDecoratedSchema<T, 'foreignKey'>;
40
+ };
41
+ type OneToOneRelationshipDecorator = {
42
+ <TRef extends ModelRef, T extends ZodTypeAny, const TConfig extends OneToOneDecoratorConfig<T> & {
43
+ field: T;
44
+ }>(target: TRef, config: TConfig): RelationDecoratedSchema<T, 'oneToOne', ModelRefTarget<TRef>, ConfigName<TConfig>, ConfigRelatedName<TConfig>>;
45
+ <TRef extends ModelRef, const TConfig extends OneToOneDecoratorConfig<z.ZodNumber> | undefined>(target: TRef, config?: TConfig): RelationDecoratedSchema<z.ZodNumber, 'oneToOne', ModelRefTarget<TRef>, ConfigName<TConfig>, ConfigRelatedName<TConfig>>;
46
+ /**
47
+ * @deprecated Use `t.oneToOne(target, { field: schema, ...options })` instead.
48
+ */
49
+ <T extends ZodTypeAny>(target: ModelRef, schema: T, options?: ReferentialOptions): RelationDecoratedSchema<T, 'oneToOne'>;
10
50
  };
11
51
  type ManyToManyDecorator = {
12
- <T extends ZodTypeAny>(target: ModelRef, schema: T): T;
13
- (target: ModelRef): z.ZodArray<z.ZodNumber>;
52
+ <TRef extends ModelRef, T extends ZodTypeAny, const TConfig extends ManyToManyDecoratorConfig<T> & {
53
+ field: T;
54
+ }>(target: TRef, config: TConfig): RelationDecoratedSchema<T, 'manyToMany', ModelRefTarget<TRef>, ConfigName<TConfig>, undefined>;
55
+ <TRef extends ModelRef, const TConfig extends ManyToManyDecoratorConfig<z.ZodArray<z.ZodNumber>> | undefined>(target: TRef, config?: TConfig): RelationDecoratedSchema<z.ZodArray<z.ZodNumber>, 'manyToMany', ModelRefTarget<TRef>, ConfigName<TConfig>, undefined>;
56
+ /**
57
+ * @deprecated Use `t.manyToMany(target, { field: schema, name })` instead.
58
+ */
59
+ <T extends ZodTypeAny>(target: ModelRef, schema: T): RelationDecoratedSchema<T, 'manyToMany'>;
14
60
  };
15
61
  export interface TangoDecorators {
62
+ field: <T extends ZodTypeAny>(schema: T) => FieldDecoratorBuilder<T>;
63
+ modelRef: <TModel extends Model>(key: ModelKeyOf<TModel>) => TypedModelRef<TModel>;
16
64
  primaryKey: UnaryFieldDecorator;
17
65
  unique: UnaryFieldDecorator;
18
66
  null: UnaryFieldDecorator;
@@ -28,7 +76,7 @@ export interface TangoDecorators {
28
76
  helpText: <T extends ZodTypeAny>(schema: T, text: string) => T;
29
77
  errorMessages: <T extends ZodTypeAny>(schema: T, map: Record<string, string>) => T;
30
78
  foreignKey: RelationshipDecorator;
31
- oneToOne: RelationshipDecorator;
79
+ oneToOne: OneToOneRelationshipDecorator;
32
80
  manyToMany: ManyToManyDecorator;
33
81
  }
34
82
  export declare const Decorators: TangoDecorators;
@@ -0,0 +1,6 @@
1
+ export declare const InternalDecoratedFieldKind: {
2
+ readonly FOREIGN_KEY: "foreignKey";
3
+ readonly ONE_TO_ONE: "oneToOne";
4
+ readonly MANY_TO_MANY: "manyToMany";
5
+ };
6
+ export type DecoratedFieldKind = (typeof InternalDecoratedFieldKind)[keyof typeof InternalDecoratedFieldKind];
@@ -0,0 +1,11 @@
1
+ import type { Model, ModelKeyOf } from '../../../domain';
2
+ declare const TANGO_TYPED_MODEL_REF_TARGET: unique symbol;
3
+ export interface TypedModelRef<TModel extends Model = Model> {
4
+ readonly key: ModelKeyOf<TModel>;
5
+ readonly [TANGO_TYPED_MODEL_REF_TARGET]?: TModel;
6
+ }
7
+ export type ModelRef<TModel extends Model = Model> = string | TModel | (() => TModel) | TypedModelRef<TModel>;
8
+ export type ModelRefTarget<TRef> = TRef extends TypedModelRef<infer TModel> ? TModel : TRef extends () => infer TModel ? TModel extends Model ? TModel : never : TRef extends Model ? TRef : never;
9
+ export declare function createTypedModelRef<TModel extends Model>(key: ModelKeyOf<TModel>): TypedModelRef<TModel>;
10
+ export declare function isTypedModelRef(value: unknown): value is TypedModelRef;
11
+ export {};
@@ -0,0 +1,24 @@
1
+ import type { DecoratedFieldKind } from './DecoratedFieldKind';
2
+ import type { Model } from '../../../domain';
3
+ import type { ZodTypeAny } from './ZodTypeAny';
4
+ declare const TANGO_DECORATED_FIELD_KIND: unique symbol;
5
+ export type RelationDecoratedSchema<TSchema extends ZodTypeAny, TKind extends DecoratedFieldKind, TTargetModel extends Model | never = never, TName extends string | undefined = undefined, TRelatedName extends string | undefined = undefined> = TSchema & {
6
+ readonly [TANGO_DECORATED_FIELD_KIND]: {
7
+ readonly relationKind: TKind;
8
+ readonly targetModel: TTargetModel;
9
+ readonly name: TName;
10
+ readonly relatedName: TRelatedName;
11
+ };
12
+ readonly __tangoRelationDecoratedSchema: {
13
+ readonly relationKind: TKind;
14
+ readonly targetModel: TTargetModel;
15
+ readonly name: TName;
16
+ readonly relatedName: TRelatedName;
17
+ };
18
+ };
19
+ export type RelationDecoratedSchemaKind<TField> = TField extends {
20
+ readonly __tangoRelationDecoratedSchema: {
21
+ readonly relationKind: infer TKind extends DecoratedFieldKind;
22
+ };
23
+ } ? TKind : never;
24
+ export {};
@@ -0,0 +1,35 @@
1
+ import type { ZodTypeAny } from './ZodTypeAny';
2
+ import type { ReferentialOptions } from './TangoFieldMeta';
3
+ /**
4
+ * Config object for `t.foreignKey(...)`.
5
+ *
6
+ * The config object is the preferred second-argument form for relation
7
+ * decorators. Omit `field` to keep Tango's default schema inference.
8
+ */
9
+ export interface ForeignKeyDecoratorConfig<TField extends ZodTypeAny = ZodTypeAny> extends ReferentialOptions {
10
+ field?: TField;
11
+ name?: string;
12
+ relatedName?: string;
13
+ }
14
+ /**
15
+ * Config object for `t.oneToOne(...)`.
16
+ *
17
+ * The config object is the preferred second-argument form for relation
18
+ * decorators. Omit `field` to keep Tango's default schema inference.
19
+ */
20
+ export interface OneToOneDecoratorConfig<TField extends ZodTypeAny = ZodTypeAny> extends ReferentialOptions {
21
+ field?: TField;
22
+ name?: string;
23
+ relatedName?: string;
24
+ }
25
+ /**
26
+ * Config object for `t.manyToMany(...)`.
27
+ *
28
+ * The config object is the preferred second-argument form for relation
29
+ * decorators. Omit `field` to keep Tango's default schema inference.
30
+ */
31
+ export interface ManyToManyDecoratorConfig<TField extends ZodTypeAny = ZodTypeAny> {
32
+ field?: TField;
33
+ name?: string;
34
+ relatedName?: never;
35
+ }
@@ -1,6 +1,6 @@
1
- import type { z } from 'zod';
2
- import type { DeleteReferentialAction, Model, UpdateReferentialAction } from '../../domain/index';
3
- export type ModelRef = string | Model | (() => Model);
1
+ import type { DeleteReferentialAction, UpdateReferentialAction } from '../../../domain';
2
+ import type { DecoratedFieldKind } from './DecoratedFieldKind';
3
+ import type { ModelRef } from './ModelRef';
4
4
  export interface ReferentialOptions {
5
5
  column?: string;
6
6
  onDelete?: DeleteReferentialAction;
@@ -24,6 +24,7 @@ export interface TangoFieldMeta {
24
24
  target: ModelRef;
25
25
  options?: ReferentialOptions;
26
26
  };
27
- relationKind?: 'foreignKey' | 'oneToOne' | 'manyToMany';
27
+ relationKind?: DecoratedFieldKind;
28
+ forwardName?: string;
29
+ reverseName?: string;
28
30
  }
29
- export type ZodTypeAny = z.ZodTypeAny;
@@ -0,0 +1,2 @@
1
+ import type { z } from 'zod';
2
+ export type ZodTypeAny = z.ZodTypeAny;
@@ -2,5 +2,12 @@
2
2
  * Domain boundary barrel: centralizes this subdomain's public contract.
3
3
  */
4
4
  export { Decorators, Decorators as t } from './Decorators';
5
- export type { TangoDecorators } from './Decorators';
6
- export type { ModelRef, ReferentialOptions, TangoFieldMeta, ZodTypeAny } from './types';
5
+ export type { FieldDecoratorBuilder, TangoDecorators } from './Decorators';
6
+ export type { ModelRef, ModelRefTarget, TypedModelRef } from './domain/ModelRef';
7
+ export { createTypedModelRef, isTypedModelRef } from './domain/ModelRef';
8
+ export type { RelationDecoratedSchema } from './domain/RelationDecoratedSchema';
9
+ export { InternalDecoratedFieldKind } from './domain/DecoratedFieldKind';
10
+ export type { DecoratedFieldKind } from './domain/DecoratedFieldKind';
11
+ export type { ForeignKeyDecoratorConfig, OneToOneDecoratorConfig, ManyToManyDecoratorConfig, } from './domain/RelationDecoratorConfig';
12
+ export type { ReferentialOptions, TangoFieldMeta } from './domain/TangoFieldMeta';
13
+ export type { ZodTypeAny } from './domain/ZodTypeAny';
@@ -1,3 +1,4 @@
1
- import type { TangoFieldMeta, ZodTypeAny } from '../decorators/types';
1
+ import type { ZodTypeAny } from '../decorators/domain/ZodTypeAny';
2
+ import type { TangoFieldMeta } from '../decorators/domain/TangoFieldMeta';
2
3
  export declare function getFieldMetadata(schema: ZodTypeAny): TangoFieldMeta | undefined;
3
4
  export declare function setFieldMetadata(schema: ZodTypeAny, meta: TangoFieldMeta): void;
@@ -0,0 +1,11 @@
1
+ import type { Field } from '../../domain/index';
2
+ export interface FinalizedStorageModel {
3
+ key: string;
4
+ table: string;
5
+ fields: readonly Field[];
6
+ pk: string;
7
+ }
8
+ export interface FinalizedStorageArtifacts {
9
+ version: number;
10
+ byModel: ReadonlyMap<string, FinalizedStorageModel>;
11
+ }
@@ -1,8 +1,13 @@
1
1
  import { z } from 'zod';
2
- import type { Field } from '../domain/index';
3
- import { ModelRegistry } from './registry/ModelRegistry';
2
+ import type { Field } from '../../domain/index';
3
+ import type { ModelRef } from '../decorators/domain/ModelRef';
4
+ import { ModelRegistry } from '../registry/ModelRegistry';
4
5
  export type InferFieldsOptions = {
5
6
  registry?: ModelRegistry;
7
+ resolveReferenceTarget?: (target: ModelRef) => {
8
+ table: string;
9
+ pk: string;
10
+ };
6
11
  };
7
12
  /**
8
13
  * Infer Tango field metadata from a Zod object schema and any attached field decorators.
@@ -1,14 +1,24 @@
1
1
  /**
2
2
  * Domain boundary barrel: centralizes this subdomain's public contract.
3
+ *
4
+ * Tango keeps both flat exports and namespaced subdomain barrels here so
5
+ * callers can choose TS-native direct imports or Django-style drill-down
6
+ * access through the bundled `model` namespace at the package root.
3
7
  */
8
+ export * as decorators from './decorators/index';
9
+ export * as meta from './meta/index';
10
+ export * as constraints from './constraints/index';
11
+ export * as registry from './registry/index';
12
+ export * as relations from './relations/index';
4
13
  export type { ModelDefinition } from './ModelDefinition';
5
- export { RelationBuilder } from './RelationBuilder';
14
+ export { RelationBuilder } from './relations/index';
6
15
  export { Model } from './Model';
7
16
  export { registerModelAugmentor } from './ModelAugmentorRegistry';
8
17
  export { Decorators, t } from './decorators/index';
9
- export type { TangoDecorators } from './decorators/index';
18
+ export type { TangoDecorators, FieldDecoratorBuilder, DecoratedFieldKind, ModelRef, ModelRefTarget, RelationDecoratedSchema, TypedModelRef, ForeignKeyDecoratorConfig, OneToOneDecoratorConfig, ManyToManyDecoratorConfig, } from './decorators/index';
19
+ export { createTypedModelRef, InternalDecoratedFieldKind, isTypedModelRef } from './decorators/index';
10
20
  export { Meta, m } from './meta/index';
11
21
  export type { ModelConstraint, ModelMetaFragment } from './meta/index';
12
22
  export { Constraints, Indexes, c, i } from './constraints/index';
13
23
  export type { ConstraintDefinition } from './constraints/index';
14
- export { ModelRegistry } from './registry/ModelRegistry';
24
+ export { ModelRegistry } from './registry/index';
@@ -1,3 +1,3 @@
1
- import { Constraints, Decorators, Indexes, Meta, Model, ModelRegistry, RelationBuilder, registerModelAugmentor } from "../model-DI8lQH1W.js";
1
+ import { Constraints, Decorators, Indexes, InternalDecoratedFieldKind, Meta, Model, ModelRegistry, RelationBuilder, constraints_exports, createTypedModelRef, decorators_exports, isTypedModelRef, meta_exports, registerModelAugmentor, registry_exports, relations_exports } from "../model-YLW1ydkV.js";
2
2
 
3
- export { Constraints, Decorators, Indexes, Meta, Model, ModelRegistry, RelationBuilder, Constraints as c, Indexes as i, Meta as m, registerModelAugmentor, Decorators as t };
3
+ export { Constraints, Decorators, Indexes, InternalDecoratedFieldKind, Meta, Model, ModelRegistry, RelationBuilder, Constraints as c, constraints_exports as constraints, createTypedModelRef, decorators_exports as decorators, Indexes as i, isTypedModelRef, Meta as m, meta_exports as meta, registerModelAugmentor, registry_exports as registry, relations_exports as relations, Decorators as t };
@@ -0,0 +1,31 @@
1
+ import { z } from 'zod';
2
+ import type { Field, Model, ModelAugmentations, ModelMetadata, ModelWriteHooks, PersistedModelOutput, RelationDef } from '../../domain/index';
3
+ import type { ModelDefinition } from '../ModelDefinition';
4
+ import type { ModelRegistry } from '../registry/ModelRegistry';
5
+ import type { NormalizedRelationStorageDescriptor } from '../relations/NormalizedRelationStorageDescriptor';
6
+ type AnySchemaModel = Model<z.ZodObject<z.ZodRawShape>>;
7
+ type AnyInternalSchemaModel = InternalSchemaModel<z.ZodObject<z.ZodRawShape>>;
8
+ export declare class InternalSchemaModel<TSchema extends z.ZodObject<z.ZodRawShape>, TKey extends string = string> implements Model<TSchema, TKey> {
9
+ static readonly BRAND: "tango.schema.internal_schema_model";
10
+ readonly __tangoBrand: typeof InternalSchemaModel.BRAND;
11
+ readonly metadata: ModelMetadata;
12
+ readonly schema: TSchema;
13
+ readonly hooks?: ModelWriteHooks<PersistedModelOutput<TSchema>>;
14
+ readonly objects: ModelAugmentations<TSchema, TKey> extends {
15
+ readonly objects: infer TObject;
16
+ } ? TObject : never;
17
+ private readonly registry;
18
+ private readonly normalizedRelations;
19
+ private readonly explicitFields?;
20
+ private readonly explicitRelations?;
21
+ private constructor();
22
+ static create<TSchema extends z.ZodObject<z.ZodRawShape>>(definition: ModelDefinition<TSchema>, registry: ModelRegistry): InternalSchemaModel<TSchema>;
23
+ static isInternalSchemaModel(value: unknown): value is AnyInternalSchemaModel;
24
+ static getRegistryOwner(model: AnySchemaModel): ModelRegistry;
25
+ static getNormalizedRelations(model: AnySchemaModel): readonly NormalizedRelationStorageDescriptor[];
26
+ static getExplicitFields(model: AnySchemaModel): readonly Field[] | undefined;
27
+ static getExplicitRelations(model: AnySchemaModel): Readonly<Record<string, RelationDef>> | undefined;
28
+ private static validateDefinition;
29
+ private static require;
30
+ }
31
+ export {};
@@ -1,5 +1,7 @@
1
- import type { Model } from '../../domain/index';
2
- import type { ModelRef } from '../decorators/types';
1
+ import type { Field, Model } from '../../domain/index';
2
+ import { type ModelRef } from '../decorators/domain/ModelRef';
3
+ import type { FinalizedStorageArtifacts } from '../fields/FinalizedStorageArtifacts';
4
+ import type { ResolvedRelationGraph } from '../relations/ResolvedRelationGraph';
3
5
  /**
4
6
  * Registry that resolves Tango models by stable identity.
5
7
  *
@@ -9,10 +11,21 @@ import type { ModelRef } from '../decorators/types';
9
11
  export declare class ModelRegistry {
10
12
  private static globalRegistry?;
11
13
  private readonly models;
14
+ private version;
15
+ private storageCache?;
16
+ private relationGraphCache?;
12
17
  /**
13
18
  * Return the shared process-wide registry used by `Model(...)`.
14
19
  */
15
20
  static global(): ModelRegistry;
21
+ /**
22
+ * Return the registry currently bound to model construction work.
23
+ */
24
+ static active(): ModelRegistry;
25
+ /**
26
+ * Run work with a specific registry bound as the active construction target.
27
+ */
28
+ static runWithRegistry<T>(registry: ModelRegistry, work: () => Promise<T> | T): Promise<T>;
16
29
  /**
17
30
  * Register a model on the shared global registry.
18
31
  */
@@ -37,6 +50,10 @@ export declare class ModelRegistry {
37
50
  * Clear the shared registry, which is mainly useful in tests.
38
51
  */
39
52
  static clear(): void;
53
+ /**
54
+ * Return the owning registry for a model.
55
+ */
56
+ static getOwner(model: Model): ModelRegistry;
40
57
  /**
41
58
  * Register a model on this registry instance.
42
59
  */
@@ -57,6 +74,18 @@ export declare class ModelRegistry {
57
74
  * Resolve a string, callback, or direct model reference into a model object.
58
75
  */
59
76
  resolveRef(ref: ModelRef): Model;
77
+ /**
78
+ * Finalize storage-only artifacts for all models in this registry.
79
+ */
80
+ finalizeStorageArtifacts(): FinalizedStorageArtifacts;
81
+ /**
82
+ * Return finalized storage fields for a specific model.
83
+ */
84
+ getFinalizedFields(model: Model | string): readonly Field[];
85
+ /**
86
+ * Resolve the registry's relation graph from finalized storage artifacts.
87
+ */
88
+ getResolvedRelationGraph(): ResolvedRelationGraph;
60
89
  /**
61
90
  * Remove all registered models from this registry instance.
62
91
  */
@@ -65,4 +94,8 @@ export declare class ModelRegistry {
65
94
  * Return all registered models in insertion order.
66
95
  */
67
96
  values(): readonly Model[];
97
+ private bumpVersion;
98
+ private freezeFields;
99
+ private inferPrimaryKeyName;
100
+ private mergeStorageFields;
68
101
  }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Domain boundary barrel: centralizes this subdomain's public contract.
3
+ */
4
+ export { ModelRegistry } from './ModelRegistry';
@@ -0,0 +1,36 @@
1
+ import type { DeleteReferentialAction, UpdateReferentialAction } from '../../domain/index';
2
+ import type { ModelRef } from '../decorators/domain/ModelRef';
3
+ export declare const InternalNormalizedRelationOrigin: {
4
+ readonly FOREIGN_KEY: "foreignKey";
5
+ readonly ONE_TO_ONE: "oneToOne";
6
+ readonly MANY_TO_MANY: "manyToMany";
7
+ };
8
+ export type NormalizedRelationOrigin = (typeof InternalNormalizedRelationOrigin)[keyof typeof InternalNormalizedRelationOrigin];
9
+ /**
10
+ * Registry-independent relation descriptor produced immediately after model
11
+ * construction.
12
+ *
13
+ * This is the handoff object between relation authoring and relation
14
+ * resolution. It preserves field-authored relation intent in a normalized form
15
+ * without yet assigning public reverse names or resolved graph edges.
16
+ */
17
+ export interface NormalizedRelationStorageDescriptor {
18
+ edgeId: string;
19
+ sourceModelKey: string;
20
+ sourceSchemaFieldKey: string;
21
+ targetRef: ModelRef;
22
+ origin: NormalizedRelationOrigin;
23
+ localFieldName: string;
24
+ dbColumnName: string;
25
+ referencedTargetColumn?: string;
26
+ onDelete?: DeleteReferentialAction;
27
+ onUpdate?: UpdateReferentialAction;
28
+ unique?: boolean;
29
+ explicitForwardName?: string;
30
+ explicitReverseName?: string;
31
+ namingHint: string;
32
+ throughModelRef?: ModelRef;
33
+ throughSourceFieldName?: string;
34
+ throughTargetFieldName?: string;
35
+ provenance: 'field-decorator';
36
+ }
@@ -0,0 +1,19 @@
1
+ import type { RelationDef } from '../../domain/index';
2
+ /**
3
+ * Public authoring DSL for model-level named relations.
4
+ *
5
+ * This is the first stage of the relations subdomain. Application code uses it
6
+ * inside `relations: (r) => ({ ... })` to declare stable relation names and
7
+ * resolve ambiguity that field decorators alone cannot express.
8
+ *
9
+ * Later internal stages normalize these authored definitions and combine them
10
+ * with field-authored relation metadata to build the resolved relation graph.
11
+ */
12
+ export declare class RelationBuilder {
13
+ /** Declare a one-to-many relation from this model to `target`. */
14
+ hasMany(target: string, foreignKey: string): RelationDef;
15
+ /** Declare an owning relation to a parent model. */
16
+ belongsTo(target: string, foreignKey: string, localKey?: string): RelationDef;
17
+ /** Declare a one-to-one relation from this model to `target`. */
18
+ hasOne(target: string, foreignKey: string): RelationDef;
19
+ }
@@ -0,0 +1,30 @@
1
+ import { z } from 'zod';
2
+ import type { NormalizedRelationStorageDescriptor } from './NormalizedRelationStorageDescriptor';
3
+ /**
4
+ * Normalizes field-authored relation declarations from a model schema into the
5
+ * shared descriptor shape consumed by storage and relation finalization.
6
+ *
7
+ * This is the normalization stage of the relations subdomain. It sits between
8
+ * authoring and resolution:
9
+ *
10
+ * - authoring: decorators attach relation intent to schema fields
11
+ * - normalization: this class converts that intent into a registry-independent
12
+ * descriptor shape
13
+ * - resolution: the graph builder combines those descriptors with finalized
14
+ * storage artifacts and explicit relation names
15
+ */
16
+ export declare class RelationDescriptorNormalizer {
17
+ private readonly sourceModelKey;
18
+ private readonly schema;
19
+ constructor(sourceModelKey: string, schema: z.ZodObject<z.ZodRawShape>);
20
+ static normalize(sourceModelKey: string, schema: z.ZodObject<z.ZodRawShape>): readonly NormalizedRelationStorageDescriptor[];
21
+ /**
22
+ * Run the field-authored relation normalization pipeline for one model
23
+ * schema and emit descriptors that later relation stages can resolve.
24
+ */
25
+ normalize(): readonly NormalizedRelationStorageDescriptor[];
26
+ private collectRelationCandidates;
27
+ private normalizeCandidate;
28
+ private buildEdgeId;
29
+ private deriveNamingHint;
30
+ }