Zum Hauptinhalt springen

Overview

Entities module for managing app data. This module provides dynamic access to all entities in the app. Each entity gets a handler with full CRUD operations and additional utility methods. Entities are accessed dynamically using the pattern: base44.entities.EntityName.method() This module is available to use with a client in all authentication modes:
  • Anonymous or User authentication (base44.entities): Access is scoped to the current user’s permissions. Anonymous users can only access public entities, while authenticated users can access entities they have permission to view or modify.
  • Service role authentication (base44.asServiceRole.entities): Operations bypass entity access rules and field-level security entirely. Can read and write any record in any entity.

Entity Handlers

An entity handler is the object you get when you access an entity through base44.entities.EntityName. Every entity in your app automatically gets a handler with CRUD methods for managing records. For example, base44.entities.Task is an entity handler for Task records, and base44.entities.User is an entity handler for User records. Each handler provides methods like list(), create(), update(), and delete(). You don’t need to instantiate or import entity handlers. They’re automatically available for every entity you create in your app.

Built-in User Entity

Every app includes a built-in User entity that stores user account information. This entity has special security rules that can’t be changed. Regular users can only read and update their own user record. With service role authentication, you can read, update, and delete any user. You can’t create users using the entities module. Instead, use the functions of the auth module to invite or register new users.

Generated Types

If you’re working in a TypeScript project, you can generate types from your entity schemas to get autocomplete and type checking on all entity methods. See the Dynamic Types guide to get started.

Examples

Entity Handler Methods


get()

get(id): Promise<T>
Gets a single record by ID. Retrieves a specific record using its unique identifier.

Parameters

id
string
erforderlich
The unique identifier of the record.

Returns

Promise<T> Promise resolving to the record.

Example


Entity handler providing CRUD operations for a specific entity type. Each entity in the app gets a handler with these methods for managing data.

list()

list<K extends keyof T>(
sort?,
limit?,
skip?,
fields?
): Promise<Pick<T, K>[]>
Lists records with optional pagination and sorting. Retrieves all records of this type with support for sorting, pagination, and field selection. Note: The maximum limit is 5,000 items per request.

Parameters

sort
SortField<T>
A SortField<T> specifying sort order, such as '-created_date' for descending. Defaults to '-created_date'.
limit
number
Maximum number of results to return. Defaults to 50.
skip
number
Number of results to skip for pagination. Defaults to 0.
fields
(keyof T)[]
Array of field names to include in the response. Defaults to all fields.

Returns

Promise resolving to an array of records with selected fields.

Examples


filter()

filter<K extends keyof T>(
query,
sort?,
limit?,
skip?,
fields?
): Promise<Pick<T, K>[]>
Filters records based on a query. Retrieves records that match specific criteria with support for sorting, pagination, and field selection. Note: The maximum limit is 5,000 items per request.

Parameters

query
EntityFilterQuery<T>
erforderlich
Query object with field-value pairs. Each key should be a field name from your entity schema, and each value is the criteria to match. Records matching all specified criteria are returned. Field names are case-sensitive. Use field-value pairs for exact matches, null for null values, arrays as shorthand for matching any of the provided values, or documented MongoDB query operators for advanced filtering.
sort
SortField<T>
A SortField<T> specifying sort order, such as '-created_date' for descending. Defaults to '-created_date'.
limit
number
Maximum number of results to return. Defaults to 50.
skip
number
Number of results to skip for pagination. Defaults to 0.
fields
(keyof T)[]
Array of field names to include in the response. Defaults to all fields.

Returns

Promise resolving to an array of filtered records with selected fields.

Examples


create()

create(data): Promise<T>
Creates a new record. Creates a new record with the provided data.

Parameters

data
Partial<T>
erforderlich
Object containing the record data.

Returns

Promise<T> Promise resolving to the created record.

Example


bulkCreate()

bulkCreate(data): Promise<T[]>
Creates multiple records in a single request. Efficiently creates multiple records at once. This is faster than creating them individually.

Parameters

data
Partial<T>[]
erforderlich
Array of record data objects.

Returns

Promise<T[]> Promise resolving to an array of created records.

Example


importEntities()

importEntities(file): Promise<ImportResult<T>>
Imports records from a file. Imports records from a file, typically CSV or similar format. The file format should match your entity structure. Requires a browser environment and can’t be used in the backend.

Parameters

file
File
erforderlich
File object to import.

Returns

ImportResult<T> Result returned when importing entities from a file.
status
"success" | "error"
erforderlich
Status of the import operation.
details
string | null
erforderlich
Details message, e.g., “Successfully imported 3 entities with RLS enforcement”.
output
T[] | null
erforderlich
Array of created entity objects when successful, or null on error.

Example


update()

