CRUD
Find by id
ts
const user = await User.crispy().find(42); // GET /users/42, 404 → nullCreate
ts
const fresh = await User.crispy().create({ name: 'Birtan', email: 'b@x' });
// POST /users — body: { name, email }
// returns: User instance with server-assigned idOr new + save:
ts
const u = new User({ name: 'Birtan' });
u.email = 'b@x';
await u.save(); // POST /users (id is missing → POST)
u.getKey(); // server-assigned idUpdate
save() chooses POST/PUT based on whether the model is persisted:
ts
const user = await User.crispy().find(42);
user.name = 'Updated';
await user.save(); // PUT /users/42 (id present → PUT)Delete
ts
await user.delete(); // DELETE /users/42Calling delete() on an unpersisted model throws.
Validation errors
Laravel returns 422 for failed validation. crisp-oquent maps that to HttpError.isValidationError:
ts
import { HttpError } from '@bir-tan/crisp-oquent';
try {
await new User({ email: '' }).save();
} catch (e) {
if (e instanceof HttpError && e.isValidationError) {
e.validationErrors; // { email: ['The email field is required.'] }
} else {
throw e;
}
}