@bjornpagen/bumbledb 0.1.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.
Files changed (108) hide show
  1. package/LICENSE +12 -0
  2. package/README.md +84 -0
  3. package/dist/brand.d.ts +59 -0
  4. package/dist/brand.d.ts.map +1 -0
  5. package/dist/brand.js +47 -0
  6. package/dist/brand.js.map +1 -0
  7. package/dist/closed.d.ts +97 -0
  8. package/dist/closed.d.ts.map +1 -0
  9. package/dist/closed.js +107 -0
  10. package/dist/closed.js.map +1 -0
  11. package/dist/count.d.ts +55 -0
  12. package/dist/count.d.ts.map +1 -0
  13. package/dist/count.js +92 -0
  14. package/dist/count.js.map +1 -0
  15. package/dist/db.d.ts +341 -0
  16. package/dist/db.d.ts.map +1 -0
  17. package/dist/db.js +1016 -0
  18. package/dist/db.js.map +1 -0
  19. package/dist/exhume.d.ts +130 -0
  20. package/dist/exhume.d.ts.map +1 -0
  21. package/dist/exhume.js +145 -0
  22. package/dist/exhume.js.map +1 -0
  23. package/dist/face.d.ts +91 -0
  24. package/dist/face.d.ts.map +1 -0
  25. package/dist/face.js +69 -0
  26. package/dist/face.js.map +1 -0
  27. package/dist/fields.d.ts +187 -0
  28. package/dist/fields.d.ts.map +1 -0
  29. package/dist/fields.js +204 -0
  30. package/dist/fields.js.map +1 -0
  31. package/dist/index.d.ts +49 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +31 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/lower.d.ts +34 -0
  36. package/dist/lower.d.ts.map +1 -0
  37. package/dist/lower.js +105 -0
  38. package/dist/lower.js.map +1 -0
  39. package/dist/marshal.d.ts +79 -0
  40. package/dist/marshal.d.ts.map +1 -0
  41. package/dist/marshal.js +167 -0
  42. package/dist/marshal.js.map +1 -0
  43. package/dist/native.d.ts +532 -0
  44. package/dist/native.d.ts.map +1 -0
  45. package/dist/native.js +64 -0
  46. package/dist/native.js.map +1 -0
  47. package/dist/query/atom.d.ts +285 -0
  48. package/dist/query/atom.d.ts.map +1 -0
  49. package/dist/query/atom.js +281 -0
  50. package/dist/query/atom.js.map +1 -0
  51. package/dist/query/lower.d.ts +145 -0
  52. package/dist/query/lower.d.ts.map +1 -0
  53. package/dist/query/lower.js +604 -0
  54. package/dist/query/lower.js.map +1 -0
  55. package/dist/query/predicate.d.ts +101 -0
  56. package/dist/query/predicate.d.ts.map +1 -0
  57. package/dist/query/predicate.js +85 -0
  58. package/dist/query/predicate.js.map +1 -0
  59. package/dist/query/run.d.ts +30 -0
  60. package/dist/query/run.d.ts.map +1 -0
  61. package/dist/query/run.js +94 -0
  62. package/dist/query/run.js.map +1 -0
  63. package/dist/query/scope.d.ts +168 -0
  64. package/dist/query/scope.d.ts.map +1 -0
  65. package/dist/query/scope.js +134 -0
  66. package/dist/query/scope.js.map +1 -0
  67. package/dist/query/select.d.ts +106 -0
  68. package/dist/query/select.d.ts.map +1 -0
  69. package/dist/query/select.js +69 -0
  70. package/dist/query/select.js.map +1 -0
  71. package/dist/relation.d.ts +120 -0
  72. package/dist/relation.d.ts.map +1 -0
  73. package/dist/relation.js +108 -0
  74. package/dist/relation.js.map +1 -0
  75. package/dist/schema.d.ts +50 -0
  76. package/dist/schema.d.ts.map +1 -0
  77. package/dist/schema.js +235 -0
  78. package/dist/schema.js.map +1 -0
  79. package/dist/spec.d.ts +218 -0
  80. package/dist/spec.d.ts.map +1 -0
  81. package/dist/spec.js +154 -0
  82. package/dist/spec.js.map +1 -0
  83. package/dist/statements.d.ts +91 -0
  84. package/dist/statements.d.ts.map +1 -0
  85. package/dist/statements.js +101 -0
  86. package/dist/statements.js.map +1 -0
  87. package/package.json +66 -0
  88. package/src/brand.ts +82 -0
  89. package/src/closed.ts +228 -0
  90. package/src/count.ts +117 -0
  91. package/src/db.ts +1519 -0
  92. package/src/exhume.ts +243 -0
  93. package/src/face.ts +161 -0
  94. package/src/fields.ts +385 -0
  95. package/src/index.ts +185 -0
  96. package/src/lower.ts +118 -0
  97. package/src/marshal.ts +220 -0
  98. package/src/native.ts +576 -0
  99. package/src/query/atom.ts +556 -0
  100. package/src/query/lower.ts +855 -0
  101. package/src/query/predicate.ts +195 -0
  102. package/src/query/run.ts +106 -0
  103. package/src/query/scope.ts +301 -0
  104. package/src/query/select.ts +140 -0
  105. package/src/relation.ts +252 -0
  106. package/src/schema.ts +297 -0
  107. package/src/spec.ts +325 -0
  108. package/src/statements.ts +148 -0