update(id, data): Promise<T>
Updates an existing record. Updates a record by ID with the provided data. Only the fields included in the data object will be updated. To update a single record by ID, use this method. To apply the same update to many records matching a query, use updateMany(). To update multiple specific records with different data each, use bulkUpdate().

Parameters

id
string
erforderlich
The unique identifier of the record to update.
data
Partial<T>
erforderlich
Object containing the fields to update.

Returns

Promise<T> Promise resolving to the updated record.

Examples


updateMany()

updateMany(query, data): Promise<UpdateManyResult>
Applies the same update to all records that match a query. Use this when you need to make the same change across all records that match specific criteria. For example, you could set every completed order to “archived”, or increment a counter on all active users. Results are batched in groups of up to 500. When has_more is true in the response, call updateMany again with the same query to update the next batch. Make sure the query excludes already-updated records so you don’t re-process the same entities on each iteration. For example, filter by status: 'pending' when setting status to 'processed'. To update a single record by ID, use update() instead. To update multiple specific records with different data each, use bulkUpdate().

Parameters

query
Partial<T>
erforderlich
Query object to filter which records to update. Use field-value pairs for exact matches, or MongoDB query operators for advanced filtering. Supported query operators include $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin, $and, $or, $not, $nor, $exists, $regex, $all, $elemMatch, and $size.
data
Record<string, Record<string, any>>
erforderlich
Update operation object containing one or more MongoDB update operators. Each field may only appear in one operator per call. Supported update operators include $set, $rename, $unset, $inc, $mul, $min, $max, $currentDate, $addToSet, $push, and $pull.

Returns

UpdateManyResult Result returned when updating multiple entities using a query.
success
boolean
erforderlich
Whether the operation was successful.
updated
number
erforderlich
Number of entities that were updated.
has_more
boolean
erforderlich
Whether there are more entities matching the query that were not updated in this batch. When true, call updateMany again with the same query to update the next batch.

Examples


bulkUpdate()

bulkUpdate(data): Promise<T[]>
Updates the specified records in a single request, each with its own data. Use this when you already know which records to update and each one needs different field values. For example, you could update the status and amount on three separate invoices in one call. You can update up to 500 records per request. To apply the same update to all records matching a query, use updateMany(). To update a single record by ID, use update().

Parameters

data
(Partial<T> & { id: string })[]
erforderlich
Array of objects to update. Each object must contain an id field identifying which record to update and any fields to change.

Returns

Promise<T[]> Promise resolving to an array of the updated records.

Examples


delete()

delete(id): Promise<DeleteResult>
Deletes a single record by ID. Permanently removes a record from the database.

Parameters

id
string
erforderlich
The unique identifier of the record to delete.

Returns

DeleteResult Result returned when deleting a single entity.
success
boolean
erforderlich
Whether the deletion was successful.

Example


deleteMany()

deleteMany(query): Promise<DeleteManyResult>
Deletes multiple records matching a query. Permanently removes all records that match the provided query.

Parameters

query
Partial<T>
erforderlich
Query object with field-value pairs. Each key should be a field name from your entity schema, and each value is the criteria to match. Records matching all specified criteria will be deleted. Field names are case-sensitive.

Returns

DeleteManyResult Result returned when deleting multiple entities.
success
boolean
erforderlich
Whether the deletion was successful.
deleted
number
erforderlich
Number of entities that were deleted.

Example


subscribe()

subscribe(callback): () => void
Subscribes to realtime updates for all records of this entity type. Establishes a WebSocket connection to receive instant updates when any record is created, updated, or deleted. Returns an unsubscribe function to clean up the connection.

Parameters

callback
RealtimeCallback<T>
erforderlich
Callback function called when an entity changes. The callback receives an event object with the following properties:
  • type: The type of change that occurred - 'create', 'update', or 'delete'.
  • data: The entity data after the change.
  • id: The unique identifier of the affected entity.
  • timestamp: ISO 8601 timestamp of when the event occurred.

Returns

Unsubscribe function to stop receiving updates.

Example

Type Definitions

EntityRecord


EntityRecord = { [K in keyof EntityTypeRegistry]: EntityTypeRegistry[K] & ServerEntityFields }
Combines the EntityTypeRegistry schemas with server fields like id, created_date, and updated_date to give the complete record type for each entity. Use this when you need to type variables holding entity data.

Example

EntityTypeRegistry


Registry mapping entity names to their TypeScript types. The types generate command fills this registry, then EntityRecord adds server fields.

SortField


SortField<T> = keyof T | `+${keyof T}` | `-${keyof T}`
Sort field type for entity queries. Accepts any field name from the entity type with an optional prefix:
  • '+' prefix or no prefix: ascending sort
  • '-' prefix: descending sort

Example