linkgress-orm 0.0.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 (147) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +196 -0
  3. package/dist/database/database-client.interface.d.ts +45 -0
  4. package/dist/database/database-client.interface.d.ts.map +1 -0
  5. package/dist/database/database-client.interface.js +20 -0
  6. package/dist/database/database-client.interface.js.map +1 -0
  7. package/dist/database/index.d.ts +5 -0
  8. package/dist/database/index.d.ts.map +1 -0
  9. package/dist/database/index.js +10 -0
  10. package/dist/database/index.js.map +1 -0
  11. package/dist/database/pg-client.d.ts +30 -0
  12. package/dist/database/pg-client.d.ts.map +1 -0
  13. package/dist/database/pg-client.js +76 -0
  14. package/dist/database/pg-client.js.map +1 -0
  15. package/dist/database/postgres-client.d.ts +44 -0
  16. package/dist/database/postgres-client.d.ts.map +1 -0
  17. package/dist/database/postgres-client.js +111 -0
  18. package/dist/database/postgres-client.js.map +1 -0
  19. package/dist/database/types.d.ts +200 -0
  20. package/dist/database/types.d.ts.map +1 -0
  21. package/dist/database/types.js +8 -0
  22. package/dist/database/types.js.map +1 -0
  23. package/dist/entity/base-entity.d.ts +21 -0
  24. package/dist/entity/base-entity.d.ts.map +1 -0
  25. package/dist/entity/base-entity.js +27 -0
  26. package/dist/entity/base-entity.js.map +1 -0
  27. package/dist/entity/db-column.d.ts +61 -0
  28. package/dist/entity/db-column.d.ts.map +1 -0
  29. package/dist/entity/db-column.js +35 -0
  30. package/dist/entity/db-column.js.map +1 -0
  31. package/dist/entity/db-context.d.ts +665 -0
  32. package/dist/entity/db-context.d.ts.map +1 -0
  33. package/dist/entity/db-context.js +1463 -0
  34. package/dist/entity/db-context.js.map +1 -0
  35. package/dist/entity/entity-base.d.ts +76 -0
  36. package/dist/entity/entity-base.d.ts.map +1 -0
  37. package/dist/entity/entity-base.js +42 -0
  38. package/dist/entity/entity-base.js.map +1 -0
  39. package/dist/entity/entity-builder.d.ts +171 -0
  40. package/dist/entity/entity-builder.d.ts.map +1 -0
  41. package/dist/entity/entity-builder.js +376 -0
  42. package/dist/entity/entity-builder.js.map +1 -0
  43. package/dist/entity/model-config.d.ts +18 -0
  44. package/dist/entity/model-config.d.ts.map +1 -0
  45. package/dist/entity/model-config.js +157 -0
  46. package/dist/entity/model-config.js.map +1 -0
  47. package/dist/index.d.ts +27 -0
  48. package/dist/index.d.ts.map +1 -0
  49. package/dist/index.js +142 -0
  50. package/dist/index.js.map +1 -0
  51. package/dist/migration/db-schema-manager.d.ts +228 -0
  52. package/dist/migration/db-schema-manager.d.ts.map +1 -0
  53. package/dist/migration/db-schema-manager.js +1055 -0
  54. package/dist/migration/db-schema-manager.js.map +1 -0
  55. package/dist/migration/enum-migrator.d.ts +29 -0
  56. package/dist/migration/enum-migrator.d.ts.map +1 -0
  57. package/dist/migration/enum-migrator.js +137 -0
  58. package/dist/migration/enum-migrator.js.map +1 -0
  59. package/dist/query/collection-strategy.factory.d.ts +16 -0
  60. package/dist/query/collection-strategy.factory.d.ts.map +1 -0
  61. package/dist/query/collection-strategy.factory.js +37 -0
  62. package/dist/query/collection-strategy.factory.js.map +1 -0
  63. package/dist/query/collection-strategy.interface.d.ts +146 -0
  64. package/dist/query/collection-strategy.interface.d.ts.map +1 -0
  65. package/dist/query/collection-strategy.interface.js +3 -0
  66. package/dist/query/collection-strategy.interface.js.map +1 -0
  67. package/dist/query/conditions.d.ts +222 -0
  68. package/dist/query/conditions.d.ts.map +1 -0
  69. package/dist/query/conditions.js +446 -0
  70. package/dist/query/conditions.js.map +1 -0
  71. package/dist/query/cte-builder.d.ts +95 -0
  72. package/dist/query/cte-builder.d.ts.map +1 -0
  73. package/dist/query/cte-builder.js +172 -0
  74. package/dist/query/cte-builder.js.map +1 -0
  75. package/dist/query/grouped-query.d.ts +186 -0
  76. package/dist/query/grouped-query.d.ts.map +1 -0
  77. package/dist/query/grouped-query.js +588 -0
  78. package/dist/query/grouped-query.js.map +1 -0
  79. package/dist/query/join-builder.d.ts +106 -0
  80. package/dist/query/join-builder.d.ts.map +1 -0
  81. package/dist/query/join-builder.js +275 -0
  82. package/dist/query/join-builder.js.map +1 -0
  83. package/dist/query/query-builder.d.ts +543 -0
  84. package/dist/query/query-builder.d.ts.map +1 -0
  85. package/dist/query/query-builder.js +2649 -0
  86. package/dist/query/query-builder.js.map +1 -0
  87. package/dist/query/strategies/jsonb-collection-strategy.d.ts +51 -0
  88. package/dist/query/strategies/jsonb-collection-strategy.d.ts.map +1 -0
  89. package/dist/query/strategies/jsonb-collection-strategy.js +210 -0
  90. package/dist/query/strategies/jsonb-collection-strategy.js.map +1 -0
  91. package/dist/query/strategies/temptable-collection-strategy.d.ts +95 -0
  92. package/dist/query/strategies/temptable-collection-strategy.d.ts.map +1 -0
  93. package/dist/query/strategies/temptable-collection-strategy.js +456 -0
  94. package/dist/query/strategies/temptable-collection-strategy.js.map +1 -0
  95. package/dist/query/subquery.d.ts +152 -0
  96. package/dist/query/subquery.d.ts.map +1 -0
  97. package/dist/query/subquery.js +206 -0
  98. package/dist/query/subquery.js.map +1 -0
  99. package/dist/schema/column-builder.d.ts +127 -0
  100. package/dist/schema/column-builder.d.ts.map +1 -0
  101. package/dist/schema/column-builder.js +184 -0
  102. package/dist/schema/column-builder.js.map +1 -0
  103. package/dist/schema/inference.d.ts +26 -0
  104. package/dist/schema/inference.d.ts.map +1 -0
  105. package/dist/schema/inference.js +3 -0
  106. package/dist/schema/inference.js.map +1 -0
  107. package/dist/schema/navigation.d.ts +215 -0
  108. package/dist/schema/navigation.d.ts.map +1 -0
  109. package/dist/schema/navigation.js +233 -0
  110. package/dist/schema/navigation.js.map +1 -0
  111. package/dist/schema/row-type.d.ts +26 -0
  112. package/dist/schema/row-type.d.ts.map +1 -0
  113. package/dist/schema/row-type.js +3 -0
  114. package/dist/schema/row-type.js.map +1 -0
  115. package/dist/schema/sequence-builder.d.ts +87 -0
  116. package/dist/schema/sequence-builder.d.ts.map +1 -0
  117. package/dist/schema/sequence-builder.js +123 -0
  118. package/dist/schema/sequence-builder.js.map +1 -0
  119. package/dist/schema/table-builder.d.ts +122 -0
  120. package/dist/schema/table-builder.d.ts.map +1 -0
  121. package/dist/schema/table-builder.js +132 -0
  122. package/dist/schema/table-builder.js.map +1 -0
  123. package/dist/schema/typed-schema.d.ts +22 -0
  124. package/dist/schema/typed-schema.d.ts.map +1 -0
  125. package/dist/schema/typed-schema.js +28 -0
  126. package/dist/schema/typed-schema.js.map +1 -0
  127. package/dist/types/column-types.d.ts +20 -0
  128. package/dist/types/column-types.d.ts.map +1 -0
  129. package/dist/types/column-types.js +14 -0
  130. package/dist/types/column-types.js.map +1 -0
  131. package/dist/types/custom-types.d.ts +85 -0
  132. package/dist/types/custom-types.d.ts.map +1 -0
  133. package/dist/types/custom-types.js +132 -0
  134. package/dist/types/custom-types.js.map +1 -0
  135. package/dist/types/enum-builder.d.ts +31 -0
  136. package/dist/types/enum-builder.d.ts.map +1 -0
  137. package/dist/types/enum-builder.js +46 -0
  138. package/dist/types/enum-builder.js.map +1 -0
  139. package/dist/types/metadata.d.ts +67 -0
  140. package/dist/types/metadata.d.ts.map +1 -0
  141. package/dist/types/metadata.js +57 -0
  142. package/dist/types/metadata.js.map +1 -0
  143. package/dist/types/type-mapper.d.ts +49 -0
  144. package/dist/types/type-mapper.d.ts.map +1 -0
  145. package/dist/types/type-mapper.js +49 -0
  146. package/dist/types/type-mapper.js.map +1 -0
  147. package/package.json +77 -0
