@kernhq/module-inventory 0.3.0 → 0.4.1
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.
- package/README.md +7 -5
- package/dist/contract/models.d.ts +24 -1
- package/dist/contract/models.d.ts.map +1 -1
- package/dist/contract/models.js +35 -3
- package/dist/contract/models.js.map +1 -1
- package/dist/contract/router.d.ts +58 -2
- package/dist/contract/router.d.ts.map +1 -1
- package/dist/contract/router.js +29 -1
- package/dist/contract/router.js.map +1 -1
- package/dist/server/router.d.ts +64 -393
- package/dist/server/router.d.ts.map +1 -1
- package/dist/server/router.js +23 -1
- package/dist/server/router.js.map +1 -1
- package/dist/server/schema.d.ts.map +1 -1
- package/dist/server/schema.js +11 -0
- package/dist/server/schema.js.map +1 -1
- package/dist/server/services/categories.d.ts +110 -10
- package/dist/server/services/categories.d.ts.map +1 -1
- package/dist/server/services/categories.js +198 -13
- package/dist/server/services/categories.js.map +1 -1
- package/migrations/0008_category_order_unique.sql +71 -0
- package/migrations/meta/_journal.json +7 -0
- package/package.json +4 -3
- package/src/client/errors.test.ts +30 -0
- package/src/client/errors.ts +31 -3
- package/src/client/messages.ts +82 -19
- package/src/client/mock.test.ts +71 -1
- package/src/client/mock.ts +51 -12
- package/src/client/module.ts +19 -1
- package/src/client/reorder.test.ts +100 -0
- package/src/client/reorder.ts +79 -0
- package/src/client/sequence.test.ts +248 -0
- package/src/client/sequence.ts +185 -0
- package/src/client/settings/CategoriesSettings.svelte +430 -105
- package/src/contract/models.ts +36 -3
- package/src/contract/router.ts +29 -0
- package/src/module.test.ts +23 -0
- package/src/server/inventory.int.test.ts +545 -10
- package/src/server/migrations.test.ts +140 -2
- package/src/server/router.ts +25 -1
- package/src/server/schema.ts +11 -0
- package/src/server/services/categories.ts +221 -20
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { KernError, uuidv7 } from '@kernhq/kernel';
|
|
2
|
-
import { and, asc, eq, isNull } from 'drizzle-orm';
|
|
2
|
+
import { and, asc, count, eq, isNull, sql } from 'drizzle-orm';
|
|
3
|
+
import { MAX_LIVE_CATEGORIES } from '../../contract/models.js';
|
|
3
4
|
import { categories } from '../schema.js';
|
|
4
5
|
import { violated } from './db-errors.js';
|
|
5
6
|
/** The unique index `0000_init.sql` put on (workspace_id, name). */
|
|
@@ -25,9 +26,16 @@ export function toCategory(row) {
|
|
|
25
26
|
*/
|
|
26
27
|
export class CategoryService {
|
|
27
28
|
/**
|
|
28
|
-
* Ordered by `order` and then by name
|
|
29
|
-
*
|
|
30
|
-
* `order`
|
|
29
|
+
* Ordered by `order` and then by name.
|
|
30
|
+
*
|
|
31
|
+
* `order` is the sequence somebody dragged their categories into, and no two **live** categories
|
|
32
|
+
* share a number: `inventory_categories_ws_order_live_uq` is what makes that true rather than
|
|
33
|
+
* intended. So for the live set the tiebreak never fires.
|
|
34
|
+
*
|
|
35
|
+
* It stays because `list` also reads the archived rows, and those are outside the index: an
|
|
36
|
+
* archived category keeps the number it had when it left, and the very next reorder renumbers a
|
|
37
|
+
* live row onto it. A duplicate must sort the same way twice, and a name is the only column a
|
|
38
|
+
* person could predict.
|
|
31
39
|
*/
|
|
32
40
|
async list(tx, workspaceId, includeArchived) {
|
|
33
41
|
const filters = [eq(categories.workspaceId, workspaceId)];
|
|
@@ -50,29 +58,47 @@ export class CategoryService {
|
|
|
50
58
|
return row;
|
|
51
59
|
}
|
|
52
60
|
/**
|
|
53
|
-
* A duplicate name is a `CONFLICT` with the
|
|
61
|
+
* A new category joins the **end** of the sequence, and a duplicate name is a `CONFLICT` with the
|
|
62
|
+
* name in it, never a 500.
|
|
54
63
|
*
|
|
55
|
-
* The unique index is what actually decides — checking first and inserting after is a
|
|
56
|
-
* two people adding "Laptops" at once will find — so the check is the insert, and the
|
|
57
|
-
* 23505 is translated into a sentence rather than shown as "Failed query: insert into
|
|
64
|
+
* The unique index is what actually decides the name — checking first and inserting after is a
|
|
65
|
+
* race that two people adding "Laptops" at once will find — so the check is the insert, and the
|
|
66
|
+
* driver's 23505 is translated into a sentence rather than shown as "Failed query: insert into
|
|
58
67
|
* mod_inventory.categories …".
|
|
68
|
+
*
|
|
69
|
+
* The position is decided the same way, in the statement rather than around it. It used to be an
|
|
70
|
+
* optional number the caller passed and defaulted to 0, so every category anybody added landed at
|
|
71
|
+
* the *front*, tied with whatever was already there, and the list resolved the tie by name — a
|
|
72
|
+
* new category appearing in the middle of a sequence somebody had arranged by hand.
|
|
73
|
+
*
|
|
74
|
+
* **The subquery is not what makes the number unique — the lock above it is.** This used to say
|
|
75
|
+
* that a subquery inside the insert stopped two creates in flight from taking the same maximum,
|
|
76
|
+
* and that is false: under READ COMMITTED each statement takes its own snapshot, so both read a
|
|
77
|
+
* list without the other's row in it and both appended to the same place. `lockAppends` is what
|
|
78
|
+
* serialises them, and `inventory_categories_ws_order_live_uq` is what refuses the pair if
|
|
79
|
+
* anything ever reaches the table around it.
|
|
59
80
|
*/
|
|
60
|
-
async create(tx, workspaceId, name
|
|
81
|
+
async create(tx, workspaceId, name) {
|
|
82
|
+
await CategoryService.lockAppends(tx, workspaceId);
|
|
83
|
+
await CategoryService.roomForOneMore(tx, workspaceId);
|
|
61
84
|
try {
|
|
62
|
-
const [row] = await tx
|
|
85
|
+
const [row] = await tx
|
|
86
|
+
.insert(categories)
|
|
87
|
+
.values({ id: uuidv7(), workspaceId, name, order: CategoryService.appended(workspaceId) })
|
|
88
|
+
.returning();
|
|
63
89
|
return row;
|
|
64
90
|
}
|
|
65
91
|
catch (err) {
|
|
66
92
|
throw CategoryService.nameTaken(err, name);
|
|
67
93
|
}
|
|
68
94
|
}
|
|
95
|
+
/** A rename, and nothing else — the sequence is `reorder`'s to write. */
|
|
69
96
|
async update(tx, workspaceId, categoryId, patch) {
|
|
70
97
|
const previous = await this.get(tx, workspaceId, categoryId);
|
|
71
|
-
// `undefined` means "not mentioned".
|
|
98
|
+
// `undefined` means "not mentioned". The column is not nullable, so there is no "clear it" here
|
|
72
99
|
// and no reason for the `null`-versus-`undefined` care `assets.update` needs.
|
|
73
100
|
const values = {
|
|
74
101
|
name: patch.name ?? previous.name,
|
|
75
|
-
order: patch.order ?? previous.order,
|
|
76
102
|
updatedAt: new Date(),
|
|
77
103
|
};
|
|
78
104
|
try {
|
|
@@ -97,17 +123,176 @@ export class CategoryService {
|
|
|
97
123
|
* entry saying "category changed to <nothing>". None of it recoverable, all of it caused by a
|
|
98
124
|
* settings screen. An archived category disappears from every picker and every filter and leaves
|
|
99
125
|
* each asset able to say what it is.
|
|
126
|
+
*
|
|
127
|
+
* **A restore appends**, for the reason `create` appends. The row kept the position it had when
|
|
128
|
+
* it left, and every live category has been renumbered since — so putting it back where its old
|
|
129
|
+
* number points lands it in the middle of somebody's arrangement, tied with whatever is there
|
|
130
|
+
* now. The end of the list is the one place a person can find it again. Archiving leaves the
|
|
131
|
+
* number alone: it is out of every list that reads it, and it is about to be overwritten anyway.
|
|
132
|
+
*
|
|
133
|
+
* A restore appends, so it races exactly as `create` does and is serialised the same way — and it
|
|
134
|
+
* is the one place other than `create` where the live set grows, so it is the other place the
|
|
135
|
+
* limit is enforced. Archiving needs neither: it takes a row out of the live set, and out of the
|
|
136
|
+
* partial index with it.
|
|
100
137
|
*/
|
|
101
138
|
async archive(tx, workspaceId, categoryId, archived) {
|
|
139
|
+
if (!archived) {
|
|
140
|
+
await CategoryService.lockAppends(tx, workspaceId);
|
|
141
|
+
await CategoryService.roomForOneMore(tx, workspaceId);
|
|
142
|
+
}
|
|
102
143
|
const [row] = await tx
|
|
103
144
|
.update(categories)
|
|
104
|
-
.set({
|
|
145
|
+
.set({
|
|
146
|
+
archivedAt: archived ? new Date() : null,
|
|
147
|
+
...(archived ? {} : { order: CategoryService.appended(workspaceId) }),
|
|
148
|
+
updatedAt: new Date(),
|
|
149
|
+
})
|
|
105
150
|
.where(and(eq(categories.workspaceId, workspaceId), eq(categories.id, categoryId)))
|
|
106
151
|
.returning();
|
|
107
152
|
if (!row)
|
|
108
153
|
throw KernError.notFound('Category');
|
|
109
154
|
return row;
|
|
110
155
|
}
|
|
156
|
+
/**
|
|
157
|
+
* The sequence, rewritten from the ids somebody put it in — the only thing that writes `order`.
|
|
158
|
+
*
|
|
159
|
+
* Three refusals before a single row is touched, and all three are the same idea: this call
|
|
160
|
+
* describes the whole live list, so a list that does not match the workspace is not a partial
|
|
161
|
+
* instruction to be completed, it is an ordering of something else.
|
|
162
|
+
*
|
|
163
|
+
* - **an id twice** — arithmetic that cannot be carried out, `BAD_REQUEST`;
|
|
164
|
+
* - **an id that is not this workspace's** — `NOT_FOUND`, the answer `update` and `archive`
|
|
165
|
+
* already give for one, and the answer that does not confirm the row exists elsewhere;
|
|
166
|
+
* - **a live category the list does not name, or an archived one it does** — somebody added,
|
|
167
|
+
* archived or restored a category while this page was open. Renumbering what was named would
|
|
168
|
+
* put the missing one wherever its stale number happened to land, silently. `CONFLICT` with
|
|
169
|
+
* `inventory.category.order_stale`, which the client turns into "reload and try again".
|
|
170
|
+
*
|
|
171
|
+
* All of it inside one transaction, opened by the router, so a refusal writes nothing and a
|
|
172
|
+
* renumbering is never half-applied. The `for update` is what makes two reorders arriving at once
|
|
173
|
+
* queue rather than interleave — without it both read the same list, both pass the check, and the
|
|
174
|
+
* writes of one land between the writes of the other, which is how a sequence ends up being
|
|
175
|
+
* neither of the two orders anybody asked for. Locking in id order is what stops two of them
|
|
176
|
+
* taking the same rows in opposite orders and deadlocking.
|
|
177
|
+
*/
|
|
178
|
+
async reorder(tx, workspaceId, categoryIds) {
|
|
179
|
+
const named = new Set(categoryIds);
|
|
180
|
+
if (named.size !== categoryIds.length)
|
|
181
|
+
throw KernError.badRequest('That list of categories names the same one more than once.');
|
|
182
|
+
const current = await tx
|
|
183
|
+
.select()
|
|
184
|
+
.from(categories)
|
|
185
|
+
.where(eq(categories.workspaceId, workspaceId))
|
|
186
|
+
.orderBy(asc(categories.id))
|
|
187
|
+
.for('update');
|
|
188
|
+
const known = new Map(current.map((row) => [row.id, row]));
|
|
189
|
+
if (categoryIds.some((id) => !known.has(id)))
|
|
190
|
+
throw KernError.notFound('Category');
|
|
191
|
+
const live = current.filter((row) => !row.archivedAt);
|
|
192
|
+
if (live.some((row) => !named.has(row.id)) || categoryIds.some((id) => known.get(id)?.archivedAt))
|
|
193
|
+
throw KernError.conflict('The categories changed while this list was open, so this order was not saved. Reload the list and arrange it again.', 'inventory.category.order_stale');
|
|
194
|
+
// Only the rows that actually move are touched — a workspace has tens of categories, they are
|
|
195
|
+
// already locked, and a change event for a row whose position did not change would tell every
|
|
196
|
+
// screen in the workspace about a write that did not happen. `moved` is settled here, before a
|
|
197
|
+
// single write, so it stays the honest list whatever the two passes below do.
|
|
198
|
+
const now = new Date();
|
|
199
|
+
const going = categoryIds
|
|
200
|
+
.map((id, index) => ({ id, index }))
|
|
201
|
+
.filter(({ id, index }) => known.get(id)?.order !== index);
|
|
202
|
+
const moved = going.map(({ id }) => id);
|
|
203
|
+
/**
|
|
204
|
+
* Parked out of the way first, and only then put down where they belong.
|
|
205
|
+
*
|
|
206
|
+
* `inventory_categories_ws_order_live_uq` is a plain unique index, and Postgres checks one of
|
|
207
|
+
* those row by row rather than at the end of the statement. There is no deferrable form to reach
|
|
208
|
+
* for either: a unique *constraint* can be deferred and cannot be partial, and this one has to be
|
|
209
|
+
* partial. So the single-pass loop writes a collision the moment two rows swap — putting the
|
|
210
|
+
* first on 1 while the second still holds 1 — and a swap is the commonest reorder there is.
|
|
211
|
+
*
|
|
212
|
+
* `park` sits above both the highest number any row in this workspace holds **and** the last
|
|
213
|
+
* place in the new sequence. That is what makes the two passes safe: the parked values are
|
|
214
|
+
* distinct from one another and from every row staying put, and the `0…n-1` the second pass
|
|
215
|
+
* writes into is empty, because every live row that could have been sitting there is either
|
|
216
|
+
* parked or already on the number it is being given.
|
|
217
|
+
*
|
|
218
|
+
* Only the rows that actually move are written, so a reorder that shifts one row does not stamp
|
|
219
|
+
* `updated_at` across the whole list — and `updated_at` is left off the parking pass, which is
|
|
220
|
+
* bookkeeping rather than a change anybody made.
|
|
221
|
+
*/
|
|
222
|
+
if (going.length > 0) {
|
|
223
|
+
const park = Math.max(...current.map((row) => row.order), categoryIds.length - 1) + 1;
|
|
224
|
+
for (const [offset, { id }] of going.entries()) {
|
|
225
|
+
await tx
|
|
226
|
+
.update(categories)
|
|
227
|
+
.set({ order: park + offset })
|
|
228
|
+
.where(and(eq(categories.workspaceId, workspaceId), eq(categories.id, id)));
|
|
229
|
+
}
|
|
230
|
+
for (const { id, index } of going) {
|
|
231
|
+
await tx
|
|
232
|
+
.update(categories)
|
|
233
|
+
.set({ order: index, updatedAt: now })
|
|
234
|
+
.where(and(eq(categories.workspaceId, workspaceId), eq(categories.id, id)));
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
const rows = await tx
|
|
238
|
+
.select()
|
|
239
|
+
.from(categories)
|
|
240
|
+
.where(and(eq(categories.workspaceId, workspaceId), isNull(categories.archivedAt)))
|
|
241
|
+
.orderBy(asc(categories.order), asc(categories.name));
|
|
242
|
+
return { rows, moved };
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* One past the highest position this workspace has used, archived rows counted.
|
|
246
|
+
*
|
|
247
|
+
* Archived rows count because one of them can be restored, and a restored category landing on a
|
|
248
|
+
* live one's number is the tie this whole change exists to remove.
|
|
249
|
+
*
|
|
250
|
+
* **Only correct under `lockAppends`.** A subquery inside the write saves a round trip and settles
|
|
251
|
+
* nothing about concurrency: under READ COMMITTED it is evaluated against the snapshot its own
|
|
252
|
+
* statement started with, so two transactions appending at the same instant read the same maximum
|
|
253
|
+
* and take the same number. That is the defect `0008` exists for.
|
|
254
|
+
*/
|
|
255
|
+
static appended(workspaceId) {
|
|
256
|
+
return sql `(select coalesce(max(${categories.order}), -1) + 1 from ${categories} where ${categories.workspaceId} = ${workspaceId})`;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Hold the right to append to this workspace's list until the transaction ends.
|
|
260
|
+
*
|
|
261
|
+
* An advisory lock rather than `select … for update`, because the thing being protected is the
|
|
262
|
+
* *next* number rather than any row that exists: a workspace with no categories at all has no row
|
|
263
|
+
* to lock, and two creates against it would still collide. Taken per workspace, so two workspaces
|
|
264
|
+
* adding a category at the same moment never wait for each other.
|
|
265
|
+
*
|
|
266
|
+
* The first key is a constant for this list, so another module taking an advisory lock on the same
|
|
267
|
+
* workspace does not queue behind this one by accident.
|
|
268
|
+
*
|
|
269
|
+
* It cannot deadlock against `reorder`, which takes row locks and never asks for this one — so
|
|
270
|
+
* there is no pair of waits pointing at each other.
|
|
271
|
+
*/
|
|
272
|
+
static async lockAppends(tx, workspaceId) {
|
|
273
|
+
await tx.execute(sql `select pg_advisory_xact_lock(hashtext('mod_inventory.categories.order'), hashtext(${workspaceId}))`);
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Refuse the one that would take a workspace past `MAX_LIVE_CATEGORIES`, and say so.
|
|
277
|
+
*
|
|
278
|
+
* The number exists because `categories.reorder` is handed every live category at once and that
|
|
279
|
+
* array needs a bound. Leaving the bound only on the array is a silent ceiling: a workspace could
|
|
280
|
+
* pass it one category at a time and then discover that the only procedure that can order them is
|
|
281
|
+
* the one it can no longer call. Enforced here, the array can always name every live category a
|
|
282
|
+
* workspace is allowed to have.
|
|
283
|
+
*
|
|
284
|
+
* **Live rows, not every row ever made**, so that archiving one frees a place — which is what the
|
|
285
|
+
* refusal tells the reader to do, and the advice has to be true.
|
|
286
|
+
*/
|
|
287
|
+
static async roomForOneMore(tx, workspaceId) {
|
|
288
|
+
const [row] = await tx
|
|
289
|
+
.select({ n: count() })
|
|
290
|
+
.from(categories)
|
|
291
|
+
.where(and(eq(categories.workspaceId, workspaceId), isNull(categories.archivedAt)));
|
|
292
|
+
if ((row?.n ?? 0) < MAX_LIVE_CATEGORIES)
|
|
293
|
+
return;
|
|
294
|
+
throw KernError.conflict(`This workspace already has ${MAX_LIVE_CATEGORIES} categories, which is as many as Inventory keeps in one order. Archive one it no longer uses to make room.`, 'inventory.category.limit_reached');
|
|
295
|
+
}
|
|
111
296
|
/**
|
|
112
297
|
* The unique index refused it, or something else did and must not be disguised.
|
|
113
298
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"categories.js","sourceRoot":"","sources":["../../../src/server/services/categories.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAW,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC3D,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"categories.js","sourceRoot":"","sources":["../../../src/server/services/categories.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAW,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC3D,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AAC9D,OAAO,EAAkC,mBAAmB,EAAE,MAAM,0BAA0B,CAAA;AAC9F,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAIzC,oEAAoE;AACpE,MAAM,UAAU,GAAG,iCAAiC,CAAA;AASpD,oGAAoG;AACpG,MAAM,UAAU,UAAU,CAAC,GAAQ;IACjC,OAAO;QACL,EAAE,EAAE,GAAG,CAAC,EAAE;QACV,WAAW,EAAE,GAAG,CAAC,WAA2C;QAC5D,IAAI,EAAE,GAAG,CAAC,IAAI;QACd,KAAK,EAAE,GAAG,CAAC,KAAK;QAChB,SAAS,EAAE,GAAG,CAAC,SAAS,CAAC,WAAW,EAAE;QACtC,SAAS,EAAE,GAAG,CAAC,SAAS,CAAC,WAAW,EAAE;QACtC,UAAU,EAAE,GAAG,CAAC,UAAU,EAAE,WAAW,EAAE,IAAI,IAAI;KAClD,CAAA;AACH,CAAC;AAED;;;;;;GAMG;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CAAC,IAAI,CAAC,CAAC,GAAG,mBAAmB;YAAE,OAAM;QAC/C,MAAM,SAAS,CAAC,QAAQ,CACtB,8BAA8B,mBAAmB,4GAA4G,EAC7J,kCAAkC,CACnC,CAAA;IACH,CAAC;IAED;;;;;;OAMG;IACK,MAAM,CAAC,SAAS,CAAC,GAAY,EAAE,IAAY;QACjD,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,UAAU,CAAC;YAAE,OAAO,GAAG,CAAA;QAC1C,OAAO,SAAS,CAAC,QAAQ,CACvB,iDAAiD,IAAI,IAAI,EACzD,+BAA+B,CAChC,CAAA;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
-- No two live categories on the same place, enforced rather than intended.
|
|
2
|
+
--
|
|
3
|
+
-- Hand-written, because the repair below is not something drizzle-kit generates — the same reason
|
|
4
|
+
-- `0001_rls.sql`, `0006_workspace_registry_read.sql` and `0007_history_sequence.sql` are. Every
|
|
5
|
+
-- statement carries its own guard, and `migrations.test.ts` applies this folder twice against a
|
|
6
|
+
-- database created from nothing to prove the guards are real rather than intended.
|
|
7
|
+
--
|
|
8
|
+
-- ## What was wrong
|
|
9
|
+
--
|
|
10
|
+
-- The contract, the changeset and `CategoryService.list`'s own comment all said that `reorder` keeps
|
|
11
|
+
-- the live categories on distinct numbers, so the `order by "order", "name"` tiebreak never fires for
|
|
12
|
+
-- them. All three were describing an intention.
|
|
13
|
+
--
|
|
14
|
+
-- A category joins the end of the list by taking `(select coalesce(max("order"), -1) + 1)`, and a
|
|
15
|
+
-- restore appends the same way. Putting that subquery *inside* the insert removes a round trip; it
|
|
16
|
+
-- does not remove the race. Under READ COMMITTED every statement takes its own snapshot, so two
|
|
17
|
+
-- transactions appending at the same instant each read a list without the other's row in it and both
|
|
18
|
+
-- take the same number. `reorder`'s `select … for update` does not serialise them either: a row lock
|
|
19
|
+
-- cannot cover a row that does not exist yet, and a restore's subquery is evaluated against the
|
|
20
|
+
-- snapshot its statement started with rather than the one it would get after waiting.
|
|
21
|
+
--
|
|
22
|
+
-- Two live categories on one number is not a crash. It is a list whose order changes under somebody
|
|
23
|
+
-- when a name changes, and a *move up* that appears to do nothing because the row it swapped with
|
|
24
|
+
-- sorts back ahead of it by name.
|
|
25
|
+
--
|
|
26
|
+
-- ## Why an index
|
|
27
|
+
--
|
|
28
|
+
-- Because checking first and writing after is the race, not the fix — the same reason
|
|
29
|
+
-- `inventory_repairs_one_open_uq` is an index in `0003`. `CategoryService` takes a per-workspace
|
|
30
|
+
-- advisory lock so the ordinary append never meets this index at all; the index is what makes the
|
|
31
|
+
-- claim true whatever else reaches the table.
|
|
32
|
+
--
|
|
33
|
+
-- **Partial, over the live rows only.** An archived category keeps the number it had when it left,
|
|
34
|
+
-- and the next reorder renumbers a live row straight onto it. That collision is invisible — an
|
|
35
|
+
-- archived category is in no picker, no filter and no sequence — so a total unique index would refuse
|
|
36
|
+
-- an archive-and-reorder that is entirely correct.
|
|
37
|
+
--
|
|
38
|
+
-- ## The repair comes first, or the upgrade takes the host service down
|
|
39
|
+
--
|
|
40
|
+
-- A module's migrations are the first thing the kernel runs, so a `CREATE UNIQUE INDEX` that meets
|
|
41
|
+
-- rows already sharing a number does not break categories: it throws during boot, and `core` hosts
|
|
42
|
+
-- five modules and never binds its port. Every instance that has run 0.2.0 may hold such a pair, so
|
|
43
|
+
-- the duplicates are renumbered before the index is built.
|
|
44
|
+
--
|
|
45
|
+
-- The renumbering is confined to the workspaces that actually have a duplicate, and it walks them in
|
|
46
|
+
-- `("order", "name", "id")` — which is the order `list` already returns and therefore the order the
|
|
47
|
+
-- screen already showed. Nobody's arrangement visibly changes. `"order" <> ordered.rn - 1` is what
|
|
48
|
+
-- makes a replay match nothing rather than rewrite rows a second time.
|
|
49
|
+
--
|
|
50
|
+
-- ## Append-only, and readable by the image before this one
|
|
51
|
+
--
|
|
52
|
+
-- 0.2.0 is published and `core` depends on it. This changes no table and no column: it adds an index
|
|
53
|
+
-- and moves some integers within the range they already occupied, so the previous image reads the
|
|
54
|
+
-- table exactly as it read it before and rolling back needs no dump.
|
|
55
|
+
|
|
56
|
+
UPDATE "mod_inventory"."categories" AS c
|
|
57
|
+
SET "order" = ordered.rn - 1
|
|
58
|
+
FROM (SELECT "id",
|
|
59
|
+
row_number() OVER (PARTITION BY "workspace_id" ORDER BY "order", "name", "id") AS rn
|
|
60
|
+
FROM "mod_inventory"."categories"
|
|
61
|
+
WHERE "archived_at" IS NULL
|
|
62
|
+
AND "workspace_id" IN (SELECT "workspace_id"
|
|
63
|
+
FROM "mod_inventory"."categories"
|
|
64
|
+
WHERE "archived_at" IS NULL
|
|
65
|
+
GROUP BY "workspace_id", "order"
|
|
66
|
+
HAVING count(*) > 1)) AS ordered
|
|
67
|
+
WHERE c."id" = ordered."id"
|
|
68
|
+
AND c."order" <> ordered.rn - 1;--> statement-breakpoint
|
|
69
|
+
|
|
70
|
+
CREATE UNIQUE INDEX IF NOT EXISTS "inventory_categories_ws_order_live_uq"
|
|
71
|
+
ON "mod_inventory"."categories" USING btree ("workspace_id","order") WHERE "archived_at" IS NULL;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kernhq/module-inventory",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Kern inventory module: the company's asset register — what it owns, item by item, with asset tags, serial numbers, purchase and warranty details",
|
|
5
5
|
"homepage": "https://github.com/KernAIO/module-inventory#readme",
|
|
6
6
|
"license": "AGPL-3.0-only",
|
|
@@ -56,13 +56,14 @@
|
|
|
56
56
|
"@orpc/contract": "^1.15.0",
|
|
57
57
|
"@orpc/server": "^1.15.0",
|
|
58
58
|
"drizzle-orm": "^0.45.0",
|
|
59
|
+
"svelte-dnd-action": "^0.9.64",
|
|
59
60
|
"zod": "^4.1.0"
|
|
60
61
|
},
|
|
61
62
|
"devDependencies": {
|
|
62
63
|
"@biomejs/biome": "^2.0.0",
|
|
63
64
|
"@changesets/cli": "^2.27.0",
|
|
64
65
|
"@kernhq/contracts": "^0.7.0",
|
|
65
|
-
"@kernhq/kernel": "^0.
|
|
66
|
+
"@kernhq/kernel": "^0.9.1",
|
|
66
67
|
"@kernhq/tsconfig": "^0.1.0",
|
|
67
68
|
"@kernhq/ui": "^0.12.0",
|
|
68
69
|
"@tanstack/svelte-query": "^6.1.0",
|
|
@@ -77,7 +78,7 @@
|
|
|
77
78
|
},
|
|
78
79
|
"peerDependencies": {
|
|
79
80
|
"@kernhq/contracts": "^0.7.0",
|
|
80
|
-
"@kernhq/kernel": "^0.
|
|
81
|
+
"@kernhq/kernel": "^0.9.1",
|
|
81
82
|
"@kernhq/ui": "^0.12.0",
|
|
82
83
|
"@tanstack/svelte-query": "^6.1.0",
|
|
83
84
|
"svelte": "^5.46.0"
|
|
@@ -2,6 +2,7 @@ import { readdirSync, readFileSync } from 'node:fs'
|
|
|
2
2
|
import { dirname, join } from 'node:path'
|
|
3
3
|
import { fileURLToPath } from 'node:url'
|
|
4
4
|
import { describe, expect, it } from 'vitest'
|
|
5
|
+
import { MAX_LIVE_CATEGORIES } from '../contract/models.js'
|
|
5
6
|
import {
|
|
6
7
|
codeOf,
|
|
7
8
|
errorLine,
|
|
@@ -203,6 +204,35 @@ describe('which sentence a refusal earns', () => {
|
|
|
203
204
|
'<error_custody_not_held>',
|
|
204
205
|
)
|
|
205
206
|
})
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* The one refusal whose sentence has a number in it.
|
|
210
|
+
*
|
|
211
|
+
* A limit that will not say what it is cannot be planned around, and `KernError.conflict` carries a
|
|
212
|
+
* reason and no data — so the figure comes from the contract, which is the same constant the server
|
|
213
|
+
* enforces. Passed through `t`, which puts it through `Intl.NumberFormat`, so the reader sees it in
|
|
214
|
+
* their own digits rather than in ASCII.
|
|
215
|
+
*/
|
|
216
|
+
it('hands the category limit its number, from the constant the server enforces', () => {
|
|
217
|
+
const refusal = conflict('inventory.category.limit_reached')
|
|
218
|
+
expect(errorLine(refusal)).toEqual({
|
|
219
|
+
key: 'error_category_limit_reached',
|
|
220
|
+
detail: null,
|
|
221
|
+
values: { max: MAX_LIVE_CATEGORIES },
|
|
222
|
+
})
|
|
223
|
+
expect(
|
|
224
|
+
errorMessage(refusal, (key, values) => `${key}:${JSON.stringify(values ?? null)}`),
|
|
225
|
+
'and the screen is handed the values along with the key',
|
|
226
|
+
).toBe(`error_category_limit_reached:{"max":${MAX_LIVE_CATEGORIES}}`)
|
|
227
|
+
expect(en['inventory.error_category_limit_reached'], 'which the sentence really asks for').toContain(
|
|
228
|
+
'{max}',
|
|
229
|
+
)
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
it('leaves values off every key that has no placeholder', () => {
|
|
233
|
+
expect(errorLine(conflict('inventory.category.order_stale')).values).toBeUndefined()
|
|
234
|
+
expect(errorLine({ code: 'NOT_FOUND' }).values).toBeUndefined()
|
|
235
|
+
})
|
|
206
236
|
})
|
|
207
237
|
|
|
208
238
|
// ------------------------------------------------------- the two lists that have to stay in step
|
package/src/client/errors.ts
CHANGED
|
@@ -25,8 +25,14 @@
|
|
|
25
25
|
*
|
|
26
26
|
* Pure and string-free: this file decides *which* key, `i18n.ts` holds the words, and a `.svelte`
|
|
27
27
|
* file cannot be unit-tested. `errorMessage()` at the bottom is the one function the screens call.
|
|
28
|
+
*
|
|
29
|
+
* The one import is a number from the contract — the same file the server reads it from — because a
|
|
30
|
+
* limit a sentence refuses to name is a limit nobody can plan around. It brings no runtime with it;
|
|
31
|
+
* what this file still must never reach for is `i18n.ts`, which drags a Svelte compiler in behind it.
|
|
28
32
|
*/
|
|
29
33
|
|
|
34
|
+
import { MAX_LIVE_CATEGORIES } from '../contract/models.js'
|
|
35
|
+
|
|
30
36
|
/** What oRPC hands a screen. `code` is the contract's `ErrorCode`; `data` is what the server put in it. */
|
|
31
37
|
export interface ServerError {
|
|
32
38
|
code?: unknown
|
|
@@ -121,10 +127,24 @@ const REASON_KEYS: Record<string, string> = {
|
|
|
121
127
|
'inventory.repair.already_complete': 'error_repair_already_complete',
|
|
122
128
|
'inventory.repair.returned_before_sent': 'error_repair_returned_before_sent',
|
|
123
129
|
'inventory.category.name_taken': 'error_category_name_taken',
|
|
130
|
+
'inventory.category.order_stale': 'error_category_order_stale',
|
|
131
|
+
'inventory.category.limit_reached': 'error_category_limit_reached',
|
|
124
132
|
}
|
|
125
133
|
|
|
126
134
|
export const reasonKeys = (): readonly string[] => Object.keys(REASON_KEYS)
|
|
127
135
|
|
|
136
|
+
/**
|
|
137
|
+
* The one refusal whose sentence has a number in it, and where that number comes from.
|
|
138
|
+
*
|
|
139
|
+
* A limit stated as "quite a lot" is not stated. `KernError.conflict` carries a reason and no data,
|
|
140
|
+
* so the server cannot hand the figure over — and it does not need to: it is a constant in the
|
|
141
|
+
* contract, which is the same file both halves read. Passed through `t`, it goes through
|
|
142
|
+
* `Intl.NumberFormat`, so a Persian reader is shown ۵۰۰ rather than 500.
|
|
143
|
+
*/
|
|
144
|
+
const REASON_VALUES: Record<string, Record<string, string | number>> = {
|
|
145
|
+
'inventory.category.limit_reached': { max: MAX_LIVE_CATEGORIES },
|
|
146
|
+
}
|
|
147
|
+
|
|
128
148
|
/**
|
|
129
149
|
* The sentence a whole class of failure earns, when no reason narrows it further.
|
|
130
150
|
*
|
|
@@ -162,12 +182,17 @@ export interface ErrorLine {
|
|
|
162
182
|
key: string
|
|
163
183
|
/** The server's own sentence, for a failure nothing here recognised. Null otherwise. */
|
|
164
184
|
detail: string | null
|
|
185
|
+
/** What the sentence's placeholders need. Absent for every key that has none. */
|
|
186
|
+
values?: Record<string, string | number>
|
|
165
187
|
}
|
|
166
188
|
|
|
167
189
|
export function errorLine(err: unknown): ErrorLine {
|
|
168
190
|
const reason = reasonOf(err)
|
|
169
191
|
const byReason = reason ? REASON_KEYS[reason] : undefined
|
|
170
|
-
if (byReason)
|
|
192
|
+
if (byReason) {
|
|
193
|
+
const values = reason ? REASON_VALUES[reason] : undefined
|
|
194
|
+
return values ? { key: byReason, detail: null, values } : { key: byReason, detail: null }
|
|
195
|
+
}
|
|
171
196
|
|
|
172
197
|
const code = codeOf(err)
|
|
173
198
|
|
|
@@ -194,8 +219,11 @@ export function errorLine(err: unknown): ErrorLine {
|
|
|
194
219
|
* `@kernhq/ui`, and that entry point drags a Svelte compiler into whatever imports it, which is
|
|
195
220
|
* what makes a helper untestable.
|
|
196
221
|
*/
|
|
197
|
-
export function errorMessage(
|
|
222
|
+
export function errorMessage(
|
|
223
|
+
err: unknown,
|
|
224
|
+
translate: (key: string, values?: Record<string, string | number>) => string,
|
|
225
|
+
): string {
|
|
198
226
|
const line = errorLine(err)
|
|
199
|
-
const sentence = translate(line.key)
|
|
227
|
+
const sentence = translate(line.key, line.values)
|
|
200
228
|
return line.detail ? `${sentence} — ${line.detail}` : sentence
|
|
201
229
|
}
|