Skip to content

Model

Creates a new Model instance.

Parameters

NameDescription
attributesInitial attributes for the model
forceFillWhether to bypass fillable restrictions

Example

typescript
interface UserAttributes {
  id: string;
  name: string;
  email: string;
  created_at?: Date;
  updated_at?: Date;
}

class User extends Model<UserAttributes> {
  protected static table = 'users';
  protected static primaryKey = 'id';
  protected static fillable = ['name', 'email'];

  // Define relationships
  posts() {
    return this.hasMany(Post);
  }

  profile() {
    return this.hasOne(Profile);
  }
}

// Usage
const user = await User.create({ name: 'John', email: 'john@example.com' });
const posts = await user.posts().get();

Model

Base Model class implementing Active Record pattern for ScyllinX ORM. Provides CRUD operations, relationships, attribute management, and ScyllaDB-specific features.

attributes

Current attribute values

original

Original attribute values from database

changes

Changed attributes since last sync

exists

Whether this model exists in the database

wasRecentlyCreated

Whether this model was recently created

defineAccessor

Defines a property accessor for an attribute. Allows direct property access to model attributes.

Parameters

NameDescription
keyThe attribute key to define accessor for

Example

typescript
// Internal usage - creates property accessors
user.name = 'John'; // Calls setAttribute internally
console.log(user.name); // Calls getAttribute internally

save

Saves the model to the database. Handles both creating new records and updating existing ones.

returns — Promise resolving to true if save was successful

update

Updates the model with new attributes and saves to database.

Parameters

NameDescription
attributesAttributes to update

Example

typescript
const user = await User.find('123');
await user.update({ name: 'Jane Doe', email: 'jane@example.com' });

returns — Promise resolving to true if update was successful

delete

Deletes the model from the database.

returns — Promise resolving to true if deletion was successful

refresh

Refreshes the model from the database. Reloads all attributes from the database, discarding any unsaved changes.

returns — Promise resolving to the refreshed model instance

replicate

Create an in-memory copy of this model, excluding its primary key, and apply any attribute overrides. Does NOT persist to the database until you call `save()` on the returned instance.

Parameters

NameDescription
overrides— Attributes to override on the replica.

Example

typescript
```ts
const user = await User.find('user-id-123');
// Clone with a new email
const newUser = user.replicate({
  email: 'new-email@example.com'
});
await newUser.save(); // Inserts as a new record



  `returnsthis` — A new model instance with copied attributes (primary key removed) and overrides applied.



## fill


Fills the model with attributes, respecting fillable/guarded rules.


### Parameters

| Name | Description |
|------|-------------|
| `attributes` | Attributes to fill |

### Example

```typescript
const user = new User();
user.fill({ name: 'John', email: 'john@example.com' });
// Only fillable attributes are set

returns — The model instance with filled attributes

forceFill

Fills the model with attributes, bypassing fillable/guarded rules.

Parameters

NameDescription
attributesAttributes to fill

Example

typescript
const user = new User();
user.forceFill({ id: '123', name: 'John', admin: true });
// All attributes are set, including guarded ones

returns — The model instance with filled attributes

touch

Update this model's `updated_at` timestamp (and optionally touch related models).

Parameters

NameDescription
relationsNames of relations to touch after updating this model.

Example

typescript
```ts
const user = await User.find('abc');
await user.touch();

// Also update timestamps on all related posts and comments
await user.touch(['posts', 'comments']);



  `returnsPromise.&lt;this&gt;` — Resolves to this model instance.



## getAttribute


Gets an attribute value from the model.
Applies mutators and casts if defined.


### Parameters

| Name | Description |
|------|-------------|
| `key` | The attribute key to get |

### Example

```typescript
const user = new User({ name: 'John' });
const name = user.getAttribute('name'); // 'John'

returns — The attribute value

setAttribute

Sets an attribute value on the model. Applies mutators and tracks changes.

Parameters

NameDescription
keyThe attribute key to set
valueThe value to set

Example

typescript
const user = new User();
user.setAttribute('name', 'John');
user.setAttribute('email', 'john@example.com');

returns — The model instance

getKey

Gets the model's primary key value.

returns — The primary key value

getKeyName

Gets the primary key column name.

returns — The primary key column name

getTable

Gets the table name for this model.

returns — The table name

getConnection

Gets the database connection name for this model.

returns — The connection name or undefined for default

isDirty

Checks if the model has unsaved changes.

Parameters

NameDescription
attributesOptional specific attributes to check

Example

typescript
const user = await User.find('123');
user.name = 'New Name';
console.log(user.isDirty()); // true
console.log(user.isDirty(['email'])); // false

returns — True if model has unsaved changes

getDirty

Gets the dirty (changed) attributes.

returns — Object containing changed attributes

setAttributes

Sets the model's attributes directly (internal use).

Parameters

NameDescription
attrsAttributes to set

Example

typescript
// Internal usage in hydration
model.setAttributes({ id: '123', name: 'John' });

setExists

