@enspirit/bmg-js 1.0.0 → 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 (103) hide show
  1. package/LICENSE.md +21 -0
  2. package/dist/Relation/Memory.d.ts +45 -0
  3. package/dist/Relation/index.d.ts +1 -0
  4. package/dist/bmg.cjs +2 -0
  5. package/dist/bmg.cjs.map +1 -0
  6. package/dist/bmg.modern.js +2 -0
  7. package/dist/bmg.modern.js.map +1 -0
  8. package/dist/bmg.module.js +2 -0
  9. package/dist/bmg.module.js.map +1 -0
  10. package/dist/bmg.umd.js +2 -0
  11. package/dist/bmg.umd.js.map +1 -0
  12. package/dist/index.d.ts +27 -0
  13. package/dist/lib-definitions.d.ts +1 -0
  14. package/dist/operators/_helpers.d.ts +142 -0
  15. package/dist/operators/allbut.d.ts +2 -0
  16. package/dist/operators/autowrap.d.ts +2 -0
  17. package/dist/operators/constants.d.ts +2 -0
  18. package/dist/operators/cross_product.d.ts +3 -0
  19. package/dist/operators/exclude.d.ts +2 -0
  20. package/dist/operators/extend.d.ts +2 -0
  21. package/dist/operators/group.d.ts +2 -0
  22. package/dist/operators/image.d.ts +2 -0
  23. package/dist/operators/index.d.ts +30 -0
  24. package/dist/operators/intersect.d.ts +2 -0
  25. package/dist/operators/isEqual.d.ts +2 -0
  26. package/dist/operators/isRelation.d.ts +1 -0
  27. package/dist/operators/join.d.ts +2 -0
  28. package/dist/operators/left_join.d.ts +2 -0
  29. package/dist/operators/matching.d.ts +2 -0
  30. package/dist/operators/minus.d.ts +2 -0
  31. package/dist/operators/not_matching.d.ts +2 -0
  32. package/dist/operators/one.d.ts +2 -0
  33. package/dist/operators/prefix.d.ts +2 -0
  34. package/dist/operators/project.d.ts +2 -0
  35. package/dist/operators/rename.d.ts +2 -0
  36. package/dist/operators/restrict.d.ts +2 -0
  37. package/dist/operators/suffix.d.ts +2 -0
  38. package/dist/operators/summarize.d.ts +2 -0
  39. package/dist/operators/transform.d.ts +2 -0
  40. package/dist/operators/ungroup.d.ts +2 -0
  41. package/dist/operators/union.d.ts +2 -0
  42. package/dist/operators/unwrap.d.ts +2 -0
  43. package/dist/operators/where.d.ts +1 -0
  44. package/dist/operators/wrap.d.ts +2 -0
  45. package/dist/operators/yByX.d.ts +2 -0
  46. package/dist/support/toPredicateFunc.d.ts +2 -0
  47. package/dist/types.d.ts +162 -0
  48. package/package.json +20 -6
  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/.claude/safe-setup/.env.example +0 -3
  54. package/.claude/safe-setup/Dockerfile.claude +0 -36
  55. package/.claude/safe-setup/HACKING.md +0 -63
  56. package/.claude/safe-setup/Makefile +0 -22
  57. package/.claude/safe-setup/docker-compose.yml +0 -18
  58. package/.claude/safe-setup/entrypoint.sh +0 -13
  59. package/.claude/settings.local.json +0 -9
  60. package/.claude/typescript-annotations.md +0 -273
  61. package/.github/workflows/test.yml +0 -26
  62. package/CLAUDE.md +0 -48
  63. package/Makefile +0 -2
  64. package/example/README.md +0 -22
  65. package/example/index.ts +0 -316
  66. package/example/package.json +0 -16
  67. package/example/tsconfig.json +0 -11
  68. package/src/utility-types.ts +0 -77
  69. package/tests/bmg.test.ts +0 -16
  70. package/tests/fixtures.ts +0 -9
  71. package/tests/operators/allbut.test.ts +0 -51
  72. package/tests/operators/autowrap.test.ts +0 -82
  73. package/tests/operators/constants.test.ts +0 -37
  74. package/tests/operators/cross_product.test.ts +0 -90
  75. package/tests/operators/exclude.test.ts +0 -43
  76. package/tests/operators/extend.test.ts +0 -45
  77. package/tests/operators/group.test.ts +0 -69
  78. package/tests/operators/image.test.ts +0 -152
  79. package/tests/operators/intersect.test.ts +0 -53
  80. package/tests/operators/isEqual.test.ts +0 -111
  81. package/tests/operators/join.test.ts +0 -116
  82. package/tests/operators/left_join.test.ts +0 -116
  83. package/tests/operators/matching.test.ts +0 -91
  84. package/tests/operators/minus.test.ts +0 -47
  85. package/tests/operators/not_matching.test.ts +0 -104
  86. package/tests/operators/one.test.ts +0 -19
  87. package/tests/operators/prefix.test.ts +0 -37
  88. package/tests/operators/project.test.ts +0 -48
  89. package/tests/operators/rename.test.ts +0 -39
  90. package/tests/operators/restrict.test.ts +0 -27
  91. package/tests/operators/suffix.test.ts +0 -37
  92. package/tests/operators/summarize.test.ts +0 -109
  93. package/tests/operators/transform.test.ts +0 -94
  94. package/tests/operators/ungroup.test.ts +0 -67
  95. package/tests/operators/union.test.ts +0 -51
  96. package/tests/operators/unwrap.test.ts +0 -50
  97. package/tests/operators/where.test.ts +0 -33
  98. package/tests/operators/wrap.test.ts +0 -54
  99. package/tests/operators/yByX.test.ts +0 -32
  100. package/tests/types/relation.test.ts +0 -296
  101. package/tsconfig.json +0 -37
  102. package/tsconfig.node.json +0 -9
  103. package/vitest.config.ts +0 -15
