Skip to content

ModelBase

Defined in: .work/repos/edinburgh/src/models.ts:491

Minimal instance-side model shape used for typing the constructor property.

LOOKUP extends ModelLookup = ModelLookup

constructor: LOOKUP & object & ModelClassRuntime<any, readonly any[], any, any> & (initial?, txn?) => any

Defined in: .work/repos/edinburgh/src/models.ts:492


_primaryKey: Uint8Array<ArrayBufferLike>

Defined in: .work/repos/edinburgh/src/models.ts:706


_primaryKeyHash: number

Defined in: .work/repos/edinburgh/src/models.ts:707


_txn: Transaction

Defined in: .work/repos/edinburgh/src/models.ts:708

optional preCommit(): void

Defined in: .work/repos/edinburgh/src/models.ts:733

Optional hook called on each modified instance right before the transaction commits. Runs before data is written to disk, so changes made here are included in the commit.

Common use cases:

  • Computing derived or denormalized fields
  • Enforcing cross-field validation rules
  • Creating or updating related model instances (newly created instances will also have their preCommit() called)

void

const Post = E.defineModel("Post", class {
id = E.field(E.identifier);
title = E.field(E.string);
slug = E.field(E.string);
preCommit() {
this.slug = this.title.toLowerCase().replace(/\s+/g, "-");
}
}, { pk: "id" });

_setLoadedField(fieldName, value): void

Defined in: .work/repos/edinburgh/src/models.ts:735

string

any

void


_restoreLazyFields(): void

Defined in: .work/repos/edinburgh/src/models.ts:749

void


getPrimaryKey(): Uint8Array

Defined in: .work/repos/edinburgh/src/models.ts:762

Uint8Array

The primary key for this instance.


_setPrimaryKey(key, hash?): void

Defined in: .work/repos/edinburgh/src/models.ts:772

Uint8Array

number

void


getPrimaryKeyHash(): number

Defined in: .work/repos/edinburgh/src/models.ts:781

number

A 53-bit positive integer non-cryptographic hash of the primary key, or undefined if not yet saved.


isLazyField(field): boolean

Defined in: .work/repos/edinburgh/src/models.ts:786

keyof ModelBase<LOOKUP>

boolean


_write(txn): Change

Defined in: .work/repos/edinburgh/src/models.ts:791

Transaction

Change


preventPersist(): ModelBase<LOOKUP>

Defined in: .work/repos/edinburgh/src/models.ts:881

Prevent this instance from being persisted to the database.

ModelBase<LOOKUP>

This model instance for chaining.

const user = User.get("user123");
user.name = "New Name";
user.preventPersist(); // Changes won't be saved

delete(): void

Defined in: .work/repos/edinburgh/src/models.ts:902

Delete this model instance from the database.

Removes the instance and all its index entries from the database and prevents further persistence.

void

const user = User.get("user123");
user.delete(); // Removes from database

validate(raise?): Error[]

Defined in: .work/repos/edinburgh/src/models.ts:921

Validate all fields in this model instance.

boolean = false

If true, throw on first validation error.

Error[]

Array of validation errors (empty if valid).

const user = new User();
const errors = user.validate();
if (errors.length > 0) {
console.log("Validation failed:", errors);
}

isValid(): boolean

Defined in: .work/repos/edinburgh/src/models.ts:946

Check if this model instance is valid.

boolean

true if all validations pass.

const user = new User({name: "John"});
if (!user.isValid()) shoutAtTheUser();

getState(): "created" | "deleted" | "loaded" | "lazy"

Defined in: .work/repos/edinburgh/src/models.ts:950

"created" | "deleted" | "loaded" | "lazy"


toString(): string

Defined in: .work/repos/edinburgh/src/models.ts:961

string