Introduction Select Fields
Select fields in Remult allows you to select only the fields you need from an entity. This is useful when you want to reduce the amount of data groing from client to server.
How to
Create a new entity, and point to an existing table in the database using the dbName option.
import { Entity, Fields, SqlDatabase } from 'remult'
SqlDatabase.LogToConsole = 'oneLiner'
@Entity<TaskLight>('TaskLight', { dbName: 'tasks', allowApiRead: true, allowApiCrud: false,})export class TaskLight { @Fields.id() id = ''
@Fields.string() title = ''}Code Explanation
- Be aware that it’s a complete separate entity, so make sure you set the right access control.
- Here,
allowApiCrudoptions is set to false, andallowApiReadis set to true so this entity is read-only (it’s also the default, just wanted to show you can set it explicitly)
Try it out
// Get Task fieldsawait repo(Task).find()
// TaskLight fields onlyawait repo(TaskLight).find()See also
We showed that you can use dbName to point at an existing table in the database.
You can also create a dynamic view, Leveraging Database Capabilities with sqlExpression.
Files
Preparing Environment
- Installing dependencies
- Starting http server