@@ -14,7 +14,7 @@ import type { CompiledQuery } from '../query-compiler/compiled-query.js'
1414import type { Compilable } from '../util/compilable.js'
1515import type { QueryExecutor } from '../query-executor/query-executor.js'
1616import type { QueryId } from '../util/query-id.js'
17- import { freeze } from '../util/object-utils.js'
17+ import { freeze , isString } from '../util/object-utils.js'
1818import type { Expression } from '../expression/expression.js'
1919import {
2020 type ComparisonOperatorExpression ,
@@ -106,73 +106,88 @@ export class CreateIndexBuilder<C = never>
106106 /**
107107 * Adds a column to the index.
108108 *
109- * Also see {@link columns} for adding multiple columns at once or {@link expression}
110- * for specifying an arbitrary expression.
109+ * Also see {@link columns} for adding multiple columns at once.
111110 *
112111 * ### Examples
113112 *
114113 * ```ts
114+ * import { sql } from 'kysely'
115+ *
115116 * await db.schema
116- * .createIndex('person_first_name_and_age_index')
117- * .on('person')
118- * .column('first_name')
119- * .column('age desc')
120- * .execute()
117+ * .createIndex('person_first_name_and_age_index')
118+ * .on('person')
119+ * .column('first_name')
120+ * .column<'last_name'>(sql`left(lower("last_name"), 1)`)
121+ * .column('age desc')
122+ * .where('last_name', 'is not', null)
123+ * .execute()
121124 * ```
122125 *
123126 * The generated SQL (PostgreSQL):
124127 *
125128 * ```sql
126- * create index "person_first_name_and_age_index" on "person" ("first_name", "age" desc)
129+ * create index "person_first_name_and_age_index"
130+ * on "person" ("first_name", left(lower("last_name"), 1), "age" desc)
131+ * where "last_name" is not null
127132 * ```
128133 */
129134 column < CL extends string > (
130135 column : OrderedColumnName < CL > ,
131- ) : CreateIndexBuilder < C | ExtractColumnNameFromOrderedColumnName < CL > > {
136+ ) : CreateIndexBuilder < C | ExtractColumnNameFromOrderedColumnName < CL > >
137+ column < CL extends string = never > (
138+ expression : Expression < any > ,
139+ ) : CreateIndexBuilder < C | CL >
140+ column ( arg : any ) : any {
132141 return new CreateIndexBuilder ( {
133142 ...this . #props,
134143 node : CreateIndexNode . cloneWithColumns ( this . #props. node , [
135- parseOrderedColumnName ( column ) ,
144+ isString ( arg ) ? parseOrderedColumnName ( arg ) : arg . toOperationNode ( ) ,
136145 ] ) ,
137146 } )
138147 }
139148
140149 /**
141- * Specifies a list of columns for the index.
150+ * Adds a list of columns to the index.
142151 *
143- * Also see {@link column} for adding a single column or {@link expression} for
144- * specifying an arbitrary expression.
152+ * Also see {@link column} for adding a single column.
145153 *
146154 * ### Examples
147155 *
148156 * ```ts
157+ * import { sql } from 'kysely'
158+ *
149159 * await db.schema
150- * .createIndex('person_first_name_and_age_index')
151- * .on('person')
152- * .columns(['first_name', 'age desc'])
153- * .execute()
160+ * .createIndex('person_first_name_and_age_index')
161+ * .on('person')
162+ * .columns(['first_name', sql`left(lower("last_name"), 1)` , 'age desc'])
163+ * .execute()
154164 * ```
155165 *
156166 * The generated SQL (PostgreSQL):
157167 *
158168 * ```sql
159- * create index "person_first_name_and_age_index" on "person" ("first_name", "age" desc)
169+ * create index "person_first_name_and_age_index"
170+ * on "person" ("first_name", left(lower("last_name"), 1), "age" desc)
160171 * ```
161172 */
162173 columns < CL extends string > (
163- columns : OrderedColumnName < CL > [ ] ,
174+ columns : ( OrderedColumnName < CL > | Expression < any > ) [ ] ,
164175 ) : CreateIndexBuilder < C | ExtractColumnNameFromOrderedColumnName < CL > > {
165176 return new CreateIndexBuilder ( {
166177 ...this . #props,
167178 node : CreateIndexNode . cloneWithColumns (
168179 this . #props. node ,
169- columns . map ( parseOrderedColumnName ) ,
180+ columns . map ( ( item ) =>
181+ isString ( item )
182+ ? parseOrderedColumnName ( item )
183+ : item . toOperationNode ( ) ,
184+ ) ,
170185 ) ,
171186 } )
172187 }
173188
174189 /**
175- * Specifies an arbitrary expression for the index.
190+ * Adds an arbitrary expression as a column to the index.
176191 *
177192 * ### Examples
178193 *
@@ -183,15 +198,20 @@ export class CreateIndexBuilder<C = never>
183198 * .createIndex('person_first_name_index')
184199 * .on('person')
185200 * .expression(sql`first_name COLLATE "fi_FI"`)
201+ * .column('gender')
186202 * .execute()
187203 * ```
188204 *
189205 * The generated SQL (PostgreSQL):
190206 *
191207 * ```sql
192- * create index "person_first_name_index" on "person" (first_name COLLATE "fi_FI")
208+ * create index "person_first_name_index"
209+ * on "person" (first_name COLLATE "fi_FI", "gender")
193210 * ```
211+ *
212+ * @deprecated Use {@link column} or {@link columns} with an {@link Expression} instead.
194213 */
214+ // TODO: remove in v0.30
195215 expression ( expression : Expression < any > ) : CreateIndexBuilder < C > {
196216 return new CreateIndexBuilder ( {
197217 ...this . #props,
@@ -203,6 +223,23 @@ export class CreateIndexBuilder<C = never>
203223
204224 /**
205225 * Specifies the index type.
226+ *
227+ * ### Examples
228+ *
229+ * ```ts
230+ * await db.schema
231+ * .createIndex('person_first_name_index')
232+ * .on('person')
233+ * .column('first_name')
234+ * .using('hash')
235+ * .execute()
236+ * ```
237+ *
238+ * The generated SQL (MySQL):
239+ *
240+ * ```sql
241+ * create index `person_first_name_index` on `person` (`first_name`) using hash
242+ * ```
206243 */
207244 using ( indexType : IndexType ) : CreateIndexBuilder < C >
208245 using ( indexType : string ) : CreateIndexBuilder < C >
@@ -218,6 +255,8 @@ export class CreateIndexBuilder<C = never>
218255 /**
219256 * Adds a where clause to the query. This Effectively turns the index partial.
220257 *
258+ * This is only supported by some dialects like PostgreSQL and MS SQL Server.
259+ *
221260 * ### Examples
222261 *
223262 * ```ts
0 commit comments