orange-orm 4.7.9 → 4.7.10-beta.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.
@@ -4218,13 +4218,13 @@ function requireNewColumn () {
4218
4218
 
4219
4219
  c.groupSum = (context, ...rest) => aggregateGroup.apply(null, [context, 'sum', c, table, ...rest]);
4220
4220
  c.groupAvg = (context, ...rest) => aggregateGroup.apply(null, [context, 'avg', c, table, ...rest]);
4221
- c.groupMin = (context, ...rest) => aggregateGroup.apply(null, [context, 'min', c, table, ...rest]);
4222
- c.groupMax = (context, ...rest) => aggregateGroup.apply(null, [context, 'max', c, table, ...rest]);
4221
+ c.groupMin = (context, ...rest) => aggregateGroup.apply(null, [context, 'min', c, table, false, ...rest]);
4222
+ c.groupMax = (context, ...rest) => aggregateGroup.apply(null, [context, 'max', c, table, false, ...rest]);
4223
4223
  c.groupCount = (context, ...rest) => aggregateGroup.apply(null, [context, 'count', c, table, false, ...rest]);
4224
4224
  c.sum = (context, ...rest) => aggregate.apply(null, [context, 'sum', c, table, ...rest]);
4225
4225
  c.avg = (context, ...rest) => aggregate.apply(null, [context, 'avg', c, table, ...rest]);
4226
- c.min = (context, ...rest) => aggregate.apply(null, [context, 'min', c, table, ...rest]);
4227
- c.max = (context, ...rest) => aggregate.apply(null, [context, 'max', c, table, ...rest]);
4226
+ c.min = (context, ...rest) => aggregate.apply(null, [context, 'min', c, table, false, ...rest]);
4227
+ c.max = (context, ...rest) => aggregate.apply(null, [context, 'max', c, table, false, ...rest]);
4228
4228
  c.count = (context, ...rest) => aggregate.apply(null, [context, 'count', c, table, false, ...rest]);
4229
4229
 
4230
4230
  function self(context) {
@@ -9415,13 +9415,13 @@ function requireRelatedColumn () {
9415
9415
 
9416
9416
  c.groupSum = (context, ...rest) => aggregateGroup.apply(null, [context, 'sum', column, relations, ...rest]);
9417
9417
  c.groupAvg = (context, ...rest) => aggregateGroup.apply(null, [context, 'avg', column, relations, ...rest]);
9418
- c.groupMin = (context, ...rest) => aggregateGroup.apply(null, [context, 'min', column, relations, ...rest]);
9419
- c.groupMax = (context, ...rest) => aggregateGroup.apply(null, [context, 'max', column, relations, ...rest]);
9418
+ c.groupMin = (context, ...rest) => aggregateGroup.apply(null, [context, 'min', column, relations, false, ...rest]);
9419
+ c.groupMax = (context, ...rest) => aggregateGroup.apply(null, [context, 'max', column, relations, false, ...rest]);
9420
9420
  c.groupCount = (context, ...rest) => aggregateGroup.apply(null, [context, 'count', column, relations, false, ...rest]);
9421
9421
  c.sum = (context, ...rest) => aggregate.apply(null, [context, 'sum', column, relations, ...rest]);
9422
9422
  c.avg = (context, ...rest) => aggregate.apply(null, [context, 'avg', column, relations, ...rest]);
9423
- c.min = (context, ...rest) => aggregate.apply(null, [context, 'min', column, relations, ...rest]);
9424
- c.max = (context, ...rest) => aggregate.apply(null, [context, 'max', column, relations, ...rest]);
9423
+ c.min = (context, ...rest) => aggregate.apply(null, [context, 'min', column, relations, false, ...rest]);
9424
+ c.max = (context, ...rest) => aggregate.apply(null, [context, 'max', column, relations, false, ...rest]);
9425
9425
  c.count = (context, ...rest) => aggregate.apply(null, [context, 'count', column, relations, false, ...rest]);
9426
9426
  c.self = (context, ...rest) => childColumn.apply(null, [context, column, relations, ...rest]);
9427
9427
 
package/dist/index.mjs CHANGED
@@ -4219,13 +4219,13 @@ function requireNewColumn () {
4219
4219
 
4220
4220
  c.groupSum = (context, ...rest) => aggregateGroup.apply(null, [context, 'sum', c, table, ...rest]);
4221
4221
  c.groupAvg = (context, ...rest) => aggregateGroup.apply(null, [context, 'avg', c, table, ...rest]);
4222
- c.groupMin = (context, ...rest) => aggregateGroup.apply(null, [context, 'min', c, table, ...rest]);
4223
- c.groupMax = (context, ...rest) => aggregateGroup.apply(null, [context, 'max', c, table, ...rest]);
4222
+ c.groupMin = (context, ...rest) => aggregateGroup.apply(null, [context, 'min', c, table, false, ...rest]);
4223
+ c.groupMax = (context, ...rest) => aggregateGroup.apply(null, [context, 'max', c, table, false, ...rest]);
4224
4224
  c.groupCount = (context, ...rest) => aggregateGroup.apply(null, [context, 'count', c, table, false, ...rest]);
4225
4225
  c.sum = (context, ...rest) => aggregate.apply(null, [context, 'sum', c, table, ...rest]);
4226
4226
  c.avg = (context, ...rest) => aggregate.apply(null, [context, 'avg', c, table, ...rest]);
4227
- c.min = (context, ...rest) => aggregate.apply(null, [context, 'min', c, table, ...rest]);
4228
- c.max = (context, ...rest) => aggregate.apply(null, [context, 'max', c, table, ...rest]);
4227
+ c.min = (context, ...rest) => aggregate.apply(null, [context, 'min', c, table, false, ...rest]);
4228
+ c.max = (context, ...rest) => aggregate.apply(null, [context, 'max', c, table, false, ...rest]);
4229
4229
  c.count = (context, ...rest) => aggregate.apply(null, [context, 'count', c, table, false, ...rest]);
4230
4230
 
4231
4231
  function self(context) {
@@ -9416,13 +9416,13 @@ function requireRelatedColumn () {
9416
9416
 
9417
9417
  c.groupSum = (context, ...rest) => aggregateGroup.apply(null, [context, 'sum', column, relations, ...rest]);
9418
9418
  c.groupAvg = (context, ...rest) => aggregateGroup.apply(null, [context, 'avg', column, relations, ...rest]);
9419
- c.groupMin = (context, ...rest) => aggregateGroup.apply(null, [context, 'min', column, relations, ...rest]);
9420
- c.groupMax = (context, ...rest) => aggregateGroup.apply(null, [context, 'max', column, relations, ...rest]);
9419
+ c.groupMin = (context, ...rest) => aggregateGroup.apply(null, [context, 'min', column, relations, false, ...rest]);
9420
+ c.groupMax = (context, ...rest) => aggregateGroup.apply(null, [context, 'max', column, relations, false, ...rest]);
9421
9421
  c.groupCount = (context, ...rest) => aggregateGroup.apply(null, [context, 'count', column, relations, false, ...rest]);
9422
9422
  c.sum = (context, ...rest) => aggregate.apply(null, [context, 'sum', column, relations, ...rest]);
9423
9423
  c.avg = (context, ...rest) => aggregate.apply(null, [context, 'avg', column, relations, ...rest]);
9424
- c.min = (context, ...rest) => aggregate.apply(null, [context, 'min', column, relations, ...rest]);
9425
- c.max = (context, ...rest) => aggregate.apply(null, [context, 'max', column, relations, ...rest]);
9424
+ c.min = (context, ...rest) => aggregate.apply(null, [context, 'min', column, relations, false, ...rest]);
9425
+ c.max = (context, ...rest) => aggregate.apply(null, [context, 'max', column, relations, false, ...rest]);
9426
9426
  c.count = (context, ...rest) => aggregate.apply(null, [context, 'count', column, relations, false, ...rest]);
9427
9427
  c.self = (context, ...rest) => childColumn.apply(null, [context, column, relations, ...rest]);
9428
9428
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orange-orm",
3
- "version": "4.7.9",
3
+ "version": "4.7.10-beta.1",
4
4
  "main": "./src/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "browser": "./dist/index.browser.mjs",
@@ -0,0 +1,132 @@
1
+ // Type definitions using a type mapping approach
2
+ type EntityMap = {
3
+ Order: {
4
+ id: string;
5
+ date: Date;
6
+ total: number;
7
+ };
8
+ Line: {
9
+ id: string;
10
+ quantity: number;
11
+ price: number;
12
+ };
13
+ Package: {
14
+ id: string;
15
+ trackingNumber: string;
16
+ weight: number;
17
+ };
18
+ Customer: {
19
+ id: string;
20
+ name: string;
21
+ email: string;
22
+ };
23
+ };
24
+
25
+ // Enhanced relation mapping that includes cardinality information
26
+ type RelationMap = {
27
+ Order: {
28
+ lines: { type: 'Line'; isArray: true };
29
+ customer: { type: 'Customer'; isArray: false };
30
+ };
31
+ Line: {
32
+ packages: { type: 'Package'; isArray: true };
33
+ order: { type: 'Order'; isArray: false };
34
+ };
35
+ Customer: {
36
+ orders: { type: 'Order'; isArray: true };
37
+ };
38
+ Package: {};
39
+ };
40
+
41
+ // Helper type to extract the entity type from a relation
42
+ type RelationEntityType<R> = R extends { type: infer T extends keyof EntityMap } ? T : never;
43
+
44
+ // Helper type to determine if a relation is an array
45
+ type IsArray<R> = R extends { isArray: infer A extends boolean } ? A : false;
46
+
47
+ // Generic deep type resolver
48
+ type Depth = [never, 0, 1, 2, 3, 4, 5];
49
+
50
+ // RecursiveType generic with improved depth handling
51
+ type RecursiveType<
52
+ T extends keyof EntityMap,
53
+ D extends number = 5
54
+ > = {
55
+ // Always include base properties
56
+ [K in keyof EntityMap[T]]: EntityMap[T][K];
57
+ } & (D extends 0
58
+ ? // At max depth, only include base properties (which are already included above)
59
+ {}
60
+ : // Otherwise include relations with appropriate depth reduction
61
+ {
62
+ [K in keyof RelationMap[T]]: IsArray<RelationMap[T][K]> extends true
63
+ ? RecursiveType<
64
+ RelationEntityType<RelationMap[T][K]>,
65
+ Depth[D]
66
+ >[]
67
+ : RecursiveType<
68
+ RelationEntityType<RelationMap[T][K]>,
69
+ Depth[D]
70
+ >;
71
+ });
72
+
73
+ // Generate the entity types with relationships resolved to the specified depth
74
+ type Order = RecursiveType<'Order', 6>;
75
+
76
+ type Line = RecursiveType<'Line'>;
77
+ type Package = RecursiveType<'Package'>;
78
+ type Customer = RecursiveType<'Customer'>;
79
+
80
+ // Example usage
81
+ function processOrder(order: Order, line: Line) {
82
+ // Can access nested properties up to depth 5
83
+ // const trackingNumber = order.customer.orders[0].lines[0].order.lines[0].packages[0].
84
+ // const orderId = order.customer.orders[0].customer.orders[0].lines[0].
85
+ line.order.customer.orders[0].lines[0].packages[0].
86
+
87
+ console.log(trackingNumber, orderId);
88
+ }
89
+
90
+ // Allow custom depth control per entity type
91
+ type OrderWithDepth1 = RecursiveType<'Order', 4>;
92
+
93
+ // Example with custom depth
94
+ function processOrderWithLimitedDepth(order: OrderWithDepth1) {
95
+ // This would work (depth 1)
96
+
97
+ console.log(order.customer.id); // Can access base properties at max depth
98
+ console.log(order.customer.name); // Can access base properties at max depth
99
+
100
+ // This would error - beyond depth 1
101
+ // console.log(order.customer.orders[0].id);
102
+ }
103
+
104
+ // Example of creating an instance with this type system
105
+ const sampleOrder: Order = {
106
+ id: "ORD-123",
107
+ date: new Date(),
108
+ total: 299.99,
109
+ customer: {
110
+ id: "CUST-456",
111
+ name: "John Doe",
112
+ email: "john@example.com",
113
+ orders: [
114
+ /* would be recursive orders */
115
+ ]
116
+ },
117
+ lines: [
118
+ {
119
+ id: "LINE-789",
120
+ quantity: 2,
121
+ price: 149.99,
122
+ order: /* circular reference */,
123
+ packages: [
124
+ {
125
+ id: "PKG-101",
126
+ trackingNumber: "TRK123456789",
127
+ weight: 1.5
128
+ }
129
+ ]
130
+ }
131
+ ]
132
+ };
package/src/index.d.ts CHANGED
@@ -4,7 +4,8 @@ import type { RequestHandler } from 'express';
4
4
  import type { D1Database } from '@cloudflare/workers-types';
5
5
  import type { ConnectionConfiguration } from 'tedious';
6
6
  import type { PoolAttributes } from 'oracledb';
7
- import type { AllowedDbMap, DbMapper, MappedDbDef } from './map';
7
+ import type { AllowedDbMap, DbMapper, MappedDbDef, MergeProperties } from './map';
8
+ import type { Filter as MapFilter, RawFilter as MapRawFilter, Pool as MapPool, PoolOptions as MapPoolOptions } from './map2';
8
9
 
9
10
  declare function r(config: r.Config): unknown;
10
11
 
@@ -26,12 +27,12 @@ declare namespace r {
26
27
  function off(type: 'query', cb: (e: QueryEvent) => void): void;
27
28
  function map<V extends AllowedDbMap<V>>(
28
29
  fn: (mapper: DbMapper<{}>) => V
29
- ): MappedDbDef<V>;
30
+ ): MappedDbDef<MergeProperties<V, V>>;
30
31
  function createPatch(original: any[], modified: any[]): JsonPatch;
31
32
  function createPatch(original: any, modified: any): JsonPatch;
32
33
 
33
34
  type JsonPatch = Array<{
34
- op: "add" | "remove" | "replace" | "copy" | "move" | "test";
35
+ op: 'add' | 'remove' | 'replace' | 'copy' | 'move' | 'test';
35
36
  path: string;
36
37
  value?: any;
37
38
  from?: string;
@@ -48,13 +49,8 @@ declare namespace r {
48
49
  result: []
49
50
  }
50
51
 
51
- export interface Pool {
52
- end(): Promise<void>;
53
- }
54
-
55
- export interface PoolOptions {
56
- size?: number;
57
- }
52
+ export type Pool = MapPool;
53
+ export type PoolOptions = MapPoolOptions;
58
54
 
59
55
  export interface Join {
60
56
  by(...columns: string[]): JoinBy;
@@ -65,8 +61,8 @@ declare namespace r {
65
61
  }
66
62
 
67
63
  export abstract class JoinRelation {
68
- columns: ColumnDef[];
69
- childTable: Table;
64
+ columns: ColumnDef[];
65
+ childTable: Table;
70
66
  }
71
67
 
72
68
  export interface Table {
@@ -155,17 +151,10 @@ declare namespace r {
155
151
  as(dbName: string): ColumnNotNullOf<T>;
156
152
  }
157
153
 
158
- var filter: Filter;
159
- export interface RawFilter {
160
- sql: string | (() => string);
161
- parameters?: any[];
162
- }
154
+ const filter: Filter;
163
155
 
164
- export interface Filter extends RawFilter {
165
- and(filter: Filter, ...filters: Filter[]): Filter;
166
- or(filter: Filter, ...filters: Filter[]): Filter;
167
- not(): Filter;
168
- }
156
+ export type RawFilter = MapRawFilter;
157
+ export type Filter = MapFilter;
169
158
 
170
159
  export type Concurrency = 'optimistic' | 'skipOnConflict' | 'overwrite';
171
160
 
@@ -223,11 +212,11 @@ declare namespace r {
223
212
  */
224
213
  iEqual(value: string | null): Filter;
225
214
  /**
226
- * ignore case, postgres only
215
+ * ignore case, postgres only
227
216
  * */
228
217
  iEq(value: string | null): Filter;
229
218
  /**
230
- * ignore case, postgres only
219
+ * ignore case, postgres only
231
220
  */
232
221
  iEq(value: string | null): Filter;
233
222
  }
package/src/map.d.ts CHANGED
@@ -4,21 +4,28 @@ import type { ConnectionConfiguration } from 'tedious';
4
4
  import type { D1Database } from '@cloudflare/workers-types';
5
5
  import type { PoolAttributes } from 'oracledb';
6
6
  import type { AxiosInterceptorManager, InternalAxiosRequestConfig, AxiosResponse } from 'axios';
7
+ import type { DBClient } from './map2';
8
+
7
9
 
8
- export type MappedDbDef<T> = {
9
- map<V extends AllowedDbMap<V>>(
10
- callback: (mapper: DbMapper<T>) => V
11
- ): MappedDbDef<MergeProperties<T, V>>;
12
- <O extends DbOptions<T>>(concurrency: O): NegotiateDbInstance<T, O>;
13
- } & T & DbConnectable<T>;
14
10
 
15
- type MergeProperties<T, V> = {
11
+ //todo
12
+ export type MergeProperties<T, V> = {
16
13
  [K in keyof T | keyof V]:
17
- K extends keyof T ? (T[K] extends MappedTableDef<infer M>
18
- ? (K extends keyof V ? (V[K] extends MappedTableDef<infer N> ? MappedTableDef<M & N> : V[K]) : T[K])
19
- : T[K])
20
- : (K extends keyof V ? V[K] : never);
21
- };
14
+ K extends keyof T
15
+ ? T[K] extends MappedTableDef<infer M>
16
+ ? K extends keyof V
17
+ ? V[K] extends MappedTableDef<infer N>
18
+ ? MappedTableDef<M & N & TableAlias<K>>
19
+ : V[K]
20
+ : T[K]
21
+ : T[K]
22
+ : K extends keyof V
23
+ ? V[K] extends MappedTableDef<infer N>
24
+ ? MappedTableDef<N & TableAlias<K>>
25
+ : V[K]
26
+ : never;
27
+ };
28
+
22
29
 
23
30
 
24
31
  export type DbMapper<T> = {
@@ -30,21 +37,21 @@ type MappedDb<T> = {
30
37
  } & DbConnectable<T>;
31
38
 
32
39
  type DbConnectable<T> = {
33
- http(url: string): MappedDbInstance<T>;
34
- d1(database: D1Database): MappedDbInstance<T>;
35
- postgres(connectionString: string, options?: PoolOptions): MappedDbInstance<T>;
36
- pglite(config?: PGliteOptions| string | undefined, options?: PoolOptions): MappedDbInstance<T>;
37
- sqlite(connectionString: string, options?: PoolOptions): MappedDbInstance<T>;
38
- sap(connectionString: string, options?: PoolOptions): MappedDbInstance<T>;
39
- mssql(connectionConfig: ConnectionConfiguration, options?: PoolOptions): MappedDbInstance<T>;
40
- mssql(connectionString: string, options?: PoolOptions): MappedDbInstance<T>;
41
- mssqlNative(connectionString: string, options?: PoolOptions): MappedDbInstance<T>;
42
- mysql(connectionString: string, options?: PoolOptions): MappedDbInstance<T>;
43
- oracle(config: PoolAttributes, options?: PoolOptions): MappedDbInstance<T>;
40
+ http(url: string): DBClient<SchemaFromMappedDb<T>>;
41
+ d1(database: D1Database): DBClient<SchemaFromMappedDb<T>>;
42
+ postgres(connectionString: string, options?: PoolOptions): DBClient<SchemaFromMappedDb<T>>;
43
+ pglite(config?: PGliteOptions| string | undefined, options?: PoolOptions): DBClient<SchemaFromMappedDb<T>>;
44
+ sqlite(connectionString: string, options?: PoolOptions): DBClient<SchemaFromMappedDb<T>>;
45
+ sap(connectionString: string, options?: PoolOptions): DBClient<SchemaFromMappedDb<T>>;
46
+ mssql(connectionConfig: ConnectionConfiguration, options?: PoolOptions): DBClient<SchemaFromMappedDb<T>>;
47
+ mssql(connectionString: string, options?: PoolOptions): DBClient<SchemaFromMappedDb<T>>;
48
+ mssqlNative(connectionString: string, options?: PoolOptions): DBClient<SchemaFromMappedDb<T>>;
49
+ mysql(connectionString: string, options?: PoolOptions): DBClient<SchemaFromMappedDb<T>>;
50
+ oracle(config: PoolAttributes, options?: PoolOptions): DBClient<SchemaFromMappedDb<T>>;
44
51
  };
45
52
 
46
53
  type NegotiateDbInstance<T, C> = C extends WithDb
47
- ? MappedDbInstance<T>
54
+ ? DBClient<SchemaFromMappedDb<T>>
48
55
  : MappedDb<T>;
49
56
 
50
57
  type WithDb = {
@@ -188,7 +195,7 @@ type ExpandedMappedTable<T, FL = ExpandedFetchingStrategy<T>> = {
188
195
  replace(
189
196
  row: StrategyToInsertRowData<T> | StrategyToInsertRowData<T>[]
190
197
  ): Promise<void>;
191
-
198
+
192
199
  replace<FS extends FetchingStrategy<T>>(
193
200
  row: StrategyToInsertRowData<T>,
194
201
  strategy: FS
@@ -313,7 +320,7 @@ type MappedTable<T> = {
313
320
  filter?: Filter | PrimaryRowFilter<T>,
314
321
  fetchingStrategy?: FS
315
322
  ): Promise<StrategyToRow<FetchedProperties<T, FS>, T>>;
316
-
323
+
317
324
  update(
318
325
  values: StrategyToUpdateRowData<T>,
319
326
  where: FetchingStrategy<T>
@@ -328,7 +335,7 @@ type MappedTable<T> = {
328
335
  replace(
329
336
  row: StrategyToInsertRowData<T> | StrategyToInsertRowData<T>[]
330
337
  ): Promise<void>;
331
-
338
+
332
339
  replace<FS extends FetchingStrategy<T>>(
333
340
  row: StrategyToInsertRowData<T>,
334
341
  strategy: FS
@@ -616,7 +623,7 @@ type AggregateStrategyBase<T> =
616
623
  };
617
624
 
618
625
 
619
- type FetchingStrategyBase<T, IsMany = true> =
626
+ type FetchingStrategyBase<T, IsMany = true> =
620
627
  {
621
628
  [K in keyof T &
622
629
  keyof RemoveNever<
@@ -624,7 +631,7 @@ type FetchingStrategyBase<T, IsMany = true> =
624
631
  >]?: T[K] extends ColumnSymbols
625
632
  ? boolean
626
633
  : boolean | FetchingStrategyBase<T[K], T[K] extends ManyRelation ? true: false> | AggType<T[K]>;
627
- } &
634
+ } &
628
635
  (IsMany extends true ? {
629
636
  limit?: number;
630
637
  offset?: number;
@@ -972,6 +979,7 @@ type MappedTableDefInit<T> = {
972
979
  ): MappedTableDef<T & V>;
973
980
  } & T;
974
981
 
982
+ //todo
975
983
  type MappedTableDef<T> = {
976
984
  map<V extends AllowedColumnsAndTablesMap<V>>(
977
985
  callback: (mapper: ColumnMapper<T>) => V
@@ -1639,7 +1647,7 @@ type DateColumnTypeDef<M> = DateValidator<M> & {
1639
1647
  serializable(value: boolean): DateColumnTypeDef<M>;
1640
1648
  JSONSchema(schema: object, options?: Options): DateColumnTypeDef<M>;
1641
1649
  default(value: string | Date | null | undefined | (() => string | Date | null | undefined)): DateColumnTypeDef<M>;
1642
- dbNull(value: String | Date): DateColumnTypeDef<M>;
1650
+ dbNull(value: string | Date): DateColumnTypeDef<M>;
1643
1651
  } & ColumnTypeOf<DateColumnType<M>> &
1644
1652
  M;
1645
1653
 
@@ -1650,7 +1658,7 @@ type DateWithTimeZoneColumnTypeDef<M> = DateValidator<M> & {
1650
1658
  serializable(value: boolean): DateWithTimeZoneColumnTypeDef<M>;
1651
1659
  JSONSchema(schema: object, options?: Options): DateWithTimeZoneColumnTypeDef<M>;
1652
1660
  default(value: string | Date | null | undefined | (() => string | Date | null | undefined)): DateWithTimeZoneColumnTypeDef<M>;
1653
- dbNull(value: String | Date): DateWithTimeZoneColumnTypeDef<M>;
1661
+ dbNull(value: string | Date): DateWithTimeZoneColumnTypeDef<M>;
1654
1662
  } & ColumnTypeOf<DateWithTimeZoneColumnType<M>> &
1655
1663
  M;
1656
1664
 
@@ -1783,4 +1791,97 @@ type Increment<C extends number> = C extends 0
1783
1791
  : C extends 4
1784
1792
  ? 5
1785
1793
  : 0;
1786
-
1794
+
1795
+
1796
+ type TableAlias<Alias> = {
1797
+ __tableAlias: Alias;
1798
+ };
1799
+
1800
+
1801
+
1802
+ export type SchemaFromMappedDb<T> = {
1803
+ [K in keyof T]: T[K] extends MappedTableDef<infer Def>
1804
+ ? TableSchema<Def>
1805
+ : never;
1806
+ };
1807
+
1808
+ type TableSchema<T> = {
1809
+ columns: {
1810
+ [K in keyof T as T[K] extends ColumnSymbols ? K : never]: ColumnToSchemaType<T[K]>;
1811
+ };
1812
+ primaryKey: ExtractPrimaryKeyNames<T>;
1813
+
1814
+ relations: {
1815
+ [K in keyof T as T[K] extends RelatedTable | ManyRelation ? K : never]:
1816
+ T[K] extends ManyRelation
1817
+ ? { type: 'hasMany'; target: RelationTarget<T[K]> }
1818
+ : T[K] extends RelatedTable
1819
+ ? { type: 'hasOne'; target: RelationTarget<T[K]> }
1820
+ : never;
1821
+ };
1822
+ };
1823
+
1824
+ type ExtractPrimaryKeyNames<T> =
1825
+ UnionToTuple<{ [K in keyof T]: T[K] extends IsPrimary ? K : never }[keyof T]> extends infer R
1826
+ ? R extends string[] ? R : []
1827
+ : [];
1828
+
1829
+
1830
+ type RelationTarget<T> =
1831
+ T extends { __tableAlias: infer S } ? Extract<S, string> : string;
1832
+
1833
+ type ColumnToSchemaType<T> =
1834
+ T extends JsonOf<infer U>
1835
+ ? { ' type': 'json'; ' tsType': U }
1836
+ & (T extends NotNullExceptInsert ? { ' notNull': true; ' notNullExceptInsert': true }
1837
+ : T extends NotNull ? { ' notNull': true }
1838
+ : {}) :
1839
+ T extends JSONColumnSymbol & JsonOf<infer U>
1840
+ ? { ' type': 'json'; ' tsType': U }
1841
+ & (T extends NotNullExceptInsert ? { ' notNull': true; ' notNullExceptInsert': true }
1842
+ : T extends NotNull ? { ' notNull': true }
1843
+ : {}) :
1844
+ T extends StringColumnSymbol
1845
+ ? { ' type': 'string' }
1846
+ & (T extends NotNullExceptInsert ? { ' notNull': true; ' notNullExceptInsert': true }
1847
+ : T extends NotNull ? { ' notNull': true }
1848
+ : {}) :
1849
+ T extends UuidColumnSymbol
1850
+ ? { ' type': 'uuid' }
1851
+ & (T extends NotNullExceptInsert ? { ' notNull': true; ' notNullExceptInsert': true }
1852
+ : T extends NotNull ? { ' notNull': true }
1853
+ : {}) :
1854
+ T extends NumericColumnSymbol
1855
+ ? { ' type': 'numeric' }
1856
+ & (T extends NotNullExceptInsert ? { ' notNull': true; ' notNullExceptInsert': true }
1857
+ : T extends NotNull ? { ' notNull': true }
1858
+ : {}) :
1859
+ T extends DateColumnSymbol | DateWithTimeZoneColumnSymbol
1860
+ ? { ' type': 'date' }
1861
+ & (T extends NotNullExceptInsert ? { ' notNull': true; ' notNullExceptInsert': true }
1862
+ : T extends NotNull ? { ' notNull': true }
1863
+ : {}) :
1864
+ T extends BooleanColumnSymbol
1865
+ ? { ' type': 'boolean' }
1866
+ & (T extends NotNullExceptInsert ? { ' notNull': true; ' notNullExceptInsert': true }
1867
+ : T extends NotNull ? { ' notNull': true }
1868
+ : {}) :
1869
+ T extends BinaryColumnSymbol
1870
+ ? { ' type': 'binary' }
1871
+ & (T extends NotNullExceptInsert ? { ' notNull': true; ' notNullExceptInsert': true }
1872
+ : T extends NotNull ? { ' notNull': true }
1873
+ : {}) :
1874
+ never;
1875
+
1876
+ export type MappedDbDef<T> = {
1877
+ map<V extends AllowedDbMap<V>>(
1878
+ callback: (mapper: DbMapper<T>) => V
1879
+ ): MappedDbDef<MergeProperties<T, V>>;
1880
+ <O extends DbOptions<T>>(concurrency: O): NegotiateDbInstance<T, O>;
1881
+
1882
+ /**
1883
+ * Returns the schema of the mapped DB as a generic type
1884
+ * Usage: type Schema = ReturnType<typeof db.toSchema>
1885
+ */
1886
+ toSchema: <U = T>() => SchemaFromMappedDb<U>;
1887
+ } & T & DbConnectable<T>;