0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-04-07 23:41:43 -05:00

feat: add indexes to setupDbTables

This commit is contained in:
bholmesdev 2024-01-29 18:20:40 -05:00
parent 67a68e737d
commit 8439766ae2

View file

@ -101,7 +101,8 @@ export async function setupDbTables({
for (const [name, collection] of Object.entries(collections)) {
const dropQuery = sql.raw(`DROP TABLE IF EXISTS ${name}`);
const createQuery = sql.raw(getCreateTableQuery(name, collection));
setupQueries.push(dropQuery, createQuery);
const indexQueries = getTableIndexQueries(name, collection);
setupQueries.push(dropQuery, createQuery, ...indexQueries);
}
for (const q of setupQueries) {
await db.run(q);
@ -159,6 +160,23 @@ export function getCreateTableQuery(collectionName: string, collection: DBCollec
return query;
}
export function getTableIndexQueries(collectionName: string, collection: DBCollection) {
let queries: SQL[] = [];
for (const [indexName, indexProps] of Object.entries(collection.indexes ?? {})) {
const onColNames = Array.isArray(indexProps.on) ? indexProps.on : [indexProps.on];
const onCols = onColNames.map((colName) => sqlite.escapeName(colName));
const unique = indexProps.unique ? 'UNIQUE ' : '';
const indexQuery = sql.raw(
`CREATE ${unique}INDEX ${sqlite.escapeName(indexName)} ON ${sqlite.escapeName(
collectionName
)} (${onCols.join(', ')})`
);
queries.push(indexQuery);
}
return queries;
}
export function schemaTypeToSqlType(type: FieldType): 'text' | 'integer' {
switch (type) {
case 'date':