Fixes https://github.com/payloadcms/payload/issues/8752 Previously, trying to define a config like this: ```ts { type: 'text', name: 'someText', index: true, }, { type: 'array', name: 'some', index: true, fields: [ { type: 'text', name: 'text', index: true, }, ], } ``` Lead to the error: ``` Warning We've found duplicated index name across public schema. Please rename your index in either the demonstration table or the table with the duplicated index name ``` Now, if we encounter duplicates, we increment the name like this: `collection_some_text_idx` `collection_some_text_1_idx` --------- Co-authored-by: Dan Ribbens <dan.ribbens@gmail.com>
28 lines
828 B
TypeScript
28 lines
828 B
TypeScript
import type { AnySQLiteColumn } from 'drizzle-orm/sqlite-core'
|
|
|
|
import { index, uniqueIndex } from 'drizzle-orm/sqlite-core'
|
|
|
|
type CreateIndexArgs = {
|
|
indexName: string
|
|
name: string | string[]
|
|
unique?: boolean
|
|
}
|
|
|
|
export const createIndex = ({ name, indexName, unique }: CreateIndexArgs) => {
|
|
return (table: { [x: string]: AnySQLiteColumn }) => {
|
|
let columns
|
|
if (Array.isArray(name)) {
|
|
columns = name
|
|
.map((columnName) => table[columnName])
|
|
// exclude fields were included in compound indexes but do not exist on the table
|
|
.filter((col) => typeof col !== 'undefined')
|
|
} else {
|
|
columns = [table[name]]
|
|
}
|
|
if (unique) {
|
|
return uniqueIndex(indexName).on(columns[0], ...columns.slice(1))
|
|
}
|
|
return index(indexName).on(columns[0], ...columns.slice(1))
|
|
}
|
|
}
|