Skip to main content

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

PropertyModifierTypeDefault valueDescriptionOverridesInherited fromDefined in
attributespublicobject{}Map of attribute names to Attribute instances defining the schema.Class_.attributes-packages/client/src/core/class.ts:67
closepublic() => voidundefinedReleases 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_.closepackages/shared/lib/utils/stack/class/index.d.ts:45
description?publicstringundefinedOptional description of the class purpose.Class_.description-packages/client/src/core/class.ts:65
id?publicstringundefinedThe unique identifier for this class (e.g., 'Task', 'Class-123').Class_.id-packages/client/src/core/class.ts:73
loggerpublicLoggerundefined-Class_.logger-packages/client/src/core/class.ts:79
modelpublicClassModelundefinedThe underlying ClassModel document.Class_.model-packages/client/src/core/class.ts:75
namepublicstringundefinedThe name of this class (e.g., 'Task', 'User').Class_.name-packages/client/src/core/class.ts:61
schemapublicobject{}The raw schema definition from the ClassModel.Class_.schema-packages/client/src/core/class.ts:69
schemaZODpublicZodObject<any>undefinedZod schema for runtime validation of document data.Class_.schemaZOD-packages/client/src/core/class.ts:71
stackpublicStackundefinedReference to the parent stack instance.Class_.stack-packages/client/src/core/class.ts:59
statepublic"busy" | "idle""idle"Current state indicating if the class is processing an operation.Class_.state-packages/client/src/core/class.ts:77
triggerspublicTrigger[][]Array of triggers that execute before/after document operations.Class_.triggers-packages/client/src/core/class.ts:81
typepublic"class" | "~self"undefinedThe class type (e.g., 'class', '~self').Class_.type-packages/client/src/core/class.ts:63
loggerstaticLoggerundefined-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
ParameterTypeDescription
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
ParameterType
...paramsArrayobject[]
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

ParameterTypeDescription
attributeAttribute | AttributeModelThe 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

ParameterTypeDescription
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

ParameterTypeDescription
paramsArrayobject[]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

ParameterType
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

ParameterTypeDescription
namestringThe trigger name
modelTriggerModelThe 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

ParameterType
pKsstring[]

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

ParameterTypeDescription
cardIdstringThe 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
ParameterTypeDescription
cardIdstringSingle 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
ParameterTypeDescription
...cardIdstring[]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

ParameterType
...namesstring[]

Returns

object

Overrides

Class_.getAttributes

getByPrimaryKeys()

getByPrimaryKeys(params): Promise<Document>;

Defined in: packages/client/src/core/class.ts:684

Parameters

ParameterType
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

ParameterTypeDescription
selector?{ [key: string]: any; }Optional PouchDB/Mango selector for filtering
fields?string[]Optional list of fields to return
skip?numberNumber of documents to skip
limit?numberMaximum 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

Attribute[]

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

ClassModel

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

ParameterType
...namesstring[]

Returns

boolean

Overrides

Class_.hasAllAttributes

hasAnyAttributes()

hasAnyAttributes(...names): boolean;

Defined in: packages/client/src/core/class.ts:501

Parameters

ParameterType
...namesstring[]

Returns

boolean

Overrides

Class_.hasAnyAttributes

hasAttribute()

hasAttribute(name): boolean;

Defined in: packages/client/src/core/class.ts:519

Parameters

ParameterType
namestring

Returns

boolean

Overrides

Class_.hasAttribute

init()

init(
stack,
id,
name,
type,
description?,
schema?): void;

Defined in: packages/client/src/core/class.ts:121

Parameters

ParameterType
stackStack
idstring
namestring
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

ParameterTypeDescription
namestringThe name of the attribute to modify
attributeAttribute | AttributeModelThe 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

ParameterTypeDescription
params{ [key: string]: any; }The document data
docId?stringOptional 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

ParameterTypeDescription
namestringThe 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

ParameterTypeDescription
namestringThe 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

ParameterType
idstring

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

ParameterTypeDescription
model?ClassModel

Returns

void

Overrides

Class_.setModel

uniqueCheck()

uniqueCheck(doc): Promise<boolean>;

Defined in: packages/client/src/core/class.ts:287

Parameters

ParameterType
docDocument

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

ParameterTypeDescription
cardIdstringThe 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

ParameterTypeDescription
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

ParameterTypeDescription
stackStackThe parent stack instance
classModelClassModelThe 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

ParameterTypeDescription
stackStackThe parent stack instance
namestringThe name for the new class
type"class" | "~self"The class type (typically 'class')
description?stringOptional 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

ParameterTypeDescription
stackStackThe parent stack instance
classNamestringThe 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

ParameterTypeDescription
stackStackThe parent stack instance
classIdstringThe 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

ParameterTypeDescription
stackStackThe parent stack instance
idstringThe class ID
namestringThe class name
type"class" | "~self"The class type
description?stringOptional description
schema?{ [name: string]: AttributeModel; }Initial schema definition
options?ClassBuildOptions-

Returns

Class

A new Class instance (not persisted)

Overrides

Class_.get