@party-stack/ontology-backend 0.2.2 → 0.3.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/lib/delivery.d.ts.map +1 -1
- package/lib/delivery.js +0 -1
- package/lib/delivery.js.map +1 -1
- package/lib/fulfillment.d.ts.map +1 -1
- package/lib/fulfillment.js +13 -12
- package/lib/fulfillment.js.map +1 -1
- package/lib/owned.js +3 -10
- package/lib/owned.js.map +1 -1
- package/lib/policy/indexes.js +3 -11
- package/lib/policy/indexes.js.map +1 -1
- package/lib/policy/planner.d.ts.map +1 -1
- package/lib/policy/planner.js +21 -44
- package/lib/policy/planner.js.map +1 -1
- package/lib/policy/view.d.ts.map +1 -1
- package/lib/policy/view.js +5 -3
- package/lib/policy/view.js.map +1 -1
- package/lib/query.d.ts +1 -1
- package/lib/subset.d.ts +2 -1
- package/lib/subset.d.ts.map +1 -1
- package/lib/subset.js +23 -32
- package/lib/subset.js.map +1 -1
- package/package.json +2 -2
package/lib/policy/view.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"view.js","names":["ref"],"sources":["../../src/policy/view.ts"],"sourcesContent":["import {\n resolveQuerySources,\n validatePolicy,\n selectedProperty,\n validateQueryDocument,\n} from \"@party-stack/ontology-spec/query\";\nimport { field as ref, eq, evaluateQuery } from \"@party-stack/ontology-storage\";\nimport { Effect } from \"effect\";\nimport { BackendError } from \"../index.js\";\nimport { queryExecutor } from \"../query.js\";\nimport { authorizationPlans } from \"./planner.js\";\nimport type { OwnedReadTransaction } from \"../owned.js\";\nimport type { QueryEntity, QueryReader } from \"../query.js\";\nimport type { OntologyIR, Query, QueryDocument, QuerySelection } from \"@party-stack/ontology-spec\";\nimport type { Query as Scan, Key, Row } from \"@party-stack/ontology-storage\";\n\nexport const denied = (operation: string) =>\n new BackendError({\n operation,\n kind: \"Forbidden\",\n message: \"This operation is not permitted.\",\n retryable: false,\n actionOutcome: \"not-committed\",\n });\nconst keyOf = (type: string, key: Key) => JSON.stringify([type, key]);\n\nexport function policyProperties(ir: OntologyIR) {\n const allowed = new Map<string, Set<string>>();\n const guaranteed = new Map<string, Set<string>>();\n type Scope = ReadonlyMap<string, Set<string>>;\n function query(q: Query, parent: Scope) {\n if (q.from.source.ontology) return;\n const object = ir.objectTypes.find((t) => t.name === q.from.source.objectType);\n if (!object) return;\n const upper = allowed.get(object.name) ?? new Set<string>();\n // Publish before descending: another binding of this type must extend\n // the same set, rather than have its grants overwritten on return.\n allowed.set(object.name, upper);\n const scope = new Map([...parent, [q.from.binding, upper]]);\n const always = new Set([object.primaryKey]);\n upper.add(object.primaryKey);\n function selections(items: readonly QuerySelection[], conditional: boolean) {\n for (const item of items) {\n if (item.kind === \"sub\") query(item.value.query, scope);\n else if (item.kind === \"selectCase\") {\n for (const branch of item.value.branches) selections(branch.select, true);\n selections(item.value.else ?? [], true);\n } else {\n const p = selectedProperty(item.value.expression);\n if (p) {\n scope.get(p.binding)?.add(p.property);\n // An ancestor field selected inside a child query is\n // conditional on that child having a matching row.\n if (!conditional && p.binding === q.from.binding) always.add(p.property);\n }\n }\n }\n }\n selections(q.select, false);\n const existing = guaranteed.get(object.name);\n guaranteed.set(object.name, existing ? new Set([...existing].filter((p) => always.has(p))) : always);\n }\n function roots(items: readonly QuerySelection[]) {\n for (const item of items) {\n if (item.kind === \"sub\") query(item.value.query, new Map());\n else if (item.kind === \"selectCase\") {\n item.value.branches.forEach((b) => roots(b.select));\n roots(item.value.else ?? []);\n }\n }\n }\n if (ir.policy) roots(resolveQuerySources(ir, ir.policy.objects).select);\n return { allowed, guaranteed };\n}\nfunction filterFields(query: Scan): Set<string> {\n const result = new Set<string>();\n const visit = (value: unknown): void => {\n if (!value || typeof value !== \"object\") return;\n if (\"_tag\" in value && value._tag === \"Field\" && \"path\" in value && Array.isArray(value.path)) {\n result.add(value.path[0]);\n return;\n }\n Object.values(value).forEach((v) => (Array.isArray(v) ? v.forEach(visit) : visit(v)));\n };\n visit(query.where);\n query.orderBy?.forEach((o) => result.add(o.field.path[0]!));\n return result;\n}\nexport interface PolicyViewOptions {\n readonly additionalObjects?: QueryDocument;\n readonly inputs?: Record<string, unknown>;\n readonly inputDefinitions?: readonly import(\"@party-stack/ontology-spec\").FieldDef[];\n}\n\n/** Bound to one authenticated context. Underlying reader is trusted and owns transaction lifetime. */\nexport function evaluatePolicyView(\n reader: QueryReader,\n context: Record<string, unknown>,\n options?: PolicyViewOptions,\n strategy: \"rewrite\" | \"eager\" = \"rewrite\"\n) {\n const ir = reader.ir;\n const issues = validatePolicy(ir);\n if (issues.length)\n throw new BackendError({\n operation: \"policy\",\n kind: \"InvalidConfiguration\",\n message: \"Invalid ontology policy.\",\n retryable: false,\n issues,\n });\n if (!ir.policy)\n throw new BackendError({\n operation: \"policy\",\n kind: \"InvalidConfiguration\",\n message: \"An explicit ontology policy is required.\",\n retryable: false,\n });\n const documents = [\n ir.policy.objects,\n ...(options?.additionalObjects ? [options.additionalObjects] : []),\n ].map((document) => resolveQuerySources(ir, document));\n const properties = policyProperties(\n options?.additionalObjects\n ? {\n ...ir,\n policy: {\n ...ir.policy,\n objects: { select: [...ir.policy.objects.select, ...options.additionalObjects.select] },\n },\n }\n : ir\n );\n const readable = new WeakMap<object, Set<string>>();\n const loadSubset = Effect.fn(\"Policy.loadSubset\")(function* (objectType: string, query: Scan = {}) {\n const guaranteed = properties.guaranteed.get(objectType);\n if (!guaranteed || [...filterFields(query)].some((f) => !guaranteed.has(f)))\n return yield* denied(\"loadSubset\");\n const grants = new Map<string, { value: Record<string, unknown>; fields: Set<string>; key: Key }>();\n for (const document of documents) {\n const proposed =\n strategy === \"eager\" ? [document] : authorizationPlans(document, objectType, query);\n // A physical caller predicate may use semantics the logical IR cannot\n // express yet. Keep the original plan rather than changing behavior.\n const plans = proposed.some(\n (plan) =>\n validateQueryDocument(ir, plan, { mode: \"entity\", inputs: options?.inputDefinitions })\n .length\n )\n ? [document]\n : proposed;\n for (const plan of plans) {\n const result = yield* queryExecutor({\n reader,\n context,\n inputs: options?.inputs,\n inputDefinitions: options?.inputDefinitions,\n mode: \"entity\",\n }).run(plan);\n for (const entity of result.entities) {\n if (entity.source || entity.objectType !== objectType) continue;\n const key = keyOf(entity.objectType, entity.key);\n const grant = grants.get(key) ?? {\n value: entity.value,\n key: entity.key,\n fields: new Set<string>(),\n };\n entity.properties.forEach((property) => grant.fields.add(property));\n grants.set(key, grant);\n }\n }\n }\n const rows: Row<Record<string, unknown>>[] = [];\n for (const grant of grants.values()) {\n const value = Object.fromEntries(\n Object.entries(grant.value).filter(([property]) => grant.fields.has(property))\n );\n readable.set(value, grant.fields);\n rows.push({ key: grant.key, value });\n }\n return yield* Effect.try({\n try: () => evaluateQuery(rows, query),\n catch: () =>\n new BackendError({\n operation: \"loadSubset\",\n kind: \"InvalidRequest\",\n message: \"Invalid authorized subset query.\",\n retryable: false,\n }),\n });\n });\n const tx: OwnedReadTransaction = {\n loadSubset,\n get: Effect.fnUntraced(function* (objectType, key) {\n const object = ir.objectTypes.find((t) => t.name === objectType);\n if (!object) return yield* denied(\"get\");\n const rows = yield* loadSubset(objectType, { where: eq(ref(object.primaryKey), key), limit: 1 });\n return rows[0]?.value;\n }),\n };\n return {\n ...properties,\n reader: { ir, loadSubset } satisfies QueryReader,\n tx,\n canReadProperty: (entity: QueryEntity, property: string) =>\n readable.get(entity.value)?.has(property) ?? false,\n };\n}\n"],"mappings":";;;;;;;;AAgBA,MAAa,UAAU,cACnB,IAAI,aAAa;CACb;CACA,MAAM;CACN,SAAS;CACT,WAAW;CACX,eAAe;AACnB,CAAC;AACL,MAAM,SAAS,MAAc,QAAa,KAAK,UAAU,CAAC,MAAM,GAAG,CAAC;AAEpE,SAAgB,iBAAiB,IAAgB;CAC7C,MAAM,0BAAU,IAAI,IAAyB;CAC7C,MAAM,6BAAa,IAAI,IAAyB;CAEhD,SAAS,MAAM,GAAU,QAAe;EACpC,IAAI,EAAE,KAAK,OAAO,UAAU;EAC5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B;GACJ;EACJ;EACA,MAAM,OAAuC,CAAC;EAC9C,KAAK,MAAM,SAAS,OAAO,OAAO,GAAG;GACjC,MAAM,QAAQ,OAAO,YACjB,OAAO,QAAQ,MAAM,KAAK,CAAC,CAAC,QAAQ,CAAC,cAAc,MAAM,OAAO,IAAI,QAAQ,CAAC,CACjF;GACA,SAAS,IAAI,OAAO,MAAM,MAAM;GAChC,KAAK,KAAK;IAAE,KAAK,MAAM;IAAK;GAAM,CAAC;EACvC;EACA,OAAO,OAAO,OAAO,IAAI;GACrB,WAAW,cAAc,MAAM,KAAK;GACpC,aACI,IAAI,aAAa;IACb,WAAW;IACX,MAAM;IACN,SAAS;IACT,WAAW;GACf,CAAC;EACT,CAAC;CACL,CAAC;CACD,MAAM,KAA2B;EAC7B;EACA,KAAK,OAAO,WAAW,WAAW,YAAY,KAAK;GAC/C,MAAM,SAAS,GAAG,YAAY,MAAM,MAAM,EAAE,SAAS,UAAU;GAC/D,IAAI,CAAC,QAAQ,OAAO,OAAO,OAAO,KAAK;GAEvC,QAAO,OADa,WAAW,YAAY;IAAE,OAAO,GAAGA,MAAI,OAAO,UAAU,GAAG,GAAG;IAAG,OAAO;GAAE,CAAC,EAAA,CACnF,EAAE,EAAE;EACpB,CAAC;CACL;CACA,OAAO;EACH,GAAG;EACH,QAAQ;GAAE;GAAI;EAAW;EACzB;EACA,kBAAkB,QAAqB,aACnC,SAAS,IAAI,OAAO,KAAK,CAAC,EAAE,IAAI,QAAQ,KAAK;CACrD;AACJ"}
|
|
1
|
+
{"version":3,"file":"view.js","names":["ref"],"sources":["../../src/policy/view.ts"],"sourcesContent":["import {\n resolveQuerySources,\n validatePolicy,\n selectedProperty,\n validateQueryDocument,\n} from \"@party-stack/ontology-spec/query\";\nimport { field as ref, eq, evaluateQuery, filterField } from \"@party-stack/ontology-storage\";\nimport { Effect } from \"effect\";\nimport { BackendError } from \"../index.js\";\nimport { queryExecutor } from \"../query.js\";\nimport { authorizationPlans } from \"./planner.js\";\nimport type { OwnedReadTransaction } from \"../owned.js\";\nimport type { QueryEntity, QueryReader } from \"../query.js\";\nimport type { OntologyIR, Query, QueryDocument, QuerySelection } from \"@party-stack/ontology-spec\";\nimport type { Query as Scan, Key, Row } from \"@party-stack/ontology-storage\";\n\nexport const denied = (operation: string) =>\n new BackendError({\n operation,\n kind: \"Forbidden\",\n message: \"This operation is not permitted.\",\n retryable: false,\n actionOutcome: \"not-committed\",\n });\nconst keyOf = (type: string, key: Key) => JSON.stringify([type, key]);\n\nexport function policyProperties(ir: OntologyIR) {\n const allowed = new Map<string, Set<string>>();\n const guaranteed = new Map<string, Set<string>>();\n type Scope = ReadonlyMap<string, Set<string>>;\n function query(q: Query, parent: Scope) {\n if (q.from.source.ontology) return;\n const object = ir.objectTypes.find((t) => t.name === q.from.source.objectType);\n if (!object) return;\n const upper = allowed.get(object.name) ?? new Set<string>();\n // Publish before descending: another binding of this type must extend\n // the same set, rather than have its grants overwritten on return.\n allowed.set(object.name, upper);\n const scope = new Map([...parent, [q.from.binding, upper]]);\n const always = new Set([object.primaryKey]);\n upper.add(object.primaryKey);\n function selections(items: readonly QuerySelection[], conditional: boolean) {\n for (const item of items) {\n if (item.kind === \"sub\") query(item.value.query, scope);\n else if (item.kind === \"selectCase\") {\n for (const branch of item.value.branches) selections(branch.select, true);\n selections(item.value.else ?? [], true);\n } else {\n const p = selectedProperty(item.value.expression);\n if (p) {\n scope.get(p.binding)?.add(p.property);\n // An ancestor field selected inside a child query is\n // conditional on that child having a matching row.\n if (!conditional && p.binding === q.from.binding) always.add(p.property);\n }\n }\n }\n }\n selections(q.select, false);\n const existing = guaranteed.get(object.name);\n guaranteed.set(object.name, existing ? new Set([...existing].filter((p) => always.has(p))) : always);\n }\n function roots(items: readonly QuerySelection[]) {\n for (const item of items) {\n if (item.kind === \"sub\") query(item.value.query, new Map());\n else if (item.kind === \"selectCase\") {\n item.value.branches.forEach((b) => roots(b.select));\n roots(item.value.else ?? []);\n }\n }\n }\n if (ir.policy) roots(resolveQuerySources(ir, ir.policy.objects).select);\n return { allowed, guaranteed };\n}\nfunction filterFields(query: Scan): Set<string> {\n const result = new Set<string>();\n // Every row field a filter reads, including inside `length`; literals read none.\n const visit = (value: unknown): void => {\n if (!value || typeof value !== \"object\") return;\n const read = filterField(value);\n if (read) {\n result.add(read.path[0]!);\n return;\n }\n if (\"kind\" in value && value.kind === \"literal\") return;\n Object.values(value).forEach((v) => (Array.isArray(v) ? v.forEach(visit) : visit(v)));\n };\n visit(query.where);\n query.orderBy?.forEach((o) => result.add(o.field.path[0]!));\n return result;\n}\nexport interface PolicyViewOptions {\n readonly additionalObjects?: QueryDocument;\n readonly inputs?: Record<string, unknown>;\n readonly inputDefinitions?: readonly import(\"@party-stack/ontology-spec\").FieldDef[];\n}\n\n/** Bound to one authenticated context. Underlying reader is trusted and owns transaction lifetime. */\nexport function evaluatePolicyView(\n reader: QueryReader,\n context: Record<string, unknown>,\n options?: PolicyViewOptions,\n strategy: \"rewrite\" | \"eager\" = \"rewrite\"\n) {\n const ir = reader.ir;\n const issues = validatePolicy(ir);\n if (issues.length)\n throw new BackendError({\n operation: \"policy\",\n kind: \"InvalidConfiguration\",\n message: \"Invalid ontology policy.\",\n retryable: false,\n issues,\n });\n if (!ir.policy)\n throw new BackendError({\n operation: \"policy\",\n kind: \"InvalidConfiguration\",\n message: \"An explicit ontology policy is required.\",\n retryable: false,\n });\n const documents = [\n ir.policy.objects,\n ...(options?.additionalObjects ? [options.additionalObjects] : []),\n ].map((document) => resolveQuerySources(ir, document));\n const properties = policyProperties(\n options?.additionalObjects\n ? {\n ...ir,\n policy: {\n ...ir.policy,\n objects: { select: [...ir.policy.objects.select, ...options.additionalObjects.select] },\n },\n }\n : ir\n );\n const readable = new WeakMap<object, Set<string>>();\n const loadSubset = Effect.fn(\"Policy.loadSubset\")(function* (objectType: string, query: Scan = {}) {\n const guaranteed = properties.guaranteed.get(objectType);\n if (!guaranteed || [...filterFields(query)].some((f) => !guaranteed.has(f)))\n return yield* denied(\"loadSubset\");\n const grants = new Map<string, { value: Record<string, unknown>; fields: Set<string>; key: Key }>();\n for (const document of documents) {\n const proposed =\n strategy === \"eager\" ? [document] : authorizationPlans(document, objectType, query);\n // A physical caller predicate may use semantics the logical IR cannot\n // express yet. Keep the original plan rather than changing behavior.\n const plans = proposed.some(\n (plan) =>\n validateQueryDocument(ir, plan, { mode: \"entity\", inputs: options?.inputDefinitions })\n .length\n )\n ? [document]\n : proposed;\n for (const plan of plans) {\n const result = yield* queryExecutor({\n reader,\n context,\n inputs: options?.inputs,\n inputDefinitions: options?.inputDefinitions,\n mode: \"entity\",\n }).run(plan);\n for (const entity of result.entities) {\n if (entity.source || entity.objectType !== objectType) continue;\n const key = keyOf(entity.objectType, entity.key);\n const grant = grants.get(key) ?? {\n value: entity.value,\n key: entity.key,\n fields: new Set<string>(),\n };\n entity.properties.forEach((property) => grant.fields.add(property));\n grants.set(key, grant);\n }\n }\n }\n const rows: Row<Record<string, unknown>>[] = [];\n for (const grant of grants.values()) {\n const value = Object.fromEntries(\n Object.entries(grant.value).filter(([property]) => grant.fields.has(property))\n );\n readable.set(value, grant.fields);\n rows.push({ key: grant.key, value });\n }\n return yield* Effect.try({\n try: () => evaluateQuery(rows, query),\n catch: () =>\n new BackendError({\n operation: \"loadSubset\",\n kind: \"InvalidRequest\",\n message: \"Invalid authorized subset query.\",\n retryable: false,\n }),\n });\n });\n const tx: OwnedReadTransaction = {\n loadSubset,\n get: Effect.fnUntraced(function* (objectType, key) {\n const object = ir.objectTypes.find((t) => t.name === objectType);\n if (!object) return yield* denied(\"get\");\n const rows = yield* loadSubset(objectType, { where: eq(ref(object.primaryKey), key), limit: 1 });\n return rows[0]?.value;\n }),\n };\n return {\n ...properties,\n reader: { ir, loadSubset } satisfies QueryReader,\n tx,\n canReadProperty: (entity: QueryEntity, property: string) =>\n readable.get(entity.value)?.has(property) ?? false,\n };\n}\n"],"mappings":";;;;;;;;AAgBA,MAAa,UAAU,cACnB,IAAI,aAAa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"}
|
package/lib/query.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export type QueryExecutionOptions = Omit<QueryExecutionOptions$1<BackendError>,
|
|
|
10
10
|
export declare function queryExecutor(options: QueryExecutionOptions): {
|
|
11
11
|
evaluate: (e: import("@party-stack/ontology-spec").Expression, locals?: Map<string, unknown> | undefined) => Effect.Effect<import("@party-stack/ontology-spec").Uncertain<unknown>, BackendError<"Conflict" | "Forbidden" | "HistoryExpired" | "Internal" | "InvalidConfiguration" | "InvalidRequest" | "InvalidResponse" | "NotFound" | "NotImplemented" | "RateLimited" | "ResultExpired" | "SchemaMismatch" | "SessionExpired" | "SessionReplaced" | "SourceMismatch" | "Unauthenticated" | "Unavailable" | "ValidationFailed" | "ViewReplaced">, never>;
|
|
12
12
|
lower: (e: import("@party-stack/ontology-spec").Expression, from: import("@party-stack/ontology-spec").QueryFrom, locals: Map<string, unknown>) => Effect.Effect<{
|
|
13
|
-
where?: import("@party-stack/ontology-storage").
|
|
13
|
+
where?: import("@party-stack/ontology-storage").Filter;
|
|
14
14
|
exact: boolean;
|
|
15
15
|
}, BackendError<"Conflict" | "Forbidden" | "HistoryExpired" | "Internal" | "InvalidConfiguration" | "InvalidRequest" | "InvalidResponse" | "NotFound" | "NotImplemented" | "RateLimited" | "ResultExpired" | "SchemaMismatch" | "SessionExpired" | "SessionReplaced" | "SourceMismatch" | "Unauthenticated" | "Unavailable" | "ValidationFailed" | "ViewReplaced">, never>;
|
|
16
16
|
run: (document: QueryDocument) => Effect.Effect<{
|
package/lib/subset.d.ts
CHANGED
|
@@ -10,7 +10,8 @@ export interface CompiledSubset {
|
|
|
10
10
|
readonly objectType: string;
|
|
11
11
|
readonly query?: Query;
|
|
12
12
|
}
|
|
13
|
-
/**
|
|
13
|
+
/** Wrap a storage query as a source-bound subset. Storage filters are spec expressions
|
|
14
|
+
* over the storage row binding, so only the binding name changes. */
|
|
14
15
|
export declare const objectSubset: (objectType: string, scan?: Query, binding?: string) => ObjectSubset;
|
|
15
16
|
/** Lower a source-bound spec subset at the engine/provider boundary, never at the public demand API. */
|
|
16
17
|
export declare const compileObjectSubset: (ir: OntologyIR, subset: ObjectSubset, context?: Record<string, unknown> | undefined) => Effect.Effect<CompiledSubset, BackendError<"Conflict" | "Forbidden" | "HistoryExpired" | "Internal" | "InvalidConfiguration" | "InvalidRequest" | "InvalidResponse" | "NotFound" | "NotImplemented" | "RateLimited" | "ResultExpired" | "SchemaMismatch" | "SessionExpired" | "SessionReplaced" | "SourceMismatch" | "Unauthenticated" | "Unavailable" | "ValidationFailed" | "ViewReplaced">, never>;
|
package/lib/subset.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subset.d.ts","names":[],"sources":["../src/subset.ts"],"mappings":";;;;;;;;iBAUiB;WACJ;WACA,QAAQ
|
|
1
|
+
{"version":3,"file":"subset.d.ts","names":[],"sources":["../src/subset.ts"],"mappings":";;;;;;;;iBAUiB;WACJ;WACA,QAAQ;;;;qBAMR,eAAY,oBAAsB,OAAQ,OAAK,qBAAyB;;qBAuBxE,sBAAmB,IAAA,YAAA,QAAA,cAAA,UAAA,wCAAA,OAAA,OAAA,gBAAA"}
|
package/lib/subset.js
CHANGED
|
@@ -3,41 +3,32 @@ import { ObjectSelectionSchema, ObjectSubsetSchema } from "./subsetSchema.js";
|
|
|
3
3
|
import { coversSubset, resolveSubsetSnapshot } from "./fulfillment.js";
|
|
4
4
|
import { queryExecutor } from "./query.js";
|
|
5
5
|
import * as q from "@party-stack/ontology-spec/query";
|
|
6
|
-
import { QuerySchema } from "@party-stack/ontology-storage";
|
|
6
|
+
import { QuerySchema, fieldExpression, rebindFilter } from "@party-stack/ontology-storage";
|
|
7
7
|
import { Effect, Predicate, Schema } from "effect";
|
|
8
8
|
import { QueryDocumentSchema } from "@party-stack/ontology-spec/effect";
|
|
9
9
|
//#region src/subset.ts
|
|
10
|
-
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
limit: scan.limit,
|
|
33
|
-
offset: scan.offset,
|
|
34
|
-
after: scan.after ? {
|
|
35
|
-
values: [...scan.after.values],
|
|
36
|
-
key: scan.after.key
|
|
37
|
-
} : void 0
|
|
38
|
-
} } : {}
|
|
39
|
-
};
|
|
40
|
-
};
|
|
10
|
+
/** Wrap a storage query as a source-bound subset. Storage filters are spec expressions
|
|
11
|
+
* over the storage row binding, so only the binding name changes. */
|
|
12
|
+
const objectSubset = (objectType, scan = {}, binding = "row") => ({
|
|
13
|
+
from: {
|
|
14
|
+
source: { objectType },
|
|
15
|
+
binding
|
|
16
|
+
},
|
|
17
|
+
...scan.where ? { where: rebindFilter(scan.where, binding) } : {},
|
|
18
|
+
...scan.orderBy ? { orderBy: scan.orderBy.map((o) => ({
|
|
19
|
+
expression: rebindFilter(fieldExpression(o.field), binding),
|
|
20
|
+
direction: o.direction,
|
|
21
|
+
nulls: o.nulls
|
|
22
|
+
})) } : {},
|
|
23
|
+
...scan.limit !== void 0 || scan.offset !== void 0 || scan.after !== void 0 ? { page: {
|
|
24
|
+
limit: scan.limit,
|
|
25
|
+
offset: scan.offset,
|
|
26
|
+
after: scan.after ? {
|
|
27
|
+
values: [...scan.after.values],
|
|
28
|
+
key: scan.after.key
|
|
29
|
+
} : void 0
|
|
30
|
+
} } : {}
|
|
31
|
+
});
|
|
41
32
|
/** Lower a source-bound spec subset at the engine/provider boundary, never at the public demand API. */
|
|
42
33
|
const compileObjectSubset = Effect.fnUntraced(function* (ir, subset, context) {
|
|
43
34
|
if (Predicate.isObject(subset) && Object.hasOwn(subset, "select")) return yield* new BackendError({
|
package/lib/subset.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subset.js","names":["EffectPredicate"],"sources":["../src/subset.ts"],"sourcesContent":["import { QueryDocumentSchema } from \"@party-stack/ontology-spec/effect\";\nimport * as q from \"@party-stack/ontology-spec/query\";\nimport { QuerySchema } from \"@party-stack/ontology-storage\";\nimport { Effect, Predicate as EffectPredicate, Schema } from \"effect\";\nimport { BackendError } from \"./errors.js\";\nimport { queryExecutor } from \"./query.js\";\nimport type { OntologyIR, Expression } from \"@party-stack/ontology-spec\";\nimport type { ObjectSubset } from \"@party-stack/ontology-spec/query\";\nimport type { Operand, Predicate, Query, Field } from \"@party-stack/ontology-storage\";\n\nexport interface CompiledSubset {\n readonly objectType: string;\n readonly query?: Query;\n}\nexport { ObjectSubsetSchema, ObjectSelectionSchema } from \"./subsetSchema.js\";\n\n/** Infrastructure adapter for existing physical provider/transaction readers. */\nexport const objectSubset = (objectType: string, scan: Query = {}, binding = \"row\"): ObjectSubset => {\n const operand = (v: Operand): Expression =>\n v && typeof v === \"object\" && \"_tag\" in v && v._tag === \"Field\"\n ? q.field(binding, ...(v as Field).path)\n : q.literal(v);\n const predicate = (p: Predicate): Expression => {\n if (p.op === \"and\" || p.op === \"or\") return q[p.op](...p.predicates.map(predicate));\n if (p.op === \"not\") return q.not(predicate(p.predicate));\n if (p.op === \"in\") return q.inArray(operand(p.left), q.literal([...p.values]));\n // A present list with no elements.\n if (p.op === \"empty\")\n return q.and(\n q.ne(operand(p.left), q.literal(undefined)),\n q.eq(q.length(operand(p.left)), q.literal(0))\n );\n return q[p.op](operand(p.left), operand(p.right));\n };\n return {\n from: { source: { objectType }, binding },\n ...(scan.where ? { where: predicate(scan.where) } : {}),\n ...(scan.orderBy\n ? {\n orderBy: scan.orderBy.map((o) => ({\n expression: operand(o.field),\n direction: o.direction,\n nulls: o.nulls,\n })),\n }\n : {}),\n ...(scan.limit !== undefined || scan.offset !== undefined || scan.after !== undefined\n ? {\n page: {\n limit: scan.limit,\n offset: scan.offset,\n after: scan.after ? { values: [...scan.after.values], key: scan.after.key } : undefined,\n },\n }\n : {}),\n };\n};\n/** Lower a source-bound spec subset at the engine/provider boundary, never at the public demand API. */\nexport const compileObjectSubset = Effect.fnUntraced(function* (\n ir: OntologyIR,\n subset: ObjectSubset,\n context?: Record<string, unknown>\n): Effect.fn.Return<CompiledSubset, BackendError> {\n if (EffectPredicate.isObject(subset) && Object.hasOwn(subset, \"select\"))\n return yield* new BackendError({\n operation: \"subset.compile\",\n kind: \"NotImplemented\",\n retryable: false,\n message: \"Query selections require query-result demand, which is not implemented yet.\",\n });\n const document = yield* Schema.decodeUnknownEffect(QueryDocumentSchema)({\n select: [q.sub(\"objects\", { ...subset, select: [] })],\n }).pipe(\n Effect.mapError(\n (cause) =>\n new BackendError({\n operation: \"subset.compile\",\n kind: \"InvalidRequest\",\n retryable: false,\n message: \"Invalid object subset.\",\n cause,\n })\n )\n );\n const selection = document.select[0]!;\n if (selection.kind !== \"sub\")\n return yield* new BackendError({\n operation: \"subset.compile\",\n kind: \"InvalidRequest\",\n retryable: false,\n message: \"Invalid object subset selection.\",\n });\n const decoded = selection.value.query;\n if (!decoded.from.source.objectType || decoded.from.source.related)\n return yield* new BackendError({\n operation: \"subset.compile\",\n kind: \"InvalidRequest\",\n retryable: false,\n message: \"An object subset requires an explicit object type.\",\n });\n subset = {\n ...decoded,\n from: {\n ...decoded.from,\n source: { ontology: decoded.from.source.ontology, objectType: decoded.from.source.objectType },\n },\n };\n const unsupported = () =>\n new BackendError({\n operation: \"subset.compile\",\n kind: \"NotImplemented\",\n retryable: false,\n message:\n \"This subset requires query-result demand; only exact object predicates and field ordering can be demanded directly.\",\n });\n const issues = yield* Effect.try({\n try: () => q.validateQueryDocument(ir, document),\n catch: (cause) =>\n new BackendError({\n operation: \"subset.compile\",\n kind: \"InvalidConfiguration\",\n retryable: false,\n message: \"Invalid ontology query environment.\",\n cause,\n }),\n });\n if (issues.length || subset.from.source.ontology)\n return yield* new BackendError({\n operation: \"subset.compile\",\n kind: \"InvalidRequest\",\n retryable: false,\n message: \"Invalid object subset.\",\n issues,\n });\n const reader = { ir, loadSubset: () => Effect.fail(unsupported()) };\n const executor = queryExecutor({ reader, context });\n let where;\n if (subset.where) {\n const lowered = yield* executor.lower(subset.where, subset.from, new Map());\n if (!lowered.exact) return yield* unsupported();\n where = lowered.where;\n }\n const orderBy = [];\n for (const order of subset.orderBy ?? []) {\n const e = order.expression;\n if (\n e.kind !== \"getAt\" ||\n e.value.source.kind !== \"localReference\" ||\n e.value.source.value.name !== subset.from.binding\n )\n return yield* unsupported();\n orderBy.push({\n field: { _tag: \"Field\" as const, path: e.value.path },\n direction: order.direction,\n nulls: order.nulls,\n });\n }\n const query = yield* Schema.decodeUnknownEffect(QuerySchema)({\n where,\n ...(orderBy.length ? { orderBy } : {}),\n ...subset.page,\n }).pipe(\n Effect.mapError(\n (cause) =>\n new BackendError({\n operation: \"subset.compile\",\n kind: \"InvalidRequest\",\n retryable: false,\n message: \"Invalid subset window or operands.\",\n cause,\n })\n )\n );\n return {\n objectType: subset.from.source.objectType,\n ...(Object.values(query).some((value) => value !== undefined) ? { query } : {}),\n };\n});\n\nexport { resolveSubsetSnapshot, coversSubset } from \"./fulfillment.js\";\n"],"mappings":";;;;;;;;;;AAiBA,MAAa,gBAAgB,YAAoB,OAAc,CAAC,GAAG,UAAU,UAAwB;CACjG,MAAM,WAAW,MACb,KAAK,OAAO,MAAM,YAAY,UAAU,KAAK,EAAE,SAAS,UAClD,EAAE,MAAM,SAAS,GAAI,EAAY,IAAI,IACrC,EAAE,QAAQ,CAAC;CACrB,MAAM,aAAa,MAA6B;EAC5C,IAAI,EAAE,OAAO,SAAS,EAAE,OAAO,MAAM,OAAO,EAAE,EAAE,GAAG,CAAC,GAAG,EAAE,WAAW,IAAI,SAAS,CAAC;EAClF,IAAI,EAAE,OAAO,OAAO,OAAO,EAAE,IAAI,UAAU,EAAE,SAAS,CAAC;EACvD,IAAI,EAAE,OAAO,MAAM,OAAO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AM,OAAO,OAAO,OAAO,sBAAM,IAAI,IAAI,CAAC;EAC1E,IAAI,CAAC,QAAQ,OAAO,OAAO,OAAO,YAAY;EAC9C,QAAQ,QAAQ;CACpB;CACA,MAAM,UAAU,CAAC;CACjB,KAAK,MAAM,SAAS,OAAO,WAAW,CAAC,GAAG;EACtC,MAAM,IAAI,MAAM;EAChB,IACI,EAAE,SAAS,WACX,EAAE,MAAM,OAAO,SAAS,oBACxB,EAAE,MAAM,OAAO,MAAM,SAAS,OAAO,KAAK,SAE1C,OAAO,OAAO,YAAY;EAC9B,QAAQ,KAAK;GACT,OAAO;IAAE,MAAM;IAAkB,MAAM,EAAE,MAAM;GAAK;GACpD,WAAW,MAAM;GACjB,OAAO,MAAM;EACjB,CAAC;CACL;CACA,MAAM,QAAQ,OAAO,OAAO,oBAAoB,WAAW,CAAC,CAAC;EACzD;EACA,GAAI,QAAQ,SAAS,EAAE,QAAQ,IAAI,CAAC;EACpC,GAAG,OAAO;CACd,CAAC,CAAC,CAAC,KACC,OAAO,UACF,UACG,IAAI,aAAa;EACb,WAAW;EACX,MAAM;EACN,WAAW;EACX,SAAS;EACT;CACJ,CAAC,CACT,CACJ;CACA,OAAO;EACH,YAAY,OAAO,KAAK,OAAO;EAC/B,GAAI,OAAO,OAAO,KAAK,CAAC,CAAC,MAAM,UAAU,UAAU,KAAA,CAAS,IAAI,EAAE,MAAM,IAAI,CAAC;CACjF;AACJ,CAAC"}
|
|
1
|
+
{"version":3,"file":"subset.js","names":["EffectPredicate"],"sources":["../src/subset.ts"],"sourcesContent":["import { QueryDocumentSchema } from \"@party-stack/ontology-spec/effect\";\nimport * as q from \"@party-stack/ontology-spec/query\";\nimport { QuerySchema, fieldExpression, rebindFilter } from \"@party-stack/ontology-storage\";\nimport { Effect, Predicate as EffectPredicate, Schema } from \"effect\";\nimport { BackendError } from \"./errors.js\";\nimport { queryExecutor } from \"./query.js\";\nimport type { OntologyIR, Expression } from \"@party-stack/ontology-spec\";\nimport type { ObjectSubset } from \"@party-stack/ontology-spec/query\";\nimport type { Query } from \"@party-stack/ontology-storage\";\n\nexport interface CompiledSubset {\n readonly objectType: string;\n readonly query?: Query;\n}\nexport { ObjectSubsetSchema, ObjectSelectionSchema } from \"./subsetSchema.js\";\n\n/** Wrap a storage query as a source-bound subset. Storage filters are spec expressions\n * over the storage row binding, so only the binding name changes. */\nexport const objectSubset = (objectType: string, scan: Query = {}, binding = \"row\"): ObjectSubset => ({\n from: { source: { objectType }, binding },\n ...(scan.where ? { where: rebindFilter(scan.where, binding) } : {}),\n ...(scan.orderBy\n ? {\n orderBy: scan.orderBy.map((o) => ({\n expression: rebindFilter(fieldExpression(o.field), binding),\n direction: o.direction,\n nulls: o.nulls,\n })),\n }\n : {}),\n ...(scan.limit !== undefined || scan.offset !== undefined || scan.after !== undefined\n ? {\n page: {\n limit: scan.limit,\n offset: scan.offset,\n after: scan.after ? { values: [...scan.after.values], key: scan.after.key } : undefined,\n },\n }\n : {}),\n});\n/** Lower a source-bound spec subset at the engine/provider boundary, never at the public demand API. */\nexport const compileObjectSubset = Effect.fnUntraced(function* (\n ir: OntologyIR,\n subset: ObjectSubset,\n context?: Record<string, unknown>\n): Effect.fn.Return<CompiledSubset, BackendError> {\n if (EffectPredicate.isObject(subset) && Object.hasOwn(subset, \"select\"))\n return yield* new BackendError({\n operation: \"subset.compile\",\n kind: \"NotImplemented\",\n retryable: false,\n message: \"Query selections require query-result demand, which is not implemented yet.\",\n });\n const document = yield* Schema.decodeUnknownEffect(QueryDocumentSchema)({\n select: [q.sub(\"objects\", { ...subset, select: [] })],\n }).pipe(\n Effect.mapError(\n (cause) =>\n new BackendError({\n operation: \"subset.compile\",\n kind: \"InvalidRequest\",\n retryable: false,\n message: \"Invalid object subset.\",\n cause,\n })\n )\n );\n const selection = document.select[0]!;\n if (selection.kind !== \"sub\")\n return yield* new BackendError({\n operation: \"subset.compile\",\n kind: \"InvalidRequest\",\n retryable: false,\n message: \"Invalid object subset selection.\",\n });\n const decoded = selection.value.query;\n if (!decoded.from.source.objectType || decoded.from.source.related)\n return yield* new BackendError({\n operation: \"subset.compile\",\n kind: \"InvalidRequest\",\n retryable: false,\n message: \"An object subset requires an explicit object type.\",\n });\n subset = {\n ...decoded,\n from: {\n ...decoded.from,\n source: { ontology: decoded.from.source.ontology, objectType: decoded.from.source.objectType },\n },\n };\n const unsupported = () =>\n new BackendError({\n operation: \"subset.compile\",\n kind: \"NotImplemented\",\n retryable: false,\n message:\n \"This subset requires query-result demand; only exact object predicates and field ordering can be demanded directly.\",\n });\n const issues = yield* Effect.try({\n try: () => q.validateQueryDocument(ir, document),\n catch: (cause) =>\n new BackendError({\n operation: \"subset.compile\",\n kind: \"InvalidConfiguration\",\n retryable: false,\n message: \"Invalid ontology query environment.\",\n cause,\n }),\n });\n if (issues.length || subset.from.source.ontology)\n return yield* new BackendError({\n operation: \"subset.compile\",\n kind: \"InvalidRequest\",\n retryable: false,\n message: \"Invalid object subset.\",\n issues,\n });\n const reader = { ir, loadSubset: () => Effect.fail(unsupported()) };\n const executor = queryExecutor({ reader, context });\n let where;\n if (subset.where) {\n const lowered = yield* executor.lower(subset.where, subset.from, new Map());\n if (!lowered.exact) return yield* unsupported();\n where = lowered.where;\n }\n const orderBy = [];\n for (const order of subset.orderBy ?? []) {\n const e = order.expression;\n if (\n e.kind !== \"getAt\" ||\n e.value.source.kind !== \"localReference\" ||\n e.value.source.value.name !== subset.from.binding\n )\n return yield* unsupported();\n orderBy.push({\n field: { _tag: \"Field\" as const, path: e.value.path },\n direction: order.direction,\n nulls: order.nulls,\n });\n }\n const query = yield* Schema.decodeUnknownEffect(QuerySchema)({\n where,\n ...(orderBy.length ? { orderBy } : {}),\n ...subset.page,\n }).pipe(\n Effect.mapError(\n (cause) =>\n new BackendError({\n operation: \"subset.compile\",\n kind: \"InvalidRequest\",\n retryable: false,\n message: \"Invalid subset window or operands.\",\n cause,\n })\n )\n );\n return {\n objectType: subset.from.source.objectType,\n ...(Object.values(query).some((value) => value !==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ACI,EAAE,SAAS,WACX,EAAE,MAAM,OAAO,SAAS,oBACxB,EAAE,MAAM,OAAO,MAAM,SAAS,OAAO,KAAK,SAE1C,OAAO,OAAO,YAAY;EAC9B,QAAQ,KAAK;GACT,OAAO;IAAE,MAAM;IAAkB,MAAM,EAAE,MAAM;GAAK;GACpD,WAAW,MAAM;GACjB,OAAO,MAAM;EACjB,CAAC;CACL;CACA,MAAM,QAAQ,OAAO,OAAO,oBAAoB,WAAW,CAAC,CAAC;EACzD;EACA,GAAI,QAAQ,SAAS,EAAE,QAAQ,IAAI,CAAC;EACpC,GAAG,OAAO;CACd,CAAC,CAAC,CAAC,KACC,OAAO,UACF,UACG,IAAI,aAAa;EACb,WAAW;EACX,MAAM;EACN,WAAW;EACX,SAAS;EACT;CACJ,CAAC,CACT,CACJ;CACA,OAAO;EACH,YAAY,OAAO,KAAK,OAAO;EAC/B,GAAI,OAAO,OAAO,KAAK,CAAC,CAAC,MAAM,UAAU,UAAU,KAAA,CAAS,IAAI,EAAE,MAAM,IAAI,CAAC;CACjF;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@party-stack/ontology-backend",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"files": [
|
|
5
5
|
"lib"
|
|
6
6
|
],
|
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
"dependencies": {
|
|
59
59
|
"temporal-polyfill": "^0.3.0",
|
|
60
60
|
"@party-stack/ontology-spec": "0.3.1",
|
|
61
|
-
"@party-stack/ontology-storage": "0.
|
|
61
|
+
"@party-stack/ontology-storage": "0.3.0"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
64
|
"@effect/vitest": "^4.0.5",
|