PrimaryKey.ts
PrimaryKey.ts overview
Section titled “PrimaryKey.ts overview”The PrimaryKey module defines a small protocol for values that can expose
a stable, string-based identifier. A value participates by implementing a
method at symbol; consumers can check unknown values with
isPrimaryKey and read the key with value.
Since v2.0.0
Exports Grouped by Category
Section titled “Exports Grouped by Category”accessors
Section titled “accessors”Extracts the string value from a PrimaryKey.
When to use
Use to read the stable string identifier from a value that implements
PrimaryKey.
Example (Reading primary key values)
import { PrimaryKey } from "effect"
class OrderId implements PrimaryKey.PrimaryKey { constructor( private timestamp: number, private sequence: number ) {}
[PrimaryKey.symbol](): string { return `order_${this.timestamp}_${this.sequence}` }}
const orderId = new OrderId(1640995200000, 1)console.log(PrimaryKey.value(orderId)) // "order_1640995200000_1"
// Can also be used with simple string-based implementationsconst simpleKey = { [PrimaryKey.symbol]: () => "simple-key-123"}console.log(PrimaryKey.value(simpleKey)) // "simple-key-123"Signature
declare const value: (self: PrimaryKey) => stringSince v2.0.0
models
Section titled “models”PrimaryKey (interface)
Section titled “PrimaryKey (interface)”An interface for objects that can provide a string-based primary key.
When to use
Use to define values that expose a stable string identifier for equality, hashing, caching, or persistence.
Details
Objects implementing this interface must provide a method that returns a unique string identifier.
Example (Implementing a primary key)
import { PrimaryKey } from "effect"
class ProductId implements PrimaryKey.PrimaryKey { constructor( private category: string, private id: number ) {}
[PrimaryKey.symbol](): string { return `${this.category}-${this.id}` }}
const productId = new ProductId("electronics", 42)console.log(PrimaryKey.value(productId)) // "electronics-42"Signature
export interface PrimaryKey { [symbol](): string}Since v2.0.0
isPrimaryKey
Section titled “isPrimaryKey”Checks whether a value implements the PrimaryKey protocol.
When to use
Use to narrow an unknown value before treating it as a PrimaryKey.
Details
This is a structural guard for the PrimaryKey.symbol property.
Gotchas
This guard does not call the method or verify that it returns a string.
See
PrimaryKeyfor the protocol being checkedvaluefor extracting the string value after narrowing
Signature
declare const isPrimaryKey: (u: unknown) => u is PrimaryKeySince v4.0.0
symbols
Section titled “symbols”symbol
Section titled “symbol”Defines the unique identifier used to identify objects that implement the PrimaryKey interface.
When to use
Use to implement the PrimaryKey protocol as a computed property key on
classes or object literals that expose a stable string identifier.
See
PrimaryKeyfor the protocol interface that declares the method keyed by this symbolvaluefor reading the string key from aPrimaryKeyvalueisPrimaryKeyfor checking whether an unknown value carries this method
Signature
declare const symbol: "~effect/interfaces/PrimaryKey"Since v2.0.0