@enspirit/bmg-js 1.0.1 → 1.0.2

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 (158) hide show
  1. package/LICENSE.md +21 -0
  2. package/dist/{src/Relation → Relation}/Memory.d.ts +45 -46
  3. package/dist/{src/src/Relation → Relation}/index.d.ts +1 -1
  4. package/dist/bmg.cjs +1 -1
  5. package/dist/bmg.cjs.map +1 -1
  6. package/dist/bmg.modern.js +1 -1
  7. package/dist/bmg.modern.js.map +1 -1
  8. package/dist/bmg.module.js +1 -1
  9. package/dist/bmg.module.js.map +1 -1
  10. package/dist/bmg.umd.js +1 -1
  11. package/dist/bmg.umd.js.map +1 -1
  12. package/dist/index.d.ts +27 -1
  13. package/dist/lib-definitions.d.ts +1 -0
  14. package/dist/{src/operators → operators}/_helpers.d.ts +142 -142
  15. package/dist/{src/operators → operators}/allbut.d.ts +2 -2
  16. package/dist/{src/operators → operators}/autowrap.d.ts +2 -2
  17. package/dist/{src/operators → operators}/constants.d.ts +2 -2
  18. package/dist/{src/operators → operators}/cross_product.d.ts +3 -3
  19. package/dist/{src/operators → operators}/exclude.d.ts +2 -2
  20. package/dist/{src/operators → operators}/extend.d.ts +2 -2
  21. package/dist/{src/src/operators → operators}/group.d.ts +2 -2
  22. package/dist/{src/operators → operators}/image.d.ts +2 -2
  23. package/dist/{src/operators → operators}/index.d.ts +30 -30
  24. package/dist/{src/src/operators → operators}/intersect.d.ts +2 -2
  25. package/dist/{src/src/operators → operators}/isEqual.d.ts +2 -2
  26. package/dist/{src/operators → operators}/isRelation.d.ts +1 -1
  27. package/dist/{src/operators → operators}/join.d.ts +2 -2
  28. package/dist/{src/operators → operators}/left_join.d.ts +2 -2
  29. package/dist/{src/src/operators → operators}/matching.d.ts +2 -2
  30. package/dist/{src/src/operators → operators}/minus.d.ts +2 -2
  31. package/dist/{src/src/operators → operators}/not_matching.d.ts +2 -2
  32. package/dist/{src/operators → operators}/one.d.ts +2 -2
  33. package/dist/{src/operators → operators}/prefix.d.ts +2 -2
  34. package/dist/{src/src/operators → operators}/project.d.ts +2 -2
  35. package/dist/{src/operators → operators}/rename.d.ts +2 -2
  36. package/dist/{src/operators → operators}/restrict.d.ts +2 -2
  37. package/dist/{src/operators → operators}/suffix.d.ts +2 -2
  38. package/dist/{src/operators → operators}/summarize.d.ts +2 -2
  39. package/dist/{src/operators → operators}/transform.d.ts +2 -2
  40. package/dist/{src/src/operators → operators}/ungroup.d.ts +2 -2
  41. package/dist/{src/operators → operators}/union.d.ts +2 -2
  42. package/dist/{src/operators → operators}/unwrap.d.ts +2 -2
  43. package/dist/{src/operators → operators}/where.d.ts +1 -1
  44. package/dist/{src/operators → operators}/wrap.d.ts +2 -2
  45. package/dist/{src/operators → operators}/yByX.d.ts +2 -2
  46. package/dist/{src/src/support → support}/toPredicateFunc.d.ts +2 -2
  47. package/dist/{src/src/types.d.ts → types.d.ts} +162 -101
  48. package/package.json +6 -4
  49. package/src/Relation/Memory.ts +13 -12
  50. package/src/index.ts +1 -1
  51. package/src/lib-definitions.ts +281 -0
  52. package/src/types.ts +142 -54
  53. package/dist/src/Relation/index.d.ts +0 -1
  54. package/dist/src/Relation.d.ts +0 -8
  55. package/dist/src/index.d.ts +0 -27
  56. package/dist/src/operators/group.d.ts +0 -2
  57. package/dist/src/operators/intersect.d.ts +0 -2
  58. package/dist/src/operators/isEqual.d.ts +0 -2
  59. package/dist/src/operators/matching.d.ts +0 -2
  60. package/dist/src/operators/minus.d.ts +0 -2
  61. package/dist/src/operators/not_matching.d.ts +0 -2
  62. package/dist/src/operators/project.d.ts +0 -2
  63. package/dist/src/operators/ungroup.d.ts +0 -2
  64. package/dist/src/src/Relation/Memory.d.ts +0 -46
  65. package/dist/src/src/index.d.ts +0 -27
  66. package/dist/src/src/operators/_helpers.d.ts +0 -142
  67. package/dist/src/src/operators/allbut.d.ts +0 -2
  68. package/dist/src/src/operators/autowrap.d.ts +0 -2
  69. package/dist/src/src/operators/constants.d.ts +0 -2
  70. package/dist/src/src/operators/cross_product.d.ts +0 -3
  71. package/dist/src/src/operators/exclude.d.ts +0 -2
  72. package/dist/src/src/operators/extend.d.ts +0 -2
  73. package/dist/src/src/operators/image.d.ts +0 -2
  74. package/dist/src/src/operators/index.d.ts +0 -30
  75. package/dist/src/src/operators/isRelation.d.ts +0 -1
  76. package/dist/src/src/operators/join.d.ts +0 -2
  77. package/dist/src/src/operators/left_join.d.ts +0 -2
  78. package/dist/src/src/operators/one.d.ts +0 -2
  79. package/dist/src/src/operators/prefix.d.ts +0 -2
  80. package/dist/src/src/operators/rename.d.ts +0 -2
  81. package/dist/src/src/operators/restrict.d.ts +0 -2
  82. package/dist/src/src/operators/suffix.d.ts +0 -2
  83. package/dist/src/src/operators/summarize.d.ts +0 -2
  84. package/dist/src/src/operators/transform.d.ts +0 -2
  85. package/dist/src/src/operators/union.d.ts +0 -2
  86. package/dist/src/src/operators/unwrap.d.ts +0 -2
  87. package/dist/src/src/operators/where.d.ts +0 -1
  88. package/dist/src/src/operators/wrap.d.ts +0 -2
  89. package/dist/src/src/operators/yByX.d.ts +0 -2
  90. package/dist/src/src/utility-types.d.ts +0 -43
  91. package/dist/src/support/toPredicateFunc.d.ts +0 -2
  92. package/dist/src/tests/bmg.test.d.ts +0 -1
  93. package/dist/src/tests/fixtures.d.ts +0 -6
  94. package/dist/src/tests/operators/allbut.test.d.ts +0 -1
  95. package/dist/src/tests/operators/autowrap.test.d.ts +0 -1
  96. package/dist/src/tests/operators/constants.test.d.ts +0 -1
  97. package/dist/src/tests/operators/cross_product.test.d.ts +0 -1
  98. package/dist/src/tests/operators/exclude.test.d.ts +0 -1
  99. package/dist/src/tests/operators/extend.test.d.ts +0 -1
  100. package/dist/src/tests/operators/group.test.d.ts +0 -1
  101. package/dist/src/tests/operators/image.test.d.ts +0 -1
  102. package/dist/src/tests/operators/intersect.test.d.ts +0 -1
  103. package/dist/src/tests/operators/isEqual.test.d.ts +0 -1
  104. package/dist/src/tests/operators/join.test.d.ts +0 -1
  105. package/dist/src/tests/operators/left_join.test.d.ts +0 -1
  106. package/dist/src/tests/operators/matching.test.d.ts +0 -1
  107. package/dist/src/tests/operators/minus.test.d.ts +0 -1
  108. package/dist/src/tests/operators/not_matching.test.d.ts +0 -1
  109. package/dist/src/tests/operators/one.test.d.ts +0 -1
  110. package/dist/src/tests/operators/prefix.test.d.ts +0 -1
  111. package/dist/src/tests/operators/project.test.d.ts +0 -1
  112. package/dist/src/tests/operators/rename.test.d.ts +0 -1
  113. package/dist/src/tests/operators/restrict.test.d.ts +0 -1
  114. package/dist/src/tests/operators/suffix.test.d.ts +0 -1
  115. package/dist/src/tests/operators/summarize.test.d.ts +0 -1
  116. package/dist/src/tests/operators/transform.test.d.ts +0 -1
  117. package/dist/src/tests/operators/ungroup.test.d.ts +0 -1
  118. package/dist/src/tests/operators/union.test.d.ts +0 -1
  119. package/dist/src/tests/operators/unwrap.test.d.ts +0 -1
  120. package/dist/src/tests/operators/where.test.d.ts +0 -1
  121. package/dist/src/tests/operators/wrap.test.d.ts +0 -1
  122. package/dist/src/tests/operators/yByX.test.d.ts +0 -1
  123. package/dist/src/tests/types/relation.test.d.ts +0 -1
  124. package/dist/src/types.d.ts +0 -101
  125. package/dist/src/utility-types.d.ts +0 -43
  126. package/dist/tests/bmg.test.d.ts +0 -1
  127. package/dist/tests/fixtures.d.ts +0 -6
  128. package/dist/tests/operators/allbut.test.d.ts +0 -1
  129. package/dist/tests/operators/autowrap.test.d.ts +0 -1
  130. package/dist/tests/operators/constants.test.d.ts +0 -1
  131. package/dist/tests/operators/cross_product.test.d.ts +0 -1
  132. package/dist/tests/operators/exclude.test.d.ts +0 -1
  133. package/dist/tests/operators/extend.test.d.ts +0 -1
  134. package/dist/tests/operators/group.test.d.ts +0 -1
  135. package/dist/tests/operators/image.test.d.ts +0 -1
  136. package/dist/tests/operators/intersect.test.d.ts +0 -1
  137. package/dist/tests/operators/isEqual.test.d.ts +0 -1
  138. package/dist/tests/operators/join.test.d.ts +0 -1
  139. package/dist/tests/operators/left_join.test.d.ts +0 -1
  140. package/dist/tests/operators/matching.test.d.ts +0 -1
  141. package/dist/tests/operators/minus.test.d.ts +0 -1
  142. package/dist/tests/operators/not_matching.test.d.ts +0 -1
  143. package/dist/tests/operators/one.test.d.ts +0 -1
  144. package/dist/tests/operators/prefix.test.d.ts +0 -1
  145. package/dist/tests/operators/project.test.d.ts +0 -1
  146. package/dist/tests/operators/rename.test.d.ts +0 -1
  147. package/dist/tests/operators/restrict.test.d.ts +0 -1
  148. package/dist/tests/operators/suffix.test.d.ts +0 -1
  149. package/dist/tests/operators/summarize.test.d.ts +0 -1
  150. package/dist/tests/operators/transform.test.d.ts +0 -1
  151. package/dist/tests/operators/ungroup.test.d.ts +0 -1
  152. package/dist/tests/operators/union.test.d.ts +0 -1
  153. package/dist/tests/operators/unwrap.test.d.ts +0 -1
  154. package/dist/tests/operators/where.test.d.ts +0 -1
  155. package/dist/tests/operators/wrap.test.d.ts +0 -1
  156. package/dist/tests/operators/yByX.test.d.ts +0 -1
  157. package/dist/tests/types/relation.test.d.ts +0 -1
  158. package/src/utility-types.ts +0 -77
