ai-database 2.0.2 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/dist/actions.d.ts +247 -0
  3. package/dist/actions.d.ts.map +1 -0
  4. package/dist/actions.js +260 -0
  5. package/dist/actions.js.map +1 -0
  6. package/dist/ai-promise-db.d.ts +34 -2
  7. package/dist/ai-promise-db.d.ts.map +1 -1
  8. package/dist/ai-promise-db.js +511 -66
  9. package/dist/ai-promise-db.js.map +1 -1
  10. package/dist/constants.d.ts +16 -0
  11. package/dist/constants.d.ts.map +1 -0
  12. package/dist/constants.js +16 -0
  13. package/dist/constants.js.map +1 -0
  14. package/dist/events.d.ts +153 -0
  15. package/dist/events.d.ts.map +1 -0
  16. package/dist/events.js +154 -0
  17. package/dist/events.js.map +1 -0
  18. package/dist/index.d.ts +8 -1
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +13 -1
  21. package/dist/index.js.map +1 -1
  22. package/dist/memory-provider.d.ts +144 -2
  23. package/dist/memory-provider.d.ts.map +1 -1
  24. package/dist/memory-provider.js +569 -13
  25. package/dist/memory-provider.js.map +1 -1
  26. package/dist/schema/cascade.d.ts +96 -0
  27. package/dist/schema/cascade.d.ts.map +1 -0
  28. package/dist/schema/cascade.js +528 -0
  29. package/dist/schema/cascade.js.map +1 -0
  30. package/dist/schema/index.d.ts +197 -0
  31. package/dist/schema/index.d.ts.map +1 -0
  32. package/dist/schema/index.js +1211 -0
  33. package/dist/schema/index.js.map +1 -0
  34. package/dist/schema/parse.d.ts +225 -0
  35. package/dist/schema/parse.d.ts.map +1 -0
  36. package/dist/schema/parse.js +732 -0
  37. package/dist/schema/parse.js.map +1 -0
  38. package/dist/schema/provider.d.ts +176 -0
  39. package/dist/schema/provider.d.ts.map +1 -0
  40. package/dist/schema/provider.js +258 -0
  41. package/dist/schema/provider.js.map +1 -0
  42. package/dist/schema/resolve.d.ts +87 -0
  43. package/dist/schema/resolve.d.ts.map +1 -0
  44. package/dist/schema/resolve.js +474 -0
  45. package/dist/schema/resolve.js.map +1 -0
  46. package/dist/schema/semantic.d.ts +53 -0
  47. package/dist/schema/semantic.d.ts.map +1 -0
  48. package/dist/schema/semantic.js +247 -0
  49. package/dist/schema/semantic.js.map +1 -0
  50. package/dist/schema/types.d.ts +528 -0
  51. package/dist/schema/types.d.ts.map +1 -0
  52. package/dist/schema/types.js +9 -0
  53. package/dist/schema/types.js.map +1 -0
  54. package/dist/schema.d.ts +24 -867
  55. package/dist/schema.d.ts.map +1 -1
  56. package/dist/schema.js +41 -1124
  57. package/dist/schema.js.map +1 -1
  58. package/dist/semantic.d.ts +175 -0
  59. package/dist/semantic.d.ts.map +1 -0
  60. package/dist/semantic.js +338 -0
  61. package/dist/semantic.js.map +1 -0
  62. package/dist/types.d.ts +14 -0
  63. package/dist/types.d.ts.map +1 -1
  64. package/dist/types.js.map +1 -1
  65. package/package.json +13 -4
  66. package/.turbo/turbo-build.log +0 -5
  67. package/TESTING.md +0 -410
  68. package/TEST_SUMMARY.md +0 -250
  69. package/TODO.md +0 -128
  70. package/src/ai-promise-db.ts +0 -1243
  71. package/src/authorization.ts +0 -1102
  72. package/src/durable-clickhouse.ts +0 -596
  73. package/src/durable-promise.ts +0 -582
  74. package/src/execution-queue.ts +0 -608
  75. package/src/index.test.ts +0 -868
  76. package/src/index.ts +0 -337
  77. package/src/linguistic.ts +0 -404
  78. package/src/memory-provider.test.ts +0 -1036
  79. package/src/memory-provider.ts +0 -1119
  80. package/src/schema.test.ts +0 -1254
  81. package/src/schema.ts +0 -2296
  82. package/src/tests.ts +0 -725
  83. package/src/types.ts +0 -1177
  84. package/test/README.md +0 -153
  85. package/test/edge-cases.test.ts +0 -646
  86. package/test/provider-resolution.test.ts +0 -402
  87. package/tsconfig.json +0 -9
  88. package/vitest.config.ts +0 -19