Sets the model's existence state (internal use).

Parameters

NameDescription
existsWhether the model exists in database

Example

typescript
// Internal usage in hydration
model.setExists(true);

setOriginal

Sets the model's original attributes (internal use).

Parameters

NameDescription
attrsOriginal attributes to set

Example

typescript
// Internal usage in hydration
model.setOriginal({ id: '123', name: 'John' });

toObject

Converts the model to a plain object. Applies visibility rules (hidden/visible attributes).

returns — Plain object representation of the model

toJSON

Converts the model to JSON string.

returns — JSON string representation of the model

newQuery

Creates a new query builder for this model instance.

returns — QueryBuilder instance

performInsert

Performs database insert operation.

Parameters

NameDescription
queryQueryBuilder instance

returns — Promise that resolves when insert is complete

performUpdate

Performs database update operation.

Parameters

NameDescription
queryQueryBuilder instance

returns — Promise that resolves when update is complete

performDeleteOnModel

Performs database delete operation.

returns — Promise that resolves when delete is complete

getAttributesForInsert

Gets attributes prepared for database insertion.

returns — Attributes object for insertion

updateTimestamps

Updates timestamp attributes.

getTimestamps

Checks if timestamps are enabled for this model.

returns — True if timestamps are enabled

getFillable

Gets the fillable attributes list.

returns — Array of fillable attribute names

getGuarded

Gets the guarded attributes list.

returns — Array of guarded attribute names

getHidden

Gets the hidden attributes list.

returns — Array of hidden attribute names

getVisible

Gets the visible attributes list.

returns — Array of visible attribute names

getCasts

Gets the attribute casting definitions.

returns — Object mapping attribute names to cast types

getScopes

Gets the query scopes defined on this model.

returns — Object mapping scope names to functions

isFillable

Checks if an attribute is fillable.

Parameters

NameDescription
keyAttribute name to check

returns — True if attribute is fillable

hasCast

Checks if an attribute has a cast defined.

Parameters

NameDescription
keyAttribute name to check

returns — True if attribute has a cast

castAttribute

Casts an attribute value to the specified type.

Parameters

NameDescription
keyAttribute name
valueValue to cast

returns — Casted value

castAttributeAsJson

Casts an attribute value for JSON storage.

Parameters

NameDescription
keyAttribute name
valueValue to cast

returns — Casted value for storage

hasGetMutator

Checks if an attribute has a getter mutator.

Parameters

NameDescription
keyAttribute name to check

returns — True if getter mutator exists

hasSetMutator

Checks if an attribute has a setter mutator.

Parameters

NameDescription
keyAttribute name to check

returns — True if setter mutator exists

mutateAttribute

Applies a getter mutator to an attribute value.

Parameters

NameDescription
keyAttribute name
valueValue to mutate

returns — Mutated value

mutateAttributeForArray

Applies a setter mutator to an attribute value.

Parameters

NameDescription
keyAttribute name
valueValue to mutate

returns — Mutated value

studly

Converts a string to StudlyCase.

Parameters

NameDescription
strString to convert

returns — StudlyCase string

fireModelEvent

Fires a model event.

Parameters

NameDescription
eventEvent name to fire
haltWhether to halt on false return

returns — Promise resolving to event result

finishSave

Finalizes the save operation by syncing state.

getPartitionKeys

Gets partition keys for ScyllaDB.

returns — Array of partition key column names

getClusteringKeys

Gets clustering keys for ScyllaDB.

returns — Array of clustering key column names

getKeyspace

Gets keyspace for ScyllaDB.

returns — Keyspace name or undefined

withTTL

Sets TTL for ScyllaDB operations.

Parameters

NameDescription
secondsTTL in seconds

Example

typescript
const user = new User();
await user.withTTL(3600).save(); // Expires in 1 hour

returns — QueryBuilder with TTL set

ifNotExists

Uses IF NOT EXISTS for ScyllaDB operations.

returns — QueryBuilder with IF NOT EXISTS set

load

Lazy loads relationships on this model instance.

Parameters

NameDescription
relationsRelationship names to load

Example

typescript
const user = await User.find('123');
await user.load('posts', 'profile');
console.log(user.posts); // Now loaded

returns — Promise resolving to this model with loaded relationships

hasOne

Defines a one-to-one relationship.

Parameters

NameDescription
relatedRelated model class
foreignKeyForeign key column name
localKeyLocal key column name

Example

typescript
class User extends Model {
  profile() {
    return this.hasOne(Profile, 'user_id', 'id');
  }
}

returns — HasOne relationship instance

hasMany

Defines a one-to-many relationship.

Parameters

NameDescription
relatedRelated model class
foreignKeyForeign key column name
localKeyLocal key column name

Example

typescript
class User extends Model {
  posts() {
    return this.hasMany(Post, 'user_id', 'id');
  }
}

returns — HasMany relationship instance

belongsTo

Defines an inverse one-to-one or one-to-many relationship.

Parameters

