@goatlab/fluent 0.8.0 → 0.8.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.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @goatlab/fluent
2
2
 
3
- A TypeScript query builder and ORM wrapper that provides a fluent interface for multiple database types with built-in validation using Zod schemas.
3
+ A TypeScript query builder and ORM wrapper that provides a fluent interface for multiple database types. Built on TypeORM with Zod validation, it offers a unified query syntax across SQL and NoSQL databases with proper type preservation and nested object support.
4
4
 
5
5
  ## Installation
6
6
 
@@ -43,12 +43,12 @@ const UserSchema = z.object({
43
43
  created: z.date().optional()
44
44
  })
45
45
 
46
- // Create a repository
46
+ // Create a repository with DataSource or getter function
47
47
  class UserRepository extends TypeOrmConnector<User> {
48
- constructor(dataSource: DataSource) {
48
+ constructor(dataSource: DataSource | (() => DataSource)) {
49
49
  super({
50
50
  entity: User,
51
- dataSource,
51
+ dataSource, // Now supports both DataSource and getter functions
52
52
  inputSchema: UserSchema
53
53
  })
54
54
  }
@@ -56,6 +56,8 @@ class UserRepository extends TypeOrmConnector<User> {
56
56
 
57
57
  // Use the repository
58
58
  const userRepo = new UserRepository(dataSource)
59
+ // or with a getter function for lazy initialization
60
+ const userRepo = new UserRepository(() => getDataSource())
59
61
 
60
62
  // Insert data
61
63
  const user = await userRepo.insert({ name: 'John', age: 25 })
@@ -79,24 +81,153 @@ await userRepo.deleteById('user-id')
79
81
 
80
82
  ## Key Features
81
83
 
82
- - **Multi-database support** - Works with MySQL, PostgreSQL, MongoDB, SQLite, and more via TypeORM
83
- - **Fluent query interface** - Chainable query methods with TypeScript support
84
- - **Built-in validation** - Automatic input/output validation using Zod schemas
85
- - **Decorators** - Simple entity definition using decorators (`@f.entity`, `@f.property`, etc.)
86
- - **Type safety** - Full TypeScript support with proper type inference
87
- - **Relations** - Support for complex relationships between entities
88
- - **Pagination** - Built-in pagination support
89
- - **Raw queries** - Execute raw SQL when needed
84
+ - **Unified Query Interface** - Consistent API across MongoDB, MySQL, PostgreSQL, SQLite
85
+ - **TypeORM Integration** - Built on the proven TypeORM foundation
86
+ - **Fluent API** - Intuitive chainable query builder with full TypeScript support
87
+ - **Zod Validation** - Automatic input/output validation with Zod schemas
88
+ - **Decorator-based Models** - Simple entity definition using decorators
89
+ - **Type Safety** - Complete type inference and preservation, even in nested queries
90
+ - **Relations** - Full support for One-to-Many, Many-to-One, Many-to-Many relationships
91
+ - **Advanced Queries** - Complex conditions, aggregations, and raw SQL support
92
+ - **Lazy Initialization** - DataSource getter functions for flexible initialization
93
+ - **Extensible** - Base classes for building custom connectors
94
+
95
+ ## Advanced Features
96
+
97
+ ### Nested Object Queries with Dot Notation (MongoDB)
98
+
99
+ MongoDB now fully supports nested object queries using dot notation, with proper type preservation:
100
+
101
+ ```typescript
102
+ // Define nested entity structure
103
+ export class Address {
104
+ @f.property({ required: true, type: 'varchar' })
105
+ street: string
106
+
107
+ @f.property({ required: true, type: 'varchar' })
108
+ city: string
109
+
110
+ @f.property({ required: false, type: 'int' })
111
+ zipCode?: number
112
+ }
113
+
114
+ @f.entity('users')
115
+ export class User {
116
+ @f.id()
117
+ id: string
118
+
119
+ @f.property({ required: true, type: 'varchar' })
120
+ name: string
121
+
122
+ @f.embed(Address)
123
+ address?: Address
124
+ }
125
+
126
+ // Query nested fields with dot notation
127
+ const users = await userRepo.findMany({
128
+ where: {
129
+ 'address.city': 'New York',
130
+ 'address.zipCode': { $gte: 10000 }
131
+ }
132
+ })
133
+
134
+ // Nested queries preserve types correctly
135
+ const users = await userRepo.findMany({
136
+ where: {
137
+ 'profile.settings.notifications': true, // boolean preserved
138
+ 'profile.settings.maxItems': { $gte: 5 } // number preserved
139
+ }
140
+ })
141
+ ```
142
+
143
+ ### MongoDB-Specific Behaviors
144
+
145
+ MongoDB has some specific behaviors that are properly handled:
146
+
147
+ 1. **CreateDateColumn**: In MongoDB, `@f.created()` fields always use the current timestamp regardless of any provided value during insertion. This is a TypeORM MongoDB driver behavior.
148
+
149
+ ```typescript
150
+ // MongoDB will ignore the provided created date and use current timestamp
151
+ await repo.insert({
152
+ name: 'Test',
153
+ created: new Date('2020-01-01') // This will be ignored in MongoDB
154
+ })
155
+ ```
156
+
157
+ 2. **Optimized Simple Queries**: Simple queries without OR/AND operators now use a more efficient query structure in MongoDB.
158
+
159
+ ### Lazy DataSource Initialization
160
+
161
+ The TypeORM connector now supports DataSource getter functions, useful for scenarios where the DataSource might not be immediately available:
162
+
163
+ ```typescript
164
+ // Traditional approach
165
+ const repo = new UserRepository(dataSource)
166
+
167
+ // With getter function (lazy initialization)
168
+ const repo = new UserRepository(() => container.get(DataSource))
169
+
170
+ // The DataSource is only accessed when needed
171
+ ```
172
+
173
+ ### Type Preservation in Queries
174
+
175
+ The query builder now properly preserves types when flattening nested objects, ensuring that:
176
+ - Numbers remain numbers (not converted to strings)
177
+ - Booleans remain booleans
178
+ - Arrays are properly handled
179
+ - Dates are correctly processed
90
180
 
91
181
  ## Supported Databases
92
182
 
93
183
  All databases supported by TypeORM:
94
184
  - MySQL / MariaDB
95
185
  - PostgreSQL
96
- - MongoDB
186
+ - MongoDB (with full dot notation support)
97
187
  - SQLite
98
188
  - Microsoft SQL Server
99
189
  - Oracle
100
190
  - CockroachDB
101
191
  - SAP Hana
102
192
  - And more...
193
+
194
+ ## Testing
195
+
196
+ The package includes comprehensive test suites that run identically across all supported databases:
197
+
198
+ ```bash
199
+ # Run all tests
200
+ pnpm test
201
+
202
+ # Run specific database tests
203
+ pnpm test:sqlite
204
+ pnpm test:mysql
205
+ pnpm test:mongodb
206
+ pnpm test:postgresql
207
+
208
+ # Run all database tests concurrently
209
+ pnpm test:db:concurrent
210
+ ```
211
+
212
+ ## Migration from Previous Versions
213
+
214
+ ### Breaking Changes in v0.8.0
215
+
216
+ 1. **Test Framework**: Migrated from Jest to Vitest. Update your test configurations accordingly.
217
+
218
+ 2. **MongoDB Nested Queries**: The query builder now properly handles dot notation for nested objects. Review your MongoDB queries to ensure they use the correct syntax.
219
+
220
+ 3. **DataSource Parameter**: The `dataSource` parameter in TypeOrmConnector now accepts both `DataSource` and `() => DataSource` types.
221
+
222
+ ## Documentation
223
+
224
+ For comprehensive documentation, please see the [docs directory](../../docs/). Key sections include:
225
+
226
+ - [Getting Started Guide](../../docs/getting-started/first-steps.md)
227
+ - [Query Builder Reference](../../docs/query-builder/overview.md)
228
+ - [TypeORM Connector Guide](../../docs/connectors/typeorm.md)
229
+ - [API Reference](../../docs/api/fluent-api.md)
230
+
231
+ ## License
232
+
233
+ MIT