@@ -1,101 +1,162 @@
1
- import type { RenameMap, Renamed, Prefixed, Suffixed, Joined, LeftJoined, Wrapped, Unwrapped, AggregatorResults } from './utility-types';
2
- /** Result of ungroup: remove relation attr, flatten its tuple type */
3
- export type Ungrouped<T, K extends keyof T> = T[K] extends Relation<infer N> ? Omit<T, K> & N : Omit<T, K>;
4
- export type AttrName = string;
5
- export type Tuple = Record<AttrName, unknown>;
6
- export interface PrefixOptions {
7
- except?: AttrName[];
8
- }
9
- export interface SuffixOptions {
10
- except?: AttrName[];
11
- }
12
- export interface AutowrapOptions {
13
- separator?: string;
14
- }
15
- /** Predicate function that receives a typed tuple */
16
- export type TypedPredicateFunc<T> = (t: T) => boolean;
17
- /** Predicate: either a partial tuple for equality matching, or a function */
18
- export type TypedPredicate<T> = Partial<T> | TypedPredicateFunc<T>;
19
- /** Extension function that receives a typed tuple */
20
- export type TypedExtensionFunc<T, R> = (tuple: T) => R;
21
- /** Extension definition: function returning value, or attribute name to copy */
22
- export type TypedExtension<T, E extends Record<string, unknown>> = {
23
- [K in keyof E]: TypedExtensionFunc<T, E[K]> | keyof T;
24
- };
25
- /**
26
- * Relation interface with generic type parameter for tuple type.
27
- * Default parameter `Tuple` ensures backwards compatibility.
28
- *
29
- * @typeParam T - The tuple type for this relation. Defaults to `Tuple` (Record<string, unknown>).
30
- *
31
- * @example
32
- * // Untyped usage (backwards compatible)
33
- * const r = Bmg([{ id: 1 }]); // Relation<Tuple>
34
- *
35
- * @example
36
- * // Typed usage with full type safety
37
- * interface Person { id: number; name: string }
38
- * const r = Bmg<Person>([{ id: 1, name: 'Alice' }]);
39
- * r.project(['id']); // Relation<{ id: number }>
40
- */
41
- export interface Relation<T = Tuple> {
42
- restrict(p: TypedPredicate<T>): Relation<T>;
43
- where(p: TypedPredicate<T>): Relation<T>;
44
- exclude(p: TypedPredicate<T>): Relation<T>;
45
- project<K extends keyof T>(attrs: K[]): Relation<Pick<T, K>>;
46
- allbut<K extends keyof T>(attrs: K[]): Relation<Omit<T, K>>;
47
- extend<E extends Record<string, unknown>>(e: TypedExtension<T, E>): Relation<T & E>;
48
- constants<C extends Tuple>(consts: C): Relation<T & C>;
49
- rename<R extends RenameMap<T>>(r: R): Relation<Renamed<T, R>>;
50
- prefix<P extends string, Ex extends keyof T = never>(pfx: P, options?: {
51
- except?: Ex[];
52
- }): Relation<Prefixed<T, P, Ex>>;
53
- suffix<S extends string, Ex extends keyof T = never>(sfx: S, options?: {
54
- except?: Ex[];
55
- }): Relation<Suffixed<T, S, Ex>>;
56
- union(right: RelationOperand<T>): Relation<T>;
57
- minus(right: RelationOperand<T>): Relation<T>;
58
- intersect(right: RelationOperand<T>): Relation<T>;
59
- matching<R>(right: RelationOperand<R>, keys?: JoinKeys): Relation<T>;
60
- not_matching<R>(right: RelationOperand<R>, keys?: JoinKeys): Relation<T>;
61
- join<R>(right: RelationOperand<R>, keys?: JoinKeys): Relation<Joined<T, R>>;
62
- left_join<R>(right: RelationOperand<R>, keys?: JoinKeys): Relation<LeftJoined<T, R>>;
63
- cross_product<R>(right: RelationOperand<R>): Relation<T & R>;
64
- cross_join<R>(right: RelationOperand<R>): Relation<T & R>;
65
- image<R, As extends string>(right: RelationOperand<R>, as: As, keys?: JoinKeys): Relation<T & Record<As, Relation<Omit<R, keyof T & keyof R>>>>;
66
- group<K extends keyof T, As extends string>(attrs: K[], as: As): Relation<Omit<T, K> & Record<As, Relation<Pick<T, K>>>>;
67
- ungroup<K extends keyof T>(attr: K): Relation<Ungrouped<T, K>>;
68
- wrap<K extends keyof T, As extends string>(attrs: K[], as: As): Relation<Wrapped<T, K, As>>;
69
- unwrap<K extends keyof T>(attr: K): Relation<Unwrapped<T, K>>;
70
- summarize<By extends keyof T, Aggs extends Aggregators>(by: By[], aggs: Aggs): Relation<Pick<T, By> & AggregatorResults<Aggs>>;
71
- transform(t: Transformation): Relation<T>;
72
- autowrap(options?: AutowrapOptions): Relation<Tuple>;
73
- one(): T;
74
- yByX<Y extends keyof T, X extends keyof T>(y: Y, x: X): Record<T[X] & PropertyKey, T[Y]>;
75
- toArray(): T[];
76
- isEqual(right: any): boolean;
77
- }
78
- export type RelationOperand<T = Tuple> = Relation<T> | T[];
79
- export interface OperationalOperand<T = Tuple> {
80
- tuples(): Iterable<T>;
81
- output(tuples: T[]): RelationOperand<T>;
82
- }
83
- export type PredicateFunc = ((t: Tuple) => any);
84
- export type Predicate = Tuple | PredicateFunc;
85
- export type Renaming = RenamingObj | RenamingFunc;
86
- export type RenamingFunc = (attr: AttrName) => AttrName;
87
- export type RenamingObj = Record<AttrName, AttrName>;
88
- export type ExtensionFunc = (tuple: Tuple) => unknown;
89
- export type Extension = Record<AttrName, ExtensionFunc | AttrName>;
90
- export type JoinKeys = AttrName[] | Record<AttrName, AttrName>;
91
- export type AggregatorName = 'count' | 'sum' | 'min' | 'max' | 'avg' | 'collect';
92
- export type AggregatorSpec = {
93
- op: AggregatorName;
94
- attr: AttrName;
95
- };
96
- export type AggregatorFunc = (tuples: Tuple[]) => unknown;
97
- export type Aggregator = AggregatorName | AggregatorSpec | AggregatorFunc;
98
- export type Aggregators = Record<AttrName, Aggregator>;
99
- export type TransformFunc = (value: unknown) => unknown;
100
- export type Transformation = TransformFunc | TransformFunc[] | Record<AttrName, TransformFunc | TransformFunc[]>;
101
- export type { RenameMap, Renamed, Prefixed, Suffixed, Joined, LeftJoined, Wrapped, Unwrapped } from './utility-types';
1
+ /**
2
+ * Type definitions for Bmg.js relational algebra library.
3
+ *
4
+ * This file defines all types needed for type-safe relational operations:
5
+ * - Base types (AttrName, Tuple)
6
+ * - Utility types for transformations (Renamed, Prefixed, Joined, etc.)
7
+ * - The Relation interface with all operators
8
+ * - Helper types for predicates, extensions, aggregators, etc.
9
+ */
10
+ /** Attribute name in a tuple */
11
+ export type AttrName = string;
12
+ /** A tuple is a record mapping attribute names to values */
13
+ export type Tuple = Record<AttrName, unknown>;
14
+ /** Map from old attribute names to new attribute names */
15
+ export type RenameMap<T> = {
16
+ [K in keyof T]?: string;
17
+ };
18
+ /** Transform tuple type by renaming keys according to RenameMap */
19
+ export type Renamed<T, R extends RenameMap<T>> = {
20
+ [K in keyof T as K extends keyof R ? (R[K] extends string ? R[K] : K) : K]: T[K];
21
+ };
22
+ /** Prefix all keys except those in Except */
23
+ export type Prefixed<T, P extends string, Except extends keyof T = never> = {
24
+ [K in keyof T as K extends Except ? K : `${P}${K & string}`]: T[K];
25
+ };
26
+ /** Suffix all keys except those in Except */
27
+ export type Suffixed<T, S extends string, Except extends keyof T = never> = {
28
+ [K in keyof T as K extends Except ? K : `${K & string}${S}`]: T[K];
29
+ };
30
+ /** Extract common keys between two tuple types */
31
+ export type CommonKeys<L, R> = Extract<keyof L, keyof R>;
32
+ /** Result of inner join: L & R with R's common keys removed */
33
+ export type Joined<L, R> = L & Omit<R, CommonKeys<L, R>>;
34
+ /** Result of left join: L & optional R attributes (common keys removed) */
35
+ export type LeftJoined<L, R> = L & Partial<Omit<R, CommonKeys<L, R>>>;
36
+ /**
37
+ * Typed join keys for array form: keys must exist on BOTH operands.
38
+ * Example: suppliers.join(parts, ['city']) - 'city' must be a key of both.
39
+ */
40
+ export type TypedJoinKeysArray<L, R> = (keyof L & keyof R & string)[];
41
+ /**
42
+ * Typed join keys for object form: maps left keys to right keys.
43
+ * Example: suppliers.join(parts, { sid: 'supplier_id' })
44
+ * - Left key (sid) must exist on L
45
+ * - Right key (supplier_id) must exist on R
46
+ */
47
+ export type TypedJoinKeysObject<L, R> = {
48
+ [K in keyof L & string]?: keyof R & string;
49
+ };
50
+ /** Result of wrap: remove wrapped attrs, add nested object */
51
+ export type Wrapped<T, K extends keyof T, As extends string> = Omit<T, K> & Record<As, Pick<T, K>>;
52
+ /** Result of unwrap: remove object attr, spread its properties */
53
+ export type Unwrapped<T, K extends keyof T> = T[K] extends Record<string, unknown> ? Omit<T, K> & T[K] : Omit<T, K>;
54
+ /** Result of ungroup: remove relation attr, flatten its tuple type */
55
+ export type Ungrouped<T, K extends keyof T> = T[K] extends Relation<infer N> ? Omit<T, K> & N : Omit<T, K>;
56
+ export type AggregatorName = 'count' | 'sum' | 'min' | 'max' | 'avg' | 'collect';
57
+ export type AggregatorSpec = {
58
+ op: AggregatorName;
59
+ attr: AttrName;
60
+ };
61
+ export type AggregatorFunc = (tuples: Tuple[]) => unknown;
62
+ export type Aggregator = AggregatorName | AggregatorSpec | AggregatorFunc;
63
+ export type Aggregators = Record<AttrName, Aggregator>;
64
+ /** Infer result type from aggregator specification */
65
+ export type AggregatorResult<A> = A extends 'count' ? number : A extends {
66
+ op: 'count';
67
+ } ? number : A extends {
68
+ op: 'sum' | 'avg' | 'min' | 'max';
69
+ } ? number | null : A extends {
70
+ op: 'collect';
71
+ } ? unknown[] : A extends (tuples: Tuple[]) => infer R ? R : unknown;
72
+ /** Map aggregator definitions to their result types */
73
+ export type AggregatorResults<Aggs extends Record<string, unknown>> = {
74
+ [K in keyof Aggs]: AggregatorResult<Aggs[K]>;
75
+ };
76
+ /** Predicate function that receives a typed tuple */
77
+ export type TypedPredicateFunc<T> = (t: T) => boolean;
78
+ /** Predicate: either a partial tuple for equality matching, or a function */
79
+ export type TypedPredicate<T> = Partial<T> | TypedPredicateFunc<T>;
80
+ export type PredicateFunc = ((t: Tuple) => any);
81
+ export type Predicate = Tuple | PredicateFunc;
82
+ /** Extension function that receives a typed tuple */
83
+ export type TypedExtensionFunc<T, R> = (tuple: T) => R;
84
+ /** Extension definition: function returning value, or attribute name to copy */
85
+ export type TypedExtension<T, E extends Record<string, unknown>> = {
86
+ [K in keyof E]: TypedExtensionFunc<T, E[K]> | keyof T;
87
+ };
88
+ export type ExtensionFunc = (tuple: Tuple) => unknown;
89
+ export type Extension = Record<AttrName, ExtensionFunc | AttrName>;
90
+ export interface PrefixOptions {
91
+ except?: AttrName[];
92
+ }
93
+ export interface SuffixOptions {
94
+ except?: AttrName[];
95
+ }
96
+ export interface AutowrapOptions {
97
+ separator?: string;
98
+ }
99
+ export type Renaming = RenamingObj | RenamingFunc;
100
+ export type RenamingFunc = (attr: AttrName) => AttrName;
101
+ export type RenamingObj = Record<AttrName, AttrName>;
102
+ export type JoinKeys = AttrName[] | Record<AttrName, AttrName>;
103
+ export type TransformFunc = (value: unknown) => unknown;
104
+ export type Transformation = TransformFunc | TransformFunc[] | Record<AttrName, TransformFunc | TransformFunc[]>;
105
+ /**
106
+ * Relation interface with generic type parameter for tuple type.
107
+ * Default parameter `Tuple` ensures backwards compatibility.
108
+ *
109
+ * @typeParam T - The tuple type for this relation. Defaults to `Tuple` (Record<string, unknown>).
110
+ *
111
+ * @example
112
+ * // Untyped usage (backwards compatible)
113
+ * const r = Bmg([{ id: 1 }]); // Relation<Tuple>
114
+ *
115
+ * @example
116
+ * // Typed usage with full type safety
117
+ * interface Person { id: number; name: string }
118
+ * const r = Bmg<Person>([{ id: 1, name: 'Alice' }]);
119
+ * r.project(['id']); // Relation<{ id: number }>
120
+ */
121
+ export interface Relation<T = Tuple> {
122
+ restrict(p: TypedPredicate<T>): Relation<T>;
123
+ where(p: TypedPredicate<T>): Relation<T>;
124
+ exclude(p: TypedPredicate<T>): Relation<T>;
125
+ project<K extends keyof T>(attrs: K[]): Relation<Pick<T, K>>;
126
+ allbut<K extends keyof T>(attrs: K[]): Relation<Omit<T, K>>;
127
+ extend<E extends Record<string, unknown>>(e: TypedExtension<T, E>): Relation<T & E>;
128
+ constants<C extends Tuple>(consts: C): Relation<T & C>;
129
+ rename<R extends RenameMap<T>>(r: R): Relation<Renamed<T, R>>;
130
+ prefix<P extends string, Ex extends keyof T = never>(pfx: P, options?: {
131
+ except?: Ex[];
132
+ }): Relation<Prefixed<T, P, Ex>>;
133
+ suffix<S extends string, Ex extends keyof T = never>(sfx: S, options?: {
134
+ except?: Ex[];
135
+ }): Relation<Suffixed<T, S, Ex>>;
136
+ union(right: RelationOperand<T>): Relation<T>;
137
+ minus(right: RelationOperand<T>): Relation<T>;
138
+ intersect(right: RelationOperand<T>): Relation<T>;
139
+ matching<R>(right: RelationOperand<R>, keys?: TypedJoinKeysArray<T, R> | TypedJoinKeysObject<T, R>): Relation<T>;
140
+ not_matching<R>(right: RelationOperand<R>, keys?: TypedJoinKeysArray<T, R> | TypedJoinKeysObject<T, R>): Relation<T>;
141
+ join<R>(right: RelationOperand<R>, keys?: TypedJoinKeysArray<T, R> | TypedJoinKeysObject<T, R>): Relation<Joined<T, R>>;
142
+ left_join<R>(right: RelationOperand<R>, keys?: TypedJoinKeysArray<T, R> | TypedJoinKeysObject<T, R>): Relation<LeftJoined<T, R>>;
143
+ cross_product<R>(right: RelationOperand<R>): Relation<T & R>;
144
+ cross_join<R>(right: RelationOperand<R>): Relation<T & R>;
145
+ image<R, As extends string>(right: RelationOperand<R>, as: As, keys?: TypedJoinKeysArray<T, R> | TypedJoinKeysObject<T, R>): Relation<T & Record<As, Relation<Omit<R, keyof T & keyof R>>>>;
146
+ group<K extends keyof T, As extends string>(attrs: K[], as: As): Relation<Omit<T, K> & Record<As, Relation<Pick<T, K>>>>;
147
+ ungroup<K extends keyof T>(attr: K): Relation<Ungrouped<T, K>>;
148
+ wrap<K extends keyof T, As extends string>(attrs: K[], as: As): Relation<Wrapped<T, K, As>>;
149
+ unwrap<K extends keyof T>(attr: K): Relation<Unwrapped<T, K>>;
150
+ summarize<By extends keyof T, Aggs extends Aggregators>(by: By[], aggs: Aggs): Relation<Pick<T, By> & AggregatorResults<Aggs>>;
151
+ transform(t: Transformation): Relation<T>;
152
+ autowrap(options?: AutowrapOptions): Relation<Tuple>;
153
+ one(): T;
154
+ yByX<Y extends keyof T, X extends keyof T>(y: Y, x: X): Record<T[X] & PropertyKey, T[Y]>;
155
+ toArray(): T[];
156
+ isEqual(right: any): boolean;
157
+ }
158
+ export type RelationOperand<T = Tuple> = Relation<T> | T[];
159
+ export interface OperationalOperand<T = Tuple> {
160
+ tuples(): Iterable<T>;
161
+ output(tuples: T[]): RelationOperand<T>;
162
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enspirit/bmg-js",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Relational algebra for JavaScript/TypeScript - work with arrays of objects using powerful set operations",
5
5
  "keywords": [
6
6
  "relational",
@@ -23,22 +23,24 @@
23
23
  "type": "module",
24
24
  "source": "src/index.ts",
25
25
  "exports": {
26
- "types": "./dist/src/index.d.ts",
26
+ "types": "./dist/index.d.ts",
27
27
  "require": "./dist/bmg.cjs",
28
28
  "default": "./dist/bmg.modern.js"
29
29
  },
30
30
  "main": "./dist/bmg.cjs",
31
31
  "module": "./dist/bmg.module.js",
32
- "types": "./dist/src/index.d.ts",
32
+ "types": "./dist/index.d.ts",
33
33
  "unpkg": "./dist/bmg.umd.js",
34
34
  "scripts": {
35
- "build": "microbundle",
35
+ "generate:lib-definitions": "npx tsx scripts/generate-lib-definitions.ts",
36
+ "build": "npm run generate:lib-definitions && microbundle --no-generateTypes && tsc -p tsconfig.build.json",
36
37
  "dev": "microbundle watch",
37
38
  "test": "vitest run",
38
39
  "test:watch": "vitest"
39
40
  },
40
41
  "devDependencies": {
41
42
  "microbundle": "^0.15.1",
43
+ "tsx": "^4.19.0",
42
44
  "vite-tsconfig-paths": "^5.1.4",
43
45
  "vitest": "^4.0.16"
44
46
  },
@@ -32,7 +32,6 @@ import {
32
32
  import type {
33
33
  AttrName,
34
34
  AutowrapOptions,
35
- JoinKeys,
36
35
  Relation,
37
36
  RelationOperand,
38
37
  Transformation,
@@ -48,8 +47,10 @@ import type {
48
47
  Wrapped,
49
48
  Unwrapped,
50
49
  Ungrouped,
50
+ AggregatorResults,
51
+ TypedJoinKeysArray,
52
+ TypedJoinKeysObject,
51
53
  } from "../types";
52
- import type { AggregatorResults } from "../utility-types";
53
54
 
54
55
  /**
55
56
  * In-memory implementation of the Relation interface.
@@ -126,22 +127,22 @@ export class MemoryRelation<T = Tuple> implements Relation<T> {
126
127
 
127
128
  // === Semi-join operators ===
128
129
 
129
- matching<R>(right: RelationOperand<R>, keys?: JoinKeys): Relation<T> {
130
- return matching(this as any, right as any, keys) as unknown as Relation<T>;
130
+ matching<R>(right: RelationOperand<R>, keys?: TypedJoinKeysArray<T, R> | TypedJoinKeysObject<T, R>): Relation<T> {
131
+ return matching(this as any, right as any, keys as any) as unknown as Relation<T>;
131
132
  }
132
133
 
133
- not_matching<R>(right: RelationOperand<R>, keys?: JoinKeys): Relation<T> {
134
- return not_matching(this as any, right as any, keys) as unknown as Relation<T>;
134
+ not_matching<R>(right: RelationOperand<R>, keys?: TypedJoinKeysArray<T, R> | TypedJoinKeysObject<T, R>): Relation<T> {
135
+ return not_matching(this as any, right as any, keys as any) as unknown as Relation<T>;
135
136
  }
136
137
 
137
138
  // === Join operators ===
138
139
 
139
- join<R>(right: RelationOperand<R>, keys?: JoinKeys): Relation<Joined<T, R>> {
140
- return join(this as any, right as any, keys) as unknown as Relation<Joined<T, R>>;
140
+ join<R>(right: RelationOperand<R>, keys?: TypedJoinKeysArray<T, R> | TypedJoinKeysObject<T, R>): Relation<Joined<T, R>> {
141
+ return join(this as any, right as any, keys as any) as unknown as Relation<Joined<T, R>>;
141
142
  }
142
143
 
143
- left_join<R>(right: RelationOperand<R>, keys?: JoinKeys): Relation<LeftJoined<T, R>> {
144
- return left_join(this as any, right as any, keys) as unknown as Relation<LeftJoined<T, R>>;
144
+ left_join<R>(right: RelationOperand<R>, keys?: TypedJoinKeysArray<T, R> | TypedJoinKeysObject<T, R>): Relation<LeftJoined<T, R>> {
145
+ return left_join(this as any, right as any, keys as any) as unknown as Relation<LeftJoined<T, R>>;
145
146
  }
146
147
 
147
148
  cross_product<R>(right: RelationOperand<R>): Relation<T & R> {
@@ -154,8 +155,8 @@ export class MemoryRelation<T = Tuple> implements Relation<T> {
154
155
 
155
156
  // === Nesting operators ===
156
157
 
157
- image<R, As extends string>(right: RelationOperand<R>, as: As, keys?: JoinKeys): Relation<T & Record<As, Relation<Omit<R, keyof T & keyof R>>>> {
158
- return image(this as any, right as any, as, keys) as unknown as Relation<T & Record<As, Relation<Omit<R, keyof T & keyof R>>>>;
158
+ image<R, As extends string>(right: RelationOperand<R>, as: As, keys?: TypedJoinKeysArray<T, R> | TypedJoinKeysObject<T, R>): Relation<T & Record<As, Relation<Omit<R, keyof T & keyof R>>>> {
159
+ return image(this as any, right as any, as, keys as any) as unknown as Relation<T & Record<As, Relation<Omit<R, keyof T & keyof R>>>>;
159
160
  }
160
161
 
161
162
  group<K extends keyof T, As extends string>(attrs: K[], as: As): Relation<Omit<T, K> & Record<As, Relation<Pick<T, K>>>> {
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export * from './operators';
2
2
  export * from './types';
3
- export * from './utility-types';
3
+ export { LIB_DEFINITIONS } from './lib-definitions';
4
4
 
5
5
  import { MemoryRelation } from './Relation';
6
6
  import { isRelation } from './operators';