mirror of
https://github.com/withastro/astro.git
synced 2025-02-10 22:38:53 -05:00
feat: add foreignkeys to create table
This commit is contained in:
parent
a016e67a3c
commit
338a1afb5c
2 changed files with 36 additions and 2 deletions
|
@ -87,6 +87,8 @@ export function getCreateTableQuery(collectionName: string, collection: DBCollec
|
||||||
colQueries.push(colQuery);
|
colQueries.push(colQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
colQueries.push(...getCreateForeignKeyQueries(collectionName, collection));
|
||||||
|
|
||||||
query += colQueries.join(', ') + ')';
|
query += colQueries.join(', ') + ')';
|
||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
|
@ -97,7 +99,7 @@ export function getCreateIndexQueries(
|
||||||
) {
|
) {
|
||||||
let queries: string[] = [];
|
let queries: string[] = [];
|
||||||
for (const [indexName, indexProps] of Object.entries(collection.indexes ?? {})) {
|
for (const [indexName, indexProps] of Object.entries(collection.indexes ?? {})) {
|
||||||
const onColNames = Array.isArray(indexProps.on) ? indexProps.on : [indexProps.on];
|
const onColNames = asArray(indexProps.on);
|
||||||
const onCols = onColNames.map((colName) => sqlite.escapeName(colName));
|
const onCols = onColNames.map((colName) => sqlite.escapeName(colName));
|
||||||
|
|
||||||
const unique = indexProps.unique ? 'UNIQUE ' : '';
|
const unique = indexProps.unique ? 'UNIQUE ' : '';
|
||||||
|
@ -109,6 +111,37 @@ export function getCreateIndexQueries(
|
||||||
return queries;
|
return queries;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getCreateForeignKeyQueries(collectionName: string, collection: DBCollection) {
|
||||||
|
let queries: string[] = [];
|
||||||
|
for (const foreignKey of collection.foreignKeys ?? []) {
|
||||||
|
const fields = asArray(foreignKey.fields);
|
||||||
|
const references = asArray(foreignKey.references());
|
||||||
|
|
||||||
|
if (fields.length !== references.length) {
|
||||||
|
throw new Error(
|
||||||
|
`Foreign key on ${collectionName} is misconfigured. \`fields\` and \`references\` must be the same length.`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const referencedCollection = references[0]?.collection;
|
||||||
|
if (!referencedCollection) {
|
||||||
|
throw new Error(
|
||||||
|
`Foreign key on ${collectionName} is misconfigured. \`references\` cannot be empty.`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const query = `FOREIGN KEY (${fields
|
||||||
|
.map((f) => sqlite.escapeName(f))
|
||||||
|
.join(', ')}) REFERENCES ${sqlite.escapeName(referencedCollection)}(${references
|
||||||
|
.map((r) => sqlite.escapeName(r.name!))
|
||||||
|
.join(', ')})`;
|
||||||
|
queries.push(query);
|
||||||
|
}
|
||||||
|
return queries;
|
||||||
|
}
|
||||||
|
|
||||||
|
function asArray<T>(value: T | T[]) {
|
||||||
|
return Array.isArray(value) ? value : [value];
|
||||||
|
}
|
||||||
|
|
||||||
export function schemaTypeToSqlType(type: FieldType): 'text' | 'integer' {
|
export function schemaTypeToSqlType(type: FieldType): 'text' | 'integer' {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'date':
|
case 'date':
|
||||||
|
|
|
@ -14,11 +14,12 @@ const Ingredient = defineCollection({
|
||||||
id: field.number({ primaryKey: true }),
|
id: field.number({ primaryKey: true }),
|
||||||
name: field.text(),
|
name: field.text(),
|
||||||
quantity: field.number(),
|
quantity: field.number(),
|
||||||
recipeId: field.text({ references: () => Recipe.fields.id }),
|
recipeId: field.number(),
|
||||||
},
|
},
|
||||||
indexes: {
|
indexes: {
|
||||||
recipeIdx: { on: 'recipeId' },
|
recipeIdx: { on: 'recipeId' },
|
||||||
},
|
},
|
||||||
|
foreignKeys: [{ fields: 'recipeId', references: () => [Recipe.fields.id] }],
|
||||||
});
|
});
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
|
|
Loading…
Add table
Reference in a new issue