# new Sequence(options)
Provides a framework for generating sequential values (like primary keys) in the persistence layer. Implementations of this class handle the specifics of how sequences are stored and incremented in different database systems.
Abstract base class for sequence generation
Parameters:
Name | Type | Description |
---|---|---|
options |
SequenceOptions
|
Configuration options for the sequence generator |
Example
```typescript
// Example implementation for a specific database
class PostgresSequence extends Sequence {
constructor(options: SequenceOptions) {
super(options);
}
async next(): Promise<number> {
// Implementation to get next value from PostgreSQL sequence
const result = await this.options.executor.raw(`SELECT nextval('${this.options.name}')`);
return parseInt(result.rows[0].nextval);
}
async current(): Promise<number> {
// Implementation to get current value from PostgreSQL sequence
const result = await this.options.executor.raw(`SELECT currval('${this.options.name}')`);
return parseInt(result.rows[0].currval);
}
async range(count: number): Promise<number[]> {
// Implementation to get a range of values
const values: number[] = [];
for (let i = 0; i < count; i++) {
values.push(await this.next());
}
return values;
}
}
// Usage
const sequence = new PostgresSequence({
name: 'user_id_seq',
executor: dbExecutor
});
const nextId = await sequence.next();
```
Classes
- Sequence
Protected constructor that initializes the sequence with the provided options
- Sequence
Protected constructor that initializes the sequence with the provided options
Members
# current
Current value of the sequence Used to generate the next sequential value
.Current value of the sequence Used to generate the next sequential value
string
|
number
# current
Current value of the sequence Used to generate the next sequential value
.Current value of the sequence Used to generate the next sequential value
Methods
# abstract current()
Retrieves the current value of the sequence without incrementing it
Gets the current value of the sequence
A promise that resolves to the current value in the sequence
# protected log() → {Logger}
Gets or initializes the logger for this sequence
Accessor for the logger instance
The logger instance
Logger
# abstract next()
Retrieves the next value from the sequence, incrementing it in the process
Gets the next value in the sequence
A promise that resolves to the next value in the sequence
# abstract range(count)
Retrieves multiple sequential values at once, which can be more efficient than calling next() multiple times
Gets a range of sequential values
Parameters:
Name | Type | Description |
---|---|---|
count |
number
|
The number of sequential values to retrieve |
A promise that resolves to an array of sequential values
# static parseValue(type, value) → {string|number|bigint}
Converts a sequence value to the specified type (Number or BigInt)
Parses a sequence value to the appropriate type
Parameters:
Name | Type | Description |
---|---|---|
type |
"Number"
|
"BigInt"
|
undefined
|
The target type to convert to |
value |
string
|
number
|
bigint
|
The value to convert |
The converted value
string
|
number
|
bigint
# static pk(model) → {string}
Utility method that returns the standardized sequence name for a model's primary key
Gets the primary key sequence name for a model
Parameters:
Name | Type | Description |
---|---|---|
model |
M
|
Constructor.<M>
|
The model instance or constructor |
The sequence name for the model's primary key
string