@@ -1,13 +0,0 @@
1
- #!/bin/bash
2
-
3
- # Configure git user/email from environment variables if provided
4
- if [ -n "$GIT_USER_NAME" ]; then
5
- git config --global user.name "$GIT_USER_NAME"
6
- fi
7
-
8
- if [ -n "$GIT_USER_EMAIL" ]; then
9
- git config --global user.email "$GIT_USER_EMAIL"
10
- fi
11
-
12
- # Execute the command passed to the container
13
- exec "$@"
@@ -1,9 +0,0 @@
1
- {
2
- "permissions": {
3
- "allow": [
4
- "Bash(npm run test:*)",
5
- "Bash(npx tsc:*)",
6
- "Bash(wc:*)"
7
- ]
8
- }
9
- }
@@ -1,273 +0,0 @@
1
- # TypeScript Type Safety Implementation Plan
2
-
3
- ## Goal
4
- Add opt-in generic type parameters to enable:
5
- - IDE autocomplete on tuple attributes
6
- - Compile-time validation of attribute names
7
- - Type transformation tracking through operator chains
8
-
9
- ## Core Changes
10
-
11
- ### 1. Generic Relation Interface (`src/types.ts`)
12
-
13
- ```typescript
14
- export interface Relation<T extends Tuple = Tuple> {
15
- one(): T;
16
- toArray(): T[];
17
-
18
- // Preserve type
19
- restrict(p: TypedPredicate<T>): Relation<T>;
20
- where(p: TypedPredicate<T>): Relation<T>;
21
-
22
- // Transform type
23
- project<K extends keyof T>(attrs: K[]): Relation<Pick<T, K>>;
24
- allbut<K extends keyof T>(attrs: K[]): Relation<Omit<T, K>>;
25
- rename<R extends RenameMap<T>>(r: R): Relation<Renamed<T, R>>;
26
- extend<E extends Record<string, unknown>>(e: TypedExtension<T, E>): Relation<T & E>;
27
- constants<C extends Tuple>(c: C): Relation<T & C>;
28
-
29
- // Binary ops - combine types
30
- join<R extends Tuple>(right: RelationOperand<R>, keys?: JoinKeys<T, R>): Relation<Joined<T, R>>;
31
- left_join<R extends Tuple>(right: RelationOperand<R>, keys?: JoinKeys<T, R>): Relation<LeftJoined<T, R>>;
32
- cross_product<R extends Tuple>(right: RelationOperand<R>): Relation<T & R>;
33
-
34
- // Nesting
35
- group<K extends keyof T, As extends string>(attrs: K[], as: As): Relation<Grouped<T, K, As>>;
36
- ungroup<K extends keyof T>(attr: K): Relation<Ungrouped<T, K>>;
37
- // ...
38
- }
39
- ```
40
-
41
- ### 2. Generic MemoryRelation Class (`src/Relation/Memory.ts`)
42
-
43
- ```typescript
44
- export class MemoryRelation<T extends Tuple = Tuple> implements Relation<T> {
45
- constructor(private tuples: T[]) {}
46
-
47
- project<K extends keyof T>(attrs: K[]): MemoryRelation<Pick<T, K>> {
48
- return project(this, attrs as string[]) as MemoryRelation<Pick<T, K>>;
49
- }
50
- // ...
51
- }
52
- ```
53
-
54
- ### 3. Generic Bmg Factory (`src/index.ts`)
55
-
56
- ```typescript
57
- export function Bmg<T extends Tuple>(tuples: T[]): MemoryRelation<T>;
58
- export function Bmg(tuples: Tuple[]): MemoryRelation<Tuple>;
59
- export function Bmg<T extends Tuple = Tuple>(tuples: T[]): MemoryRelation<T> {
60
- return new MemoryRelation<T>(tuples);
61
- }
62
- ```
63
-
64
- ### 4. Utility Types (`src/utility-types.ts` - new file)
65
-
66
- ```typescript
67
- // Rename: { name: 'fullName' } transforms { name, age } to { fullName, age }
68
- export type RenameMap<T> = { [K in keyof T]?: string };
69
- export type Renamed<T, R extends RenameMap<T>> = {
70
- [K in keyof T as K extends keyof R ? (R[K] extends string ? R[K] : K) : K]: T[K];
71
- };
72
-
73
- // Join: combine left & right, remove duplicate keys from right
74
- export type CommonKeys<L, R> = Extract<keyof L, keyof R>;
75
- export type Joined<L, R> = L & Omit<R, CommonKeys<L, R>>;
76
- export type LeftJoined<L, R> = L & Partial<Omit<R, CommonKeys<L, R>>>;
77
-
78
- // Group/Ungroup
79
- export type Grouped<T, K extends keyof T, As extends string> =
80
- Omit<T, K> & Record<As, Relation<Pick<T, K>>>;
81
- export type Ungrouped<T, K extends keyof T> =
82
- T[K] extends Relation<infer N> ? Omit<T, K> & N : never;
83
-
84
- // Wrap/Unwrap
85
- export type Wrapped<T, K extends keyof T, As extends string> =
86
- Omit<T, K> & Record<As, Pick<T, K>>;
87
- export type Unwrapped<T, K extends keyof T> =
88
- T[K] extends Record<string, unknown> ? Omit<T, K> & T[K] : never;
89
-
90
- // Prefix/Suffix (template literal types)
91
- export type Prefixed<T, P extends string, Ex extends keyof T = never> = {
92
- [K in keyof T as K extends Ex ? K : `${P}${K & string}`]: T[K];
93
- };
94
- ```
95
-
96
- ### 5. Typed Predicates (`src/types.ts`)
97
-
98
- ```typescript
99
- export type TypedPredicateFunc<T> = (t: T) => boolean;
100
- export type TypedPredicate<T> = TypedPredicateFunc<T> | Partial<T>;
101
- ```
102
-
103
- ## Implementation Phases
104
-
105
- ### Phase 1: Foundation
106
- 1. Add `Relation<T = Tuple>` interface with generics
107
- 2. Add `MemoryRelation<T = Tuple>` class
108
- 3. Update `Bmg<T>()` factory
109
- 4. Type these operators first (highest value):
110
- - `one()`, `toArray()` - return `T` / `T[]`
111
- - `project()` - `Pick<T, K>`
112
- - `allbut()` - `Omit<T, K>`
113
- - `restrict()`, `where()`, `exclude()` - preserve `T` with typed predicate
114
-
115
- ### Phase 2: Structure-Changing Operators
116
- - `extend()` - `T & E`
117
- - `constants()` - `T & C`
118
- - `rename()` - `Renamed<T, R>`
119
- - `prefix()`, `suffix()` - template literal types
120
-
121
- ### Phase 3: Binary Operations
122
- - `union()`, `minus()`, `intersect()` - require same `T`
123
- - `join()` - `Joined<T, R>`
124
- - `left_join()` - `LeftJoined<T, R>`
125
- - `cross_product()` - `T & R`
126
- - `matching()`, `not_matching()` - preserve `T`
127
-
128
- ### Phase 4: Nesting Operators
129
- - `group()` - `Grouped<T, K, As>`
130
- - `ungroup()` - `Ungrouped<T, K>`
131
- - `wrap()` - `Wrapped<T, K, As>`
132
- - `unwrap()` - `Unwrapped<T, K>`
133
- - `image()` - adds `Relation<...>` attribute
134
-
135
- ### Phase 5: Complex Operators
136
- - `summarize()` - infer result from aggregators
137
- - `transform()` - preserve structure, optional value type tracking
138
- - `autowrap()` - return `Relation<Tuple>` (dynamic, un-typeable)
139
-
140
- ## Backwards Compatibility
141
-
142
- - Default type parameter `= Tuple` ensures existing untyped code compiles
143
- - When `T = Tuple = Record<string, unknown>`, `keyof T = string`, so any string[] is accepted
144
- - No breaking changes to existing API
145
-
146
- ## Files to Modify
147
-
148
- | File | Changes |
149
- |------|---------|
150
- | `src/types.ts` | Add generics to `Relation`, typed predicates |
151
- | `src/utility-types.ts` (new) | Utility types: `Renamed`, `Joined`, `Grouped`, etc. |
152
- | `src/Relation/Memory.ts` | Add `<T>` parameter, update method signatures |
153
- | `src/index.ts` | Update `Bmg` factory with type inference |
154
- | `src/operators/_helpers.ts` | Generic `toOperationalOperand<T>` |
155
-
156
- ## Example Usage After Implementation
157
-
158
- ```typescript
159
- // Opt-in: provide type for full type safety
160
- interface Supplier {
161
- sid: string;
162
- name: string;
163
- status: number;
164
- city: string;
165
- }
166
-
167
- const suppliers = Bmg<Supplier>([
168
- { sid: 'S1', name: 'Smith', status: 20, city: 'London' },
169
- ]);
170
-
171
- // IDE autocomplete: suggests 'sid', 'name', 'status', 'city'
172
- const result = suppliers
173
- .project(['sid', 'name']) // Relation<{ sid: string; name: string }>
174
- .rename({ name: 'fullName' }) // Relation<{ sid: string; fullName: string }>
175
- .extend({ upper: t => t.fullName.toUpperCase() }); // t.fullName autocompletes
176
-
177
- // Compile error for invalid attributes
178
- suppliers.project(['invalid']); // Error: 'invalid' not in keyof Supplier
179
-
180
- // Untyped usage still works (backwards compatible)
181
- const r = Bmg([{ a: 1 }]); // Relation<Tuple>
182
- r.project(['a']); // Works, no validation
183
- ```
184
-
185
- ## Test Plan
186
-
187
- ### 1. Compile-Time Type Tests
188
-
189
- Create `tests/types/relation.test-d.ts` using vitest's type testing:
190
-
191
- ```typescript
192
- import { Bmg } from 'src';
193
- import { expectTypeOf } from 'vitest';
194
-
195
- interface Person { id: number; name: string; age: number }
196
-
197
- describe('Type Safety', () => {
198
- const people = Bmg<Person>([{ id: 1, name: 'Alice', age: 30 }]);
199
-
200
- it('project narrows type to Pick<T, K>', () => {
201
- const result = people.project(['id', 'name']);
202
- expectTypeOf(result.one()).toEqualTypeOf<{ id: number; name: string }>();
203
- });
204
-
205
- it('rejects invalid attribute names', () => {
206
- // @ts-expect-error - 'invalid' is not a key of Person
207
- people.project(['invalid']);
208
- });
209
-
210
- it('restrict preserves type with typed predicate', () => {
211
- const result = people.restrict(t => t.age > 25);
212
- expectTypeOf(result.one()).toEqualTypeOf<Person>();
213
- });
214
-
215
- it('predicate function receives typed tuple', () => {
216
- people.restrict(t => {
217
- expectTypeOf(t.id).toBeNumber();
218
- expectTypeOf(t.name).toBeString();
219
- return true;
220
- });
221
- });
222
-
223
- it('join combines types correctly', () => {
224
- interface Order { orderId: number; personId: number; total: number }
225
- const orders = Bmg<Order>([]);
226
- const joined = people.join(orders, { id: 'personId' });
227
- expectTypeOf(joined.one()).toEqualTypeOf<{
228
- id: number; name: string; age: number; orderId: number; total: number
229
- }>();
230
- });
231
-
232
- it('rename transforms keys', () => {
233
- const renamed = people.rename({ name: 'fullName' });
234
- expectTypeOf(renamed.one()).toEqualTypeOf<{ id: number; fullName: string; age: number }>();
235
- });
236
-
237
- it('untyped usage still works', () => {
238
- const r = Bmg([{ a: 1 }]); // Relation<Tuple>
239
- r.project(['a']); // Should compile
240
- r.project(['anything']); // Should compile (no validation when T=Tuple)
241
- });
242
- });
243
- ```
244
-
245
- ### 2. Runtime Tests
246
-
247
- All 152 existing tests must continue to pass without modification - validates backwards compatibility.
248
-
249
- ### 3. Type Test Coverage by Phase
250
-
251
- | Phase | Type Tests |
252
- |-------|-----------|
253
- | Phase 1 | `project`, `allbut`, `restrict`, `one`, `toArray` |
254
- | Phase 2 | `extend`, `constants`, `rename`, `prefix`, `suffix` |
255
- | Phase 3 | `join`, `left_join`, `cross_product`, `union`, `minus` |
256
- | Phase 4 | `group`, `ungroup`, `wrap`, `unwrap`, `image` |
257
- | Phase 5 | `summarize`, `transform`, `autowrap` (escape hatch) |
258
-
259
- ### 4. Test File Structure
260
-
261
- ```
262
- tests/
263
- types/
264
- relation.test-d.ts # Core Relation<T> type tests
265
- operators.test-d.ts # Operator type transformation tests
266
- utility-types.test-d.ts # Utility type unit tests
267
- ```
268
-
269
- ## Limitations
270
-
271
- - Function-based `rename()` loses type tracking (only object syntax typed)
272
- - `autowrap()` returns `Relation<Tuple>` (dynamic structure)
273
- - Requires TypeScript 4.1+ for template literal types
@@ -1,26 +0,0 @@
1
- name: Tests
2
-
3
- on:
4
- push:
5
- branches: ['*']
6
- pull_request:
7
- branches: ['*']
8
-
9
- jobs:
10
- test:
11
- runs-on: ubuntu-latest
12
-
13
- steps:
14
- - uses: actions/checkout@v4
15
-
16
- - uses: pnpm/action-setup@v4
17
-
18
- - uses: actions/setup-node@v4
19
- with:
20
- node-version: '20'
21
-
22
- - name: Install dependencies
23
- run: pnpm install
24
-
25
- - name: Run tests
26
- run: pnpm test
package/CLAUDE.md DELETED
@@ -1,48 +0,0 @@
1
- # What is Bmg.js ?
2
-
3
- A Typescript implementation of BMG, a relational algebra originally implemented
4
- in/for Ruby :
5
-
6
- - https://github.com/enspirit/bmg
7
- - https://www.relational-algebra.dev/
8
-
9
- The aim of bmg.js is NOT to implement the SQL compiler, but only to provide an
10
- implementation of main algebra operators to work on arrays of javascript objects.
11
-
12
- ## Development flow
13
-
14
- * List operators to support, by order of importance
15
- * Add each operator, in order, with unit tests.
16
- * One commit per operator, don't forget to adapt the README.
17
- * Tests MUST succeed at all times, run them with `npm run test`
18
-
19
- ## Theory
20
-
21
- IMPORTANT rules:
22
-
23
- 1. Relations NEVER have duplicates
24
- 2. Order of tuples and order or attributes in a tuple are not important semantically
25
- 3. Mathematically, relations are sets of tuples ; tuples are sets of (attr, value) pairs.
26
- 4. Two relations are equal of they have the exact same set of exact same tuples.
27
-
28
- ## About unit tests
29
-
30
- IMPORTANT rules:
31
-
32
- * Favor purely relational tests: compare an obtained relation with the expected relation
33
- using `isEqual`.
34
- * Do NEVER access the "first" tuple, since there is no such tuple.
35
- * Instead, use `r.restrict(...predicate...).one` with a predicate that selects the
36
- tuple you are interested in. `one` will correctly fail if your assumption is wrong.
37
-
38
- ## Implemented operators
39
-
40
- **Relational:** restrict, where, exclude, project, allbut, extend, rename, prefix, suffix, constants, union, minus, intersect, matching, not_matching, join, left_join, cross_product, cross_join, image, summarize, group, ungroup, wrap, unwrap, autowrap, transform
41
-
42
- **Non-relational:** one, yByX, toArray, isRelation, isEqual
43
-
44
- ## TODO
45
-
46
- - [ ] sort - Order tuples by attributes
47
- - [ ] page - Pagination (offset + limit)
48
- - [ ] size, empty, first, exists - Utility helpers
package/Makefile DELETED
@@ -1,2 +0,0 @@
1
- dist/bmg.cjs: src/**/*
2
- pnpm run build
package/example/README.md DELETED
@@ -1,22 +0,0 @@
1
- # Bmg.js Example
2
-
3
- Demonstrates relational algebra operations using the classic suppliers/parts/shipments dataset with full TypeScript type safety.
4
-
5
- ## Running the example
6
-
7
- ```bash
8
- npm install
9
- npm start
10
- ```
11
-
12
- ## What's covered
13
-
14
- - Basic operations: `restrict`, `project`, `rename`
15
- - Extending relations: `extend`, `constants`
16
- - Set operations: `union`, `minus`, `intersect`
17
- - Join operations: `join`, `left_join`, `matching`, `not_matching`
18
- - Aggregation: `summarize` with `count`, `sum`, `avg`, `min`, `max`
19
- - Grouping and nesting: `group`, `image`, `wrap`
20
- - Data transformation: `transform`, `allbut`
21
- - Extracting tuples: `one()`
22
- - Relation properties: `isEqual`, `Bmg.isRelation`