Class: Domain
Defined in: packages/client/src/core/domain.ts:34
Represents a relationship definition between two Classes in the DocStack database.
A Domain defines how documents of different classes relate to each other, supporting cardinality constraints (1:1, 1:N, N:1, N:N). Relation documents are created to track the links between source and target documents.
Use the static factory methods (Domain.create, Domain.fetch) to instantiate domains - the constructor is private.
Example
// Define a 1:N relationship between Projects and Tasks
const projectTaskDomain = await Domain.create(
stack,
null,
'ProjectTasks',
'domain',
'1:N',
projectClass,
taskClass
);
// Create a relation between a project and a task
await projectTaskDomain.addRelation(taskDoc, 'Project-1');
Extends
Domain
Properties
| Property | Modifier | Type | Default value | Description | Overrides | Inherited from | Defined in |
|---|---|---|---|---|---|---|---|
close | public | () => void | undefined | Releases this instance's live document subscription. The Domain counterpart to Class.close: a Domain no longer in use stays subscribed for the lifetime of the stack until this is called. Safe to call twice. | - | Domain_.close | packages/shared/lib/utils/stack/domain.d.ts:29 |
description | public | string | undefined | Optional description of the domain purpose. | Domain_.description | - | packages/client/src/core/domain.ts:42 |
id | public | string | undefined | The unique identifier for this domain. | Domain_.id | - | packages/client/src/core/domain.ts:44 |
logger | public | Logger | undefined | - | Domain_.logger | - | packages/client/src/core/domain.ts:56 |
model | public | DomainModel | undefined | The underlying DomainModel document. | Domain_.model | - | packages/client/src/core/domain.ts:52 |
name | public | string | undefined | The name of this domain (e.g., 'ProjectTasks'). | Domain_.name | - | packages/client/src/core/domain.ts:38 |
relation | public | "1:1" | "1:N" | "N:1" | "N:N" | undefined | The cardinality of the relationship: '1:1', '1:N', 'N:1', or 'N:N'. | Domain_.relation | - | packages/client/src/core/domain.ts:46 |
sourceClass | public | Class | undefined | The source class in the relationship. | Domain_.sourceClass | - | packages/client/src/core/domain.ts:48 |
stack | public | Stack | undefined | Reference to the parent stack instance. | Domain_.stack | - | packages/client/src/core/domain.ts:36 |
state | public | "busy" | "idle" | "idle" | Current state indicating if the domain is processing an operation. | Domain_.state | - | packages/client/src/core/domain.ts:54 |
targetClass | public | Class | undefined | The target class in the relationship. | Domain_.targetClass | - | packages/client/src/core/domain.ts:50 |
type | public | "~self" | "domain" | undefined | The domain type (typically 'domain'). | Domain_.type | - | packages/client/src/core/domain.ts:40 |
logger | static | Logger | undefined | - | Domain_.logger | - | packages/client/src/core/domain.ts:55 |
Methods
addRelation()
addRelation(document, referenceId): Promise<RelationDocument>;
Defined in: packages/client/src/core/domain.ts:469
Creates a relation between documents. Validates cardinality constraints before creating the relation.
Parameters
| Parameter | Type | Description |
|---|---|---|
document | Document | The document to link (source or target based on role) |
referenceId | string | The ID of the document to link to |
Returns
Promise<RelationDocument>
The created relation document, or existing relation if already exists
Example
// Link a task to a project (assuming 1:N from Project to Task)
const relation = await projectTaskDomain.addRelation(taskDoc, 'Project-1');
Overrides
Domain_.addRelation
assertReferenceAllowed()
assertReferenceAllowed(role): void;
Defined in: packages/client/src/core/domain.ts:321
Parameters
| Parameter | Type |
|---|---|
role | "target" | "source" |
Returns
void
Overrides
Domain_.assertReferenceAllowed
build()
build(): Promise<Domain>;
Defined in: packages/client/src/core/domain.ts:90
Returns
Promise<Domain>
Overrides
Domain_.build
buildRelationParams()
buildRelationParams(
doc,
referenceId,
role): DomainRelationParams;
Defined in: packages/client/src/core/domain.ts:342
Parameters
| Parameter | Type |
|---|---|
doc | Document |
referenceId | string |
role | "target" | "source" |
Returns
DomainRelationParams
Overrides
Domain_.buildRelationParams
deleteRelation()
deleteRelation(sourceId, targetId): Promise<boolean>;
Defined in: packages/client/src/core/domain.ts:490
Deletes a relation between two documents by their IDs.
Parameters
| Parameter | Type | Description |
|---|---|---|
sourceId | string | The source document ID |
targetId | string | The target document ID |
Returns
Promise<boolean>
true if deleted, false if relation not found
Overrides
Domain_.deleteRelation
deleteRelationDoc()
deleteRelationDoc(relationDocId): Promise<boolean>;
Defined in: packages/client/src/core/domain.ts:507
Deletes a relation document by its ID.
Parameters
| Parameter | Type | Description |
|---|---|---|
relationDocId | string | The relation document ID to delete |
Returns
Promise<boolean>
true if deleted successfully
Overrides
Domain_.deleteRelationDoc
ensureCardinalityConstraints()
ensureCardinalityConstraints(params): Promise<void>;
Defined in: packages/client/src/core/domain.ts:395
Parameters
| Parameter | Type |
|---|---|
params | DomainRelationParams |
Returns
Promise<void>
Overrides
Domain_.ensureCardinalityConstraints
fetchReferenceDocument()
fetchReferenceDocument(referenceId, expectedType): Promise<Document>;
Defined in: packages/client/src/core/domain.ts:365
Parameters
| Parameter | Type |
|---|---|
referenceId | string |
expectedType | string |
Returns
Promise<Document>
Overrides
Domain_.fetchReferenceDocument
findRelationDoc()
findRelationDoc(selector): Promise<RelationDocument>;
Defined in: packages/client/src/core/domain.ts:374
Parameters
| Parameter | Type |
|---|---|
selector | { [key: string]: any; } |
Returns
Promise<RelationDocument>
Overrides
Domain_.findRelationDoc
getDocumentRole()
getDocumentRole(doc): "target" | "source";
Defined in: packages/client/src/core/domain.ts:311
Parameters
| Parameter | Type |
|---|---|
doc | Document |
Returns
"target" | "source"
Overrides
Domain_.getDocumentRole
getModel()
getModel(): DomainModel;
Defined in: packages/client/src/core/domain.ts:288
Returns the current DomainModel representation of this domain.
Returns
The DomainModel document
Overrides
Domain_.getModel
getName()
getName(): string;
Defined in: packages/client/src/core/domain.ts:66
Returns
string
Overrides
Domain_.getName
getRelations()
getRelations(
selector?,
fields?,
skip?,
limit?): Promise<RelationDocument[]>;
Defined in: packages/client/src/core/domain.ts:447
Retrieves relation documents for this domain.
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 |
Returns
Promise<RelationDocument[]>
Array of matching relation documents
Example
// Get all relations
const allRelations = await domain.getRelations();
// Get relations for a specific source
const projectRelations = await domain.getRelations({ sourceId: { $eq: 'Project-1' } });
Overrides
Domain_.getRelations
getStack()
getStack(): Stack;
Defined in: packages/client/src/core/domain.ts:62
Returns
Stack
Overrides
Domain_.getStack
init()
init(
stack,
id,
name,
type,
relation,
sourceClass,
targetClass,
description?): void;
Defined in: packages/client/src/core/domain.ts:110
Parameters
| Parameter | Type |
|---|---|
stack | Stack |
id | string |
name | string |
type | "~self" | "domain" |
relation | "1:1" | "1:N" | "N:1" | "N:N" |
sourceClass | Class |
targetClass | Class |
description? | string |
Returns
void
Overrides
Domain_.init
requireStack()
requireStack(): Stack;
Defined in: packages/client/src/core/domain.ts:304
Returns
Stack
Overrides
Domain_.requireStack
setId()
setId(id): void;
Defined in: packages/client/src/core/domain.ts:70
Parameters
| Parameter | Type |
|---|---|
id | string |
Returns
void
Overrides
Domain_.setId
setModel()
setModel(model?): void;
Defined in: packages/client/src/core/domain.ts:74
Parameters
| Parameter | Type |
|---|---|
model? | DomainModel |
Returns
void
Overrides
Domain_.setModel
throwIfRelationExists()
throwIfRelationExists(filter, params): Promise<RelationDocument>;
Defined in: packages/client/src/core/domain.ts:384
Parameters
| Parameter | Type |
|---|---|
filter | { [key: string]: any; } |
params | DomainRelationParams |
Returns
Promise<RelationDocument>
Overrides
Domain_.throwIfRelationExists
validateRelation()
validateRelation(doc, referenceId): Promise<DomainRelationValidation>;
Defined in: packages/client/src/core/domain.ts:410
Parameters
| Parameter | Type |
|---|---|
doc | Document |
referenceId | string |
Returns
Promise<DomainRelationValidation>
Overrides
Domain_.validateRelation
buildFromModel()
static buildFromModel(stack, domainModel): Promise<Domain>;
Defined in: packages/client/src/core/domain.ts:233
Builds a Domain instance from an existing DomainModel document. Fetches the source and target classes and hydrates the domain.
Parameters
| Parameter | Type | Description |
|---|---|---|
stack | Stack | The parent stack instance |
domainModel | DomainModel | The DomainModel document from the database |
Returns
Promise<Domain>
The hydrated Domain instance
Throws
Error if source or target class is not found
Overrides
Domain_.buildFromModel
create()
static create(
stack,
id,
name,
type,
relation,
sourceClass,
targetClass,
description?,
schema?): Promise<Domain>;
Defined in: packages/client/src/core/domain.ts:204
Creates a new domain and persists it to the database. This is the primary factory method for creating new domains.
Parameters
| Parameter | Type | Description |
|---|---|---|
stack | Stack | The parent stack instance |
id | string | The domain ID (typically null for auto-generation) |
name | string | The name for the new domain |
type | "~self" | "domain" | The domain type (typically 'domain') |
relation | "1:1" | "1:N" | "N:1" | "N:N" | The cardinality constraint: '1:1', '1:N', 'N:1', or 'N:N' |
sourceClass | Class | The source Class in the relationship |
targetClass | Class | The target Class in the relationship |
description? | string | Optional description of the domain |
schema? | any | Optional schema definition |
Returns
Promise<Domain>
The persisted Domain instance
Example
const domain = await Domain.create(
stack, null, 'UserProjects', 'domain', '1:N',
userClass, projectClass, 'Users can have many projects'
);
Overrides
Domain_.create
fetch()
static fetch(stack, domainName): Promise<Domain>;
Defined in: packages/client/src/core/domain.ts:275
Fetches a domain by its name. This is the most common way to retrieve an existing domain.
Parameters
| Parameter | Type | Description |
|---|---|---|
stack | Stack | The parent stack instance |
domainName | string | The domain name to fetch |
Returns
Promise<Domain>
The Domain instance, or null if not found
Example
const domain = await Domain.fetch(stack, 'ProjectTasks');
if (domain) {
const relations = await domain.getRelations();
}
Overrides
Domain_.fetch
get()
static get(
stack,
id,
name,
type,
relation,
sourceClass,
targetClass,
description?,
schema?): Domain;
Defined in: packages/client/src/core/domain.ts:157
Gets a Domain instance without persisting it to the database. Use this for working with existing domain 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 domain ID (or null for new domains) |
name | string | The domain name |
type | "~self" | "domain" | The domain type (typically 'domain') |
relation | "1:1" | "1:N" | "N:1" | "N:N" | The cardinality constraint |
sourceClass | Class | The source Class in the relationship |
targetClass | Class | The target Class in the relationship |
description? | string | Optional description |
schema? | any | Optional schema definition |
Returns
Domain
A new Domain instance (not persisted)
Overrides
Domain_.get