Skip to main content

Standard Model Methods

Hysteria ORM models provide a rich set of static methods for interacting with your database. These methods are fully type-safe and support advanced options.

CRUD Methods

find

Fetch multiple records matching criteria.

const users = await User.find({ where: { status: 'active' } });

findOne

Fetch a single record matching criteria.

const user = await User.findOne({ where: { email: 'john@example.com' } });

findOneOrFail

Fetch a single record or throw if not found.

const user = await User.findOneOrFail({ where: { email: 'john@example.com' } });

insert

Insert a new record.

const user = await User.insert({ name: 'John', email: 'john@example.com' });

insertMany

Insert multiple records.

const users = await User.insertMany([
{ name: 'John', email: 'john@example.com' },
{ name: 'Jane', email: 'jane@example.com' },
]);

updateRecord

Update a record by primary key.

user.name = 'Johnny';
const updated = await User.updateRecord(user);

deleteRecord

Delete a record by primary key.

await User.deleteRecord(user);

firstOrInsert

Find a record or create it if it doesn't exist.

const user = await User.firstOrInsert(
{ email: 'john@example.com' },
{ name: 'John', email: 'john@example.com' }
);

upsert

Insert or update a record based on conflict keys.

const user = await User.upsert(
{ email: 'john@example.com' },
{ name: 'John', email: 'john@example.com' }
);

upsertMany

Insert or update multiple records.

const users = await User.upsertMany(
['email'],
[
{ email: 'john@example.com', name: 'John' },
{ email: 'jane@example.com', name: 'Jane' },
]
);

Best Practices

  • Always use the static methods for database operations.
  • Use findOneOrFail for required lookups.
  • Use firstOrInsert and upsert for idempotent operations.

Next: Query Builder