@@ -0,0 +1,665 @@
1
+ import { DatabaseClient, QueryResult } from '../database/database-client.interface';
2
+ import { TableBuilder, TableSchema, InferTableType } from '../schema/table-builder';
3
+ import { UnwrapDbColumns, InsertData, ExtractDbColumns } from './db-column';
4
+ import { DbEntity, EntityConstructor } from './entity-base';
5
+ import { DbModelConfig } from './model-config';
6
+ import { Condition, SqlFragment } from '../query/conditions';
7
+ import { ResolveCollectionResults, SelectQueryBuilder, QueryBuilder } from '../query/query-builder';
8
+ import { InferRowType } from '../schema/row-type';
9
+ import { DbSchemaManager } from '../migration/db-schema-manager';
10
+ import { DbSequence, SequenceConfig } from '../schema/sequence-builder';
11
+ /**
12
+ * Collection aggregation strategy type
13
+ */
14
+ export type CollectionStrategyType = 'jsonb' | 'temptable';
15
+ /**
16
+ * Query execution options
17
+ */
18
+ export interface QueryOptions {
19
+ /** Enable SQL query logging */
20
+ logQueries?: boolean;
21
+ /** Custom logger function (defaults to console.log) */
22
+ logger?: (message: string) => void;
23
+ /** Log query execution time */
24
+ logExecutionTime?: boolean;
25
+ /** Log query parameters */
26
+ logParameters?: boolean;
27
+ /** Collection aggregation strategy (default: 'jsonb') */
28
+ collectionStrategy?: CollectionStrategyType;
29
+ }
30
+ /**
31
+ * @deprecated Use QueryOptions instead
32
+ */
33
+ export type LoggingOptions = QueryOptions;
34
+ /**
35
+ * Query executor with optional logging
36
+ */
37
+ export declare class QueryExecutor {
38
+ private client;
39
+ private options;
40
+ constructor(client: DatabaseClient, options?: QueryOptions);
41
+ query(sql: string, params?: any[]): Promise<QueryResult>;
42
+ /**
43
+ * Execute a multi-statement query using the simple protocol (no parameters)
44
+ * Only available for clients that support it (e.g., PostgresClient)
45
+ */
46
+ querySimple(sql: string): Promise<QueryResult>;
47
+ /**
48
+ * Execute a multi-statement query and return ALL result sets
49
+ * Only available for PostgresClient
50
+ */
51
+ querySimpleMulti(sql: string): Promise<QueryResult[]>;
52
+ }
53
+ /**
54
+ * Conflict target for upsert operations
55
+ */
56
+ export interface ConflictTarget {
57
+ columns?: string[];
58
+ constraint?: string;
59
+ }
60
+ /**
61
+ * Insert configuration for bulk operations
62
+ */
63
+ export interface InsertConfig {
64
+ /**
65
+ * Size of insert chunk. If not provided, auto-detected based on max PG query parameters limit
66
+ */
67
+ chunkSize?: number;
68
+ /**
69
+ * Use OVERRIDING SYSTEM VALUE to allow inserting into identity/serial columns
70
+ */
71
+ overridingSystemValue?: boolean;
72
+ }
73
+ /**
74
+ * Upsert configuration
75
+ */
76
+ export interface UpsertConfig {
77
+ /**
78
+ * Size of insert chunk for bulk upserts
79
+ */
80
+ chunkSize?: number;
81
+ /**
82
+ * Primary key columns for conflict detection. If not specified, table's primary keys are used
83
+ */
84
+ primaryKey?: string | string[];
85
+ /**
86
+ * Use OVERRIDING SYSTEM VALUE (auto-detected if not specified)
87
+ */
88
+ overridingSystemValue?: boolean;
89
+ /**
90
+ * WHERE clause for the conflict target
91
+ */
92
+ targetWhere?: string;
93
+ /**
94
+ * WHERE clause for the UPDATE SET
95
+ */
96
+ setWhere?: string;
97
+ /**
98
+ * Reference item to detect columns. If not specified, first value from array is used
99
+ */
100
+ referenceItem?: any;
101
+ /**
102
+ * List of column names that should be updated on conflict. If not specified, all non-PK columns are updated
103
+ */
104
+ updateColumns?: string[];
105
+ /**
106
+ * Filter function to determine if column should be updated on conflict
107
+ */
108
+ updateColumnFilter?: (columnName: string) => boolean;
109
+ }
110
+ /**
111
+ * Insert builder for upsert operations
112
+ */
113
+ export declare class InsertBuilder<TSchema extends TableSchema> {
114
+ private schema;
115
+ private client;
116
+ private executor?;
117
+ private dataArray;
118
+ private conflictTarget?;
119
+ private conflictAction;
120
+ private updateColumns?;
121
+ private updateColumnFilter?;
122
+ private targetWhereClause?;
123
+ private setWhereClause?;
124
+ private overridingSystemValue;
125
+ constructor(schema: TSchema, client: DatabaseClient, executor?: QueryExecutor | undefined);
126
+ /**
127
+ * Get qualified table name with schema prefix if specified
128
+ */
129
+ private getQualifiedTableName;
130
+ /**
131
+ * Set the values to insert (single row or multiple rows)
132
+ */
133
+ values(data: Partial<InferTableType<TSchema>> | Partial<InferTableType<TSchema>>[]): this;
134
+ /**
135
+ * Specify conflict target (columns or constraint name)
136
+ */
137
+ onConflict(target?: ConflictTarget | string[]): this;
138
+ /**
139
+ * Do nothing on conflict
140
+ */
141
+ doNothing(): this;
142
+ /**
143
+ * Update on conflict (upsert)
144
+ */
145
+ doUpdate(options?: {
146
+ set?: Partial<InferTableType<TSchema>>;
147
+ where?: string;
148
+ updateColumns?: string[];
149
+ updateColumnFilter?: (columnName: string) => boolean;
150
+ }): this;
151
+ /**
152
+ * Set target WHERE clause for ON CONFLICT
153
+ */
154
+ targetWhere(where: string): this;
155
+ /**
156
+ * Enable OVERRIDING SYSTEM VALUE
157
+ */
158
+ setOverridingSystemValue(value?: boolean): this;
159
+ /**
160
+ * Execute the insert/upsert
161
+ */
162
+ execute(): Promise<InferTableType<TSchema>[]>;
163
+ }
164
+ /**
165
+ * Table accessor with query methods
166
+ */
167
+ export declare class TableAccessor<TBuilder extends TableBuilder<any>> {
168
+ private tableBuilder;
169
+ private client;
170
+ private schemaRegistry;
171
+ private executor?;
172
+ private collectionStrategy?;
173
+ private schema;
174
+ constructor(tableBuilder: TBuilder, client: DatabaseClient, schemaRegistry: Map<string, TableSchema>, executor?: QueryExecutor | undefined, collectionStrategy?: CollectionStrategyType | undefined);
175
+ /**
176
+ * Configure query options for the current query chain
177
+ * Returns a new TableAccessor instance with the specified options
178
+ *
179
+ * @example
180
+ * ```typescript
181
+ * const results = await db.users
182
+ * .withQueryOptions({ logQueries: true, collectionStrategy: 'temptable' })
183
+ * .select(u => ({ id: u.id, name: u.username }))
184
+ * .toList();
185
+ * ```
186
+ */
187
+ withQueryOptions(options: QueryOptions): TableAccessor<TBuilder>;
188
+ /**
189
+ * Start a select query with automatic type inference
190
+ */
191
+ select<TSelection>(selector: (row: InferRowType<TBuilder>) => TSelection): SelectQueryBuilder<TSelection>;
192
+ /**
193
+ * Add WHERE condition before select
194
+ */
195
+ where(condition: (row: InferRowType<TBuilder>) => Condition): QueryBuilder<TableSchema, InferRowType<TBuilder>>;
196
+ /**
197
+ * Left join with another table and selector
198
+ */
199
+ leftJoin<TRight, TSelection>(rightTable: {
200
+ _getSchema: () => TableSchema;
201
+ } | import('../query/subquery').Subquery<TRight, 'table'>, condition: (left: InferRowType<TBuilder>, right: TRight) => Condition, selector: (left: InferRowType<TBuilder>, right: TRight) => TSelection, alias?: string): SelectQueryBuilder<TSelection>;
202
+ /**
203
+ * Inner join with another table or subquery and selector
204
+ */
205
+ innerJoin<TRight, TSelection>(rightTable: {
206
+ _getSchema: () => TableSchema;
207
+ } | import('../query/subquery').Subquery<TRight, 'table'>, condition: (left: InferRowType<TBuilder>, right: TRight) => Condition, selector: (left: InferRowType<TBuilder>, right: TRight) => TSelection, alias?: string): SelectQueryBuilder<TSelection>;
208
+ /**
209
+ * Get table schema (internal use for joins)
210
+ */
211
+ _getSchema(): TableSchema;
212
+ /**
213
+ * Get table schema
214
+ */
215
+ getSchema(): TableSchema;
216
+ /**
217
+ * Get table name
218
+ */
219
+ getTableName(): string;
220
+ /**
221
+ * Get qualified table name with schema prefix if specified
222
+ */
223
+ private getQualifiedTableName;
224
+ /**
225
+ * Insert a row
226
+ */
227
+ insert(data: Partial<InferTableType<TableSchema>>): Promise<InferTableType<TableSchema>>;
228
+ /**
229
+ * Bulk insert with advanced configuration
230
+ */
231
+ insertBulk(value: Partial<InferTableType<TableSchema>> | Partial<InferTableType<TableSchema>>[], insertConfig?: InsertConfig): Promise<InferTableType<TableSchema>[]>;
232
+ /**
233
+ * Insert a single chunk (internal method)
234
+ */
235
+ private insertBulkSingle;
236
+ /**
237
+ * Upsert with advanced configuration
238
+ */
239
+ upsertBulk(values: Partial<InferTableType<TableSchema>>[], config?: UpsertConfig): Promise<InferTableType<TableSchema>[]>;
240
+ /**
241
+ * Upsert a single chunk (internal method)
242
+ */
243
+ private upsertBulkSingle;
244
+ /**
245
+ * Bulk insert multiple rows (simple version, kept for compatibility)
246
+ */
247
+ insertMany(dataArray: Partial<InferTableType<TableSchema>>[]): Promise<InferTableType<TableSchema>[]>;
248
+ /**
249
+ * Insert with conflict resolution (upsert)
250
+ */
251
+ onConflictDoNothing(): InsertBuilder<TableSchema>;
252
+ /**
253
+ * Insert with conflict resolution (upsert) - start building the upsert query
254
+ */
255
+ values(data: Partial<InferTableType<TableSchema>> | Partial<InferTableType<TableSchema>>[]): InsertBuilder<TableSchema>;
256
+ /**
257
+ * Update rows
258
+ */
259
+ update(id: any, data: Partial<InferTableType<TableSchema>>): Promise<InferTableType<TableSchema> | null>;
260
+ /**
261
+ * Delete a row by id
262
+ */
263
+ delete(id: any): Promise<boolean>;
264
+ }
265
+ /**
266
+ * Schema definition for DataContext
267
+ */
268
+ export type ContextSchema = {
269
+ [tableName: string]: TableBuilder<any>;
270
+ };
271
+ /**
272
+ * Infer table accessor types from schema with proper relation types
273
+ */
274
+ export type InferContextSchema<T extends ContextSchema> = {
275
+ [K in keyof T]: T[K] extends TableBuilder<any> ? TableAccessor<T[K]> : never;
276
+ };
277
+ /**
278
+ * DataContext - main entry point for database operations
279
+ */
280
+ export declare class DataContext<TSchema extends ContextSchema = any> {
281
+ protected client: DatabaseClient;
282
+ private schemaRegistry;
283
+ private tableAccessors;
284
+ private executor?;
285
+ private queryOptions?;
286
+ constructor(client: DatabaseClient, schema: TSchema, queryOptions?: QueryOptions);
287
+ /**
288
+ * Initialize schema and create table accessors
289
+ */
290
+ private initializeSchema;
291
+ /**
292
+ * Get table accessor by name
293
+ */
294
+ getTable<K extends keyof TSchema>(name: K): InferContextSchema<TSchema>[K];
295
+ /**
296
+ * Execute raw SQL
297
+ */
298
+ query(sql: string, params?: any[]): Promise<any>;
299
+ /**
300
+ * Execute in transaction
301
+ */
302
+ transaction<TResult>(fn: (ctx: this) => Promise<TResult>): Promise<TResult>;
303
+ /**
304
+ * Get schema manager for create/drop operations and automatic migrations
305
+ */
306
+ getSchemaManager(): DbSchemaManager;
307
+ /**
308
+ * Close database connection
309
+ */
310
+ dispose(): Promise<void>;
311
+ }
312
+ /**
313
+ * Typed upsert configuration for entities
314
+ */
315
+ export type EntityUpsertConfig<TEntity extends DbEntity> = {
316
+ /**
317
+ * Size of insert chunk for bulk upserts
318
+ */
319
+ chunkSize?: number;
320
+ /**
321
+ * Primary key columns for conflict detection. If not specified, table's primary keys are used
322
+ * Can be specified as property names (strings) or using lambda selectors
323
+ */
324
+ primaryKey?: keyof ExtractDbColumns<TEntity> | (keyof ExtractDbColumns<TEntity>)[] | ((entity: TEntity) => any);
325
+ /**
326
+ * Use OVERRIDING SYSTEM VALUE (auto-detected if not specified)
327
+ */
328
+ overridingSystemValue?: boolean;
329
+ /**
330
+ * WHERE clause for the conflict target
331
+ */
332
+ targetWhere?: string;
333
+ /**
334
+ * WHERE clause for the UPDATE SET
335
+ */
336
+ setWhere?: string;
337
+ /**
338
+ * Reference item to detect columns. If not specified, first value from array is used
339
+ */
340
+ referenceItem?: any;
341
+ /**
342
+ * List of columns that should be updated on conflict. Can be property names or lambda selectors
343
+ */
344
+ updateColumns?: (keyof ExtractDbColumns<TEntity>)[] | ((entity: TEntity) => Partial<ExtractDbColumns<TEntity>>);
345
+ /**
346
+ * Filter function to determine if column should be updated on conflict
347
+ */
348
+ updateColumnFilter?: (columnName: string) => boolean;
349
+ };
350
+ /**
351
+ * Type helper to build entity query type with navigation support
352
+ */
353
+ export type EntityQuery<TEntity extends DbEntity> = {
354
+ [K in keyof TEntity]: TEntity[K] extends (infer U)[] | undefined ? U extends DbEntity ? EntityCollectionQuery<U> : TEntity[K] : TEntity[K] extends DbEntity | undefined ? EntityQuery<NonNullable<TEntity[K]>> : TEntity[K];
355
+ };
356
+ /**
357
+ * Collection query builder type for navigation collections
358
+ */
359
+ export interface EntityCollectionQuery<TEntity extends DbEntity> {
360
+ select<TSelection>(selector: (item: EntityQuery<TEntity>) => TSelection): EntityCollectionQueryWithSelect<TEntity, TSelection>;
361
+ selectDistinct<TSelection>(selector: (item: EntityQuery<TEntity>) => TSelection): EntityCollectionQueryWithSelect<TEntity, TSelection>;
362
+ where(condition: (item: EntityQuery<TEntity>) => any): this;
363
+ orderBy(selector: (item: EntityQuery<TEntity>) => any): this;
364
+ orderBy(selector: (item: EntityQuery<TEntity>) => any[]): this;
365
+ orderBy(selector: (item: EntityQuery<TEntity>) => Array<[any, 'ASC' | 'DESC']>): this;
366
+ limit(count: number): this;
367
+ offset(count: number): this;
368
+ min<TSelection>(selector: (item: EntityQuery<TEntity>) => TSelection): SqlFragment<number | null>;
369
+ max<TSelection>(selector: (item: EntityQuery<TEntity>) => TSelection): SqlFragment<number | null>;
370
+ sum<TSelection>(selector: (item: EntityQuery<TEntity>) => TSelection): SqlFragment<number | null>;
371
+ count(): SqlFragment<number>;
372
+ toNumberList(asName?: string): number[];
373
+ toStringList(asName?: string): string[];
374
+ toList(asName: string): TEntity[];
375
+ }
376
+ export interface EntityCollectionQueryWithSelect<TEntity extends DbEntity, TSelection> {
377
+ where(condition: (item: EntityQuery<TEntity>) => any): this;
378
+ orderBy(selector: (item: TSelection) => any): this;
379
+ orderBy(selector: (item: TSelection) => any[]): this;
380
+ orderBy(selector: (item: TSelection) => Array<[any, 'ASC' | 'DESC']>): this;
381
+ limit(count: number): this;
382
+ offset(count: number): this;
383
+ min(): Promise<TSelection | null>;
384
+ max(): Promise<TSelection | null>;
385
+ sum(): Promise<TSelection | null>;
386
+ count(): Promise<number>;
387
+ toNumberList(asName?: string): number[];
388
+ toStringList(asName?: string): string[];
389
+ toList(asName: string): TSelection[];
390
+ }
391
+ /**
392
+ * Strongly-typed query builder for entities
393
+ * Results automatically unwrap DbColumn<T> to T
394
+ */
395
+ export interface EntitySelectQueryBuilder<TEntity extends DbEntity, TSelection> {
396
+ select<TNewSelection>(selector: (entity: TSelection extends DbEntity ? EntityQuery<TSelection> : TSelection) => TNewSelection): EntitySelectQueryBuilder<TEntity, TNewSelection>;
397
+ selectDistinct<TNewSelection>(selector: (entity: TSelection extends DbEntity ? EntityQuery<TSelection> : TSelection) => TNewSelection): EntitySelectQueryBuilder<TEntity, TNewSelection>;
398
+ where(condition: (entity: TSelection extends DbEntity ? EntityQuery<TSelection> : TSelection) => any): EntitySelectQueryBuilder<TEntity, TSelection>;
399
+ orderBy(selector: (row: TSelection) => any): EntitySelectQueryBuilder<TEntity, TSelection>;
400
+ orderBy(selector: (row: TSelection) => any[]): EntitySelectQueryBuilder<TEntity, TSelection>;
401
+ orderBy(selector: (row: TSelection) => Array<[any, 'ASC' | 'DESC']>): EntitySelectQueryBuilder<TEntity, TSelection>;
402
+ limit(count: number): EntitySelectQueryBuilder<TEntity, TSelection>;
403
+ offset(count: number): EntitySelectQueryBuilder<TEntity, TSelection>;
404
+ count(): Promise<number>;
405
+ first(): Promise<ResolveCollectionResults<TSelection>>;
406
+ firstOrDefault(): Promise<ResolveCollectionResults<TSelection> | null>;
407
+ firstOrThrow(): Promise<ResolveCollectionResults<TSelection>>;
408
+ leftJoin<TRight extends DbEntity | Record<string, any>, TNewSelection>(rightTable: DbEntityTable<TRight extends DbEntity ? TRight : never> | import('../query/subquery').Subquery<TRight, 'table'> | import('../query/cte-builder').DbCte<TRight>, condition: (left: TSelection extends DbEntity ? EntityQuery<TSelection> : TSelection, right: TRight extends DbEntity ? (EntityQuery<TRight> | TRight) : TRight) => Condition, selector: (left: TSelection extends DbEntity ? EntityQuery<TSelection> : TSelection, right: TRight extends DbEntity ? (EntityQuery<TRight> | TRight) : TRight) => TNewSelection, alias?: string): EntitySelectQueryBuilder<TEntity, TNewSelection>;
409
+ innerJoin<TRight extends DbEntity | Record<string, any>, TNewSelection>(rightTable: DbEntityTable<TRight extends DbEntity ? TRight : never> | import('../query/subquery').Subquery<TRight, 'table'> | import('../query/cte-builder').DbCte<TRight>, condition: (left: TSelection extends DbEntity ? EntityQuery<TSelection> : TSelection, right: TRight extends DbEntity ? (EntityQuery<TRight> | TRight) : TRight) => Condition, selector: (left: TSelection extends DbEntity ? EntityQuery<TSelection> : TSelection, right: TRight extends DbEntity ? (EntityQuery<TRight> | TRight) : TRight) => TNewSelection, alias?: string): EntitySelectQueryBuilder<TEntity, TNewSelection>;
410
+ groupBy<TGroupingKey>(selector: (entity: TSelection extends DbEntity ? EntityQuery<TSelection> : TSelection) => TGroupingKey): import('../query/grouped-query').GroupedQueryBuilder<TSelection extends DbEntity ? EntityQuery<TSelection> : TSelection, TGroupingKey>;
411
+ min<TResult = TSelection>(selector?: (entity: TSelection extends DbEntity ? EntityQuery<TSelection> : TSelection) => TResult): Promise<TResult | null>;
412
+ max<TResult = TSelection>(selector?: (entity: TSelection extends DbEntity ? EntityQuery<TSelection> : TSelection) => TResult): Promise<TResult | null>;
413
+ sum<TResult = TSelection>(selector?: (entity: TSelection extends DbEntity ? EntityQuery<TSelection> : TSelection) => TResult): Promise<TResult | null>;
414
+ count(): Promise<number>;
415
+ asSubquery<TMode extends 'scalar' | 'array' | 'table' = 'table'>(mode?: TMode): import('../query/subquery').Subquery<TMode extends 'scalar' ? UnwrapDbColumns<TSelection> : TMode extends 'array' ? UnwrapDbColumns<TSelection>[] : UnwrapDbColumns<TSelection>, TMode>;
416
+ with(...ctes: import('../query/cte-builder').DbCte<any>[]): this;
417
+ update(data: Partial<InsertData<TEntity>>): Promise<UnwrapDbColumns<TEntity>[]>;
418
+ delete(): Promise<void>;
419
+ toList(): Promise<ResolveCollectionResults<TSelection>[]>;
420
+ first(): Promise<ResolveCollectionResults<TSelection>>;
421
+ firstOrDefault(): Promise<ResolveCollectionResults<TSelection> | null>;
422
+ }
423
+ /**
424
+ * DbEntity insert builder for upsert operations with proper typing
425
+ */
426
+ export declare class EntityInsertBuilder<TEntity extends DbEntity> {
427
+ private builder;
428
+ constructor(builder: InsertBuilder<TableSchema>);
429
+ /**
430
+ * Specify conflict target (columns or constraint name)
431
+ */
432
+ onConflict(target?: ConflictTarget | string[]): this;
433
+ /**
434
+ * Do nothing on conflict
435
+ */
436
+ doNothing(): this;
437
+ /**
438
+ * Update on conflict (upsert)
439
+ */
440
+ doUpdate(options?: {
441
+ set?: InsertData<TEntity>;
442
+ where?: string;
443
+ }): this;
444
+ /**
445
+ * Execute the insert/upsert
446
+ */
447
+ execute(): Promise<UnwrapDbColumns<TEntity>[]>;
448
+ }
449
+ /**
450
+ * Table accessor with entity typing
451
+ */
452
+ export declare class DbEntityTable<TEntity extends DbEntity> {
453
+ private context;
454
+ private tableName;
455
+ private tableBuilder;
456
+ constructor(context: DataContext, tableName: string, tableBuilder: TableBuilder<any>);
457
+ /**
458
+ * Get the table schema for this entity
459
+ * @internal
460
+ */
461
+ _getSchema(): TableSchema;
462
+ /**
463
+ * Get the database client
464
+ * @internal
465
+ */
466
+ _getClient(): DatabaseClient;
467
+ /**
468
+ * Get the query executor for logging
469
+ * @internal
470
+ */
471
+ _getExecutor(): any;
472
+ /**
473
+ * Get the collection strategy
474
+ * @internal
475
+ */
476
+ _getCollectionStrategy(): CollectionStrategyType | undefined;
477
+ /**
478
+ * Get qualified table name with schema prefix if specified
479
+ * @internal
480
+ */
481
+ private _getQualifiedTableName;
482
+ /**
483
+ * Configure query options for the current query chain
484
+ * Returns a new DbEntityTable instance with a modified context that has the specified options
485
+ *
486
+ * @example
487
+ * ```typescript
488
+ * const results = await db.users
489
+ * .withQueryOptions({ logQueries: true, collectionStrategy: 'temptable' })
490
+ * .select(u => ({ id: u.id, name: u.username }))
491
+ * .toList();
492
+ * ```
493
+ */
494
+ withQueryOptions(options: QueryOptions): DbEntityTable<TEntity>;
495
+ /**
496
+ * Select all records - returns full entities with unwrapped DbColumns
497
+ */
498
+ toList(): Promise<UnwrapDbColumns<TEntity>[]>;
499
+ /**
500
+ * Count all records
501
+ */
502
+ count(): Promise<number>;
503
+ /**
504
+ * Order by field(s)
505
+ */
506
+ orderBy(selector: (row: TEntity) => any): EntitySelectQueryBuilder<TEntity, TEntity>;
507
+ orderBy(selector: (row: TEntity) => any[]): EntitySelectQueryBuilder<TEntity, TEntity>;
508
+ orderBy(selector: (row: TEntity) => Array<[any, 'ASC' | 'DESC']>): EntitySelectQueryBuilder<TEntity, TEntity>;
509
+ /**
510
+ * Limit results
511
+ */
512
+ limit(count: number): EntitySelectQueryBuilder<TEntity, TEntity>;
513
+ /**
514
+ * Offset results
515
+ */
516
+ offset(count: number): EntitySelectQueryBuilder<TEntity, TEntity>;
517
+ /**
518
+ * Select query
519
+ */
520
+ select<TSelection>(selector: (entity: EntityQuery<TEntity>) => TSelection): EntitySelectQueryBuilder<TEntity, TSelection>;
521
+ /**
522
+ * Select distinct
523
+ */
524
+ selectDistinct<TSelection>(selector: (entity: EntityQuery<TEntity>) => TSelection): EntitySelectQueryBuilder<TEntity, TSelection>;
525
+ /**
526
+ * Where query - returns all columns by default
527
+ */
528
+ where(condition: (entity: EntityQuery<TEntity>) => any): EntitySelectQueryBuilder<TEntity, TEntity>;
529
+ /**
530
+ * Left join with another table or subquery and selector
531
+ */
532
+ leftJoin<TRight extends DbEntity, TSelection>(rightTable: DbEntityTable<TRight> | import('../query/subquery').Subquery<TRight, 'table'>, condition: (left: EntityQuery<TEntity>, right: EntityQuery<TRight> | TRight) => Condition, selector: (left: EntityQuery<TEntity>, right: EntityQuery<TRight> | TRight) => TSelection, alias?: string): EntitySelectQueryBuilder<TEntity, TSelection>;
533
+ /**
534
+ * Inner join with another table or subquery and selector
535
+ */
536
+ innerJoin<TRight extends DbEntity, TSelection>(rightTable: DbEntityTable<TRight> | import('../query/subquery').Subquery<TRight, 'table'>, condition: (left: EntityQuery<TEntity>, right: EntityQuery<TRight> | TRight) => Condition, selector: (left: EntityQuery<TEntity>, right: EntityQuery<TRight> | TRight) => TSelection, alias?: string): EntitySelectQueryBuilder<TEntity, TSelection>;
537
+ /**
538
+ * Insert - accepts only DbColumn properties (excludes navigation properties)
539
+ */
540
+ insert(data: InsertData<TEntity>): Promise<UnwrapDbColumns<TEntity>>;
541
+ /**
542
+ * Insert multiple records
543
+ */
544
+ insertMany(data: InsertData<TEntity>[]): Promise<UnwrapDbColumns<TEntity>[]>;
545
+ /**
546
+ * Upsert (insert or update on conflict)
547
+ */
548
+ upsert(data: InsertData<TEntity>[], config?: EntityUpsertConfig<TEntity>): Promise<UnwrapDbColumns<TEntity>[]>;
549
+ /**
550
+ * Bulk insert with advanced configuration
551
+ * Supports automatic chunking for large datasets
552
+ */
553
+ insertBulk(value: InsertData<TEntity> | InsertData<TEntity>[], insertConfig?: InsertConfig): Promise<UnwrapDbColumns<TEntity>[]>;
554
+ /**
555
+ * Upsert with advanced configuration
556
+ * Auto-detects primary keys and supports chunking
557
+ */
558
+ upsertBulk(values: InsertData<TEntity>[], config?: EntityUpsertConfig<TEntity>): Promise<UnwrapDbColumns<TEntity>[]>;
559
+ /**
560
+ * Map database column names back to property names
561
+ */
562
+ private mapResultToEntity;
563
+ /**
564
+ * Map array of database results to entities
565
+ */
566
+ private mapResultsToEntities;
567
+ /**
568
+ * Extract property names from lambda selector
569
+ */
570
+ private extractPropertyNames;
571
+ /**
572
+ * Start building an upsert query with values
573
+ */
574
+ values(data: InsertData<TEntity> | InsertData<TEntity>[]): EntityInsertBuilder<TEntity>;
575
+ /**
576
+ * Update records matching condition
577
+ * Usage: db.users.update({ age: 30 }, u => eq(u.id, 1))
578
+ */
579
+ update(data: Partial<InsertData<TEntity>>, condition: (entity: EntityQuery<TEntity>) => Condition): Promise<UnwrapDbColumns<TEntity>[]>;
580
+ /**
581
+ * Delete records matching condition
582
+ * Usage: db.users.delete(u => eq(u.id, 1))
583
+ */
584
+ delete(condition: (entity: EntityQuery<TEntity>) => Condition): Promise<void>;
585
+ /**
586
+ * Create a mock entity for type inference in lambdas
587
+ */
588
+ private createMockEntity;
589
+ }
590
+ /**
591
+ * Base database context with entity-first approach
592
+ */
593
+ export declare abstract class DatabaseContext extends DataContext {
594
+ private modelConfig;
595
+ private entityTables;
596
+ private sequenceRegistry;
597
+ private sequenceInstances;
598
+ constructor(client: DatabaseClient, queryOptions?: QueryOptions);
599
+ /**
600
+ * Override this method to configure your entities
601
+ */
602
+ protected abstract setupModel(modelConfig: DbModelConfig): void;
603
+ /**
604
+ * Optional: Override this method to register sequences.
605
+ * This is called during construction to ensure sequences are registered before schema creation.
606
+ *
607
+ * @example
608
+ * ```typescript
609
+ * protected setupSequences(): void {
610
+ * // Access sequence getters to register them
611
+ * this.mySeq;
612
+ * this.anotherSeq;
613
+ * }
614
+ * ```
615
+ */
616
+ protected setupSequences?(): void;
617
+ /**
618
+ * Hook called after database migrations/schema creation are complete.
619
+ * Override this method to execute custom SQL scripts that are outside the scope of the ORM.
620
+ *
621
+ * @example
622
+ * ```typescript
623
+ * protected async onMigrationComplete(client: DatabaseClient): Promise<void> {
624
+ * // Create custom functions, views, triggers, etc.
625
+ * await client.query(`
626
+ * CREATE OR REPLACE FUNCTION custom_function()
627
+ * RETURNS void AS $$
628
+ * BEGIN
629
+ * -- Custom logic here
630
+ * END;
631
+ * $$ LANGUAGE plpgsql;
632
+ * `);
633
+ * }
634
+ * ```
635
+ *
636
+ * @param client - Database client for executing custom SQL
637
+ */
638
+ protected onMigrationComplete(client: DatabaseClient): Promise<void>;
639
+ /**
640
+ * Register a sequence in the schema
641
+ * @param config - Sequence configuration
642
+ */
643
+ protected registerSequence(config: SequenceConfig): void;
644
+ /**
645
+ * Get a sequence instance for interacting with the database
646
+ * @param config - Sequence configuration
647
+ * @returns DbSequence instance with nextValue() and resync() methods
648
+ */
649
+ protected sequence(config: SequenceConfig): DbSequence;
650
+ /**
651
+ * Get all registered sequences
652
+ * @internal
653
+ */
654
+ getSequenceRegistry(): Map<string, SequenceConfig>;
655
+ /**
656
+ * Get schema manager for create/drop operations with post-migration hook support
657
+ */
658
+ getSchemaManager(): DbSchemaManager;
659
+ /**
660
+ * Get strongly-typed table accessor for an entity
661
+ * @internal - Use property accessors on derived class instead
662
+ */
663
+ protected table<TEntity extends DbEntity>(entityClass: EntityConstructor<TEntity>): DbEntityTable<TEntity>;
664
+ }
665
+ //# sourceMappingURL=db-context.d.ts.map