Services
In ElectroDB a Service represents a collection of related Entities. Services allow you to build queries that span across Entities. Similar to Entities, Services can coexist on a single table without collision. You can use Entities independent of Services, and you do not need to import models into a Service to use them individually. However, you do need to use a Service if you intend to make queries that “join” multiple Entities.
Creation
The property name you assign the entity will become the “alias” by which you can reference the entity through the Service. Aliases can be useful if you are building a service with multiple versions of the same entity, or if you wish to change the reference name of an entity without impacting the schema/key names of that entity.
import { Service } from "electrodb";
const TaskApp = new Service(
{
employee: Employee, // available at TaskApp.entities.employee
task: Task, // available at TaskApp.entities.task
},
{ table, client },
);
As with Entities, Services take an optional second parameter that allows you to specify a client and table. Using this constructor interface, the Service will inherit these values from its entities, if they were provided. Values passed directly to the Service will override the client or table values on the individual entities.
Options
| Property | Description |
|---|---|
| table | The name of the DynamoDB table in AWS. |
| client | (optional) An instance of DynamoDBClient from the aws-sdk to be used when querying a DynamoDB table. This is optional if you only wish to use the params functionality, but required if you actually need to query against a database. |
Validation
When joining an Entity to a Service, ElectroDB will perform several validations to ensure the provided Entities are consistently defined. The following is a list of the validations performed on Service creation:
- Entity names must be unique across a Service.
- Collection names must be unique across a Service.
- All Collections map to the same DynamoDB indexes with the same index field names. See Indexes.
- Partition Key [Composite Attributes](#composite attribute-arrays) on a Collection must have the same attribute names and labels (if applicable). See Attribute Definitions.
- The name of the Service in the Model must match the Name defined on the Service instance.
- If the attributes of an Entity have overlapping names with other attributes in that service, they must all have compatible or matching attribute definitions.