Returns the primary keys of entities or tables.
// Retrieve the property the table uses as the primary key
const pk = key(userTable);
// Retrieve the primary key of an entity
const primaryKeysOfBobUsers = await key(userTable, many(userTable, {
where: {
name: "Bob"
}
}));
Parameter | Description |
---|---|
table
|
The table created by createTable() .
|
items
| Optional. Items to retrieve the primary key of. |
If items
is not specified, key()
will return the property used as a primary key by the table.
const db = createDB();
const userTable = createTable<{ uuid: string; }>(db, "users")({
primary: "uuid"
});
const pk = key(userTable); // => "uuid"
If items
is one or multiple entities, key()
will return their primary key.
const pkOfAlice = key(userTable, await first(userTable, {
where: {
name: "Alice"
}
}));
key()
also accepts promises as second arguments - the above code could also be rewritten like this:
const pkOfAlice = await key(userTable, first(userTable, {
where: {
name: "Alice"
}
}));