You are a full-stack expert using remult with:
- TypeScript
Key Principles
- Remult is the single source of truth for your application.
## Entities are the SSoT
```ts filename=src/shared/Task.ts
import { Entity, Fields } from 'remult'
@Entity('tasks', {
allowApiCrud: true,
})
export class Task {
@Fields.cuid()
id!: string
@Fields.string()
title: string = ''
@Fields.boolean()
completed: boolean = false
@Fields.createdAt()
createdAt?: Date
}
```
## In the backend and the frontend you can do Pagination, Sorting, Filtering and Aggregation
```ts filename=src/shared/Task.ts
import { repo } from 'remult'
repo(Task)
.find({
limit: 20,
orderBy: { createdAt: "asc" },
where: { completed: true }
})
```
## All CRUD operations are available in frontend and backend
```ts filename=src/shared/Task.ts
// create
await repo(Task).insert({ title: newTaskTitle });
// update
await repo(Task).update(taskId, { title: newTaskTitle });
// delete
await repo(Task).delete(taskId);
```
## Add validation to your entities
```ts filename=src/shared/Task.ts
@Entity('tasks', {
allowApiCrud: true,
})
export class Task {
import { Validators } from 'remult';
@Fields.string({
validate: Validators.required
})
title: string = '';
}
```
## Live Queries
```ts filename=src/shared/Task.ts
repo(Task)
.liveQuery({ where: { completed: true } })
.subscribe((info) => {
tasks = info.applyChanges(tasks);
});
```
## Database
By default Remult use JSON database, but you can use any database listed here: https://remult.dev/docs/installation/database/
example for PostgreSQL:
```ts filename=remult.config.ts
import { createPostgresDataProvider } from 'remult/postgres'
// Server options
{
// ...
entities: [Task],
dataProvider: DATABASE_URL
? createPostgresDataProvider({ connectionString: DATABASE_URL })
: undefined,
// ...
}
```
Documentation
- Remult Documentation: https://remult.dev/docs