@sigitex/outlaw 1.2.0 → 2.0.0

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.
@@ -1,4 +1,5 @@
1
1
  // oxlint-disable typescript/no-explicit-any
2
+ export type { ColumnRef } from "./ColumnRef"
2
3
  export type Text = "text"
3
4
  export type Integer = "integer"
4
5
  export type Real = "real"
@@ -16,7 +17,9 @@ export type TableData = {
16
17
  readonly constraints: TableConstraintData[]
17
18
  }
18
19
 
19
- export type CheckExpression = string | ((name: string, column: ColumnData) => string)
20
+ export type CheckExpression =
21
+ | string
22
+ | ((name: string, column: ColumnData) => string)
20
23
 
21
24
  export type ColumnData = {
22
25
  readonly name: string
@@ -30,14 +33,15 @@ export type ColumnData = {
30
33
  readonly check: CheckExpression | undefined
31
34
  }
32
35
 
33
- export type ColumnRef = {
36
+ export type ForeignKeyData = {
34
37
  readonly table: string
35
38
  readonly column: string
36
39
  }
37
40
 
38
- export type ForeignKeyData = ColumnRef
39
-
40
- export type TableConstraintData = TablePrimaryKeyData | TableUniqueData | TableCheckData
41
+ export type TableConstraintData =
42
+ | TablePrimaryKeyData
43
+ | TableUniqueData
44
+ | TableCheckData
41
45
 
42
46
  export type TablePrimaryKeyData = {
43
47
  readonly type: "primaryKey"
@@ -51,7 +55,9 @@ export type TableUniqueData = {
51
55
 
52
56
  export type TableCheckData = {
53
57
  readonly type: "check"
54
- readonly expression: string | ((columns: Record<string, string>, table: TableData) => string)
58
+ readonly expression:
59
+ | string
60
+ | ((columns: Record<string, string>, table: TableData) => string)
55
61
  }
56
62
 
57
63
  export type PrimaryKeyData = {
@@ -1,9 +1,18 @@
1
1
  // oxlint-disable typescript/no-explicit-any
2
- import type { CheckExpression, ColumnData, ColumnRef, IndexData, TableData, ViewData } from "./metadata"
2
+ import type {
3
+ CheckExpression,
4
+ ColumnData,
5
+ ColumnRef,
6
+ IndexData,
7
+ TableData,
8
+ ViewData,
9
+ } from "./metadata"
3
10
  import type { Mapping } from "./Mapping"
11
+ import type { QuerySource } from "../queryBuilder/QuerySource"
12
+ import type { QueryScope } from "../queryBuilder/QueryScope"
4
13
 
5
14
  export type SchemaMembers = {
6
- readonly [key: string]: AnyBuildTable | BuildView<any> | BuildIndex
15
+ readonly [key: string]: AnyBuildTable | AnyBuildView | BuildIndex
7
16
  }
8
17
 
9
18
  export type TablesOf<M extends SchemaMembers> = {
@@ -11,7 +20,7 @@ export type TablesOf<M extends SchemaMembers> = {
11
20
  }
12
21
 
13
22
  export type ViewsOf<M extends SchemaMembers> = {
14
- [K in keyof M as M[K] extends BuildView<any> ? K : never]: M[K]
23
+ [K in keyof M as M[K] extends AnyBuildView ? K : never]: M[K]
15
24
  }
16
25
 
17
26
  export type IndexesOf<M extends SchemaMembers> = {
@@ -30,7 +39,10 @@ export type BuildColumns = {
30
39
  readonly [columnName: string]: BuildColumn<any, any>
31
40
  }
32
41
 
33
- export type InferColumn<BC> = BC extends BuildColumn<infer Type, infer Defined>
42
+ export type InferColumn<BC> = BC extends {
43
+ readonly $value?: infer Type
44
+ readonly $defined?: infer Defined
45
+ }
34
46
  ? "notNull" extends Defined
35
47
  ? Type
36
48
  : "primaryKey" extends Defined
@@ -38,7 +50,7 @@ export type InferColumn<BC> = BC extends BuildColumn<infer Type, infer Defined>
38
50
  : Type | null
39
51
  : never
40
52
 
41
- export type InferTable<BT> = BT extends BuildTable<infer Columns>
53
+ export type InferTable<BT> = BT extends { readonly $columns: infer Columns }
42
54
  ? { [K in keyof Columns]: InferColumn<Columns[K]> }
43
55
  : never
44
56
 
@@ -49,25 +61,36 @@ export type RefBy = {
49
61
  readonly value: unknown
50
62
  }
51
63
 
52
- type BuildTableDSL<DefineColumns> = {
64
+ type BuildTableDSL<DefineColumns, Name extends string> = {
53
65
  readonly $kind: "table"
54
- primaryKey: (...columns: (keyof DefineColumns)[]) => BuildTable<DefineColumns>
55
- unique: (...columns: (keyof DefineColumns)[]) => BuildTable<DefineColumns>
56
- check: (expression: string | ((columns: ColumnNames<DefineColumns>, table: TableData) => string)) => BuildTable<DefineColumns>
57
- select(all: "*"): SchemaSelect<DefineColumns, DefineColumns>
58
- select<C extends keyof DefineColumns>(...columns: C[]): SchemaSelect<Pick<DefineColumns, C>, DefineColumns>
66
+ readonly $columns: DefineColumns
67
+ primaryKey: (
68
+ ...columns: (keyof DefineColumns)[]
69
+ ) => BuildTable<DefineColumns, Name>
70
+ unique: (
71
+ ...columns: (keyof DefineColumns)[]
72
+ ) => BuildTable<DefineColumns, Name>
73
+ check: (
74
+ expression:
75
+ | string
76
+ | ((columns: ColumnNames<DefineColumns>, table: TableData) => string),
77
+ ) => BuildTable<DefineColumns, Name>
59
78
  readonly by: {
60
79
  readonly [K in keyof DefineColumns]: (
61
80
  value: InferColumn<DefineColumns[K]>,
62
81
  ) => RefBy
63
82
  }
64
83
  readonly $meta: TableData
65
- readonly infer: InferTable<BuildTable<DefineColumns>>
84
+ readonly infer: {
85
+ [Key in keyof DefineColumns]: InferColumn<DefineColumns[Key]>
86
+ }
66
87
  }
67
88
 
68
- export type BuildTable<DefineColumns> = BuildTableDSL<DefineColumns> & {
69
- readonly [K in keyof DefineColumns]: ColumnRef
70
- }
89
+ export type BuildTable<
90
+ DefineColumns,
91
+ Name extends string = string,
92
+ > = BuildTableDSL<DefineColumns, Name> &
93
+ QuerySource<Name, QueryScope.SchemaColumns<DefineColumns>>
71
94
 
72
95
  export type AnyBuildTable = {
73
96
  readonly $kind: "table"
@@ -75,8 +98,16 @@ export type AnyBuildTable = {
75
98
  readonly [key: string]: unknown
76
99
  }
77
100
 
78
- // oxlint-disable-next-line no-unused-vars
79
- export type BuildView<SelectColumns = unknown> = {
101
+ export type BuildView<
102
+ SelectColumns extends QueryScope.Columns = QueryScope.Columns,
103
+ Name extends string = string,
104
+ > = QuerySource<Name, SelectColumns> & {
105
+ readonly $kind: "view"
106
+ readonly $meta: ViewData
107
+ readonly $tableData: TableData
108
+ }
109
+
110
+ export type AnyBuildView = {
80
111
  readonly $kind: "view"
81
112
  readonly $meta: ViewData
82
113
  readonly $tableData: TableData
@@ -87,27 +118,23 @@ export type BuildIndex = {
87
118
  readonly $meta: IndexData
88
119
  }
89
120
 
90
- /** Schema-level select builder — tracks column types without requiring a connection. */
91
- export type SchemaSelect<SelectColumns, Columns> = {
92
- where<Column extends keyof Columns>(column: Column, value: unknown): SchemaSelect<SelectColumns, Columns>
93
- where<Column extends keyof Columns>(column: Column, operator: string): SchemaSelect<SelectColumns, Columns>
94
- where<Column extends keyof Columns>(column: Column, operator: string, value: unknown): SchemaSelect<SelectColumns, Columns>
95
- orderBy(sorts: [keyof Columns, "asc" | "desc"][]): SchemaSelect<SelectColumns, Columns>
96
- limit(n: number): SchemaSelect<SelectColumns, Columns>
97
- offset(n: number): SchemaSelect<SelectColumns, Columns>
98
- join<JC>(table: BuildTable<JC>): SchemaSelect<SelectColumns & JC, Columns & JC>
99
- leftJoin<JC>(table: BuildTable<JC>): SchemaSelect<SelectColumns & JC, Columns & JC>
100
- on(left: ColumnRef, operator: string, right: ColumnRef): SchemaSelect<SelectColumns, Columns>
101
- }
121
+ export type SchemaSelect<
122
+ SelectColumns extends QueryScope.Columns,
123
+ Scope extends QueryScope,
124
+ > = QueryScope.Selection<SelectColumns, Scope>
102
125
 
103
126
  export type BuildColumn<Type, Defined extends string> = Omit<
104
127
  {
128
+ readonly $value?: Type
129
+ readonly $defined?: Defined
105
130
  readonly notNull: BuildColumn<Type, Defined | "notNull">
106
131
  readonly primaryKey: BuildColumn<Type, Defined | "primaryKey"> &
107
132
  BuildPrimaryKey<Type, Defined | "primaryKey">
108
133
  readonly default: (sql: string) => BuildColumn<Type, Defined | "default">
109
134
  readonly unique: BuildColumn<Type, Defined | "unique">
110
- readonly check: (expression: CheckExpression) => BuildColumn<Type, Defined | "check">
135
+ readonly check: (
136
+ expression: CheckExpression,
137
+ ) => BuildColumn<Type, Defined | "check">
111
138
  readonly foreignKey: BuildForeignKey<Type, Defined>
112
139
  readonly map: BuildColumnMap<Type, Defined>
113
140
  },