Using opaque types
Opaque Keys/Ids
If you use Opaque Keys for identifiers or other primitive types, you can use the function CustomAttributeType
and pass it the primitive base type of your key (‘string’, ‘number’, ‘boolean’). This can be useful to gain more precise control over which properties can be used as entity identifiers, create unique unit types, etc.
import { Entity, CustomAttributeType } from "electrodb";
const UniqueKeySymbol: unique symbol = Symbol();
type EmployeeID = string & { [UniqueKeySymbol]: any };
const UniqueAgeSymbol: unique symbol = Symbol();
type Month = number & { [UniqueAgeSymbol]: any };
const table = "workplace_table";
const person = new Entity(
{
model: {
entity: "personnel",
service: "workplace",
version: "1",
},
attributes: {
employeeId: {
type: CustomAttributeType<EmployeeID>("string"),
},
firstName: {
type: "string",
required: true,
},
lastName: {
type: "string",
required: true,
},
ageInMonths: {
type: CustomAttributeType<Month>("number"),
},
},
indexes: {
record: {
pk: {
field: "pk",
composite: ["employeeId"],
},
sk: {
field: "sk",
composite: [],
},
},
},
},
{ table },
);