Decorators · Strata Sync
{
"compilerOptions" : {
"experimentalDecorators" : true
}
}
Shared by several decorators. Individual sections reference this table.
Option Type Default Description lazybooleanfalseDefers hydration until first access. serializerPropertySerializer- Custom serializer for converting values to and from their stored representation. See Custom serializers .
Registers a synced model.
Copy import { Model, ClientModel } from "@stratasync/core" ;
@ ClientModel ( "Task" )
class Task extends Model {
// properties...
} Signature: ClientModel(modelName: string, options?: ModelOptions)
Option Type Default Description loadStrategyLoadStrategy"instant"How and when the model data is fetched. partialLoadModePartialLoadMode- Hydration priority for partial models. usedForPartialIndexesboolean- Whether this model is used for partial index dependencies. schemaVersionnumber- Schema version for migration tracking. tableNamestring- Database table name override. groupKeystring- Sync-group key field for multi-tenancy.
Copy @ ClientModel ( "Project" , { loadStrategy: "instant" })
class Project extends Model {
/* ... */
}
@ ClientModel ( "Attachment" , { loadStrategy: "lazy" })
class Attachment extends Model {
/* ... */
}
@ ClientModel ( "Comment" , { loadStrategy: "partial" })
class Comment extends Model {
/* ... */
} Strategy Behavior "instant"Loaded during bootstrap. Always available in memory. "lazy"Loaded on first access, cached thereafter. "partial"Loaded on demand, partially hydrated. "explicitlyRequested"Never auto-loaded. Must be explicitly requested via ensureModel. "local"Never synced. Only stored locally on the device.
Marks a synced property for change tracking and delta processing.
Copy import { Model, ClientModel, Property } from "@stratasync/core" ;
@ ClientModel ( "Task" )
class Task extends Model {
@ Property ()
title = "" ;
@ Property ()
status = "todo" ;
@ Property ({ lazy: true })
description = "" ;
@ Property ({ serializer: dateSerializer })
dueDate : Date | null = null ;
} Signature: Property(options?: PropertyOptions)
Handle types that need conversion for storage:
Copy import type { PropertySerializer } from "@stratasync/core" ;
const dateSerializer : PropertySerializer < Date | null > = {
serialize ( value : Date | null ) : unknown {
return value ? value. toISOString () : null ;
},
deserialize ( value : unknown ) : Date | null {
return typeof value === "string" ? new Date (value) : null ;
},
};
@ ClientModel ( "Event" )
class Event extends Model {
@ Property ({ serializer: dateSerializer })
startsAt : Date | null = null ;
} Observable but not persisted to the server. Use for reactive local UI state.
Copy @ ClientModel ( "Task" )
class Task extends Model {
@ Property ()
title = "" ;
@ EphemeralProperty ()
isExpanded = false ;
@ EphemeralProperty ()
localDraft = "" ;
} Signature: EphemeralProperty(options?: PropertyOptions)
Same common options as @Property. Participates in MobX observability but excluded from transactions and deltas.
Creates a foreign-key reference to another model with a lazy-resolved accessor.
Copy @ ClientModel ( "Task" )
class Task extends Model {
@ Reference (() => Project, "tasks" )
project !: Project ;
// Also creates: projectId: string (the foreign key)
} Signature: Reference(modelFactory: () => ModelConstructor, inverseProperty?: string, options?: ReferenceOptions)
Parameter Description modelFactoryFactory function returning the referenced model constructor. Uses a factory to avoid circular deps. inversePropertyName of the inverse collection property on the referenced model (for example, "tasks" on Project).
Option Type Default Description foreignKeystring"<propertyName>Id"Override the foreign key field name. nullableboolean- Whether the reference can be null. indexedbooleantrue (when inverseProperty is set)Whether to index this foreign key.
Copy @ ClientModel ( "Task" )
class Task extends Model {
@ Reference (() => User, undefined , { foreignKey: "ownerId" })
assignee !: User ;
// Creates: ownerId: string (instead of default "assigneeId")
} Alias for @Reference. Accepts a model name string or factory function.
Copy @ ClientModel ( "Task" )
class Task extends Model {
@ ManyToOne ( "Project" , "tasks" )
project !: Project ;
@ ManyToOne (() => User)
assignee !: User ;
} Signature: ManyToOne(modelNameOrFactory: string | (() => ModelConstructor), inverseProperty?: string, options?: ReferenceOptions)
Defines the parent side of a one-to-many relationship. Returns a LazyCollection of child models.
Copy @ ClientModel ( "Project" )
class Project extends Model {
@ Property ()
name = "" ;
@ OneToMany ()
tasks !: LazyCollection < Task >;
} Signature: OneToMany(options?: ReferenceCollectionOptions)
ReferenceCollectionOptions Option Type Default Description foreignKeystring- Override the foreign key used for the collection lookup. indexedboolean- Whether the collection uses an index. nullableboolean- Whether the collection can be null.
@ManyToOne on the child creates the foreign key; @OneToMany on the parent creates the collection.
Copy @ ClientModel ( "Project" )
class Project extends Model {
@ Property ()
name = "" ;
@ OneToMany ()
tasks !: LazyCollection < Task >;
}
@ ClientModel ( "Task" )
class Task extends Model {
@ Property ()
title = "" ;
@ ManyToOne ( "Project" , "tasks" )
project !: Project ;
// Creates projectId foreign key, which @OneToMany uses
} Defines an inverse lookup for relationships declared on the other side.
Copy @ ClientModel ( "Comment" )
class Comment extends Model {
@ Property ()
body = "" ;
@ BackReference ({ foreignKey: "commentId" })
replies !: Reply [];
} Signature: BackReference(options?: BackReferenceOptions)
Option Type Default Description foreignKeystring- The foreign key field on the related model.
An ordered array of references, typically for many-to-many relationships through a join model.
Copy @ ClientModel ( "Task" )
class Task extends Model {
@ Property ()
title = "" ;
@ ReferenceArray ({ through: "TaskTag" })
tags !: Tag [];
} Signature: ReferenceArray(options?: ReferenceArrayOptions)
Option Type Default Description throughstring- Join/through model name for many-to-many relationships.
Alias for @OneToMany. Use whichever name reads better in your domain model. See @OneToMany for options and usage.
Every decorator in a minimal project management domain:
Copy import {
Model,
ClientModel,
Property,
EphemeralProperty,
ManyToOne,
OneToMany,
ReferenceArray,
} from "@stratasync/core" ;
import type { LazyCollection } from "@stratasync/core" ;
@ ClientModel ( "Project" , { loadStrategy: "instant" })
class Project extends Model {
@ Property ()
name = "" ;
@ OneToMany ()
tasks !: LazyCollection < Task >;
}
@ ClientModel ( "Task" , { loadStrategy: "instant" })
class Task extends Model {
@ Property ()
title = "" ;
@ Property ({ serializer: dateSerializer })
dueDate : Date | null = null ;
@ ManyToOne ( "Project" , "tasks" )
project !: Project ;
@ ReferenceArray ({ through: "TaskTag" })
tags !: Tag [];
@ EphemeralProperty ()
isSelected = false ;
}