fatedb 0.0.0-stage → 0.10.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/LICENSE +21 -0
- package/README.md +264 -2
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +3 -0
- package/dist/cli.d.ts +15 -0
- package/dist/cli.js +256 -0
- package/dist/client.d.ts +152 -0
- package/dist/client.js +416 -0
- package/dist/cloudflare.d.ts +197 -0
- package/dist/cloudflare.js +159 -0
- package/dist/datalog/ast.d.ts +35 -0
- package/dist/datalog/ast.js +40 -0
- package/dist/datalog/evaluator.d.ts +14 -0
- package/dist/datalog/evaluator.js +395 -0
- package/dist/datalog/fact-source.d.ts +30 -0
- package/dist/datalog/fact-source.js +1 -0
- package/dist/datalog/fingerprint.d.ts +12 -0
- package/dist/datalog/fingerprint.js +50 -0
- package/dist/datalog/parser.d.ts +3 -0
- package/dist/datalog/parser.js +302 -0
- package/dist/datalog/value.d.ts +34 -0
- package/dist/datalog/value.js +104 -0
- package/dist/domain/date-validation.d.ts +2 -0
- package/dist/domain/date-validation.js +32 -0
- package/dist/domain/fact-value.d.ts +91 -0
- package/dist/domain/fact-value.js +167 -0
- package/dist/domain/fact.d.ts +14 -0
- package/dist/domain/fact.js +47 -0
- package/dist/domain/failure.d.ts +44 -0
- package/dist/domain/failure.js +30 -0
- package/dist/domain/graph-id.d.ts +7 -0
- package/dist/domain/graph-id.js +10 -0
- package/dist/domain/histogram.d.ts +29 -0
- package/dist/domain/histogram.js +48 -0
- package/dist/domain/identifiers.d.ts +26 -0
- package/dist/domain/identifiers.js +75 -0
- package/dist/domain/numbers.d.ts +12 -0
- package/dist/domain/numbers.js +77 -0
- package/dist/domain/projection.d.ts +252 -0
- package/dist/domain/projection.js +51 -0
- package/dist/domain/query-metrics.d.ts +31 -0
- package/dist/domain/query-metrics.js +35 -0
- package/dist/domain/read-token.d.ts +37 -0
- package/dist/domain/read-token.js +93 -0
- package/dist/domain/result.d.ts +22 -0
- package/dist/domain/result.js +16 -0
- package/dist/domain/schema-observation.d.ts +20 -0
- package/dist/domain/schema-observation.js +61 -0
- package/dist/domain/string-order.d.ts +2 -0
- package/dist/domain/string-order.js +15 -0
- package/dist/domain/vocabulary.d.ts +101 -0
- package/dist/domain/vocabulary.js +276 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +14 -0
- package/dist/projection-config.d.ts +3 -0
- package/dist/projection-config.js +212 -0
- package/dist/server/access.d.ts +32 -0
- package/dist/server/access.js +117 -0
- package/dist/server/admin.d.ts +233 -0
- package/dist/server/admin.js +483 -0
- package/dist/server/authorization.d.ts +35 -0
- package/dist/server/authorization.js +76 -0
- package/dist/server/console-assets.d.ts +6 -0
- package/dist/server/console-assets.js +2 -0
- package/dist/server/console.d.ts +8 -0
- package/dist/server/console.js +301 -0
- package/dist/server/index.d.ts +122 -0
- package/dist/server/index.js +487 -0
- package/dist/server/mcp.d.ts +31 -0
- package/dist/server/mcp.js +169 -0
- package/dist/server/telemetry-do.d.ts +31 -0
- package/dist/server/telemetry-do.js +342 -0
- package/dist/server/telemetry.d.ts +27 -0
- package/dist/server/telemetry.js +191 -0
- package/dist/storage/constants.d.ts +20 -0
- package/dist/storage/constants.js +21 -0
- package/dist/storage/environment.d.ts +438 -0
- package/dist/storage/environment.js +145 -0
- package/dist/storage/facts.d.ts +41 -0
- package/dist/storage/facts.js +73 -0
- package/dist/storage/http.d.ts +55 -0
- package/dist/storage/http.js +169 -0
- package/dist/storage/keys.d.ts +50 -0
- package/dist/storage/keys.js +84 -0
- package/dist/storage/layout.d.ts +67 -0
- package/dist/storage/layout.js +134 -0
- package/dist/storage/log.d.ts +41 -0
- package/dist/storage/log.js +137 -0
- package/dist/storage/manifest.d.ts +26 -0
- package/dist/storage/manifest.js +206 -0
- package/dist/storage/operations.d.ts +303 -0
- package/dist/storage/operations.js +719 -0
- package/dist/storage/partition.d.ts +125 -0
- package/dist/storage/partition.js +816 -0
- package/dist/storage/projection.d.ts +72 -0
- package/dist/storage/projection.js +1349 -0
- package/dist/storage/root.d.ts +120 -0
- package/dist/storage/root.js +596 -0
- package/dist/storage/segment.d.ts +145 -0
- package/dist/storage/segment.js +451 -0
- package/dist/storage/shard.d.ts +82 -0
- package/dist/storage/shard.js +710 -0
- package/dist/storage/source.d.ts +60 -0
- package/dist/storage/source.js +199 -0
- package/dist/telemetry/model.d.ts +123 -0
- package/dist/telemetry/model.js +292 -0
- package/dist/version.d.ts +4 -0
- package/dist/version.js +6 -0
- package/package.json +82 -4
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { FactValue } from "./fact-value.js";
|
|
2
|
+
import type { ObservedField, ObservedValueTag } from "./schema-observation.js";
|
|
3
|
+
/** The schema:* vocabulary: the classes of schema terms, and the fields that describe them. */
|
|
4
|
+
export declare const SCHEMA: {
|
|
5
|
+
/** owl:Thing */
|
|
6
|
+
readonly entity: "schema:kind:entity";
|
|
7
|
+
/** owl:Class */
|
|
8
|
+
readonly class: "schema:kind:class";
|
|
9
|
+
/** rdf:Property */
|
|
10
|
+
readonly field: "schema:kind:field";
|
|
11
|
+
/** rdfs:Datatype */
|
|
12
|
+
readonly value: "schema:kind:value";
|
|
13
|
+
/** rdf:type */
|
|
14
|
+
readonly type: "schema:entity:type";
|
|
15
|
+
/** rdfs:label */
|
|
16
|
+
readonly label: "schema:entity:label";
|
|
17
|
+
/** rdfs:comment */
|
|
18
|
+
readonly doc: "schema:entity:doc";
|
|
19
|
+
/** owl:deprecated */
|
|
20
|
+
readonly deprecated: "schema:entity:deprecated";
|
|
21
|
+
/** rdfs:subClassOf */
|
|
22
|
+
readonly subclassOf: "schema:class:subclass_of";
|
|
23
|
+
/** rdfs:domain */
|
|
24
|
+
readonly domain: "schema:field:domain";
|
|
25
|
+
/** rdfs:range */
|
|
26
|
+
readonly range: "schema:field:range";
|
|
27
|
+
/** rdfs:subPropertyOf */
|
|
28
|
+
readonly subfieldOf: "schema:field:subfield_of";
|
|
29
|
+
/** owl:inverseOf */
|
|
30
|
+
readonly inverseOf: "schema:field:inverse_of";
|
|
31
|
+
/** owl:FunctionalProperty */
|
|
32
|
+
readonly functional: "schema:field:functional";
|
|
33
|
+
/** owl:InverseFunctionalProperty */
|
|
34
|
+
readonly inverseFunctional: "schema:field:inverse_functional";
|
|
35
|
+
/** owl:TransitiveProperty */
|
|
36
|
+
readonly transitive: "schema:field:transitive";
|
|
37
|
+
/** owl:SymmetricProperty */
|
|
38
|
+
readonly symmetric: "schema:field:symmetric";
|
|
39
|
+
};
|
|
40
|
+
export declare const VALUE_TAGS: readonly ObservedValueTag[];
|
|
41
|
+
/** The value type of one kind of fact value, such as `schema:value:date`. */
|
|
42
|
+
export declare const valueType: (tag: ObservedValueTag) => string;
|
|
43
|
+
export type TermKind = "class" | "field" | "value";
|
|
44
|
+
/** A class, field or value type, as the graph's schema and its facts describe it. */
|
|
45
|
+
export type SchemaTerm = Readonly<{
|
|
46
|
+
id: string;
|
|
47
|
+
kind: TermKind;
|
|
48
|
+
/** One of FateDB's own terms: its vocabulary and its value types. */
|
|
49
|
+
builtIn: boolean;
|
|
50
|
+
/** FateDB defines it, or the graph states something about it as a schema term. */
|
|
51
|
+
declared: boolean;
|
|
52
|
+
label: string | null;
|
|
53
|
+
doc: string | null;
|
|
54
|
+
deprecated: boolean;
|
|
55
|
+
/** The RDF Schema or OWL term a built-in term stands for. */
|
|
56
|
+
rdf: string | null;
|
|
57
|
+
/** For a value type: one value of it, as a fact holds it. */
|
|
58
|
+
example: string | null;
|
|
59
|
+
subclassOf: readonly string[];
|
|
60
|
+
domain: readonly string[];
|
|
61
|
+
range: readonly string[];
|
|
62
|
+
subfieldOf: readonly string[];
|
|
63
|
+
inverseOf: readonly string[];
|
|
64
|
+
functional: boolean;
|
|
65
|
+
inverseFunctional: boolean;
|
|
66
|
+
transitive: boolean;
|
|
67
|
+
symmetric: boolean;
|
|
68
|
+
/** For a field: the tags of its active facts' values, ascending; none when no fact uses it. */
|
|
69
|
+
observed: readonly ObservedValueTag[];
|
|
70
|
+
}>;
|
|
71
|
+
/**
|
|
72
|
+
* One schema fact: about `entity`, in `field`, with its value. Ids, such as a domain's class, are
|
|
73
|
+
* references, or text holding one, as queries match them; flags are the boolean true; labels and
|
|
74
|
+
* docs are text. A statement whose value is of another type says nothing.
|
|
75
|
+
*/
|
|
76
|
+
export type SchemaStatement = Readonly<{
|
|
77
|
+
entity: string;
|
|
78
|
+
field: string;
|
|
79
|
+
value: FactValue;
|
|
80
|
+
}>;
|
|
81
|
+
/** The schema fields read on terms the graph types as classes, fields or value types. */
|
|
82
|
+
export declare const ANNOTATIONS: readonly ["schema:entity:label", "schema:entity:doc", "schema:entity:deprecated"];
|
|
83
|
+
/** The schema fields read wherever they are, since only schema terms hold them. */
|
|
84
|
+
export declare const DEFINITIONS: readonly ["schema:class:subclass_of", "schema:field:domain", "schema:field:range", "schema:field:subfield_of", "schema:field:inverse_of", "schema:field:functional", "schema:field:inverse_functional", "schema:field:transitive", "schema:field:symmetric"];
|
|
85
|
+
export declare const KINDS: Readonly<Record<string, TermKind>>;
|
|
86
|
+
/** FateDB's own terms, which no graph's facts change. */
|
|
87
|
+
export declare function isBuiltIn(id: string): boolean;
|
|
88
|
+
/**
|
|
89
|
+
* The graph's schema terms: what its schema facts state, what they imply, and the fields its facts
|
|
90
|
+
* use. A term is a class, field or value type by its type fact, else by how the schema uses it, as
|
|
91
|
+
* RDF Schema infers it: whatever has a domain is a field, and whatever is a domain is a class.
|
|
92
|
+
*/
|
|
93
|
+
export declare function schemaTerms(statements: readonly SchemaStatement[], observed: readonly ObservedField[]): SchemaTerm[];
|
|
94
|
+
/** The term and every term below it, by one relation: a class's subclasses, or a field's subfields. */
|
|
95
|
+
export declare function below(terms: readonly SchemaTerm[], id: string, up: (term: SchemaTerm) => readonly string[]): string[];
|
|
96
|
+
/** The term and every term above it: a class's superclasses, or a field's superfields. */
|
|
97
|
+
export declare function above(terms: readonly SchemaTerm[], id: string, up: (term: SchemaTerm) => readonly string[]): string[];
|
|
98
|
+
/** The fields that name an entity's classes: Type, and the graph's fields below it. */
|
|
99
|
+
export declare const typeFields: (terms: readonly SchemaTerm[]) => string[];
|
|
100
|
+
/** The fields that name an entity for people: Label, and the graph's fields below it. */
|
|
101
|
+
export declare const labelFields: (terms: readonly SchemaTerm[]) => string[];
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
// A graph's schema, declared with facts in the style of RDF Schema and OWL: its classes, fields and
|
|
2
|
+
// value types are entities, and facts in the schema:* fields describe them. The schema namespace
|
|
3
|
+
// holds the whole vocabulary for describing schemas; it is FateDB's own, and each term's comment
|
|
4
|
+
// names the RDFS or OWL term it means. A schema describes the graph and never refuses a write.
|
|
5
|
+
/** The schema:* vocabulary: the classes of schema terms, and the fields that describe them. */
|
|
6
|
+
export const SCHEMA = {
|
|
7
|
+
/** owl:Thing */
|
|
8
|
+
entity: "schema:kind:entity",
|
|
9
|
+
/** owl:Class */
|
|
10
|
+
class: "schema:kind:class",
|
|
11
|
+
/** rdf:Property */
|
|
12
|
+
field: "schema:kind:field",
|
|
13
|
+
/** rdfs:Datatype */
|
|
14
|
+
value: "schema:kind:value",
|
|
15
|
+
/** rdf:type */
|
|
16
|
+
type: "schema:entity:type",
|
|
17
|
+
/** rdfs:label */
|
|
18
|
+
label: "schema:entity:label",
|
|
19
|
+
/** rdfs:comment */
|
|
20
|
+
doc: "schema:entity:doc",
|
|
21
|
+
/** owl:deprecated */
|
|
22
|
+
deprecated: "schema:entity:deprecated",
|
|
23
|
+
/** rdfs:subClassOf */
|
|
24
|
+
subclassOf: "schema:class:subclass_of",
|
|
25
|
+
/** rdfs:domain */
|
|
26
|
+
domain: "schema:field:domain",
|
|
27
|
+
/** rdfs:range */
|
|
28
|
+
range: "schema:field:range",
|
|
29
|
+
/** rdfs:subPropertyOf */
|
|
30
|
+
subfieldOf: "schema:field:subfield_of",
|
|
31
|
+
/** owl:inverseOf */
|
|
32
|
+
inverseOf: "schema:field:inverse_of",
|
|
33
|
+
/** owl:FunctionalProperty */
|
|
34
|
+
functional: "schema:field:functional",
|
|
35
|
+
/** owl:InverseFunctionalProperty */
|
|
36
|
+
inverseFunctional: "schema:field:inverse_functional",
|
|
37
|
+
/** owl:TransitiveProperty */
|
|
38
|
+
transitive: "schema:field:transitive",
|
|
39
|
+
/** owl:SymmetricProperty */
|
|
40
|
+
symmetric: "schema:field:symmetric",
|
|
41
|
+
};
|
|
42
|
+
export const VALUE_TAGS = ["text", "number", "decimal", "boolean", "date", "date_time", "bytes", "reference", "list", "map", "null"];
|
|
43
|
+
/** The value type of one kind of fact value, such as `schema:value:date`. */
|
|
44
|
+
export const valueType = (tag) => `schema:value:${tag}`;
|
|
45
|
+
/** The schema fields read on terms the graph types as classes, fields or value types. */
|
|
46
|
+
export const ANNOTATIONS = [SCHEMA.label, SCHEMA.doc, SCHEMA.deprecated];
|
|
47
|
+
/** The schema fields read wherever they are, since only schema terms hold them. */
|
|
48
|
+
export const DEFINITIONS = [
|
|
49
|
+
SCHEMA.subclassOf, SCHEMA.domain, SCHEMA.range, SCHEMA.subfieldOf, SCHEMA.inverseOf,
|
|
50
|
+
SCHEMA.functional, SCHEMA.inverseFunctional, SCHEMA.transitive, SCHEMA.symmetric,
|
|
51
|
+
];
|
|
52
|
+
export const KINDS = { [SCHEMA.class]: "class", [SCHEMA.field]: "field", [SCHEMA.value]: "value" };
|
|
53
|
+
/** The definitions whose values are ids. */
|
|
54
|
+
const DEFINES = new Set([SCHEMA.subclassOf, SCHEMA.domain, SCHEMA.range, SCHEMA.subfieldOf, SCHEMA.inverseOf]);
|
|
55
|
+
function blank(id) {
|
|
56
|
+
return {
|
|
57
|
+
id, kind: "field", builtIn: false, declared: false, label: null, doc: null, deprecated: false, rdf: null, example: null,
|
|
58
|
+
subclassOf: [], domain: [], range: [], subfieldOf: [], inverseOf: [],
|
|
59
|
+
functional: false, inverseFunctional: false, transitive: false, symmetric: false, observed: [],
|
|
60
|
+
stated: null, inferred: null,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
const BUILT_IN = [
|
|
64
|
+
{ id: SCHEMA.entity, kind: "class", label: "Entity", rdf: "owl:Thing", doc: "Anything a fact can be about. Every class is a kind of entity." },
|
|
65
|
+
{ id: SCHEMA.class, kind: "class", label: "Class", rdf: "owl:Class", doc: "A kind of entity, such as Character or Crew. An entity names its classes in a type field." },
|
|
66
|
+
{ id: SCHEMA.field, kind: "class", label: "Field", rdf: "rdf:Property", doc: "What a fact says about its entity. A field whose range is a class points at entities; one whose range is a value type holds values." },
|
|
67
|
+
{ id: SCHEMA.value, kind: "class", label: "Value type", rdf: "rdfs:Datatype", doc: "A kind of value a fact can hold: text, a number, a date and the rest.", subclassOf: [SCHEMA.class] },
|
|
68
|
+
{ id: SCHEMA.type, kind: "field", label: "Type", rdf: "rdf:type", doc: "A class the entity is an instance of. A graph's own type field, such as onepiece:kind, says so by being a subfield of this one.", domain: [SCHEMA.entity], range: [SCHEMA.class] },
|
|
69
|
+
{ id: SCHEMA.label, kind: "field", label: "Label", rdf: "rdfs:label", doc: "The name people know the entity by. A graph's own name field can be a subfield of this one.", domain: [SCHEMA.entity], range: [valueType("text")] },
|
|
70
|
+
{ id: SCHEMA.doc, kind: "field", label: "Doc", rdf: "rdfs:comment", doc: "What the entity is, for the people and agents who read the graph.", domain: [SCHEMA.entity], range: [valueType("text")] },
|
|
71
|
+
{ id: SCHEMA.deprecated, kind: "field", label: "Deprecated", rdf: "owl:deprecated", doc: "True when the term should no longer be used.", domain: [SCHEMA.entity], range: [valueType("boolean")], functional: true },
|
|
72
|
+
{ id: SCHEMA.subclassOf, kind: "field", label: "Subclass of", rdf: "rdfs:subClassOf", doc: "A class this one is a kind of: each of its instances is an instance of that class too.", domain: [SCHEMA.class], range: [SCHEMA.class], transitive: true },
|
|
73
|
+
{ id: SCHEMA.domain, kind: "field", label: "Domain", rdf: "rdfs:domain", doc: "The class of the entities the field describes.", domain: [SCHEMA.field], range: [SCHEMA.class] },
|
|
74
|
+
{ id: SCHEMA.range, kind: "field", label: "Range", rdf: "rdfs:range", doc: "What the field's values are: instances of a class, or values of a value type.", domain: [SCHEMA.field], range: [SCHEMA.class] },
|
|
75
|
+
{ id: SCHEMA.subfieldOf, kind: "field", label: "Subfield of", rdf: "rdfs:subPropertyOf", doc: "A broader field: a fact in this field says what a fact in that field would say.", domain: [SCHEMA.field], range: [SCHEMA.field], transitive: true },
|
|
76
|
+
{ id: SCHEMA.inverseOf, kind: "field", label: "Inverse of", rdf: "owl:inverseOf", doc: "A field that says the same thing from the other end: A is the captain of B when B is captained by A.", domain: [SCHEMA.field], range: [SCHEMA.field], symmetric: true },
|
|
77
|
+
{ id: SCHEMA.functional, kind: "field", label: "Functional", rdf: "owl:FunctionalProperty", doc: "True when an entity has at most one value in the field.", domain: [SCHEMA.field], range: [valueType("boolean")], functional: true },
|
|
78
|
+
{ id: SCHEMA.inverseFunctional, kind: "field", label: "Inverse functional", rdf: "owl:InverseFunctionalProperty", doc: "True when a value belongs to at most one entity, so the value identifies it.", domain: [SCHEMA.field], range: [valueType("boolean")], functional: true },
|
|
79
|
+
{ id: SCHEMA.transitive, kind: "field", label: "Transitive", rdf: "owl:TransitiveProperty", doc: "True when A to B and B to C mean A to C, as for a part of a part.", domain: [SCHEMA.field], range: [valueType("boolean")], functional: true },
|
|
80
|
+
{ id: SCHEMA.symmetric, kind: "field", label: "Symmetric", rdf: "owl:SymmetricProperty", doc: "True when A to B means B to A, as for an ally.", domain: [SCHEMA.field], range: [valueType("boolean")], functional: true },
|
|
81
|
+
{ id: valueType("text"), kind: "value", label: "Text", rdf: "xsd:string", doc: "Unicode text. Queries compare it character by character.", example: `{ "type": "text", "value": "Monkey D. Luffy" }` },
|
|
82
|
+
{ id: valueType("number"), kind: "value", label: "Number", rdf: "xsd:integer", doc: "An integer of any size, written as its digits so none is lost. Queries compare it as a number; code gets a bigint.", example: `{ "type": "number", "value": "3000000000" }` },
|
|
83
|
+
{ id: valueType("decimal"), kind: "value", label: "Decimal", rdf: "xsd:decimal", doc: "A decimal of any precision, written out: 1.50 is kept as 1.5. Queries compare it as a number, with integers too; code gets its digits as a string.", example: `{ "type": "decimal", "value": "1.5" }` },
|
|
84
|
+
{ id: valueType("boolean"), kind: "value", label: "Boolean", rdf: "xsd:boolean", doc: "True or false. Queries match it as the text true or false; code gets a boolean.", example: `{ "type": "boolean", "value": true }` },
|
|
85
|
+
{ id: valueType("date"), kind: "value", label: "Date", rdf: "xsd:date", doc: "A calendar date, YYYY-MM-DD. Queries see its text, which sorts in date order; code gets a Date at its midnight in UTC.", example: `{ "type": "date", "value": "1997-07-22" }` },
|
|
86
|
+
{ id: valueType("date_time"), kind: "value", label: "Date and time", rdf: "xsd:dateTime", doc: "An instant, kept in UTC to the millisecond, whose text sorts in time order. Write it as a Date or in RFC 3339 with any offset; code gets a Date.", example: `{ "type": "date_time", "value": "1997-07-22T00:00:00.000Z" }` },
|
|
87
|
+
{ id: valueType("bytes"), kind: "value", label: "Bytes", rdf: "xsd:base64Binary", doc: "Raw bytes, each from 0 to 255. Queries see them as JSON.", example: `{ "type": "bytes", "value": [80, 79, 78, 69] }` },
|
|
88
|
+
{ id: valueType("reference"), kind: "value", label: "Reference", rdf: "rdfs:Resource", doc: "The id of an entity. A field whose range is a class holds references to its instances; this value type allows any entity.", example: `{ "type": "reference", "value": "onepiece:kind:character" }` },
|
|
89
|
+
{ id: valueType("list"), kind: "value", label: "List", rdf: "rdf:List", doc: "Values in order. A query matches each of them.", example: `{ "type": "list", "value": [{ "type": "text", "value": "Straw Hat" }] }` },
|
|
90
|
+
{ id: valueType("map"), kind: "value", label: "Map", rdf: "rdf:JSON", doc: "Values by name. Queries see it as JSON.", example: `{ "type": "map", "value": { "bounty": { "type": "number", "value": "3000000000" } } }` },
|
|
91
|
+
{ id: valueType("null"), kind: "value", label: "Null", rdf: null, doc: "No value: a fact that something is unknown or empty.", example: `{ "type": "null" }` },
|
|
92
|
+
];
|
|
93
|
+
const BUILT_IN_IDS = new Set(BUILT_IN.map((term) => term.id));
|
|
94
|
+
const VALUE_TYPES = new Set(VALUE_TAGS.map(valueType));
|
|
95
|
+
/** FateDB's own terms, which no graph's facts change. */
|
|
96
|
+
export function isBuiltIn(id) {
|
|
97
|
+
return BUILT_IN_IDS.has(id);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* The graph's schema terms: what its schema facts state, what they imply, and the fields its facts
|
|
101
|
+
* use. A term is a class, field or value type by its type fact, else by how the schema uses it, as
|
|
102
|
+
* RDF Schema infers it: whatever has a domain is a field, and whatever is a domain is a class.
|
|
103
|
+
*/
|
|
104
|
+
export function schemaTerms(statements, observed) {
|
|
105
|
+
const terms = new Map();
|
|
106
|
+
for (const b of BUILT_IN) {
|
|
107
|
+
const base = blank(b.id);
|
|
108
|
+
terms.set(b.id, {
|
|
109
|
+
...base,
|
|
110
|
+
kind: b.kind,
|
|
111
|
+
label: b.label,
|
|
112
|
+
doc: b.doc,
|
|
113
|
+
deprecated: b.deprecated ?? base.deprecated,
|
|
114
|
+
rdf: b.rdf ?? base.rdf,
|
|
115
|
+
example: b.example ?? base.example,
|
|
116
|
+
subclassOf: b.subclassOf ? [...b.subclassOf] : base.subclassOf,
|
|
117
|
+
domain: b.domain ? [...b.domain] : base.domain,
|
|
118
|
+
range: b.range ? [...b.range] : base.range,
|
|
119
|
+
subfieldOf: b.subfieldOf ? [...b.subfieldOf] : base.subfieldOf,
|
|
120
|
+
inverseOf: b.inverseOf ? [...b.inverseOf] : base.inverseOf,
|
|
121
|
+
functional: b.functional ?? base.functional,
|
|
122
|
+
inverseFunctional: b.inverseFunctional ?? base.inverseFunctional,
|
|
123
|
+
transitive: b.transitive ?? base.transitive,
|
|
124
|
+
symmetric: b.symmetric ?? base.symmetric,
|
|
125
|
+
observed: b.observed ? [...b.observed] : base.observed,
|
|
126
|
+
builtIn: true,
|
|
127
|
+
declared: true,
|
|
128
|
+
stated: b.kind,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
const at = (id) => {
|
|
132
|
+
let term = terms.get(id);
|
|
133
|
+
if (term === undefined)
|
|
134
|
+
terms.set(id, (term = blank(id)));
|
|
135
|
+
return term;
|
|
136
|
+
};
|
|
137
|
+
const infer = (id, kind) => {
|
|
138
|
+
const term = at(id);
|
|
139
|
+
term.inferred ??= kind;
|
|
140
|
+
};
|
|
141
|
+
const add = (list, id) => {
|
|
142
|
+
if (!list.includes(id))
|
|
143
|
+
list.push(id);
|
|
144
|
+
};
|
|
145
|
+
for (const s of [...statements].sort(byStatement)) {
|
|
146
|
+
if (isBuiltIn(s.entity))
|
|
147
|
+
continue;
|
|
148
|
+
const id = s.value.type === "reference" || s.value.type === "text" ? s.value.value : null;
|
|
149
|
+
const text = s.value.type === "text" ? s.value.value : null;
|
|
150
|
+
const yes = s.value.type === "boolean" && s.value.value;
|
|
151
|
+
const usable = s.field === SCHEMA.label || s.field === SCHEMA.doc ? text !== null
|
|
152
|
+
: s.field === SCHEMA.type || DEFINES.has(s.field) ? id !== null
|
|
153
|
+
: s.value.type === "boolean";
|
|
154
|
+
if (!usable)
|
|
155
|
+
continue;
|
|
156
|
+
const term = at(s.entity);
|
|
157
|
+
term.declared = true;
|
|
158
|
+
switch (s.field) {
|
|
159
|
+
case SCHEMA.type:
|
|
160
|
+
if (id !== null)
|
|
161
|
+
term.stated ??= KINDS[id] ?? null;
|
|
162
|
+
break;
|
|
163
|
+
case SCHEMA.label:
|
|
164
|
+
term.label ??= text;
|
|
165
|
+
break;
|
|
166
|
+
case SCHEMA.doc:
|
|
167
|
+
term.doc ??= text;
|
|
168
|
+
break;
|
|
169
|
+
case SCHEMA.deprecated:
|
|
170
|
+
term.deprecated ||= yes;
|
|
171
|
+
break;
|
|
172
|
+
case SCHEMA.subclassOf:
|
|
173
|
+
if (id !== null) {
|
|
174
|
+
term.inferred ??= "class";
|
|
175
|
+
add(term.subclassOf, id);
|
|
176
|
+
infer(id, "class");
|
|
177
|
+
}
|
|
178
|
+
break;
|
|
179
|
+
case SCHEMA.domain:
|
|
180
|
+
if (id !== null) {
|
|
181
|
+
term.inferred ??= "field";
|
|
182
|
+
add(term.domain, id);
|
|
183
|
+
infer(id, "class");
|
|
184
|
+
}
|
|
185
|
+
break;
|
|
186
|
+
case SCHEMA.range:
|
|
187
|
+
if (id !== null) {
|
|
188
|
+
term.inferred ??= "field";
|
|
189
|
+
add(term.range, id);
|
|
190
|
+
infer(id, VALUE_TYPES.has(id) ? "value" : "class");
|
|
191
|
+
}
|
|
192
|
+
break;
|
|
193
|
+
case SCHEMA.subfieldOf:
|
|
194
|
+
if (id !== null) {
|
|
195
|
+
term.inferred ??= "field";
|
|
196
|
+
add(term.subfieldOf, id);
|
|
197
|
+
infer(id, "field");
|
|
198
|
+
}
|
|
199
|
+
break;
|
|
200
|
+
case SCHEMA.inverseOf:
|
|
201
|
+
if (id !== null) {
|
|
202
|
+
term.inferred ??= "field";
|
|
203
|
+
add(term.inverseOf, id);
|
|
204
|
+
// owl:inverseOf is symmetric: each field is the other's inverse.
|
|
205
|
+
if (!isBuiltIn(id))
|
|
206
|
+
add(at(id).inverseOf, s.entity);
|
|
207
|
+
infer(id, "field");
|
|
208
|
+
}
|
|
209
|
+
break;
|
|
210
|
+
case SCHEMA.functional:
|
|
211
|
+
term.inferred ??= "field";
|
|
212
|
+
term.functional ||= yes;
|
|
213
|
+
break;
|
|
214
|
+
case SCHEMA.inverseFunctional:
|
|
215
|
+
term.inferred ??= "field";
|
|
216
|
+
term.inverseFunctional ||= yes;
|
|
217
|
+
break;
|
|
218
|
+
case SCHEMA.transitive:
|
|
219
|
+
term.inferred ??= "field";
|
|
220
|
+
term.transitive ||= yes;
|
|
221
|
+
break;
|
|
222
|
+
case SCHEMA.symmetric:
|
|
223
|
+
term.inferred ??= "field";
|
|
224
|
+
term.symmetric ||= yes;
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
for (const field of observed) {
|
|
229
|
+
const term = at(field.field);
|
|
230
|
+
term.observed = [...field.valueTags];
|
|
231
|
+
term.inferred ??= "field";
|
|
232
|
+
}
|
|
233
|
+
return [...terms.values()]
|
|
234
|
+
.map(({ stated, inferred, ...term }) => ({ ...term, kind: stated ?? inferred ?? "field" }))
|
|
235
|
+
.sort(byTerm);
|
|
236
|
+
}
|
|
237
|
+
const KIND_ORDER = { class: 0, field: 1, value: 2 };
|
|
238
|
+
function byTerm(a, b) {
|
|
239
|
+
return KIND_ORDER[a.kind] - KIND_ORDER[b.kind] || Number(a.builtIn) - Number(b.builtIn) || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
240
|
+
}
|
|
241
|
+
function byStatement(a, b) {
|
|
242
|
+
return cmp(a.entity, b.entity) || cmp(a.field, b.field) || cmp(JSON.stringify(a.value), JSON.stringify(b.value));
|
|
243
|
+
}
|
|
244
|
+
const cmp = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
245
|
+
/** The term and every term below it, by one relation: a class's subclasses, or a field's subfields. */
|
|
246
|
+
export function below(terms, id, up) {
|
|
247
|
+
const found = [id];
|
|
248
|
+
for (let i = 0; i < found.length; i++) {
|
|
249
|
+
const current = found[i];
|
|
250
|
+
if (current === undefined)
|
|
251
|
+
break;
|
|
252
|
+
for (const term of terms)
|
|
253
|
+
if (up(term).includes(current) && !found.includes(term.id))
|
|
254
|
+
found.push(term.id);
|
|
255
|
+
}
|
|
256
|
+
return found;
|
|
257
|
+
}
|
|
258
|
+
/** The term and every term above it: a class's superclasses, or a field's superfields. */
|
|
259
|
+
export function above(terms, id, up) {
|
|
260
|
+
const byId = new Map(terms.map((term) => [term.id, term]));
|
|
261
|
+
const found = [id];
|
|
262
|
+
for (let i = 0; i < found.length; i++) {
|
|
263
|
+
const current = found[i];
|
|
264
|
+
if (current === undefined)
|
|
265
|
+
break;
|
|
266
|
+
const term = byId.get(current);
|
|
267
|
+
for (const parent of term === undefined ? [] : up(term))
|
|
268
|
+
if (!found.includes(parent))
|
|
269
|
+
found.push(parent);
|
|
270
|
+
}
|
|
271
|
+
return found;
|
|
272
|
+
}
|
|
273
|
+
/** The fields that name an entity's classes: Type, and the graph's fields below it. */
|
|
274
|
+
export const typeFields = (terms) => below(terms, SCHEMA.type, (term) => term.subfieldOf);
|
|
275
|
+
/** The fields that name an entity for people: Label, and the graph's fields below it. */
|
|
276
|
+
export const labelFields = (terms) => below(terms, SCHEMA.label, (term) => term.subfieldOf);
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { parseFactDraft, parseFactDrafts, type FactDraft, type NonEmptyReadonlyArray, } from "./domain/fact.js";
|
|
2
|
+
export { parseGraphId, type GraphId } from "./domain/graph-id.js";
|
|
3
|
+
export { parseFactValue, toValue, type FactValue, type FactValueInput, type Value, type ValueList, type ValueMap, } from "./domain/fact-value.js";
|
|
4
|
+
export { BASE62_ID_LENGTH, MAX_CLIENT_REQUEST_TOKEN_BYTES, MAX_TRANSACTION_ID_BYTES, MAX_URI_LENGTH, encodeBase62Id, newTransactionId, parseClientRequestToken, parseTransactionId, parseUri, type ClientRequestToken, type TransactionId, type Uri, } from "./domain/identifiers.js";
|
|
5
|
+
export { parseQueryMetrics, type QueryMetrics, type ReadCosts, } from "./domain/query-metrics.js";
|
|
6
|
+
export { MAX_READ_TOKEN_LENGTH, MAX_STALENESS_MS, encodeReadToken, mergeReadTokens, newReadToken, newSession, parseConsistency, parseMaxStalenessMs, parseReadToken, type Consistency, type ReadToken, type ReadTokenState, } from "./domain/read-token.js";
|
|
7
|
+
export { MAX_SCHEMA_FIELDS, parseGraphSchemaObservation, type GraphSchemaObservation, type ObservedField, type ObservedValueTag, } from "./domain/schema-observation.js";
|
|
8
|
+
export { DEFAULT_R2_PREFIX, PROJECTION_NAME, projectionEntityKey, projectionMatchesGraph, type ProjectionBackfillSince, type ProjectionBackfillStatus, type ProjectionBatchOptions, type ProjectionConfig, type ProjectionDeadLetter, type ProjectionDefinitions, type ProjectionDelivery, type ProjectionDeliveryReason, type ProjectionDetail, type ProjectionEntityDocument, type ProjectionEvent, type ProjectionEventKind, type ProjectionPartitionCursor, type ProjectionPendingBatch, type ProjectionR2SinkConfig, type ProjectionRetryOptions, type ProjectionSelector, type ProjectionSink, type ProjectionSinkConfig, type ProjectionState, type ProjectionStatus, type ProjectionWorkerSinkConfig, type ResolvedProjectionConfig, type ResolvedProjectionSelector, type ResolvedProjectionSink, } from "./domain/projection.js";
|
|
9
|
+
export { SCHEMA, valueType, type SchemaTerm, type TermKind } from "./domain/vocabulary.js";
|
|
10
|
+
export { MAX_QUERY_CLAUSES, MAX_QUERY_MANY, createDatalogQuery, type BuiltinClause, type BuiltinName, type DatalogClause, type DatalogQuery, type FactClause, type Term, } from "./datalog/ast.js";
|
|
11
|
+
export { QueryResultLimitExceeded, evaluateDatalog, type Binding, type EvaluationOptions, } from "./datalog/evaluator.js";
|
|
12
|
+
export type { FactLookup, FactSource, FactTuple, FactValueSetLookup, FieldIndex } from "./datalog/fact-source.js";
|
|
13
|
+
export { parseDatalogQuery } from "./datalog/parser.js";
|
|
14
|
+
export { boundValue, compareDatalogValues, decimalValue, equalDatalogValues, factValueToDatalogValues, integerValue, matchedItem, referenceValue, sameDatalogValue, stringValue, type BoundValue, type DatalogValue, } from "./datalog/value.js";
|
|
15
|
+
export { createDatabase, type DatabaseClient, type DatabaseConfig, type DatabaseWithDefaultGraph, type FactInput, type FetchFunction, type GraphClient, type GraphDefinition, type ParseError, type ParseResult, type QueryAnswer, type QueryBinding, type QueryManyAnswer, type QueryManyItem, type QueryOptions, type ReadFailure, type Result, type TransactionOptions, type TransactionReceipt, type WriteFailure, } from "./client.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { parseFactDraft, parseFactDrafts, } from "./domain/fact.js";
|
|
2
|
+
export { parseGraphId } from "./domain/graph-id.js";
|
|
3
|
+
export { parseFactValue, toValue, } from "./domain/fact-value.js";
|
|
4
|
+
export { BASE62_ID_LENGTH, MAX_CLIENT_REQUEST_TOKEN_BYTES, MAX_TRANSACTION_ID_BYTES, MAX_URI_LENGTH, encodeBase62Id, newTransactionId, parseClientRequestToken, parseTransactionId, parseUri, } from "./domain/identifiers.js";
|
|
5
|
+
export { parseQueryMetrics, } from "./domain/query-metrics.js";
|
|
6
|
+
export { MAX_READ_TOKEN_LENGTH, MAX_STALENESS_MS, encodeReadToken, mergeReadTokens, newReadToken, newSession, parseConsistency, parseMaxStalenessMs, parseReadToken, } from "./domain/read-token.js";
|
|
7
|
+
export { MAX_SCHEMA_FIELDS, parseGraphSchemaObservation, } from "./domain/schema-observation.js";
|
|
8
|
+
export { DEFAULT_R2_PREFIX, PROJECTION_NAME, projectionEntityKey, projectionMatchesGraph, } from "./domain/projection.js";
|
|
9
|
+
export { SCHEMA, valueType } from "./domain/vocabulary.js";
|
|
10
|
+
export { MAX_QUERY_CLAUSES, MAX_QUERY_MANY, createDatalogQuery, } from "./datalog/ast.js";
|
|
11
|
+
export { QueryResultLimitExceeded, evaluateDatalog, } from "./datalog/evaluator.js";
|
|
12
|
+
export { parseDatalogQuery } from "./datalog/parser.js";
|
|
13
|
+
export { boundValue, compareDatalogValues, decimalValue, equalDatalogValues, factValueToDatalogValues, integerValue, matchedItem, referenceValue, sameDatalogValue, stringValue, } from "./datalog/value.js";
|
|
14
|
+
export { createDatabase, } from "./client.js";
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { type ParsedProjections, type ProjectionDefinitions } from "./domain/projection.js";
|
|
2
|
+
/** Parses and validates `projections` from `cloudflare.config.ts` (`fatedb({ projections })`). */
|
|
3
|
+
export declare function parseProjectionsConfig(input: ProjectionDefinitions | undefined): ParsedProjections;
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
// Parses and validates `projections` definitions from `cloudflare.config.ts`.
|
|
2
|
+
// Kept above `domain/` and `datalog/` so `domain/` never imports `datalog/`.
|
|
3
|
+
import { parseDatalogQuery } from "./datalog/parser.js";
|
|
4
|
+
import { parseGraphId } from "./domain/graph-id.js";
|
|
5
|
+
import { MAX_URI_LENGTH, parseUri } from "./domain/identifiers.js";
|
|
6
|
+
import { DEFAULT_INITIAL_BACKOFF_MS, DEFAULT_MAX_ATTEMPTS, DEFAULT_MAX_BACKOFF_MS, DEFAULT_MAX_ENTITIES, DEFAULT_R2_PREFIX, MAX_BATCH_ENTITIES, MAX_RETRY_ATTEMPTS, PROJECTION_NAME, } from "./domain/projection.js";
|
|
7
|
+
import { isRecord } from "./domain/result.js";
|
|
8
|
+
import { compareCodeUnits } from "./domain/string-order.js";
|
|
9
|
+
const WORKER_NAME = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
|
|
10
|
+
const BUCKET_NAME = /^[a-z0-9](?:[a-z0-9-]{1,61}[a-z0-9])$/;
|
|
11
|
+
const ENTRYPOINT_NAME = /^[A-Za-z_$][A-Za-z0-9_$]{0,63}$/;
|
|
12
|
+
/** Parses and validates `projections` from `cloudflare.config.ts` (`fatedb({ projections })`). */
|
|
13
|
+
export function parseProjectionsConfig(input) {
|
|
14
|
+
if (input === undefined)
|
|
15
|
+
return { configs: [], workerBindings: {}, r2Bindings: {} };
|
|
16
|
+
if (!isRecord(input))
|
|
17
|
+
throw new RangeError("projections must be an object keyed by projection name");
|
|
18
|
+
const configs = [];
|
|
19
|
+
const workerBindings = {};
|
|
20
|
+
const r2Bindings = {};
|
|
21
|
+
const workerKeyToBinding = new Map();
|
|
22
|
+
const r2KeyToBinding = new Map();
|
|
23
|
+
let workerIdx = 0;
|
|
24
|
+
let r2Idx = 0;
|
|
25
|
+
for (const [name, raw] of Object.entries(input)) {
|
|
26
|
+
if (!PROJECTION_NAME.test(name)) {
|
|
27
|
+
throw new RangeError(`projection ${JSON.stringify(name)} must be 1 to 63 lowercase letters, digits, hyphens, or underscores, starting with a letter or digit`);
|
|
28
|
+
}
|
|
29
|
+
if (!isRecord(raw))
|
|
30
|
+
throw new RangeError(`projection ${JSON.stringify(name)} must be an object`);
|
|
31
|
+
const graphs = parseGraphs(name, raw["graph"]);
|
|
32
|
+
const select = parseSelector(name, raw["select"]);
|
|
33
|
+
const sink = parseSink(name, raw["sink"], (worker, entrypoint) => {
|
|
34
|
+
const key = `${worker}\0${entrypoint ?? ""}`;
|
|
35
|
+
let binding = workerKeyToBinding.get(key);
|
|
36
|
+
if (binding === undefined) {
|
|
37
|
+
binding = `PROJECTION_WORKER_${workerIdx++}`;
|
|
38
|
+
workerKeyToBinding.set(key, binding);
|
|
39
|
+
workerBindings[binding] = entrypoint === undefined ? { type: "worker", worker } : { type: "worker", worker, exportName: entrypoint };
|
|
40
|
+
}
|
|
41
|
+
return binding;
|
|
42
|
+
}, (bucket) => {
|
|
43
|
+
let binding = r2KeyToBinding.get(bucket);
|
|
44
|
+
if (binding === undefined) {
|
|
45
|
+
binding = `PROJECTION_R2_${r2Idx++}`;
|
|
46
|
+
r2KeyToBinding.set(bucket, binding);
|
|
47
|
+
r2Bindings[binding] = { type: "r2", name: bucket };
|
|
48
|
+
}
|
|
49
|
+
return binding;
|
|
50
|
+
});
|
|
51
|
+
const batch = parseBatch(name, raw["batch"]);
|
|
52
|
+
const retry = parseRetry(name, raw["retry"]);
|
|
53
|
+
const deadLetter = raw["deadLetter"] === undefined ? true : parseBool(name, "deadLetter", raw["deadLetter"]);
|
|
54
|
+
const autoStart = raw["autoStart"] === undefined ? true : parseBool(name, "autoStart", raw["autoStart"]);
|
|
55
|
+
configs.push({ name, graphs, select, sink, batch, retry, deadLetter, autoStart });
|
|
56
|
+
}
|
|
57
|
+
return { configs, workerBindings, r2Bindings };
|
|
58
|
+
}
|
|
59
|
+
function parseBool(projection, field, value) {
|
|
60
|
+
if (typeof value !== "boolean")
|
|
61
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: ${field} must be a boolean`);
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
function parseIntRange(projection, field, value, fallback, min, max) {
|
|
65
|
+
if (value === undefined)
|
|
66
|
+
return fallback;
|
|
67
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < min || value > max) {
|
|
68
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: ${field} must be an integer from ${min} to ${max}`);
|
|
69
|
+
}
|
|
70
|
+
return value;
|
|
71
|
+
}
|
|
72
|
+
function parseGraphs(projection, raw) {
|
|
73
|
+
const list = typeof raw === "string" ? [raw] : Array.isArray(raw) ? raw : null;
|
|
74
|
+
if (list === null || list.length === 0) {
|
|
75
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: graph must be a graph id, "*", or a non-empty list of graph ids`);
|
|
76
|
+
}
|
|
77
|
+
const seen = new Set();
|
|
78
|
+
for (const item of list) {
|
|
79
|
+
if (typeof item !== "string")
|
|
80
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: graph entries must be strings`);
|
|
81
|
+
if (item === "*") {
|
|
82
|
+
if (list.length > 1)
|
|
83
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: "*" cannot be combined with specific graph ids`);
|
|
84
|
+
return ["*"];
|
|
85
|
+
}
|
|
86
|
+
const parsed = parseGraphId(item);
|
|
87
|
+
if (!parsed.ok)
|
|
88
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: invalid graph id ${JSON.stringify(item)} (${parsed.error.message})`);
|
|
89
|
+
seen.add(parsed.value);
|
|
90
|
+
}
|
|
91
|
+
return [...seen].sort(compareCodeUnits);
|
|
92
|
+
}
|
|
93
|
+
function parseSelector(projection, raw) {
|
|
94
|
+
if (raw === undefined) {
|
|
95
|
+
return { entityKinds: [], typeField: null, fields: [], where: null, whereEntityVar: null, whereFields: [] };
|
|
96
|
+
}
|
|
97
|
+
if (!isRecord(raw))
|
|
98
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: select must be an object`);
|
|
99
|
+
const entityKinds = [];
|
|
100
|
+
if (raw["entityKind"] !== undefined) {
|
|
101
|
+
const list = typeof raw["entityKind"] === "string" ? [raw["entityKind"]] : Array.isArray(raw["entityKind"]) ? raw["entityKind"] : null;
|
|
102
|
+
if (list === null || list.length === 0)
|
|
103
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: select.entityKind must be a string or non-empty string list`);
|
|
104
|
+
const seen = new Set();
|
|
105
|
+
for (const item of list) {
|
|
106
|
+
if (typeof item !== "string" || item.length === 0 || item.length > MAX_URI_LENGTH) {
|
|
107
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: select.entityKind entries must be non-empty strings up to ${MAX_URI_LENGTH} characters`);
|
|
108
|
+
}
|
|
109
|
+
if (!seen.has(item)) {
|
|
110
|
+
seen.add(item);
|
|
111
|
+
entityKinds.push(item);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
let typeField = null;
|
|
116
|
+
if (raw["typeField"] !== undefined) {
|
|
117
|
+
const parsed = parseUri(raw["typeField"], "select.typeField");
|
|
118
|
+
if (!parsed.ok)
|
|
119
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: ${parsed.error.message}`);
|
|
120
|
+
typeField = parsed.value;
|
|
121
|
+
}
|
|
122
|
+
const fields = [];
|
|
123
|
+
if (raw["fields"] !== undefined) {
|
|
124
|
+
if (!Array.isArray(raw["fields"]) || raw["fields"].length === 0) {
|
|
125
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: select.fields must be a non-empty string list`);
|
|
126
|
+
}
|
|
127
|
+
const seen = new Set();
|
|
128
|
+
for (const item of raw["fields"]) {
|
|
129
|
+
const parsed = parseUri(item, "select.fields");
|
|
130
|
+
if (!parsed.ok)
|
|
131
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: ${parsed.error.message}`);
|
|
132
|
+
if (!seen.has(parsed.value)) {
|
|
133
|
+
seen.add(parsed.value);
|
|
134
|
+
fields.push(parsed.value);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
let where = null;
|
|
139
|
+
let whereEntityVar = null;
|
|
140
|
+
const whereFields = [];
|
|
141
|
+
if (raw["where"] !== undefined) {
|
|
142
|
+
if (typeof raw["where"] !== "string" || raw["where"].trim() === "") {
|
|
143
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: select.where must be a non-empty Datalog query string`);
|
|
144
|
+
}
|
|
145
|
+
const parsed = parseDatalogQuery(raw["where"]);
|
|
146
|
+
if (!parsed.ok)
|
|
147
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: select.where is not valid Datalog: ${parsed.error.message}`);
|
|
148
|
+
const vars = [];
|
|
149
|
+
const predicates = new Set();
|
|
150
|
+
for (const clause of parsed.value.facts) {
|
|
151
|
+
predicates.add(clause.predicate);
|
|
152
|
+
if (clause.entity.kind === "variable" && !vars.includes(clause.entity.name))
|
|
153
|
+
vars.push(clause.entity.name);
|
|
154
|
+
if (clause.value.kind === "variable" && !vars.includes(clause.value.name))
|
|
155
|
+
vars.push(clause.value.name);
|
|
156
|
+
}
|
|
157
|
+
const firstVar = vars[0];
|
|
158
|
+
if (firstVar === undefined) {
|
|
159
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: select.where must bind at least one variable (such as E)`);
|
|
160
|
+
}
|
|
161
|
+
where = raw["where"].trim();
|
|
162
|
+
whereEntityVar = vars.includes("E") ? "E" : firstVar;
|
|
163
|
+
whereFields.push(...predicates);
|
|
164
|
+
}
|
|
165
|
+
return { entityKinds, typeField, fields, where, whereEntityVar, whereFields };
|
|
166
|
+
}
|
|
167
|
+
function parseSink(projection, raw, bindWorker, bindR2) {
|
|
168
|
+
if (!isRecord(raw))
|
|
169
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: sink must be an object`);
|
|
170
|
+
if (raw["kind"] === "worker") {
|
|
171
|
+
const worker = raw["worker"];
|
|
172
|
+
if (typeof worker !== "string" || !WORKER_NAME.test(worker)) {
|
|
173
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: sink.worker must be a valid Cloudflare Worker name`);
|
|
174
|
+
}
|
|
175
|
+
const entrypoint = raw["entrypoint"];
|
|
176
|
+
if (entrypoint !== undefined && (typeof entrypoint !== "string" || !ENTRYPOINT_NAME.test(entrypoint))) {
|
|
177
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: sink.entrypoint must be a valid JavaScript export identifier`);
|
|
178
|
+
}
|
|
179
|
+
const binding = bindWorker(worker, entrypoint);
|
|
180
|
+
return entrypoint === undefined ? { kind: "worker", binding, worker } : { kind: "worker", binding, worker, entrypoint };
|
|
181
|
+
}
|
|
182
|
+
if (raw["kind"] === "r2") {
|
|
183
|
+
const bucket = raw["bucket"];
|
|
184
|
+
if (typeof bucket !== "string" || !BUCKET_NAME.test(bucket)) {
|
|
185
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: sink.bucket must be a valid R2 bucket name (3 to 63 lowercase letters, digits and hyphens)`);
|
|
186
|
+
}
|
|
187
|
+
const prefix = raw["prefix"] ?? DEFAULT_R2_PREFIX;
|
|
188
|
+
if (typeof prefix !== "string" || prefix.startsWith("/") || prefix.includes("..")) {
|
|
189
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: sink.prefix must be a relative key prefix without leading "/" or ".."`);
|
|
190
|
+
}
|
|
191
|
+
const binding = bindR2(bucket);
|
|
192
|
+
return { kind: "r2", binding, bucket, prefix };
|
|
193
|
+
}
|
|
194
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: sink.kind must be "worker" or "r2"`);
|
|
195
|
+
}
|
|
196
|
+
function parseBatch(projection, raw) {
|
|
197
|
+
if (raw === undefined)
|
|
198
|
+
return { maxEntities: DEFAULT_MAX_ENTITIES };
|
|
199
|
+
if (!isRecord(raw))
|
|
200
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: batch must be an object`);
|
|
201
|
+
return { maxEntities: parseIntRange(projection, "batch.maxEntities", raw["maxEntities"], DEFAULT_MAX_ENTITIES, 1, MAX_BATCH_ENTITIES) };
|
|
202
|
+
}
|
|
203
|
+
function parseRetry(projection, raw) {
|
|
204
|
+
if (raw === undefined)
|
|
205
|
+
return { maxAttempts: DEFAULT_MAX_ATTEMPTS, initialBackoffMs: DEFAULT_INITIAL_BACKOFF_MS, maxBackoffMs: DEFAULT_MAX_BACKOFF_MS };
|
|
206
|
+
if (!isRecord(raw))
|
|
207
|
+
throw new RangeError(`projection ${JSON.stringify(projection)}: retry must be an object`);
|
|
208
|
+
const maxAttempts = parseIntRange(projection, "retry.maxAttempts", raw["maxAttempts"], DEFAULT_MAX_ATTEMPTS, 1, MAX_RETRY_ATTEMPTS);
|
|
209
|
+
const initialBackoffMs = parseIntRange(projection, "retry.initialBackoffMs", raw["initialBackoffMs"], DEFAULT_INITIAL_BACKOFF_MS, 100, 60_000);
|
|
210
|
+
const maxBackoffMs = parseIntRange(projection, "retry.maxBackoffMs", raw["maxBackoffMs"], Math.max(DEFAULT_MAX_BACKOFF_MS, initialBackoffMs), initialBackoffMs, 3_600_000);
|
|
211
|
+
return { maxAttempts, initialBackoffMs, maxBackoffMs };
|
|
212
|
+
}
|