@@ -0,0 +1,145 @@
1
+ /**
2
+ * `query()` and the IR lowering (PRD-08). A query is built inside a scope
3
+ * callback (variable identity is lexical and typed) and is an INERT value:
4
+ * `Query<Rels, Row, Params>` with `Row` inferred from `select` and
5
+ * `Params` from every parameter the returned rules (and the predicates
6
+ * they reach) use. Lowering is a pure function of the query value down to
7
+ * the bridge's `ProgramIr` (`bumbledb/crates/bumbledb/src/ir.rs`, the
8
+ * bijection target): relations and predicates by declaration ordinal (the
9
+ * declaration-order-is-ids law the engine's manifest pins), variables by
10
+ * dense per-rule first-occurrence ids (rule-scoped, exactly as the IR
11
+ * scopes them), params by scope declaration order. Lowering is STABLE —
12
+ * the same query value lowers to deeply-equal IR every time, and two
13
+ * identically-written queries lower identically (prepared-query caching
14
+ * upstream keys on this). Construction validates negation safety (typed,
15
+ * naming the variable — earlier and warmer than the engine's refusal);
16
+ * everything else (strata, types, aggregates rosters, rule caps) is the
17
+ * ENGINE's judge, surfacing its typed errors at prepare. No invented
18
+ * limits: rule counts and predicate counts are never pre-checked here.
19
+ */
20
+ import { phantom } from "#brand.ts";
21
+ import { type FieldData } from "#fields.ts";
22
+ import type { ProgramIr, TaggedValue } from "#native.ts";
23
+ import type { AnyBodyItem } from "#query/atom.ts";
24
+ import type { ColumnValues, Predicate, PredicateColumnsInput, PredicateParams, PredicateRuleInput, PredicateSelf } from "#query/predicate.ts";
25
+ import type { AnyVar, ItemParams, MaskParam, Param, ParamSet, ParamsRecord, ParamsShape, QueryRegistry, Var } from "#query/scope.ts";
26
+ import type { AggregateData, RowOf, SelectShape } from "#query/select.ts";
27
+ import type { FieldRef } from "#relation.ts";
28
+ import type { Schema, SchemaRelations } from "#schema.ts";
29
+ /**
30
+ * The scope value the `query()` build callback receives: variable and
31
+ * parameter declaration plus predicate declaration (engine recursion).
32
+ */
33
+ interface Scope<Rels extends SchemaRelations> {
34
+ /**
35
+ * Declares one query variable, typed by the field it is declared from
36
+ * — usable in ANY atom position whose field carries the same brand
37
+ * (the nominal join discipline). Two calls are two variables.
38
+ */
39
+ var<V>(field: FieldRef<keyof Rels & string, string, V>): Var<V>;
40
+ /**
41
+ * Declares one scalar parameter under a mandatory name literal — the
42
+ * key of the typed params object `execute` takes.
43
+ */
44
+ param<const Name extends string, V>(name: Name, field: FieldRef<keyof Rels & string, string, V>): Param<Name, V>;
45
+ /**
46
+ * Declares one set parameter (the IR's `ParamSet`): bound at execution
47
+ * to a readonly array; a binding position matches on set membership.
48
+ */
49
+ paramSet<const Name extends string, V>(name: Name, field: FieldRef<keyof Rels & string, string, V>): ParamSet<Name, V>;
50
+ /**
51
+ * Declares one Allen-mask parameter (`MaskTerm::Param`): the temporal
52
+ * relation as a bind-time 13-bit mask argument.
53
+ */
54
+ allenParam<const Name extends string>(name: Name): MaskParam<Name>;
55
+ /**
56
+ * Declares one IDB predicate with a named, field-typed head; its rules
57
+ * are given in the declaration through a callback receiving the
58
+ * predicate's own reference (self-recursion is `self.match({...})`).
59
+ * `.match` on the returned value uses it as a body atom elsewhere.
60
+ */
61
+ predicate<const Cols extends PredicateColumnsInput, const Rules extends readonly PredicateRuleInput<ColumnValues<Cols>>[]>(name: string, columns: Cols, rules: (self: PredicateSelf<ColumnValues<Cols>>) => Rules): Predicate<ColumnValues<Cols>, PredicateParams<Rules>>;
62
+ }
63
+ /**
64
+ * What the build callback returns: `rules` is an array of conjunctions
65
+ * (multiple rules = set union — answers are SETS, no order/limit exists;
66
+ * the host sorts); `select` is the head record — non-aggregate entries are
67
+ * the implicit group key.
68
+ */
69
+ interface QueryBuild {
70
+ readonly rules: readonly (readonly AnyBodyItem[])[];
71
+ readonly select: SelectShape;
72
+ }
73
+ /** One classified select column as runtime data. */
74
+ type SelectEntryData = {
75
+ readonly kind: "var";
76
+ readonly over: AnyVar;
77
+ } | {
78
+ readonly kind: "measure";
79
+ readonly over: AnyVar;
80
+ } | {
81
+ readonly kind: "aggregate";
82
+ readonly aggregate: AggregateData;
83
+ };
84
+ /** One answer column: its name (the row object key) and its entry. */
85
+ interface SelectColumn {
86
+ readonly name: string;
87
+ readonly entry: SelectEntryData;
88
+ }
89
+ /** A query's runtime description — everything lowering and execution read. */
90
+ interface QueryData {
91
+ readonly registry: QueryRegistry;
92
+ readonly rules: readonly (readonly AnyBodyItem[])[];
93
+ readonly select: readonly SelectColumn[];
94
+ }
95
+ /**
96
+ * An inert query value. `Row` is the inferred answer-row object type;
97
+ * `Params` the inferred execute-params object type. Prepare with
98
+ * `db.prepare(q)`; nothing here touches an engine.
99
+ */
100
+ interface Query<Rels extends SchemaRelations, Row, Params extends ParamsRecord> {
101
+ readonly schema: Schema<Rels>;
102
+ readonly data: QueryData;
103
+ readonly [phantom]?: {
104
+ readonly row: Row;
105
+ readonly params: Params;
106
+ };
107
+ }
108
+ /** Any query value, whatever its schema and inferred types. */
109
+ type AnyQuery = Query<SchemaRelations, unknown, ParamsRecord>;
110
+ /** Extracts a query value's inferred answer-row type. */
111
+ type QueryRow<Q extends AnyQuery> = Exclude<Q[typeof phantom], undefined>["row"];
112
+ /** Extracts a query value's inferred execute-params type. */
113
+ type QueryParams<Q extends AnyQuery> = Exclude<Q[typeof phantom], undefined>["params"];
114
+ /** The params object type a build result implies (union over every rule item). */
115
+ type BuildParams<R extends QueryBuild> = ParamsShape<ItemParams<R["rules"][number][number]>>;
116
+ /**
117
+ * Builds a query as a typed value: runs `build` inside a fresh scope,
118
+ * classifies the select record (written order = answer column order),
119
+ * validates negation safety across every rule (the predicates' rules
120
+ * included), and freezes. Rule counts, strata legality, and every deeper
121
+ * roster stay the engine's judge at prepare.
122
+ */
123
+ declare function query<Rels extends SchemaRelations, const R extends QueryBuild>(theory: Schema<Rels>, build: ($: Scope<Rels>) => R): Query<Rels, RowOf<R["select"]>, BuildParams<R>>;
124
+ /**
125
+ * Tags one host literal at a FIELD position (atom bindings): the field's
126
+ * structural type directs the tag, never a guess. At an interval field a
127
+ * bigint literal tags as the ELEMENT type — the IR's membership typing
128
+ * rule (point membership), an interval-shaped literal as the interval
129
+ * (value equality). A closed-reference literal is its branded id, tagged
130
+ * u64 after a roster re-verification (queries cross ids, never handle
131
+ * names).
132
+ */
133
+ declare function taggedLiteral(context: string, field: FieldData, value: unknown): TaggedValue;
134
+ /**
135
+ * Lowers a query value to the bridge's `ProgramIr` — pure and stable: the
136
+ * declared predicates in declaration order (`PredId` = index), the output
137
+ * predicate (built from `rules` + `select`) appended last. Relations lower
138
+ * by declaration ordinal, the law the engine's own manifest pins;
139
+ * `db.prepare` re-verifies the alignment against the live manifest before
140
+ * sending.
141
+ */
142
+ declare function lowerQuery(q: AnyQuery): ProgramIr;
143
+ export type { AnyQuery, BuildParams, Query, QueryBuild, QueryData, QueryParams, QueryRow, Scope, SelectColumn, SelectEntryData };
144
+ export { lowerQuery, query, taggedLiteral };
145
+ //# sourceMappingURL=lower.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lower.d.ts","sourceRoot":"","sources":["../../src/query/lower.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAGH,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,EAA6B,KAAK,SAAS,EAAE,MAAM,YAAY,CAAA;AACtE,OAAO,KAAK,EAQX,SAAS,EAET,WAAW,EAEX,MAAM,YAAY,CAAA;AAEnB,OAAO,KAAK,EACX,WAAW,EAQX,MAAM,gBAAgB,CAAA;AAEvB,OAAO,KAAK,EACX,YAAY,EACZ,SAAS,EACT,qBAAqB,EAErB,eAAe,EACf,kBAAkB,EAClB,aA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