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.

shared/TaskLight.ts
import { Entity, Fields, SqlDatabase } from 'remult'
SqlDatabase.LogToConsole = 'oneLiner'
@Entity<TaskLight>('TaskLight', {
dbName: 'tasks',
allowApiRead: true,
allowApiCrud: false,
})
export class TaskLight {
@Fields.uuid()
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, allowApiCrud options is set to false, and allowApiRead is 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 fields
await repo(Task).find()
// TaskLight fields only
await 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.

Powered by WebContainers
Files
Preparing Environment
  • Installing dependencies
  • Starting http server