@kernhq/module-inventory 0.2.0 → 0.3.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.
- package/README.md +104 -25
- package/dist/contract/capabilities.d.ts +4 -4
- package/dist/contract/capabilities.d.ts.map +1 -1
- package/dist/contract/capabilities.js +51 -9
- package/dist/contract/capabilities.js.map +1 -1
- package/dist/contract/events.d.ts +43 -0
- package/dist/contract/events.d.ts.map +1 -1
- package/dist/contract/events.js +40 -0
- package/dist/contract/events.js.map +1 -1
- package/dist/contract/index.d.ts +1 -0
- package/dist/contract/index.d.ts.map +1 -1
- package/dist/contract/index.js +1 -0
- package/dist/contract/index.js.map +1 -1
- package/dist/contract/models.d.ts +322 -0
- package/dist/contract/models.d.ts.map +1 -1
- package/dist/contract/models.js +213 -0
- package/dist/contract/models.js.map +1 -1
- package/dist/contract/notifications.d.ts +24 -0
- package/dist/contract/notifications.d.ts.map +1 -0
- package/dist/contract/notifications.js +72 -0
- package/dist/contract/notifications.js.map +1 -0
- package/dist/contract/permissions.d.ts +52 -2
- package/dist/contract/permissions.d.ts.map +1 -1
- package/dist/contract/permissions.js +55 -2
- package/dist/contract/permissions.js.map +1 -1
- package/dist/contract/router.d.ts +1052 -0
- package/dist/contract/router.d.ts.map +1 -1
- package/dist/contract/router.js +225 -1
- package/dist/contract/router.js.map +1 -1
- package/dist/contract/settings.d.ts +2 -0
- package/dist/contract/settings.d.ts.map +1 -1
- package/dist/contract/settings.js +24 -4
- package/dist/contract/settings.js.map +1 -1
- package/dist/server/index.d.ts +8 -4
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +289 -5
- package/dist/server/index.js.map +1 -1
- package/dist/server/jobs.d.ts +48 -0
- package/dist/server/jobs.d.ts.map +1 -0
- package/dist/server/jobs.js +358 -0
- package/dist/server/jobs.js.map +1 -0
- package/dist/server/router.d.ts +1337 -0
- package/dist/server/router.d.ts.map +1 -1
- package/dist/server/router.js +363 -7
- package/dist/server/router.js.map +1 -1
- package/dist/server/schema.d.ts +134 -1
- package/dist/server/schema.d.ts.map +1 -1
- package/dist/server/schema.js +158 -4
- package/dist/server/schema.js.map +1 -1
- package/dist/server/services/assets.d.ts +69 -3
- package/dist/server/services/assets.d.ts.map +1 -1
- package/dist/server/services/assets.js +199 -31
- package/dist/server/services/assets.js.map +1 -1
- package/dist/server/services/attachments.d.ts +80 -0
- package/dist/server/services/attachments.d.ts.map +1 -0
- package/dist/server/services/attachments.js +182 -0
- package/dist/server/services/attachments.js.map +1 -0
- package/dist/server/services/audience.d.ts +15 -0
- package/dist/server/services/audience.d.ts.map +1 -0
- package/dist/server/services/audience.js +64 -0
- package/dist/server/services/audience.js.map +1 -0
- package/dist/server/services/categories.d.ts +57 -0
- package/dist/server/services/categories.d.ts.map +1 -0
- package/dist/server/services/categories.js +124 -0
- package/dist/server/services/categories.js.map +1 -0
- package/dist/server/services/cursor.d.ts +68 -0
- package/dist/server/services/cursor.d.ts.map +1 -0
- package/dist/server/services/cursor.js +39 -0
- package/dist/server/services/cursor.js.map +1 -0
- package/dist/server/services/custody.d.ts +175 -0
- package/dist/server/services/custody.d.ts.map +1 -0
- package/dist/server/services/custody.js +367 -0
- package/dist/server/services/custody.js.map +1 -0
- package/dist/server/services/db-errors.d.ts +7 -0
- package/dist/server/services/db-errors.d.ts.map +1 -0
- package/dist/server/services/db-errors.js +32 -0
- package/dist/server/services/db-errors.js.map +1 -0
- package/dist/server/services/index.d.ts +16 -0
- package/dist/server/services/index.d.ts.map +1 -1
- package/dist/server/services/index.js +25 -1
- package/dist/server/services/index.js.map +1 -1
- package/dist/server/services/members.d.ts +27 -0
- package/dist/server/services/members.d.ts.map +1 -0
- package/dist/server/services/members.js +39 -0
- package/dist/server/services/members.js.map +1 -0
- package/dist/server/services/notify.d.ts +43 -1
- package/dist/server/services/notify.d.ts.map +1 -1
- package/dist/server/services/notify.js +47 -5
- package/dist/server/services/notify.js.map +1 -1
- package/dist/server/services/offboarding.d.ts +70 -0
- package/dist/server/services/offboarding.d.ts.map +1 -0
- package/dist/server/services/offboarding.js +116 -0
- package/dist/server/services/offboarding.js.map +1 -0
- package/dist/server/services/repairs.d.ts +204 -0
- package/dist/server/services/repairs.d.ts.map +1 -0
- package/dist/server/services/repairs.js +476 -0
- package/dist/server/services/repairs.js.map +1 -0
- package/dist/server/services/search.d.ts +85 -0
- package/dist/server/services/search.d.ts.map +1 -0
- package/dist/server/services/search.js +142 -0
- package/dist/server/services/search.js.map +1 -0
- package/dist/server/services/stats.d.ts +42 -0
- package/dist/server/services/stats.d.ts.map +1 -0
- package/dist/server/services/stats.js +80 -0
- package/dist/server/services/stats.js.map +1 -0
- package/dist/server/services/status.d.ts +102 -0
- package/dist/server/services/status.d.ts.map +1 -0
- package/dist/server/services/status.js +71 -0
- package/dist/server/services/status.js.map +1 -0
- package/migrations/0002_custody_and_categories.sql +23 -0
- package/migrations/0003_repairs.sql +23 -0
- package/migrations/0004_platform_surfaces.sql +51 -0
- package/migrations/0005_repair_dates.sql +35 -0
- package/migrations/0006_workspace_registry_read.sql +50 -0
- package/migrations/0007_history_sequence.sql +83 -0
- package/migrations/meta/0002_snapshot.json +1054 -0
- package/migrations/meta/0003_snapshot.json +1070 -0
- package/migrations/meta/0004_snapshot.json +1130 -0
- package/migrations/meta/0005_snapshot.json +1135 -0
- package/migrations/meta/_journal.json +42 -0
- package/package.json +1 -1
- package/src/client/api-instance.ts +27 -2
- package/src/client/bidi.test.ts +148 -0
- package/src/client/bidi.ts +85 -0
- package/src/client/components/AssetDetailPanel.svelte +614 -0
- package/src/client/components/AssetFormDialog.svelte +71 -22
- package/src/client/components/AssetPhoto.svelte +178 -0
- package/src/client/components/AttachmentsSection.svelte +327 -0
- package/src/client/components/CustodyDialog.svelte +201 -0
- package/src/client/components/RepairDialog.svelte +271 -0
- package/src/client/components/RepairsSection.svelte +318 -0
- package/src/client/components/Timeline.svelte +347 -0
- package/src/client/components/TimelineText.svelte +124 -0
- package/src/client/core-api.ts +71 -0
- package/src/client/custody.test.ts +31 -0
- package/src/client/custody.ts +34 -0
- package/src/client/errors.test.ts +365 -0
- package/src/client/errors.ts +201 -0
- package/src/client/links.test.ts +74 -0
- package/src/client/links.ts +44 -0
- package/src/client/members.test.ts +132 -0
- package/src/client/members.ts +116 -0
- package/src/client/messages.test.ts +127 -0
- package/src/client/messages.ts +1043 -18
- package/src/client/mock.test.ts +395 -1
- package/src/client/mock.ts +1066 -79
- package/src/client/module.ts +54 -0
- package/src/client/pages/AssetsPage.svelte +317 -45
- package/src/client/permissions.ts +7 -0
- package/src/client/price.test.ts +151 -3
- package/src/client/price.ts +155 -11
- package/src/client/query.ts +36 -0
- package/src/client/repairs.test.ts +38 -0
- package/src/client/repairs.ts +38 -0
- package/src/client/settings/CategoriesSettings.svelte +421 -0
- package/src/client/settings/GeneralSettings.svelte +0 -0
- package/src/client/status.ts +29 -0
- package/src/client/timeline.test.ts +175 -0
- package/src/client/timeline.ts +206 -0
- package/src/client/widgets/OverviewWidget.svelte +127 -26
- package/src/client/widgets/RepairsWidget.svelte +124 -0
- package/src/contract/capabilities.ts +53 -9
- package/src/contract/events.ts +49 -0
- package/src/contract/index.ts +1 -0
- package/src/contract/models.ts +237 -0
- package/src/contract/notifications.ts +73 -0
- package/src/contract/permissions.ts +55 -2
- package/src/contract/router.ts +255 -1
- package/src/contract/settings.ts +24 -4
- package/src/module.test.ts +194 -4
- package/src/server/index.ts +311 -4
- package/src/server/inventory.int.test.ts +3573 -18
- package/src/server/jobs.ts +444 -0
- package/src/server/migrations.test.ts +113 -0
- package/src/server/router.ts +462 -6
- package/src/server/schema.ts +160 -3
- package/src/server/services/assets.ts +214 -54
- package/src/server/services/attachments.ts +215 -0
- package/src/server/services/audience.ts +77 -0
- package/src/server/services/categories.ts +136 -0
- package/src/server/services/cursor.ts +104 -0
- package/src/server/services/custody.ts +471 -0
- package/src/server/services/db-errors.ts +42 -0
- package/src/server/services/index.ts +34 -1
- package/src/server/services/members.ts +54 -0
- package/src/server/services/notify.ts +52 -7
- package/src/server/services/offboarding.ts +150 -0
- package/src/server/services/repairs.ts +567 -0
- package/src/server/services/search.ts +166 -0
- package/src/server/services/stats.ts +88 -0
- package/src/server/services/status.test.ts +34 -0
- package/src/server/services/status.ts +143 -0
- package/src/client/settings/core-api.ts +0 -32
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
-- The one read in this module that has no workspace to be bound to.
|
|
2
|
+
--
|
|
3
|
+
-- Hand-written, because drizzle-kit does not generate policies — the same reason `0001_rls.sql` is
|
|
4
|
+
-- hand-written, and it carries the same guard: `create policy` has no `if not exists`, so it is
|
|
5
|
+
-- preceded by an explicit drop. `migrations.test.ts` applies this folder twice against a database
|
|
6
|
+
-- created from nothing and is what proves the guard is real rather than intended.
|
|
7
|
+
--
|
|
8
|
+
-- **Append-only.** 0.2.0 is published and `core` depends on it. This adds a policy and changes no
|
|
9
|
+
-- table, column or index, so the 0.2.0 image reads the schema afterwards exactly as it read it
|
|
10
|
+
-- before and rolling back needs no dump.
|
|
11
|
+
--
|
|
12
|
+
-- ## What was wrong
|
|
13
|
+
--
|
|
14
|
+
-- `mod_inventory.workspaces` is the registry the two nightly sweeps enumerate: a cron handler is
|
|
15
|
+
-- woken by a clock, so it has no workspace and `app.workspace_id` is unset for it by definition.
|
|
16
|
+
-- The table is a tenant table like every other — it carries `workspace_id` — so `0004` gave it
|
|
17
|
+
-- `enable`, `force` and the standard per-workspace policy, and the enumeration was left resting on
|
|
18
|
+
-- "the application connects as the schema's owner, and an owner bypasses RLS".
|
|
19
|
+
--
|
|
20
|
+
-- That is false, and `force row level security` is exactly the clause that makes it false: forcing
|
|
21
|
+
-- subjects the **table owner** to the policies as well. Only a superuser (or a role with BYPASSRLS)
|
|
22
|
+
-- is exempt. So on every deployment whose application role is an ordinary login role — which is what
|
|
23
|
+
-- the project's own RLS note asks for — `select workspace_id from mod_inventory.workspaces` with no
|
|
24
|
+
-- workspace bound returned **zero rows**. Not an error, not a warning: both sweeps found nothing to
|
|
25
|
+
-- do, every night, for ever. The development database runs as a superuser, which is precisely why
|
|
26
|
+
-- nothing noticed.
|
|
27
|
+
--
|
|
28
|
+
-- ## Why this shape of fix
|
|
29
|
+
--
|
|
30
|
+
-- Permissive policies are OR-ed, so this adds a second `for select` policy rather than touching the
|
|
31
|
+
-- first. It admits a session with **no workspace bound** — which is the scheduler, by construction,
|
|
32
|
+
-- since every request-bound read in this module goes through `kernel.database.withWorkspace` and
|
|
33
|
+
-- sets the GUC. A request-bound session therefore still matches only the original policy and still
|
|
34
|
+
-- sees exactly its own row: this widens nothing for anybody holding a workspace.
|
|
35
|
+
--
|
|
36
|
+
-- It is keyed on the *absence of a binding* rather than on a role name because a role name is a
|
|
37
|
+
-- deployment's choice — `kern`, `kern_app`, whatever an operator called it — and a migration that
|
|
38
|
+
-- named one would be right on one instance and wrong on the next. `set_config(..., true)` is
|
|
39
|
+
-- transaction-local, so an unbound read on a pooled connection sees the setting unset (NULL) or
|
|
40
|
+
-- empty; both are covered.
|
|
41
|
+
--
|
|
42
|
+
-- The table holds nothing but workspace ids and a timestamp — it is this module's own bookkeeping,
|
|
43
|
+
-- not tenant data anybody reads through the API. `assets`, `custody_periods`, `repairs` and the rest
|
|
44
|
+
-- are untouched and keep answering nothing at all to a session with no workspace, which is what
|
|
45
|
+
-- `inventory.int.test.ts` proves against a plain login role.
|
|
46
|
+
|
|
47
|
+
drop policy if exists "workspaces_unbound_read" on "mod_inventory"."workspaces";--> statement-breakpoint
|
|
48
|
+
create policy "workspaces_unbound_read" on "mod_inventory"."workspaces"
|
|
49
|
+
for select
|
|
50
|
+
using (coalesce(current_setting('app.workspace_id', true), '') = '');
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
-- The timeline's own ordering key, because a uuidv7 is only ordered to the millisecond.
|
|
2
|
+
--
|
|
3
|
+
-- Hand-written, because drizzle-kit does not generate a sequence, a backfill or a `setval` — the
|
|
4
|
+
-- same reason `0001_rls.sql` and `0006_workspace_registry_read.sql` are hand-written. 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
|
+
-- `asset_history` is read newest-first ordered by its primary key, on the grounds that a uuidv7
|
|
11
|
+
-- already carries the clock and is unique where `occurred_at` is not. Half of that is true. The
|
|
12
|
+
-- kernel's `uuidv7()` puts the millisecond in bytes 0-5 and fills bytes 6-15 from `randomUUID()`
|
|
13
|
+
-- with **no intra-millisecond counter**, so two entries written in the same millisecond sort in an
|
|
14
|
+
-- order decided by ten random bytes. It is stable for a given set of rows, so keyset paging stayed
|
|
15
|
+
-- correct — no gaps, no repeats — and the defect never showed up as a broken list. What it showed
|
|
16
|
+
-- up as is a timeline that renders "Bruno removed the file" above "Bruno added the file", which is
|
|
17
|
+
-- the one record a company later argues from.
|
|
18
|
+
--
|
|
19
|
+
-- `occurred_at` is no rescue: it defaults to `now()`, which is the *transaction* timestamp, so two
|
|
20
|
+
-- entries written in one transaction share it exactly.
|
|
21
|
+
--
|
|
22
|
+
-- ## Why a sequence
|
|
23
|
+
--
|
|
24
|
+
-- A sequence is exact rather than probabilistic: `nextval` hands out strictly increasing values
|
|
25
|
+
-- whatever the clock does, at whatever rate, and two entries written a microsecond apart are two
|
|
26
|
+
-- values. It is also cheap — one `nextval` per history row, no extra round trip — and it is the one
|
|
27
|
+
-- ordering key a module can own outright, where the clock belongs to the machine.
|
|
28
|
+
--
|
|
29
|
+
-- The unique index is on `(workspace_id, asset_id, seq)` because that is what the timeline reads:
|
|
30
|
+
-- one asset's entries, newest first, paged on `seq`. `inventory_asset_history_ws_asset_row_idx` is
|
|
31
|
+
-- left where it is — dropping an index is a schema change and this file only adds — and goes one
|
|
32
|
+
-- release later.
|
|
33
|
+
--
|
|
34
|
+
-- ## Append-only, and readable by the image before this one
|
|
35
|
+
--
|
|
36
|
+
-- 0.2.0 is published and `core` depends on it, so this file only adds: a sequence, a column, an
|
|
37
|
+
-- index. The column is `not null` **with a default**, which is what keeps the previous image
|
|
38
|
+
-- writing: it never names `seq`, so the default fills it, and it never selects `seq`, so it reads
|
|
39
|
+
-- the table exactly as it read it before. Rolling that image back needs no dump.
|
|
40
|
+
--
|
|
41
|
+
-- The backfill orders existing rows by `(occurred_at, id)` rather than leaving them to the physical
|
|
42
|
+
-- order a rewrite would produce. Within one millisecond that is still the arbitrary uuid order this
|
|
43
|
+
-- migration exists to replace — nothing can recover an order that was never recorded — but across
|
|
44
|
+
-- milliseconds it is the order the timeline already showed, so an existing instance's history does
|
|
45
|
+
-- not visibly reshuffle on upgrade.
|
|
46
|
+
|
|
47
|
+
CREATE SEQUENCE IF NOT EXISTS "mod_inventory"."asset_history_seq" AS bigint;--> statement-breakpoint
|
|
48
|
+
|
|
49
|
+
ALTER TABLE "mod_inventory"."asset_history" ADD COLUMN IF NOT EXISTS "seq" bigint;--> statement-breakpoint
|
|
50
|
+
|
|
51
|
+
-- Only the rows that have none, so a replay matches nothing and changes nothing.
|
|
52
|
+
--
|
|
53
|
+
-- `created_at` rather than `occurred_at`: the drizzle field is `occurredAt` and the *column* it maps
|
|
54
|
+
-- to is `created_at`, because `asset_history` reuses the module's `created()` factory. A migration
|
|
55
|
+
-- talks to the column, and this file's first draft talked to the field — which fails at
|
|
56
|
+
-- `column "occurred_at" does not exist`, during the module's own migration, which is a host service
|
|
57
|
+
-- that never binds its port rather than a broken timeline.
|
|
58
|
+
UPDATE "mod_inventory"."asset_history" AS h
|
|
59
|
+
SET "seq" = ordered.rn
|
|
60
|
+
FROM (SELECT "id", row_number() OVER (ORDER BY "created_at", "id") AS rn
|
|
61
|
+
FROM "mod_inventory"."asset_history"
|
|
62
|
+
WHERE "seq" IS NULL) AS ordered
|
|
63
|
+
WHERE h."id" = ordered."id";--> statement-breakpoint
|
|
64
|
+
|
|
65
|
+
-- `GREATEST` so this only ever moves the sequence forward. A replay against a live database must
|
|
66
|
+
-- not hand back a value some open transaction has already taken.
|
|
67
|
+
SELECT setval('mod_inventory.asset_history_seq',
|
|
68
|
+
GREATEST((SELECT coalesce(max("seq"), 0) FROM "mod_inventory"."asset_history"),
|
|
69
|
+
(SELECT last_value FROM "mod_inventory"."asset_history_seq")),
|
|
70
|
+
true);--> statement-breakpoint
|
|
71
|
+
|
|
72
|
+
ALTER TABLE "mod_inventory"."asset_history"
|
|
73
|
+
ALTER COLUMN "seq" SET DEFAULT nextval('mod_inventory.asset_history_seq');--> statement-breakpoint
|
|
74
|
+
|
|
75
|
+
ALTER TABLE "mod_inventory"."asset_history" ALTER COLUMN "seq" SET NOT NULL;--> statement-breakpoint
|
|
76
|
+
|
|
77
|
+
-- So `drop schema mod_inventory cascade` takes the sequence with it, and so does dropping the
|
|
78
|
+
-- column one release from now.
|
|
79
|
+
ALTER SEQUENCE "mod_inventory"."asset_history_seq"
|
|
80
|
+
OWNED BY "mod_inventory"."asset_history"."seq";--> statement-breakpoint
|
|
81
|
+
|
|
82
|
+
CREATE UNIQUE INDEX IF NOT EXISTS "inventory_asset_history_ws_asset_seq_uq"
|
|
83
|
+
ON "mod_inventory"."asset_history" USING btree ("workspace_id","asset_id","seq");
|