Class: Attribute
Defined in: packages/client/src/core/attribute.ts:31
Represents a single attribute (field) within a Class schema.
Attributes define the structure of documents, including their type, validation rules (via Zod), and configuration options like mandatory, default values, primary keys, and foreign key references.
Use the static factory method Attribute.create to instantiate attributes with proper validation and persistence.
Example
// Create a string attribute
const titleAttr = await Attribute.create(taskClass, 'title', 'string', 'Task Title', {
mandatory: true,
maxLength: 200
});
// Create a foreign key reference
const assigneeAttr = await Attribute.create(taskClass, 'assigneeId', 'foreign_key', 'Assigned User', {
targetClass: 'User'
});
Extends
Attribute
Constructors
Constructor
new Attribute(
classObj,
name,
type,
description?,
config?): Attribute;
Defined in: packages/client/src/core/attribute.ts:55
Creates a new Attribute instance. For most use cases, prefer using Attribute.create which also persists the attribute.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
classObj | Class | null | The parent Class for this attribute |
name | string | undefined | The attribute name |
type | | "string" | "boolean" | "object" | "integer" | "date" | "decimal" | "foreign_key" | "enum" | "reference" | undefined | The attribute type (e.g., 'string', 'integer', 'boolean', 'enum') |
description? | string | undefined | Optional description |
config? | | AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | undefined | Type-specific configuration options |
Returns
Attribute
Overrides
Attribute_.constructor
Properties
| Property | Type | Description | Overrides | Defined in |
|---|---|---|---|---|
class | Class | Reference to the parent Class that owns this attribute. | Attribute_.class | packages/client/src/core/attribute.ts:41 |
defaultValue? | any | Default value to use when the field is not provided. | Attribute_.defaultValue | packages/client/src/core/attribute.ts:43 |
description? | string | Optional description explaining the attribute's purpose. | Attribute_.description | packages/client/src/core/attribute.ts:35 |
field | any | Zod schema for runtime validation of this field. | Attribute_.field | packages/client/src/core/attribute.ts:39 |
model | AttributeModel | The underlying AttributeModel containing type and configuration. | Attribute_.model | packages/client/src/core/attribute.ts:37 |
name | string | The attribute name used as the document field key. | Attribute_.name | packages/client/src/core/attribute.ts:33 |
Methods
checkTypeValidity()
checkTypeValidity(type): boolean;
Defined in: packages/client/src/core/attribute.ts:441
Parameters
| Parameter | Type |
|---|---|
type | string |
Returns
boolean
Overrides
Attribute_.checkTypeValidity
ensureReferenceConfigIsValid()
ensureReferenceConfigIsValid(): Promise<void>;
Defined in: packages/client/src/core/attribute.ts:74
Validates that reference-type attributes have proper domain configuration. Called automatically during Attribute.create.
Returns
Promise<void>
Throws
Error if the reference configuration is invalid
Overrides
Attribute_.ensureReferenceConfigIsValid
getClass()
getClass(): Class;
Defined in: packages/client/src/core/attribute.ts:360
Returns the parent Class for this attribute.
Returns
The Class instance
Throws
Error if the attribute has no parent class
Overrides
Attribute_.getClass
getEmpty()
getEmpty(): object;
Defined in: packages/client/src/core/attribute.ts:424
Returns an empty/default value for this attribute. Uses the Zod schema's default value if configured.
Returns
object
Object with attribute name as key and default/null value
Overrides
Attribute_.getEmpty
getModel()
getModel(): AttributeModel;
Defined in: packages/client/src/core/attribute.ts:351
Returns the AttributeModel for this attribute.
Returns
The underlying AttributeModel
Overrides
Attribute_.getModel
getName()
getName(): string;
Defined in: packages/client/src/core/attribute.ts:437
Returns the attribute name.
Returns
string
The attribute name string
Overrides
Attribute_.getName
getType()
getType(type):
| "string"
| "boolean"
| "object"
| "integer"
| "date"
| "decimal"
| "foreign_key"
| "enum"
| "reference";
Defined in: packages/client/src/core/attribute.ts:412
Parameters
| Parameter | Type |
|---|---|
type | | "string" | "boolean" | "object" | "integer" | "date" | "decimal" | "foreign_key" | "enum" | "reference" |
Returns
| "string"
| "boolean"
| "object"
| "integer"
| "date"
| "decimal"
| "foreign_key"
| "enum"
| "reference"
Overrides
Attribute_.getType
getTypeConf()
getTypeConf(type, config):
| AttributeTypeConfig
| object & AttributeTypeConfig
| object & AttributeTypeConfig
| object & AttributeTypeConfig
| object & AttributeTypeConfig
| object & AttributeTypeConfig
| object & AttributeTypeConfig
| object & AttributeTypeConfig;
Defined in: packages/client/src/core/attribute.ts:454
Parameters
| Parameter | Type |
|---|---|
type | | "string" | "boolean" | "object" | "integer" | "date" | "decimal" | "foreign_key" | "enum" | "reference" |
config | | AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig |
Returns
| AttributeTypeConfig
| object & AttributeTypeConfig
| object & AttributeTypeConfig
| object & AttributeTypeConfig
| object & AttributeTypeConfig
| object & AttributeTypeConfig
| object & AttributeTypeConfig
| object & AttributeTypeConfig
Overrides
Attribute_.getTypeConf
isMandatory()
isMandatory(): boolean;
Defined in: packages/client/src/core/attribute.ts:343
Checks if this attribute is mandatory (required).
Returns
boolean
true if the attribute is mandatory
Overrides
Attribute_.isMandatory
isPrimaryKey()
isPrimaryKey(): boolean;
Defined in: packages/client/src/core/attribute.ts:334
Checks if this attribute is marked as a primary key.
Returns
boolean
true if this is a primary key attribute
Overrides
Attribute_.isPrimaryKey
setField()
setField(): void;
Defined in: packages/client/src/core/attribute.ts:135
Builds the Zod validation schema based on attribute type and configuration. Called automatically during construction.
Returns
void
Overrides
Attribute_.setField
setModel()
setModel(model): void;
Defined in: packages/client/src/core/attribute.ts:404
Parameters
| Parameter | Type |
|---|---|
model | AttributeModel |
Returns
void
Overrides
Attribute_.setModel
validate()
validate(data): Promise<ZodSafeParseResult<any>>;
Defined in: packages/client/src/core/attribute.ts:381
Validates a value against this attribute's Zod schema.
Parameters
| Parameter | Type | Description |
|---|---|---|
data | any | The value to validate |
Returns
Promise<ZodSafeParseResult<any>>
Zod safe parse result with success status and data/error
Example
const result = await priceAttr.validate(19.99);
if (result.success) {
console.log('Valid:', result.data);
} else {
console.log('Invalid:', result.error);
}
Overrides
Attribute_.validate
build()
static build(attributeObj): Promise<Attribute>;
Defined in: packages/client/src/core/attribute.ts:393
Adds an attribute to its parent class and persists to the database. Used internally by Attribute.create.
Parameters
| Parameter | Type | Description |
|---|---|---|
attributeObj | Attribute | The Attribute instance to build |
Returns
Promise<Attribute>
The Attribute instance
Throws
Error if the class has no stack connection
Overrides
Attribute_.build
create()
static create(
classObj,
name,
type,
description?,
config?): Promise<Attribute>;
Defined in: packages/client/src/core/attribute.ts:317
Creates a new Attribute and persists it to the parent Class. This is the primary factory method for creating attributes.
Parameters
| Parameter | Type | Description |
|---|---|---|
classObj | Class | The parent Class to add the attribute to |
name | string | The attribute name |
type | | "string" | "boolean" | "object" | "integer" | "date" | "decimal" | "foreign_key" | "enum" | "reference" | The attribute type |
description? | string | Optional description |
config? | | AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | object & AttributeTypeConfig | Type-specific configuration |
Returns
Promise<Attribute>
The created Attribute instance
Example
const priceAttr = await Attribute.create(productClass, 'price', 'decimal', 'Product price', {
min: 0,
precision: 2,
mandatory: true
});
Overrides
Attribute_.create