Class: Class
Defined in: packages/client/src/core/class.ts:57
Represents a data class (schema definition) in the DocStack database.
A Class defines the structure of documents, including their attributes, validation rules (via Zod), and triggers that execute during document operations.
Use the static factory methods (Class.create, Class.fetch) to instantiate classes - the constructor is private.
Example
// Create a new class with schema
const taskClass = await Class.create(stack, 'Task', 'class', 'User Tasks');
// Add attributes to define the schema
await Attribute.create(taskClass, 'title', 'string', 'Task Title', { mandatory: true });
await Attribute.create(taskClass, 'isComplete', 'boolean', 'Done?', { defaultValue: false });
// Create documents (cards) of this class
const task = await taskClass.add({ title: 'My Task', isComplete: false });
Extends
Class
Properties
| Property | Modifier | Type | Default value | Description | Overrides | Inherited from | Defined in |
|---|---|---|---|---|---|---|---|
attributes | public | object | {} | Map of attribute names to Attribute instances defining the schema. | Class_.attributes | - | packages/client/src/core/class.ts:67 |
close | public | () => void | undefined | Releases this instance's live document subscription. Subscribing is not free: the handle keeps its handler - and everything that handler closes over - reachable for as long as the stack lives. Close a Class that was built outside the stack's cache (getClass(name, true), or an entry a class list has replaced) once it is no longer needed. Afterwards the instance stops emitting doc events; its data methods still work. Safe to call twice. Example const snapshot = await stack.getClass('Task', true); // ... snapshot.close(); | - | Class_.close | packages/shared/lib/utils/stack/class/index.d.ts:45 |
description? | public | string | undefined | Optional description of the class purpose. | Class_.description | - | packages/client/src/core/class.ts:65 |
id? | public | string | undefined | The unique identifier for this class (e.g., 'Task', 'Class-123'). | Class_.id | - | packages/client/src/core/class.ts:73 |
logger | public | Logger | undefined | - | Class_.logger | - | packages/client/src/core/class.ts:79 |
model | public | ClassModel | undefined | The underlying ClassModel document. | Class_.model | - | packages/client/src/core/class.ts:75 |
name | public | string | undefined | The name of this class (e.g., 'Task', 'User'). | Class_.name | - | packages/client/src/core/class.ts:61 |
schema | public | object | {} | The raw schema definition from the ClassModel. | Class_.schema | - | packages/client/src/core/class.ts:69 |
schemaZOD | public | ZodObject<any> | undefined | Zod schema for runtime validation of document data. | Class_.schemaZOD | - | packages/client/src/core/class.ts:71 |
stack | public | Stack | undefined | Reference to the parent stack instance. | Class_.stack | - | packages/client/src/core/class.ts:59 |
state | public | "busy" | "idle" | "idle" | Current state indicating if the class is processing an operation. | Class_.state | - | packages/client/src/core/class.ts:77 |
triggers | public | Trigger[] | [] | Array of triggers that execute before/after document operations. | Class_.triggers | - | packages/client/src/core/class.ts:81 |
type | public | "class" | "~self" | undefined | The class type (e.g., 'class', '~self'). | Class_.type | - | packages/client/src/core/class.ts:63 |
logger | static | Logger | undefined | - | Class_.logger | - | packages/client/src/core/class.ts:78 |
Methods
add()
Call Signature
add(params): Promise<Document>;
Defined in: packages/client/src/core/class.ts:730
Creates one or more documents of this class type. Convenience method that handles both single and batch creation.
Parameters
| Parameter | Type | Description |
|---|---|---|
params | { [key: string]: any; } | Document data (single object for one doc, or multiple for batch) |
Returns
Promise<Document>
Single document when one param passed, array when multiple
Example
// Single document
const task = await taskClass.add({ title: 'Task 1' });
// Multiple documents
const tasks = await taskClass.add(
{ title: 'Task 1' },
{ title: 'Task 2' }
);
Overrides
Class_.add
Call Signature
add(...paramsArray): Promise<Document[]>;
Defined in: packages/client/src/core/class.ts:731
Creates one or more documents of this class type. Convenience method that handles both single and batch creation.
Parameters
| Parameter | Type |
|---|---|
...paramsArray | object[] |
Returns
Promise<Document[]>
Single document when one param passed, array when multiple
Example
// Single document
const task = await taskClass.add({ title: 'Task 1' });
// Multiple documents
const tasks = await taskClass.add(
{ title: 'Task 1' },
{ title: 'Task 2' }
);
Overrides
Class_.add
addAttribute()
addAttribute(attribute): Promise<Class>;
Defined in: packages/client/src/core/class.ts:537
Adds a new attribute to the class schema. Persists the change to the database.
Parameters
| Parameter | Type | Description |
|---|---|---|
attribute | Attribute | AttributeModel | The Attribute instance or AttributeModel to add |
Returns
Promise<Class>
This Class instance for chaining
Example
await taskClass.addAttribute(new Attribute(taskClass, 'dueDate', 'date', 'Due Date'));
// Or use Attribute.create() for a simpler API
Overrides
Class_.addAttribute
addCard()
addCard(params): Promise<Document>;
Defined in: packages/client/src/core/class.ts:656
Creates a new document (card) of this class type.
Parameters
| Parameter | Type | Description |
|---|---|---|
params | { [key: string]: any; } | The document data |
Returns
Promise<Document>
The created document, or null if stack is not defined
Example
const task = await taskClass.addCard({
title: 'My Task',
isComplete: false
});
Overrides
Class_.addCard
addCards()
addCards(paramsArray): Promise<Document[]>;
Defined in: packages/client/src/core/class.ts:671
Creates multiple documents (cards) of this class type in a batch.
Parameters
| Parameter | Type | Description |
|---|---|---|
paramsArray | object[] | Array of document data objects |
Returns
Promise<Document[]>
Array of created documents
Overrides
Class_.addCards
addOrUpdateCard()
addOrUpdateCard(params, cardId?): Promise<Document>;
Defined in: packages/client/src/core/class.ts:740
Parameters
| Parameter | Type |
|---|---|
params | { [key: string]: any; } |
cardId? | string |
Returns
Promise<Document>
Overrides
Class_.addOrUpdateCard
addTrigger()
addTrigger(name, model): Promise<Class>;
Defined in: packages/client/src/core/class.ts:887
Adds a trigger to this class. Triggers execute before or after document operations.
Parameters
| Parameter | Type | Description |
|---|---|---|
name | string | The trigger name |
model | TriggerModel | The trigger model containing the execution logic |
Returns
Promise<Class>
This Class instance for chaining
Example
await taskClass.addTrigger('generate-slug', {
name: 'generate-slug',
order: 'before',
run: `document.slug = document.title.toLowerCase().replace(/\\s+/g, '-'); return document;`
});
Overrides
Class_.addTrigger
build()
build(): Promise<Class>;
Defined in: packages/client/src/core/class.ts:99
Returns
Promise<Class>
Overrides
Class_.build
buildSchema()
buildSchema(): object;
Defined in: packages/client/src/core/class.ts:389
Builds the schema object from the current attributes.
Returns
object
The schema definition object
Overrides
Class_.buildSchema
bulkUniqueCheck()
bulkUniqueCheck(pKs): Promise<boolean>;
Defined in: packages/client/src/core/class.ts:299
Parameters
| Parameter | Type |
|---|---|
pKs | string[] |
Returns
Promise<boolean>
Overrides
Class_.bulkUniqueCheck
deleteCard()
deleteCard(cardId): Promise<boolean>;
Defined in: packages/client/src/core/class.ts:801
Soft-deletes a document by setting its active flag to false.
Parameters
| Parameter | Type | Description |
|---|---|---|
cardId | string | The document ID to delete |
Returns
Promise<boolean>
true if successful, false otherwise
Overrides
Class_.deleteCard
get()
Call Signature
get(cardId): Promise<Document>;
Defined in: packages/client/src/core/class.ts:852
Retrieves one or more documents by their IDs.
Parameters
| Parameter | Type | Description |
|---|---|---|
cardId | string | Single ID or multiple IDs to fetch |
Returns
Promise<Document>
Single document (or null) when one ID passed, array when multiple
Example
// Get single document
const task = await taskClass.get('Task-123');
// Get multiple documents
const tasks = await taskClass.get('Task-1', 'Task-2', 'Task-3');
Overrides
Class_.get
Call Signature
get(...cardId): Promise<Document[]>;
Defined in: packages/client/src/core/class.ts:853
Retrieves one or more documents by their IDs.
Parameters
| Parameter | Type | Description |
|---|---|---|
...cardId | string[] | Single ID or multiple IDs to fetch |
Returns
Promise<Document[]>
Single document (or null) when one ID passed, array when multiple
Example
// Get single document
const task = await taskClass.get('Task-123');
// Get multiple documents
const tasks = await taskClass.get('Task-1', 'Task-2', 'Task-3');
Overrides
Class_.get
getAttributes()
getAttributes(...names): object;
Defined in: packages/client/src/core/class.ts:472
Parameters
| Parameter | Type |
|---|---|
...names | string[] |
Returns
object
Overrides
Class_.getAttributes
getByPrimaryKeys()
getByPrimaryKeys(params): Promise<Document>;
Defined in: packages/client/src/core/class.ts:684
Parameters
| Parameter | Type |
|---|---|
params | { [key: string]: any; } |
Returns
Promise<Document>
Overrides
Class_.getByPrimaryKeys
getCards()
getCards(
selector?,
fields?,
skip?,
limit?,
sort?): Promise<Document[]>;
Defined in: packages/client/src/core/class.ts:830
Retrieves documents (cards) of this class type.
Parameters
| Parameter | Type | Description |
|---|---|---|
selector? | { [key: string]: any; } | Optional PouchDB/Mango selector for filtering |
fields? | string[] | Optional list of fields to return |
skip? | number | Number of documents to skip |
limit? | number | Maximum number of documents to return |
sort? | object[] | - |
Returns
Promise<Document[]>
Array of matching documents
Example
// Get all tasks
const allTasks = await taskClass.getCards();
// Get incomplete tasks
const incomplete = await taskClass.getCards({ isComplete: { $eq: false } });
Overrides
Class_.getCards
getDescription()
getDescription(): string;
Defined in: packages/client/src/core/class.ts:373
Returns
string
Overrides
Class_.getDescription
getEncryptedAttributes()
getEncryptedAttributes(): Attribute[];
Defined in: packages/client/src/core/class.ts:511
Returns
Overrides
Class_.getEncryptedAttributes
getId()
getId(): string;
Defined in: packages/client/src/core/class.ts:381
Returns
string
Overrides
Class_.getId
getModel()
getModel(): ClassModel;
Defined in: packages/client/src/core/class.ts:401
Returns the current ClassModel representation of this class.
Returns
The ClassModel document
Overrides
Class_.getModel
getName()
getName(): string;
Defined in: packages/client/src/core/class.ts:365
Returns
string
Overrides
Class_.getName
getPrimaryKeys()
getPrimaryKeys(): string[];
Defined in: packages/client/src/core/class.ts:467
Returns the primary key attribute names for this class.
Returns
string[]
Array of attribute names marked as primary keys
Overrides
Class_.getPrimaryKeys
getStack()
getStack(): Stack;
Defined in: packages/client/src/core/class.ts:369
Returns
Stack
Overrides
Class_.getStack
getType()
getType(): "class" | "~self";
Defined in: packages/client/src/core/class.ts:377
Returns
"class" | "~self"
Overrides
Class_.getType
hasAllAttributes()
hasAllAttributes(...names): boolean;
Defined in: packages/client/src/core/class.ts:491
Parameters
| Parameter | Type |
|---|---|
...names | string[] |
Returns
boolean
Overrides
Class_.hasAllAttributes
hasAnyAttributes()
hasAnyAttributes(...names): boolean;
Defined in: packages/client/src/core/class.ts:501
Parameters
| Parameter | Type |
|---|---|
...names | string[] |
Returns
boolean
Overrides
Class_.hasAnyAttributes
hasAttribute()
hasAttribute(name): boolean;
Defined in: packages/client/src/core/class.ts:519
Parameters
| Parameter | Type |
|---|---|
name | string |
Returns
boolean
Overrides
Class_.hasAttribute
init()
init(
stack,
id,
name,
type,
description?,
schema?): void;
Defined in: packages/client/src/core/class.ts:121
Parameters
| Parameter | Type |
|---|---|
stack | Stack |
id | string |
name | string |
type | "class" | "~self" |
description? | string |
schema? | { [name: string]: AttributeModel; } |
Returns
void
Overrides
Class_.init
modifyAttribute()
modifyAttribute(name, attribute): Promise<Class>;
Defined in: packages/client/src/core/class.ts:591
Modifies an existing attribute in the class schema.
Parameters
| Parameter | Type | Description |
|---|---|---|
name | string | The name of the attribute to modify |
attribute | Attribute | AttributeModel | The new Attribute or AttributeModel definition |
Returns
Promise<Class>
This Class instance for chaining
Overrides
Class_.modifyAttribute
push()
push(params, docId?): Promise<Document>;
Defined in: packages/client/src/core/class.ts:772
Pushes a document to the database. This is an alias for addOrUpdateCard.
Parameters
| Parameter | Type | Description |
|---|---|---|
params | { [key: string]: any; } | The document data |
docId? | string | Optional document ID. If provided, performs an update. |
Returns
Promise<Document>
The created or updated document
Overrides
Class_.push
removeAttribute()
removeAttribute(name): Promise<Class>;
Defined in: packages/client/src/core/class.ts:621
Removes an attribute from the class schema.
Parameters
| Parameter | Type | Description |
|---|---|---|
name | string | The name of the attribute to remove |
Returns
Promise<Class>
This Class instance for chaining
Overrides
Class_.removeAttribute
removeTrigger()
removeTrigger(name): Promise<Class>;
Defined in: packages/client/src/core/class.ts:910
Removes a trigger from this class by name.
Parameters
| Parameter | Type | Description |
|---|---|---|
name | string | The name of the trigger to remove |
Returns
Promise<Class>
This Class instance for chaining
Overrides
Class_.removeTrigger
setId()
setId(id): void;
Defined in: packages/client/src/core/class.ts:361
Parameters
| Parameter | Type |
|---|---|
id | string |
Returns
void
Overrides
Class_.setId
setModel()
setModel(model?): void;
Defined in: packages/client/src/core/class.ts:425
It hydrates attributes and triggers from given model
Parameters
| Parameter | Type | Description |
|---|---|---|
model? | ClassModel |
Returns
void
Overrides
Class_.setModel
uniqueCheck()
uniqueCheck(doc): Promise<boolean>;
Defined in: packages/client/src/core/class.ts:287
Parameters
| Parameter | Type |
|---|---|
doc | Document |
Returns
Promise<boolean>
Overrides
Class_.uniqueCheck
updateCard()
updateCard(cardId, params): Promise<Document>;
Defined in: packages/client/src/core/class.ts:783
Updates an existing document (card) of this class.
Parameters
| Parameter | Type | Description |
|---|---|---|
cardId | string | The document ID to update |
params | { [key: string]: any; } | The updated document data |
Returns
Promise<Document>
The updated document, or null if stack is not defined
Overrides
Class_.updateCard
validate()
validate(data): Promise<boolean>;
Defined in: packages/client/src/core/class.ts:349
Validates document data against the class schema using Zod.
Parameters
| Parameter | Type | Description |
|---|---|---|
data | { [key: string]: any; } | The document data to validate |
Returns
Promise<boolean>
true if validation passes, false otherwise
Overrides
Class_.validate
buildFromModel()
static buildFromModel(
stack,
classModel,
options?): Promise<Class>;
Defined in: packages/client/src/core/class.ts:226
Builds a Class instance from an existing ClassModel document. Hydrates attributes and triggers from the model.
Parameters
| Parameter | Type | Description |
|---|---|---|
stack | Stack | The parent stack instance |
classModel | ClassModel | The ClassModel document from the database |
options? | ClassBuildOptions | - |
Returns
Promise<Class>
The hydrated Class instance
Overrides
Class_.buildFromModel
create()
static create(
stack,
name,
type,
description?,
schema?): Promise<Class>;
Defined in: packages/client/src/core/class.ts:206
Creates a new class and persists it to the database. This is the primary factory method for creating new classes.
Parameters
| Parameter | Type | Description |
|---|---|---|
stack | Stack | The parent stack instance |
name | string | The name for the new class |
type | "class" | "~self" | The class type (typically 'class') |
description? | string | Optional description of the class |
schema? | { [name: string]: AttributeModel; } | Initial schema definition |
Returns
Promise<Class>
The persisted Class instance
Example
const userClass = await Class.create(stack, 'User', 'class', 'Application users');
Overrides
Class_.create
fetch()
static fetch(
stack,
className,
options?): Promise<Class>;
Defined in: packages/client/src/core/class.ts:278
Fetches a class by its name. This is the most common way to retrieve an existing class.
Parameters
| Parameter | Type | Description |
|---|---|---|
stack | Stack | The parent stack instance |
className | string | The class name to fetch |
options? | ClassBuildOptions | - |
Returns
Promise<Class>
The Class instance, or null if not found
Example
const taskClass = await Class.fetch(stack, 'Task');
if (taskClass) {
const tasks = await taskClass.getCards();
}
Overrides
Class_.fetch
fetchById()
static fetchById(stack, classId): Promise<Class>;
Defined in: packages/client/src/core/class.ts:252
Fetches a class by its document ID.
Parameters
| Parameter | Type | Description |
|---|---|---|
stack | Stack | The parent stack instance |
classId | string | The class document ID |
Returns
Promise<Class>
The Class instance
Throws
Error if the class is not found
Overrides
Class_.fetchById
get()
static get(
stack,
id,
name,
type,
description?,
schema?,
options?): Class;
Defined in: packages/client/src/core/class.ts:169
Gets a Class instance without persisting it to the database. Use this for working with existing class models or for testing. Sets up a document change listener for real-time updates.
Parameters
| Parameter | Type | Description |
|---|---|---|
stack | Stack | The parent stack instance |
id | string | The class ID |
name | string | The class name |
type | "class" | "~self" | The class type |
description? | string | Optional description |
schema? | { [name: string]: AttributeModel; } | Initial schema definition |
options? | ClassBuildOptions | - |
Returns
Class
A new Class instance (not persisted)
Overrides
Class_.get