Serial
The Serial
class generates incremental IDs. It’s used to generate IDs for RPC requests in bidirectional communication. IDs are generated cyclically to prevent exceeding a safe integer value. If the ID exceeds that value, it resets to 1.
Import
import { Serial } from "@tai-kun/surrealdb/utils";
.constructor()
new Serial(max?: number);
Arguments
max
Sets the maximum value for the incremental ID. Integers from 1 to 2^53-1 are valid. The default value is 2^53-1.
Return Value
When called via new
, Serial
returns its instance.
Example
The following example demonstrates basic usage:
import { Serial } from "@tai-kun/surrealdb/utils";
const id = new Serial(3);
console.log(id.next());console.log(id.next());console.log(id.next());
console.log(id.next());console.log(id.next());
id.reset();
console.log(id.next());console.log(id.next());
Output:
1231212
.next()
instance method
.next
is a function that retrieves the next ID.
next(): number;
Arguments
None.
Return Value
Returns an integer between 1 and max
, inclusive. If this value is the maximum value, the next retrieved value will be 1.
.reset()
instance method
reset(): void;
Arguments
None.
Return Value
None.