@byline/db-postgres 3.18.0 → 3.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -17,6 +17,7 @@ export declare class CounterCommands implements ICounterCommands {
|
|
|
17
17
|
sequenceName: string;
|
|
18
18
|
}>;
|
|
19
19
|
nextCounterValue(groupName: string): Promise<number>;
|
|
20
|
+
nextScopedCounterValue(scopeName: string): Promise<number>;
|
|
20
21
|
}
|
|
21
22
|
export declare function createCounterCommands(db: DatabaseConnection): ICounterCommands;
|
|
22
23
|
export {};
|
|
@@ -90,6 +90,25 @@ export class CounterCommands {
|
|
|
90
90
|
}
|
|
91
91
|
return typeof row.value === 'number' ? row.value : Number(row.value);
|
|
92
92
|
}
|
|
93
|
+
async nextScopedCounterValue(scopeName) {
|
|
94
|
+
if (!scopeName || typeof scopeName !== 'string') {
|
|
95
|
+
throw new Error(`nextScopedCounterValue: scopeName must be a non-empty string`);
|
|
96
|
+
}
|
|
97
|
+
// Fast path: the scope has been used before — its registry row and
|
|
98
|
+
// sequence already exist, so this is exactly a nextCounterValue call.
|
|
99
|
+
const rows = await this.db
|
|
100
|
+
.select({ sequence_name: counterGroups.sequence_name })
|
|
101
|
+
.from(counterGroups)
|
|
102
|
+
.where(eq(counterGroups.group_name, scopeName))
|
|
103
|
+
.limit(1);
|
|
104
|
+
if (rows.length === 0) {
|
|
105
|
+
// First allocation for this scope: self-register (idempotent — a
|
|
106
|
+
// concurrent racer leaves exactly one sequence + registry row, and
|
|
107
|
+
// both callers draw distinct values from the same sequence).
|
|
108
|
+
await this.ensureCounterGroup(scopeName);
|
|
109
|
+
}
|
|
110
|
+
return this.nextCounterValue(scopeName);
|
|
111
|
+
}
|
|
93
112
|
}
|
|
94
113
|
export function createCounterCommands(db) {
|
|
95
114
|
return new CounterCommands(db);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This Source Code is subject to the terms of the Mozilla Public
|
|
3
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) Infonomic Company Limited
|
|
7
|
+
*/
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This Source Code is subject to the terms of the Mozilla Public
|
|
3
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) Infonomic Company Limited
|
|
7
|
+
*/
|
|
8
|
+
import { eq, sql } from 'drizzle-orm';
|
|
9
|
+
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
|
10
|
+
import { counterGroups } from '../../../database/schema/index.js';
|
|
11
|
+
import { setupTestDB, teardownTestDB } from '../../../lib/test-helper.js';
|
|
12
|
+
import { createCounterCommands } from '../counters-commands.js';
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
// Track-and-clean fixtures
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
//
|
|
17
|
+
// Integration tests share the dev database. Each scope this suite touches
|
|
18
|
+
// creates a registry row + a Postgres sequence; track the scope names and
|
|
19
|
+
// drop both on teardown so repeated runs start from a clean slate (and so
|
|
20
|
+
// monotonicity assertions are not satisfied by a previous run's sequence).
|
|
21
|
+
let db;
|
|
22
|
+
let counters;
|
|
23
|
+
const trackedScopes = new Set();
|
|
24
|
+
async function cleanupScope(scopeName) {
|
|
25
|
+
const rows = await db
|
|
26
|
+
.select({ sequence_name: counterGroups.sequence_name })
|
|
27
|
+
.from(counterGroups)
|
|
28
|
+
.where(eq(counterGroups.group_name, scopeName))
|
|
29
|
+
.limit(1);
|
|
30
|
+
const sequenceName = rows[0]?.sequence_name;
|
|
31
|
+
await db.delete(counterGroups).where(eq(counterGroups.group_name, scopeName));
|
|
32
|
+
if (sequenceName) {
|
|
33
|
+
await db.execute(sql.raw(`DROP SEQUENCE IF EXISTS "${sequenceName}"`));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function scope(name) {
|
|
37
|
+
trackedScopes.add(name);
|
|
38
|
+
return name;
|
|
39
|
+
}
|
|
40
|
+
beforeAll(async () => {
|
|
41
|
+
const setup = setupTestDB();
|
|
42
|
+
db = setup.db;
|
|
43
|
+
counters = createCounterCommands(db);
|
|
44
|
+
// Clean any leftovers from a previously aborted run.
|
|
45
|
+
for (const name of trackedScopes) {
|
|
46
|
+
await cleanupScope(name);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
afterAll(async () => {
|
|
50
|
+
for (const name of trackedScopes) {
|
|
51
|
+
await cleanupScope(name);
|
|
52
|
+
}
|
|
53
|
+
await teardownTestDB();
|
|
54
|
+
});
|
|
55
|
+
describe('nextScopedCounterValue', () => {
|
|
56
|
+
it('self-registers an unknown scope and allocates from 1', async () => {
|
|
57
|
+
const scopeName = scope('test:doc-a:files');
|
|
58
|
+
const first = await counters.nextScopedCounterValue(scopeName);
|
|
59
|
+
expect(first).toBe(1);
|
|
60
|
+
// The registry row and its backing sequence now exist.
|
|
61
|
+
const rows = await db
|
|
62
|
+
.select({ sequence_name: counterGroups.sequence_name })
|
|
63
|
+
.from(counterGroups)
|
|
64
|
+
.where(eq(counterGroups.group_name, scopeName));
|
|
65
|
+
expect(rows).toHaveLength(1);
|
|
66
|
+
});
|
|
67
|
+
it('is monotonic within a scope and never reuses a value', async () => {
|
|
68
|
+
const scopeName = scope('test:doc-b:files');
|
|
69
|
+
const a = await counters.nextScopedCounterValue(scopeName);
|
|
70
|
+
const b = await counters.nextScopedCounterValue(scopeName);
|
|
71
|
+
const c = await counters.nextScopedCounterValue(scopeName);
|
|
72
|
+
expect([a, b, c]).toEqual([1, 2, 3]);
|
|
73
|
+
});
|
|
74
|
+
it('scopes are independent — one scope does not advance another', async () => {
|
|
75
|
+
const scopeA = scope('test:doc-c:files');
|
|
76
|
+
const scopeB = scope('test:doc-d:files');
|
|
77
|
+
await counters.nextScopedCounterValue(scopeA);
|
|
78
|
+
await counters.nextScopedCounterValue(scopeA);
|
|
79
|
+
const bFirst = await counters.nextScopedCounterValue(scopeB);
|
|
80
|
+
expect(bFirst).toBe(1);
|
|
81
|
+
});
|
|
82
|
+
it('interoperates with nextCounterValue once registered (same sequence)', async () => {
|
|
83
|
+
const scopeName = scope('test:doc-e:files');
|
|
84
|
+
const viaScoped = await counters.nextScopedCounterValue(scopeName);
|
|
85
|
+
// After self-registration the static-path allocator sees the group too.
|
|
86
|
+
const viaStatic = await counters.nextCounterValue(scopeName);
|
|
87
|
+
expect(viaStatic).toBe(viaScoped + 1);
|
|
88
|
+
});
|
|
89
|
+
it('rejects an empty scope name', async () => {
|
|
90
|
+
await expect(counters.nextScopedCounterValue('')).rejects.toThrow(/scopeName must be a non-empty string/);
|
|
91
|
+
});
|
|
92
|
+
it('parallel allocations within one scope yield distinct values', async () => {
|
|
93
|
+
const scopeName = scope('test:doc-f:files');
|
|
94
|
+
const values = await Promise.all(Array.from({ length: 8 }, () => counters.nextScopedCounterValue(scopeName)));
|
|
95
|
+
const unique = new Set(values);
|
|
96
|
+
expect(unique.size).toBe(8);
|
|
97
|
+
expect(Math.min(...values)).toBe(1);
|
|
98
|
+
expect(Math.max(...values)).toBe(8);
|
|
99
|
+
});
|
|
100
|
+
});
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@byline/db-postgres",
|
|
3
3
|
"private": false,
|
|
4
4
|
"license": "MPL-2.0",
|
|
5
|
-
"version": "3.
|
|
5
|
+
"version": "3.19.0",
|
|
6
6
|
"engines": {
|
|
7
7
|
"node": ">=20.9.0"
|
|
8
8
|
},
|
|
@@ -56,9 +56,9 @@
|
|
|
56
56
|
"npm-run-all": "^4.1.5",
|
|
57
57
|
"pg": "^8.22.0",
|
|
58
58
|
"uuid": "^14.0.1",
|
|
59
|
-
"@byline/
|
|
60
|
-
"@byline/
|
|
61
|
-
"@byline/
|
|
59
|
+
"@byline/admin": "3.19.0",
|
|
60
|
+
"@byline/auth": "3.19.0",
|
|
61
|
+
"@byline/core": "3.19.0"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
64
|
"@biomejs/biome": "2.5.2",
|