@shivaedev/changes 0.4.0 → 0.5.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/CHANGELOG.md +24 -0
- package/README.md +27 -0
- package/dist/buckets.d.ts +10 -0
- package/dist/buckets.d.ts.map +1 -0
- package/dist/buckets.js +14 -0
- package/dist/buckets.js.map +1 -0
- package/dist/keys.d.ts +52 -0
- package/dist/keys.d.ts.map +1 -0
- package/dist/keys.js +39 -0
- package/dist/keys.js.map +1 -0
- package/package.json +3 -3
- package/src/buckets.ts +18 -0
- package/src/keys.ts +86 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.5.0 - 2026-10-28
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- Mark model fields with `bucket(schema)` from `@shivaedev/changes/buckets.ts`. SQL repositories announce each written record and its old and new buckets after commit, including inserts, deletes, void writes and conflict writes. Bucket codecs decode stored values before building query keys; absent values have no bucket. Bucket writes require PostgreSQL 18 or later for atomic old-row observations.
|
|
8
|
+
|
|
9
|
+
### Fixed
|
|
10
|
+
|
|
11
|
+
- Enforce declared query read policies at the hosted RPC handler boundary, including contracts without identity middleware, whose viewer is anonymous. Query RPC error schemas include `QueryReadDenied` so native clients receive typed refusals before handlers run.
|
|
12
|
+
- Keep collection methods and untyped schema internals from making data parameters appear to carry callable services. Direct service implementations, nested dependency fields and service arrays remain subject to `effect/service-parameters`.
|
|
13
|
+
|
|
14
|
+
### Breaking
|
|
15
|
+
|
|
16
|
+
- Move key imports from `@shivaedev/contract/keys.ts` to `@shivaedev/changes/keys.ts`. Declare bucket fields with `collection(name, idSchema, fields)` and publish affected buckets alongside record keys.
|
|
17
|
+
- Replace collection policy maps, `mergeReadPolicies`, `ReadPolicy` and `ReadPolicyMemoWindow` with each query's `readPolicy(payload, viewer)`. Declare the same query's live scope with `reads(payload, viewer)`. Bound query atoms and subscriptions refuse missing policies with a declaration error; one-off query `run` calls remain available. Denied queries refuse the entire subscription. Server adapters replace `QueryReads` and `ChangesReads` with `QueryDefinition` and `ChangesQueries`. Custom middleware calls `authorizeReads(effect, request, viewer)` and declares `MissingReadPolicy` and `QueryReadDenied` errors.
|
|
18
|
+
- Custom subscription adapters yield `ChangesQueries.resolve(references)`, call `querySubscription(requests, viewer)` and pass the authorized result to `makeLiveServer.subscribe(subscription, context)`. Metadata values expose `QueryDefinition.definition` and `CommandInvalidation.invalidates` instead of bare definitions and callbacks. Yield the Effect returned by `queryKeys(queries, references)` when projecting keys for a custom stream. `resolveChanges` supplies the authorized subscription in hosted applications. Changed hints can carry query positions so bindings can rerun session-scoped reads without deriving session scopes in the browser.
|
|
19
|
+
- Remove `appHost`'s `readPolicy` option and module policy registration. Session and permission middleware apply query policies using the current session viewer. Handle the new typed policy errors in native clients, including `QueryReadDenied` on query RPCs without identity middleware; widen explicit query error annotations accordingly.
|
|
20
|
+
- Replace `sessionCommandChanges` with `commandChanges` for custom session middleware. Command invalidation callbacks can receive the session viewer as their third argument. Announce viewer buckets there; background writers must explicitly publish those bucket keys after commit.
|
|
21
|
+
- Built-in admin queries declare their permission policies alongside their schemas and read scopes. Consumers keep supplying the existing session and permission middleware layers.
|
|
22
|
+
- Local Work Board and Fleet queries declare their existing loopback read access explicitly so their generated live subscriptions remain available.
|
|
23
|
+
- `withChangeCoverage` now requires every declared record and bucket key, and a table name alone no longer covers a raw write. Migrate `covers` values from strings to typed item and bucket keys, and record `invalidationKeys(changedKeys)` through `invalidateOnCommit`. Repository writes declare their own keys automatically; raw writers declare all affected records and buckets.
|
|
24
|
+
- Bulk repository writes retain every record key instead of replacing them with `<table>:*` above 1,000 rows. Consumers observing raw notifications should handle the complete key set, which can span several NOTIFY payloads.
|
|
25
|
+
- Direct callers of the repository implementation helpers must pass a `RepositoryChanges` boundary to `makeCompositeCrud`; `versionedStatements` no longer returns `returningKeys`. Use the boundary's `returning()` fragment so void writes retain the row's bucket fields.
|
|
26
|
+
|
|
3
27
|
## 0.4.0 - 2026-10-27
|
|
4
28
|
|
|
5
29
|
### Breaking
|
package/README.md
CHANGED
|
@@ -18,6 +18,33 @@ yield* Effect.gen(function* () {
|
|
|
18
18
|
|
|
19
19
|
`membershipChanges` is the channel defined below. It groups notifications by subject and domain, so another write to the same member inside this transaction does not add a second notification. A nested transaction that rolls back contributes no notification, and a failed outer commit publishes nothing. Feature code describes what changed; the native transaction decides whether it happened.
|
|
20
20
|
|
|
21
|
+
## Record keys and buckets
|
|
22
|
+
|
|
23
|
+
`keys.ts` owns the shared dependency format for contract queries, committed writes and subscriptions. An item key identifies a record by id. A bucket identifies every row with a declared field value; a collection list key covers the whole collection.
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { Schema } from "effect";
|
|
27
|
+
import { collection, invalidationKeys } from "@shivaedev/changes/keys.ts";
|
|
28
|
+
|
|
29
|
+
const notes = collection("notes", Schema.Number, { userId: Schema.String, listId: Schema.Number });
|
|
30
|
+
const keys = [notes.item(1), notes.bucket.userId("alice"), notes.bucket.listId(42)];
|
|
31
|
+
const invalidations = invalidationKeys(keys);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Use `readKeys(keys)` to register dependencies with native Reactivity and `invalidationKeys(keys)` when announcing committed writes. A bucket changes when the writer names it; SQL repositories derive those names from model fields marked with `bucket`. Its value is typed by the declared field schema. A filtered list query still reads its entire bucket and reruns when any row there changes. Record changes also invalidate the collection list, while buckets remain explicit. The channel can buffer these keys until commit just like any other change vocabulary.
|
|
35
|
+
|
|
36
|
+
`bucket(schema)` from `@shivaedev/changes/buckets.ts` declares a model field as a write bucket without changing its schema type or variants:
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { bucket } from "@shivaedev/changes/buckets.ts";
|
|
40
|
+
|
|
41
|
+
const listId = bucket(Schema.NumberFromString);
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The SQL binding reads these declarations with `declaredBuckets(model.fields)` and decodes old and new stored values into the same identity values queries use. Bucket schemas decode to strings, numbers, null or undefined and require no services. Absent values contribute no key. See the [SQL guide](../sql/README.md#transactions-and-invalidation) for repository writes and PostgreSQL requirements.
|
|
45
|
+
|
|
46
|
+
Query read policies belong to the contract. The query checks its session viewer before its record or bucket scope can become a subscription; a change key grants no read permission. See the [contract query guide](../contract/README.md#query-policies-record-keys-and-buckets).
|
|
47
|
+
|
|
21
48
|
## Using it
|
|
22
49
|
|
|
23
50
|
### How to think about it
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import type { Identity } from "#keys.ts";
|
|
3
|
+
type BucketValue = Identity | null | undefined;
|
|
4
|
+
export declare function bucket<TSchema extends Schema.Top & Schema.ConstraintCodec<BucketValue, unknown, never, never>>(schema: TSchema): TSchema["Rebuild"];
|
|
5
|
+
export declare function declaredBuckets(fields: Schema.Struct.Fields): {
|
|
6
|
+
decode: (input: unknown, options?: import("effect/SchemaAST").ParseOptions) => import("effect/Effect").Effect<BucketValue, Schema.SchemaError, never>;
|
|
7
|
+
field: string;
|
|
8
|
+
}[];
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=buckets.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buckets.d.ts","sourceRoot":"","sources":["../src/buckets.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEzC,KAAK,WAAW,GAAG,QAAQ,GAAG,IAAI,GAAG,SAAS,CAAC;AAE/C,wBAAgB,MAAM,CAAC,OAAO,SAAS,MAAM,CAAC,GAAG,GAAG,MAAM,CAAC,eAAe,CAAC,WAAW,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC,SAAS,CAAC,CAEnJ;AAED,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM;;;IAQ3D"}
|
package/dist/buckets.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
export function bucket(schema) {
|
|
3
|
+
return schema.annotate({ "@shivaedev/changes/bucket": true });
|
|
4
|
+
}
|
|
5
|
+
export function declaredBuckets(fields) {
|
|
6
|
+
return Object.entries(fields).flatMap(([field, schema]) => {
|
|
7
|
+
if (Schema.resolveAnnotations(schema)?.["@shivaedev/changes/bucket"] !== true) {
|
|
8
|
+
return [];
|
|
9
|
+
}
|
|
10
|
+
const codec = Schema.make(schema.ast);
|
|
11
|
+
return [{ decode: Schema.decodeUnknownEffect(codec), field }];
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=buckets.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buckets.js","sourceRoot":"","sources":["../src/buckets.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAKhC,MAAM,UAAU,MAAM,CAA0F,MAAe;IAC9H,OAAO,MAAM,CAAC,QAAQ,CAAC,EAAE,2BAA2B,EAAE,IAAI,EAAE,CAAC,CAAC;AAC/D,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,MAA4B;IAC3D,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,EAAE;QACzD,IAAI,MAAM,CAAC,kBAAkB,CAAC,MAAM,CAAC,EAAE,CAAC,2BAA2B,CAAC,KAAK,IAAI,EAAE,CAAC;YAC/E,OAAO,EAAE,CAAC;QACX,CAAC;QACD,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAqC,MAAM,CAAC,GAAG,CAAC,CAAC;QAC1E,OAAO,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,mBAAmB,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;IAC/D,CAAC,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/keys.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
export type Identity = string | number;
|
|
3
|
+
export interface ListKey<TName extends string = string> {
|
|
4
|
+
readonly _tag: "List";
|
|
5
|
+
readonly collection: TName;
|
|
6
|
+
}
|
|
7
|
+
export interface ItemKey<TName extends string = string> {
|
|
8
|
+
readonly _tag: "Item";
|
|
9
|
+
readonly collection: TName;
|
|
10
|
+
readonly id: Identity;
|
|
11
|
+
}
|
|
12
|
+
export interface BucketKey<TName extends string = string> {
|
|
13
|
+
readonly _tag: "Bucket";
|
|
14
|
+
readonly collection: TName;
|
|
15
|
+
readonly field: string;
|
|
16
|
+
readonly value: Identity;
|
|
17
|
+
}
|
|
18
|
+
export type Key = ListKey | ItemKey | BucketKey;
|
|
19
|
+
export declare const Key: Schema.Union<readonly [Schema.Struct<{
|
|
20
|
+
readonly _tag: Schema.Literal<"Bucket">;
|
|
21
|
+
readonly collection: Schema.String;
|
|
22
|
+
readonly field: Schema.String;
|
|
23
|
+
readonly value: Schema.Union<readonly [Schema.String, Schema.Number]>;
|
|
24
|
+
}>, Schema.Struct<{
|
|
25
|
+
readonly _tag: Schema.Literal<"List">;
|
|
26
|
+
readonly collection: Schema.String;
|
|
27
|
+
}>, Schema.Struct<{
|
|
28
|
+
readonly _tag: Schema.Literal<"Item">;
|
|
29
|
+
readonly collection: Schema.String;
|
|
30
|
+
readonly id: Schema.Union<readonly [Schema.String, Schema.Number]>;
|
|
31
|
+
}>]>;
|
|
32
|
+
type BucketFields = Readonly<Record<string, Schema.Top & {
|
|
33
|
+
readonly Type: Identity;
|
|
34
|
+
}>>;
|
|
35
|
+
export interface Collection<TName extends string, TId extends Identity, TFields extends BucketFields = Record<never, never>> {
|
|
36
|
+
readonly bucket: {
|
|
37
|
+
readonly [TField in keyof TFields]: (value: TFields[TField]["Type"]) => BucketKey<TName>;
|
|
38
|
+
};
|
|
39
|
+
readonly item: (id: TId) => ItemKey<TName>;
|
|
40
|
+
readonly list: ListKey<TName>;
|
|
41
|
+
readonly name: TName;
|
|
42
|
+
}
|
|
43
|
+
export declare function collection<const TName extends string, TId extends Schema.Top & {
|
|
44
|
+
readonly Type: Identity;
|
|
45
|
+
}, const TFields extends BucketFields = Record<never, never>>(name: TName, _id: TId, fields?: TFields): Collection<TName, TId["Type"], TFields>;
|
|
46
|
+
export interface KeyContext {
|
|
47
|
+
readonly scope: string;
|
|
48
|
+
}
|
|
49
|
+
export declare function readKeys(keys: readonly Key[], context?: KeyContext): readonly string[];
|
|
50
|
+
export declare function invalidationKeys(keys: readonly Key[], context?: KeyContext): readonly string[];
|
|
51
|
+
export {};
|
|
52
|
+
//# sourceMappingURL=keys.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keys.d.ts","sourceRoot":"","sources":["../src/keys.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,MAAM,CAAC;AAEvC,MAAM,WAAW,OAAO,CAAC,KAAK,SAAS,MAAM,GAAG,MAAM;IACrD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,UAAU,EAAE,KAAK,CAAC;CAC3B;AAED,MAAM,WAAW,OAAO,CAAC,KAAK,SAAS,MAAM,GAAG,MAAM;IACrD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,UAAU,EAAE,KAAK,CAAC;IAC3B,QAAQ,CAAC,EAAE,EAAE,QAAQ,CAAC;CACtB;AAED,MAAM,WAAW,SAAS,CAAC,KAAK,SAAS,MAAM,GAAG,MAAM;IACvD,QAAQ,CAAC,IAAI,EAAE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MAAM,CAAC,EAAE,OAAO,GAAG,UAAU,CAAC,KAAK,EAAE,GAAG,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,CAAC;AAYpF,MAAM,WAAW,UAAU;IAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACvB;AAiBD,wBAAgB,QAAQ,CAAC,IAAI,EAAE,SAAS,GAAG,EAAE,EAAE,OAAO,CAAC,EAAE,UAAU,GAAG,SAAS,MAAM,EAAE,CAItF;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,SAAS,GAAG,EAAE,EAAE,OAAO,CAAC,EAAE,UAAU,GAAG,SAAS,MAAM,EAAE,CAE9F"}
|
package/dist/keys.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
export const Key = Schema.Union([
|
|
3
|
+
Schema.Struct({
|
|
4
|
+
_tag: Schema.Literal("Bucket"),
|
|
5
|
+
collection: Schema.String,
|
|
6
|
+
field: Schema.String,
|
|
7
|
+
value: Schema.Union([Schema.String, Schema.Number]),
|
|
8
|
+
}),
|
|
9
|
+
Schema.Struct({ _tag: Schema.Literal("List"), collection: Schema.String }),
|
|
10
|
+
Schema.Struct({ _tag: Schema.Literal("Item"), collection: Schema.String, id: Schema.Union([Schema.String, Schema.Number]) }),
|
|
11
|
+
]);
|
|
12
|
+
export function collection(name, _id, fields = {}) {
|
|
13
|
+
return {
|
|
14
|
+
bucket: Object.fromEntries(Object.keys(fields).map((field) => [field, (value) => ({ _tag: "Bucket", collection: name, field, value })])),
|
|
15
|
+
item: (id) => ({ _tag: "Item", collection: name, id }),
|
|
16
|
+
list: { _tag: "List", collection: name },
|
|
17
|
+
name,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function contextual(key, context) {
|
|
21
|
+
return context === undefined ? key : `${JSON.stringify(["session", context.scope])}:${key}`;
|
|
22
|
+
}
|
|
23
|
+
function own(key) {
|
|
24
|
+
switch (key._tag) {
|
|
25
|
+
case "List":
|
|
26
|
+
return key.collection;
|
|
27
|
+
case "Item":
|
|
28
|
+
return `${key.collection}:${key.id}`;
|
|
29
|
+
case "Bucket":
|
|
30
|
+
return JSON.stringify(["bucket", key.collection, key.field, key.value]);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export function readKeys(keys, context) {
|
|
34
|
+
return [...new Set(keys.flatMap((key) => (key._tag === "Item" ? [own(key), `${key.collection}:*`] : [own(key)])))].map((key) => contextual(key, context));
|
|
35
|
+
}
|
|
36
|
+
export function invalidationKeys(keys, context) {
|
|
37
|
+
return [...new Set(keys.flatMap((key) => (key._tag === "Item" ? [own(key), key.collection] : [own(key)])))].map((key) => contextual(key, context));
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=keys.js.map
|
package/dist/keys.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keys.js","sourceRoot":"","sources":["../src/keys.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAuBhC,MAAM,CAAC,MAAM,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC;IAC/B,MAAM,CAAC,MAAM,CAAC;QACb,IAAI,EAAE,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC;QAC9B,UAAU,EAAE,MAAM,CAAC,MAAM;QACzB,KAAK,EAAE,MAAM,CAAC,MAAM;QACpB,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;KACnD,CAAC;IACF,MAAM,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC;IAC1E,MAAM,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,UAAU,EAAE,MAAM,CAAC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AAS,CAAC,CAAC,QAAQ,EAAE,GAAG,CAAC,UAAU,EAAE,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC;IAC1E,CAAC;AACF,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,IAAoB,EAAE,OAAoB;IAClE,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,GAAG,GAAG,CAAC,UAAU,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAC9H,UAAU,CAAC,GAAG,EAAE,OAAO,CAAC,CACxB,CAAC;AACH,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,IAAoB,EAAE,OAAoB;IAC1E,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC;AACpJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/changes",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Commit-bound change channels for Effect: publish recorded changes only after the transaction commits",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"effect"
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"README.md"
|
|
44
44
|
],
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@shivaedev/quality": "0.
|
|
47
|
-
"@shivaedev/test-story": "0.
|
|
46
|
+
"@shivaedev/quality": "0.43.0",
|
|
47
|
+
"@shivaedev/test-story": "0.22.0",
|
|
48
48
|
"@types/node": "24.19.2",
|
|
49
49
|
"effect": "4.0.5",
|
|
50
50
|
"typescript": "7.0.2",
|
package/src/buckets.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import type { Identity } from "#keys.ts";
|
|
3
|
+
|
|
4
|
+
type BucketValue = Identity | null | undefined;
|
|
5
|
+
|
|
6
|
+
export function bucket<TSchema extends Schema.Top & Schema.ConstraintCodec<BucketValue, unknown, never, never>>(schema: TSchema): TSchema["Rebuild"] {
|
|
7
|
+
return schema.annotate({ "@shivaedev/changes/bucket": true });
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function declaredBuckets(fields: Schema.Struct.Fields) {
|
|
11
|
+
return Object.entries(fields).flatMap(([field, schema]) => {
|
|
12
|
+
if (Schema.resolveAnnotations(schema)?.["@shivaedev/changes/bucket"] !== true) {
|
|
13
|
+
return [];
|
|
14
|
+
}
|
|
15
|
+
const codec = Schema.make<Schema.Codec<BucketValue, unknown>>(schema.ast);
|
|
16
|
+
return [{ decode: Schema.decodeUnknownEffect(codec), field }];
|
|
17
|
+
});
|
|
18
|
+
}
|
package/src/keys.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
|
|
3
|
+
export type Identity = string | number;
|
|
4
|
+
|
|
5
|
+
export interface ListKey<TName extends string = string> {
|
|
6
|
+
readonly _tag: "List";
|
|
7
|
+
readonly collection: TName;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface ItemKey<TName extends string = string> {
|
|
11
|
+
readonly _tag: "Item";
|
|
12
|
+
readonly collection: TName;
|
|
13
|
+
readonly id: Identity;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface BucketKey<TName extends string = string> {
|
|
17
|
+
readonly _tag: "Bucket";
|
|
18
|
+
readonly collection: TName;
|
|
19
|
+
readonly field: string;
|
|
20
|
+
readonly value: Identity;
|
|
21
|
+
}
|
|
22
|
+
export type Key = ListKey | ItemKey | BucketKey;
|
|
23
|
+
|
|
24
|
+
export const Key = Schema.Union([
|
|
25
|
+
Schema.Struct({
|
|
26
|
+
_tag: Schema.Literal("Bucket"),
|
|
27
|
+
collection: Schema.String,
|
|
28
|
+
field: Schema.String,
|
|
29
|
+
value: Schema.Union([Schema.String, Schema.Number]),
|
|
30
|
+
}),
|
|
31
|
+
Schema.Struct({ _tag: Schema.Literal("List"), collection: Schema.String }),
|
|
32
|
+
Schema.Struct({ _tag: Schema.Literal("Item"), collection: Schema.String, id: Schema.Union([Schema.String, Schema.Number]) }),
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
type BucketFields = Readonly<Record<string, Schema.Top & { readonly Type: Identity }>>;
|
|
36
|
+
export interface Collection<TName extends string, TId extends Identity, TFields extends BucketFields = Record<never, never>> {
|
|
37
|
+
readonly bucket: { readonly [TField in keyof TFields]: (value: TFields[TField]["Type"]) => BucketKey<TName> };
|
|
38
|
+
readonly item: (id: TId) => ItemKey<TName>;
|
|
39
|
+
readonly list: ListKey<TName>;
|
|
40
|
+
readonly name: TName;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function collection<
|
|
44
|
+
const TName extends string,
|
|
45
|
+
TId extends Schema.Top & { readonly Type: Identity },
|
|
46
|
+
const TFields extends BucketFields = Record<never, never>,
|
|
47
|
+
>(name: TName, _id: TId, fields?: TFields): Collection<TName, TId["Type"], TFields>;
|
|
48
|
+
export function collection(name: string, _id: Schema.Top, fields: BucketFields = {}) {
|
|
49
|
+
return {
|
|
50
|
+
bucket: Object.fromEntries(
|
|
51
|
+
Object.keys(fields).map((field) => [field, (value: Identity): BucketKey => ({ _tag: "Bucket", collection: name, field, value })]),
|
|
52
|
+
),
|
|
53
|
+
item: (id: Identity): ItemKey => ({ _tag: "Item", collection: name, id }),
|
|
54
|
+
list: { _tag: "List", collection: name },
|
|
55
|
+
name,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface KeyContext {
|
|
60
|
+
readonly scope: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function contextual(key: string, context?: KeyContext): string {
|
|
64
|
+
return context === undefined ? key : `${JSON.stringify(["session", context.scope])}:${key}`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function own(key: Key): string {
|
|
68
|
+
switch (key._tag) {
|
|
69
|
+
case "List":
|
|
70
|
+
return key.collection;
|
|
71
|
+
case "Item":
|
|
72
|
+
return `${key.collection}:${key.id}`;
|
|
73
|
+
case "Bucket":
|
|
74
|
+
return JSON.stringify(["bucket", key.collection, key.field, key.value]);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function readKeys(keys: readonly Key[], context?: KeyContext): readonly string[] {
|
|
79
|
+
return [...new Set(keys.flatMap((key) => (key._tag === "Item" ? [own(key), `${key.collection}:*`] : [own(key)])))].map((key) =>
|
|
80
|
+
contextual(key, context),
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function invalidationKeys(keys: readonly Key[], context?: KeyContext): readonly string[] {
|
|
85
|
+
return [...new Set(keys.flatMap((key) => (key._tag === "Item" ? [own(key), key.collection] : [own(key)])))].map((key) => contextual(key, context));
|
|
86
|
+
}
|