Skip to main content

Utilities

The package includes a number of hooks that you can use in your work.

useRattusContext

Returns a special object RattusContext, which provides access to managing databases and obtaining repositories.

declare class RattusContext {
$database: Database;
$databases: Record<string, Database>;
createDatabase(connection?: string, isPrimary?: boolean): Database;
$repo<M extends typeof Model>(model: M, connection?: string): Repository<InstanceType<M>>;
}

useRepository

The useRepository composition function returns all methods of the Repository class. These methods can be used with destructuring:

function App() {
const { query, save } = useRepository(User)
const user = query().where('id', '1').first()

return (
<>
<p>{ user.email }</p>
<button type="button" onClick={() => save({ id: '1', email: 'updated@test.com' })}>Update email</button>
</>
)
}

Remember that the obtained method works only with the data of the User model. To work with other models, you can invoke the hook again.

If you have previously registered a custom repository, you can pass it as a parameter in the generic: useRepository<UserCustomRepository>(User). All custom methods and properties will also be available for destructuring.

Reactivity

Unlike integrations with Vue, the obtained data are already reactive. In the example above, the output of user.email will update immediately.

However, it is important to remember the context in which you are working. If you call the repository methods inside a functional component, the data will be reactive, and changes will trigger re-renders.

When used outside of a component, the Data provider will return non-reactive data. Under the hood, it uses the Redux hook useSelector, or direct access to the state. The method of data retrieval is determined automatically.