NameDescription
relatedRelated model class
foreignKeyForeign key column name
ownerKeyOwner key column name

Example

typescript
class Post extends Model {
  user() {
    return this.belongsTo(User, 'user_id', 'id');
  }
}

returns — BelongsTo relationship instance

belongsToMany

Defines a many-to-many relationship.

Parameters

NameDescription
relatedRelated model class
pivotTablePivot table name
foreignPivotKeyForeign pivot key column name
relatedPivotKeyRelated pivot key column name
parentKeyParent key column name
relatedKeyRelated key column name

Example

typescript
class User extends Model {
  roles() {
    return this.belongsToMany(Role, 'user_roles', 'user_id', 'role_id');
  }
}

returns — BelongsToMany relationship instance

morphOne

Defines a polymorphic one-to-one relationship.

Parameters

NameDescription
relatedRelated model class
nameMorph name
typeMorph type column name
idMorph ID column name
localKeyLocal key column name

Example

typescript
class User extends Model {
  avatar() {
    return this.morphOne(Image, 'imageable');
  }
}

returns — MorphOne relationship instance

morphMany

Defines a polymorphic one-to-many relationship.

Parameters

NameDescription
relatedRelated model class
nameMorph name
typeMorph type column name
idMorph ID column name
localKeyLocal key column name

Example

typescript
class Post extends Model {
  comments() {
    return this.morphMany(Comment, 'commentable');
  }
}

returns — MorphMany relationship instance

morphTo

Defines a polymorphic belongs-to relationship.

Parameters

NameDescription
nameMorph name
typeMorph type column name
idMorph ID column name
ownerKeyOwner key column name

Example

typescript
class Comment extends Model {
  commentable() {
    return this.morphTo('commentable');
  }
}

returns — MorphTo relationship instance

primaryKey

The primary key column name

partitionKeys

ScyllaDB partition key columns

clusteringKeys

ScyllaDB clustering key columns

fillable

Attributes that are mass assignable

guarded

Attributes that are not mass assignable

hidden

Attributes that should be hidden from serialization

visible

Attributes that should be visible in serialization

casts

Attribute casting definitions

dates

Date attribute names

timestamps

Whether to automatically manage timestamps

softDeletes

Whether to use soft deletes

scopes

Query scopes defined on this model

custom

Custom inspect method for better debugging output.

returns — Object representation for debugging

query

Creates a new query builder for the model. Entry point for all database queries on this model.

Parameters

NameDescription
thisThe model class (static context)

Example

typescript
const activeUsers = await User.query().where('status', 'active').get();
const user = await User.query().where('id', '123').first();
const count = await User.query().count();

returns — QueryBuilder instance configured for this model

find

Finds a model by its primary key.

Parameters

NameDescription
thisThe model class (static context)
idThe primary key value to search for

Example

typescript
const user = await User.find('123');
if (user) {
  console.log(user.name);
}

const nonExistent = await User.find('999'); // Returns null

returns — Promise resolving to model instance or null if not found

findOrFail

Finds a model by its primary key or throws an exception.

Parameters

NameDescription
thisThe model class (static context)
idThe primary key value to search for

Example

typescript
try {
  const user = await User.findOrFail('123');
  console.log(user.name);
} catch (error) {
  console.log('User not found');
}

returns — Promise resolving to model instance

create

Creates a new model instance and saves it to the database.

Parameters

NameDescription
thisThe model class (static context)
attrsAttributes for the new model

Example

typescript
const user = await User.create({
  name: 'John Doe',
  email: 'john@example.com'
});
console.log(user.id); // Auto-generated ID

returns — Promise resolving to the created model instance

createMany

Creates multiple records in bulk.

For each attribute set in `items`, a new model instance is created (bypassing fillable), saved to the database, and collected into an array.

Parameters

NameDescription
itemsArray of attribute objects to insert.

Example

typescript
// Create multiple users at once
const users = await User.createMany([
  { name: 'Alice', email: 'alice@example.com' },
  { name: 'Bob', email: 'bob@example.com' },
  { name: 'Charlie', email: 'charlie@example.com' }
]);

console.log(`Created ${users.length} users`);

returns — - Resolves with an array of newly created model instances.

all

Retrieves all models from the database.

Parameters

NameDescription
thisThe model class (static context)

Example

typescript
const allUsers = await User.all();
console.log(`Found ${allUsers.length} users`);

returns — Promise resolving to array of model instances

first

Gets the first model matching the query.

Parameters

NameDescription
thisThe model class (static context)

Example

typescript
const firstUser = await User.first();
if (firstUser) {
  console.log('First user:', firstUser.name);
}

returns — Promise resolving to first model instance or null

updateOrCreate

Updates an existing model or creates a new one.

Parameters

NameDescription
thisThe model class (static context)
attributesAttributes to search by
valuesValues to update or create with

Example

typescript
const user = await User.updateOrCreate(
  { email: 'john@example.com' },
  { name: 'John Doe', status: 'active' }
);

returns — Promise resolving to the model instance

Released under the MIT License.