package/TEST_SUMMARY.md DELETED
@@ -1,250 +0,0 @@
1
- # ai-database Test Suite Summary
2
-
3
- ## Overview
4
-
5
- Comprehensive test suite for the ai-database package with **174 tests** across **5 test files**, all passing.
6
-
7
- ## Test Files
8
-
9
- ### 1. `src/schema.test.ts` (22 tests)
10
- **Purpose**: Schema parsing and bi-directional relationship validation
11
-
12
- **Coverage**:
13
- - Primitive field types (string, number, boolean, date, datetime, json, markdown, url)
14
- - Optional fields with `?` modifier
15
- - Array fields with `[]` and `[type]` syntax
16
- - Simple relations and bi-directional relationships
17
- - Automatic backref generation (e.g., `Post.author: 'Author.posts'` auto-creates `Author.posts`)
18
- - Complex multi-entity schemas
19
- - Self-referential relations
20
- - Edge cases (empty schemas, missing entities, etc.)
21
- - TypeScript type inference validation
22
-
23
- **Key Tests**:
24
- - ✅ Parses all primitive types correctly
25
- - ✅ Handles optional and array modifiers
26
- - ✅ Creates automatic backrefs for one-to-many relations
27
- - ✅ Creates automatic backrefs for many-to-many relations
28
- - ✅ Validates DB factory creates typed database
29
-
30
- ---
31
-
32
- ### 2. `src/memory-provider.test.ts` (40 tests)
33
- **Purpose**: In-memory database provider implementation
34
-
35
- **Coverage**:
36
- - **CRUD Operations**: Create, get, update, delete
37
- - **Querying**: List with filtering, sorting, pagination
38
- - **Search**: Full-text search with scoring and field filtering
39
- - **Relationships**: Create, query, and remove relations between entities
40
- - **Utility Methods**: `clear()` and `stats()`
41
- - **Concurrency**: Multiple simultaneous operations
42
- - **Special Cases**: UUID generation, timestamp tracking, error handling
43
-
44
- **Key Tests**:
45
- - ✅ Creates entities with auto-generated or explicit IDs
46
- - ✅ Lists with where clauses, ordering, limits, and offsets
47
- - ✅ Searches with relevance scoring and min score filtering
48
- - ✅ Creates and queries many-to-many relationships
49
- - ✅ Cleans up relations when entities are deleted
50
- - ✅ Tracks entity and relation counts
51
-
52
- ---
53
-
54
- ### 3. `src/index.test.ts` (31 tests)
55
- **Purpose**: Integration tests for the full DB API
56
-
57
- **Coverage**:
58
- - **DB Factory**: Creating typed database instances from schemas
59
- - **Entity Operations**: All CRUD methods on typed entities
60
- - **List & Query**: Filtering, sorting, pagination
61
- - **Search**: Entity-specific and global search
62
- - **Relationships**: Managing relations through both DB API and provider
63
- - **Global Methods**: `db.get()` by URL, `db.search()` across all types
64
- - **Type Safety**: Verifying TypeScript inference works correctly
65
- - **Complex Scenarios**: Multi-entity operations, self-referential relations
66
-
67
- **Key Tests**:
68
- - ✅ Creates, reads, updates, deletes entities via typed API
69
- - ✅ Upserts work for both create and update scenarios
70
- - ✅ Lists with various query options and pagination
71
- - ✅ Searches within entity type and globally
72
- - ✅ Iterates over entities with `forEach()`
73
- - ✅ Gets entities by URL (multiple formats supported)
74
- - ✅ Handles complex multi-entity schemas
75
-
76
- ---
77
-
78
- ### 4. `test/provider-resolution.test.ts` (38 tests)
79
- **Purpose**: DATABASE_URL parsing and provider resolution
80
-
81
- **Coverage**:
82
- - **URL Format Detection**: All supported DATABASE_URL formats
83
- - `:memory:` → In-memory provider
84
- - `./content` → Filesystem provider
85
- - `sqlite://./content` → Local SQLite provider
86
- - `libsql://db.turso.io` → Remote Turso provider
87
- - `chdb://./content` → Local ClickHouse (chDB)
88
- - `clickhouse://host:8123` → Remote ClickHouse
89
- - **Provider Selection**: Environment variable handling
90
- - **URL Parsing**: Extracting provider type, paths, and remote URLs
91
- - **Provider Initialization**: Memory provider setup and isolation
92
- - **Interface Compliance**: Ensuring all providers implement required methods
93
- - **Documentation Examples**: Verifying all README examples work
94
-
95
- **Key Tests**:
96
- - ✅ Detects all provider types from URL format
97
- - ✅ Generates correct .db folder paths for local providers
98
- - ✅ Handles query parameters and database names in URLs
99
- - ✅ Memory provider implements all required methods
100
- - ✅ Provider instances are properly isolated
101
- - ✅ All README URL examples are valid
102
-
103
- ---
104
-
105
- ### 5. `test/edge-cases.test.ts` (43 tests)
106
- **Purpose**: Edge cases, boundary conditions, and error scenarios
107
-
108
- **Coverage**:
109
- - **Empty/Minimal Schemas**: Handling edge case configurations
110
- - **Special Characters**: IDs and data with unicode, symbols, paths
111
- - **Large Data**: 100KB strings, 1000+ entities, bulk operations
112
- - **Concurrent Operations**: Parallel creates, updates, queries
113
- - **Optional Fields**: Missing, undefined, and null values
114
- - **Array Fields**: Empty arrays, array mutations
115
- - **URL Parsing**: Various URL formats, query params, hashes
116
- - **Search Edge Cases**: Empty queries, special regex chars, extreme scores
117
- - **Pagination**: Out-of-bounds offsets, zero/negative limits
118
- - **Type Coercion**: Handling different value types
119
-
120
- **Key Tests**:
121
- - ✅ Handles empty schemas and entities with no fields
122
- - ✅ Supports IDs with hyphens, underscores, dots, slashes, UUIDs
123
- - ✅ Stores unicode characters and emojis correctly
124
- - ✅ Handles 100KB strings and 1000+ entities
125
- - ✅ Concurrent operations don't interfere with each other
126
- - ✅ Optional fields can be set, unset, and omitted
127
- - ✅ Parses full HTTPS URLs, HTTP URLs, and type/id paths
128
- - ✅ Search handles empty strings and special regex characters
129
- - ✅ Pagination handles out-of-bounds offsets gracefully
130
-
131
- ---
132
-
133
- ## Test Execution
134
-
135
- ```bash
136
- # Run all tests
137
- pnpm test
138
-
139
- # Run tests once (non-watch mode)
140
- pnpm test run
141
-
142
- # Run specific test file
143
- pnpm test src/schema.test.ts
144
-
145
- # Build package (verifies no TypeScript errors)
146
- pnpm build
147
- ```
148
-
149
- ## Test Results
150
-
151
- ```
152
- ✓ src/index.test.ts (31 tests) 7ms
153
- ✓ test/edge-cases.test.ts (43 tests) 6ms
154
- ✓ src/memory-provider.test.ts (40 tests) 14ms
155
- ✓ src/schema.test.ts (22 tests) 2ms
156
- ✓ test/provider-resolution.test.ts (38 tests) 2ms
157
-
158
- Test Files 5 passed (5)
159
- Tests 174 passed (174)
160
- Duration 215ms
161
- ```
162
-
163
- ## Coverage Areas
164
-
165
- | Area | Test File | Tests | Status |
166
- |------|-----------|-------|--------|
167
- | Schema Parsing | `src/schema.test.ts` | 22 | ✅ 100% |
168
- | Memory Provider | `src/memory-provider.test.ts` | 40 | ✅ 100% |
169
- | Integration | `src/index.test.ts` | 31 | ✅ 100% |
170
- | Provider Resolution | `test/provider-resolution.test.ts` | 38 | ✅ 100% |
171
- | Edge Cases | `test/edge-cases.test.ts` | 43 | ✅ 100% |
172
- | **TOTAL** | | **174** | **✅ 100%** |
173
-
174
- ## Key Features Tested
175
-
176
- ### ✅ Schema-First Design
177
- - Declarative schema definitions
178
- - Automatic bi-directional relationships
179
- - TypeScript type inference from schema
180
- - Support for all primitive types
181
-
182
- ### ✅ CRUD Operations
183
- - Create with auto-generated or explicit IDs
184
- - Get by ID with null for not-found
185
- - Update with partial data merging
186
- - Upsert for create-or-update semantics
187
- - Delete with cascade cleanup of relations
188
-
189
- ### ✅ Querying & Search
190
- - List with where clauses
191
- - Sorting (ascending/descending)
192
- - Pagination (limit/offset)
193
- - Full-text search with scoring
194
- - Field-specific search
195
- - Global search across all entity types
196
-
197
- ### ✅ Relationships
198
- - One-to-many with automatic backrefs
199
- - Many-to-many with array syntax
200
- - Self-referential relations
201
- - Lazy-loaded relation traversal
202
- - Relation cleanup on entity deletion
203
-
204
- ### ✅ Provider System
205
- - In-memory provider (fully implemented)
206
- - Pluggable provider interface
207
- - DATABASE_URL parsing for all provider types
208
- - Graceful fallback to memory provider
209
- - Provider isolation for testing
210
-
211
- ### ✅ Type Safety
212
- - Full TypeScript inference
213
- - Typed entity operations
214
- - Type-safe query results
215
- - Compile-time schema validation
216
-
217
- ## Implementation Notes
218
-
219
- 1. **In-Memory Provider**: Fully functional with all operations implemented
220
- 2. **Dynamic Imports**: Other providers (`@mdxdb/fs`, `@mdxdb/sqlite`, `@mdxdb/clickhouse`) are dynamically imported with graceful fallback
221
- 3. **Test Isolation**: Each test suite uses fresh provider instances via `beforeEach`
222
- 4. **TypeScript**: All code compiles without errors, strict mode enabled
223
- 5. **Vitest**: Using vitest as test runner with fork pool for isolation
224
-
225
- ## Future Test Additions
226
-
227
- Potential areas for expansion once additional providers are implemented:
228
-
229
- 1. **Filesystem Provider Tests**: When `@mdxdb/fs` is implemented
230
- 2. **SQLite Provider Tests**: When `@mdxdb/sqlite` is implemented
231
- 3. **ClickHouse Provider Tests**: When `@mdxdb/clickhouse` is implemented
232
- 4. **Cross-Provider Tests**: Verifying behavior consistency
233
- 5. **Performance Benchmarks**: Large dataset tests with timing
234
- 6. **Migration Tests**: Schema evolution and data migration
235
- 7. **Transaction Tests**: If/when transaction support is added
236
- 8. **Validation Tests**: Runtime schema validation
237
- 9. **Index Tests**: Query optimization with indexes
238
-
239
- ## Conclusion
240
-
241
- The ai-database package has **comprehensive test coverage** with **174 passing tests** covering:
242
- - ✅ All core functionality
243
- - ✅ Schema parsing and validation
244
- - ✅ Complete CRUD operations
245
- - ✅ Advanced querying and search
246
- - ✅ Relationship management
247
- - ✅ Provider resolution and initialization
248
- - ✅ Edge cases and error scenarios
249
-
250
- The test suite provides a **solid foundation** for the package and ensures reliability as new providers and features are added.
package/TODO.md DELETED
@@ -1,128 +0,0 @@
1
- # ai-database TODO
2
-
3
- ## Core Features
4
-
5
- ### Natural Language Queries
6
- - [x] Define `NLQueryResult`, `NLQueryFn`, `NLQueryContext`, `NLQueryPlan` types
7
- - [x] Implement `setNLQueryGenerator()` for custom AI integration
8
- - [x] Implement `buildNLQueryContext()` to extract schema metadata
9
- - [x] Implement `executeNLQuery()` with fallback to search
10
- - [x] Add tagged template support for `db\`query\`` syntax
11
- - [ ] Wire up `db.ask` and type-specific NL queries in DB factory
12
- - [ ] Add streaming support for NL query results
13
- - [ ] Add query caching/memoization
14
- - [ ] Add query history/suggestions
15
-
16
- ### Self-Describing Schema
17
- - [x] Define `ThingSchema`, `NounSchema`, `VerbSchema`, `EdgeSchema`
18
- - [x] Define `SystemSchema` combining all system types
19
- - [ ] Auto-populate Noun records when DB() is called
20
- - [ ] Auto-populate Edge records when relationships are defined
21
- - [ ] Auto-populate Verb records for standard actions
22
- - [ ] Add `db.Noun`, `db.Verb`, `db.Edge` accessors
23
- - [ ] Implement `Thing.type` -> `Noun` relationship
24
- - [ ] Implement `Noun.things` -> all instances backref
25
-
26
- ### Noun & Verb Types
27
- - [x] Define `Noun`, `NounProperty`, `NounRelationship` interfaces
28
- - [x] Define `Verb` interface with all conjugation forms
29
- - [x] Implement `defineNoun()` and `defineVerb()` helpers
30
- - [x] Implement `nounToSchema()` converter
31
- - [x] Define standard `Verbs` constant (create, update, delete, publish, archive)
32
-
33
- ### AI-Powered Linguistic Inference
34
- - [x] Basic `conjugate()`, `pluralize()`, `singularize()` with common rules
35
- - [x] Basic `inferNoun()` from type name
36
- - [ ] **Use AI for unknown nouns/verbs** - if not in Things table, ask AI once and cache
37
- - [ ] Remove brittle rule-based code in favor of AI inference
38
- - [ ] Store AI-generated forms in Noun/Verb records for future lookups
39
- - [ ] Add confidence scores to AI-generated linguistic forms
40
-
41
- ### TypeMeta
42
- - [x] Define `TypeMeta` interface
43
- - [x] Implement `Type()` accessor function
44
- - [x] Include slug/slugPlural for URL generation
45
- - [x] Include event type names (created, updated, deleted)
46
- - [x] Include verb-derived fields (createdAt, createdBy, etc.)
47
-
48
- ## Provider Implementation
49
-
50
- ### MemoryProvider
51
- - [x] Implement `Semaphore` for concurrency control
52
- - [x] Implement Event storage and emission
53
- - [x] Implement Action lifecycle management
54
- - [x] Implement Artifact storage with invalidation
55
- - [x] Add pattern matching for event subscriptions
56
- - [ ] Add persistence option (save/load to JSON)
57
- - [ ] Add TTL support for artifacts
58
-
59
- > **Note**: Production providers (SQLite, ClickHouse, Postgres, etc.) are implemented in `@mdxdb/*` packages.
60
-
61
- ## Query Capabilities
62
-
63
- ### Filtering
64
- - [x] Document SQL-style operators ($gt, $lt, $in, etc.)
65
- - [x] Document Document-style nested queries
66
- - [x] Document Graph-style relationship traversal
67
- - [ ] Implement all documented operators in MemoryProvider
68
- - [ ] Add query validation
69
- - [ ] Add query optimization hints
70
-
71
- ### Search
72
- - [x] Define SearchOptions interface
73
- - [ ] Implement hybrid search (vector + BM25)
74
- - [ ] Add embedding generation
75
- - [ ] Add chunking for long content
76
- - [ ] Add re-ranking
77
-
78
- ## Testing
79
-
80
- ### Unit Tests
81
- - [x] Schema parsing tests
82
- - [x] Noun/Verb type tests
83
- - [x] Basic conjugate/pluralize/singularize tests
84
- - [x] TypeMeta tests
85
- - [x] Semaphore tests
86
- - [x] Event emission tests
87
- - [x] Action lifecycle tests
88
- - [x] Artifact storage tests
89
- - [x] System schema tests
90
- - [x] Edge creation tests
91
- - [ ] NL query execution tests
92
- - [ ] AI linguistic inference tests
93
-
94
- ### Integration Tests
95
- - [ ] Full CRUD workflow
96
- - [ ] Relationship traversal
97
- - [ ] Event-driven workflows
98
- - [ ] Search accuracy benchmarks
99
-
100
- ## Documentation
101
-
102
- - [x] README: Core primitives
103
- - [x] README: Actions for durable execution
104
- - [x] README: Events for reactivity
105
- - [x] README: Artifacts for caching
106
- - [x] README: Query styles (SQL, Document, Graph)
107
- - [x] README: Natural language queries
108
- - [x] README: Self-describing schema
109
- - [x] README: Noun & Verb types
110
- - [x] README: AI auto-generation
111
- - [ ] API reference with examples
112
- - [ ] Migration guide
113
- - [ ] Performance tuning guide
114
-
115
- ## Future Ideas
116
-
117
- - [ ] Schema migrations
118
- - [ ] Real-time subscriptions (WebSocket)
119
- - [ ] Offline-first with sync
120
- - [ ] Multi-tenancy support
121
- - [ ] Row-level security
122
- - [ ] Audit logging
123
- - [ ] Data lineage tracking
124
- - [ ] Schema versioning
125
- - [ ] Import/export (JSON, CSV, Parquet)
126
- - [ ] GraphQL API generation
127
- - [ ] REST API generation
128
- - [ ] Admin UI