0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

Update seed to use export default function() instead of top-level await (#10334)

* Update fixtures seed files to export default function

* Call default export when running seed files

* Add changeset
This commit is contained in:
Chris Swithinbank 2024-03-07 19:04:16 +01:00 committed by GitHub
parent 5a9dab286f
commit bad9b583a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 117 additions and 81 deletions

View file

@ -0,0 +1,16 @@
---
"@astrojs/db": minor
---
Changes the seed file format to require exporting a default function instead of running seed code at the top level.
To migrate a seed file, wrap your existing code in a default function export:
```diff
// db/seed.ts
import { db, Table } from 'astro:db';
+ export default async function() {
await db.insert(Table).values({ foo: 'bar' });
+ }
```

View file

@ -37,6 +37,13 @@ export const SEED_ERROR = (error: string) => {
return `${red(`Error while seeding database:`)}\n\n${error}`;
};
export const SEED_DEFAULT_EXPORT_ERROR = (fileName: string) => {
return (
red('Error while seeding database:') +
`\n\nMissing default function export in ${bold(fileName)}`
);
};
export const REFERENCE_DNE_ERROR = (columnName: string) => {
return `Column ${bold(
columnName

View file

@ -7,6 +7,7 @@ import {
FOREIGN_KEY_REFERENCES_EMPTY_ERROR,
FOREIGN_KEY_REFERENCES_LENGTH_ERROR,
REFERENCE_DNE_ERROR,
SEED_DEFAULT_EXPORT_ERROR,
SEED_ERROR,
} from '../core/errors.js';
import type {
@ -35,19 +36,25 @@ export async function seedLocal({
}: {
db: SqliteDB;
tables: DBTables;
fileGlob: Record<string, () => Promise<void>>;
fileGlob: Record<string, () => Promise<{ default?: () => Promise<void> }>>;
}) {
await recreateTables({ db, tables });
for (const fileName of SEED_DEV_FILE_NAME) {
const key = Object.keys(fileGlob).find((f) => f.endsWith(fileName));
if (key) {
await fileGlob[key]().catch((e) => {
try {
const mod = await fileGlob[key]();
if (!mod.default) {
throw new Error(SEED_DEFAULT_EXPORT_ERROR(key));
}
await mod.default();
} catch (e) {
if (e instanceof LibsqlError) {
throw new Error(SEED_ERROR(e.message));
}
throw e;
});
return;
}
break;
}
}
}

View file

@ -2,18 +2,20 @@ import { asDrizzleTable } from '@astrojs/db/utils';
import { Themes as ThemesConfig } from './theme';
import { Author, db } from 'astro:db';
const Themes = asDrizzleTable('Themes', ThemesConfig);
export default async function () {
const Themes = asDrizzleTable('Themes', ThemesConfig);
await db
.insert(Themes)
.values([{ name: 'dracula' }, { name: 'monokai', added: new Date() }])
.returning({ name: Themes.name });
await db
.insert(Author)
.values([
{ name: 'Ben' },
{ name: 'Nate' },
{ name: 'Erika' },
{ name: 'Bjorn' },
{ name: 'Sarah' },
]);
await db
.insert(Themes)
.values([{ name: 'dracula' }, { name: 'monokai', added: new Date() }])
.returning({ name: Themes.name });
await db
.insert(Author)
.values([
{ name: 'Ben' },
{ name: 'Nate' },
{ name: 'Erika' },
{ name: 'Bjorn' },
{ name: 'Sarah' },
]);
}

View file

@ -1,60 +1,62 @@
import { Ingredient, Recipe, db } from 'astro:db';
const pancakes = await db
.insert(Recipe)
.values({
title: 'Pancakes',
description: 'A delicious breakfast',
})
.returning()
.get();
export default async function () {
const pancakes = await db
.insert(Recipe)
.values({
title: 'Pancakes',
description: 'A delicious breakfast',
})
.returning()
.get();
await db.insert(Ingredient).values([
{
name: 'Flour',
quantity: 1,
recipeId: pancakes.id,
},
{
name: 'Eggs',
quantity: 2,
recipeId: pancakes.id,
},
{
name: 'Milk',
quantity: 1,
recipeId: pancakes.id,
},
]);
await db.insert(Ingredient).values([
{
name: 'Flour',
quantity: 1,
recipeId: pancakes.id,
},
{
name: 'Eggs',
quantity: 2,
recipeId: pancakes.id,
},
{
name: 'Milk',
quantity: 1,
recipeId: pancakes.id,
},
]);
const pizza = await db
.insert(Recipe)
.values({
title: 'Pizza',
description: 'A delicious dinner',
})
.returning()
.get();
const pizza = await db
.insert(Recipe)
.values({
title: 'Pizza',
description: 'A delicious dinner',
})
.returning()
.get();
await db.insert(Ingredient).values([
{
name: 'Flour',
quantity: 1,
recipeId: pizza.id,
},
{
name: 'Eggs',
quantity: 2,
recipeId: pizza.id,
},
{
name: 'Milk',
quantity: 1,
recipeId: pizza.id,
},
{
name: 'Tomato Sauce',
quantity: 1,
recipeId: pizza.id,
},
]);
await db.insert(Ingredient).values([
{
name: 'Flour',
quantity: 1,
recipeId: pizza.id,
},
{
name: 'Eggs',
quantity: 2,
recipeId: pizza.id,
},
{
name: 'Milk',
quantity: 1,
recipeId: pizza.id,
},
{
name: 'Tomato Sauce',
quantity: 1,
recipeId: pizza.id,
},
]);
}

View file

@ -1,10 +1,12 @@
import { Event, db } from 'astro:db';
await db.insert(Event).values({
name: 'Sampha LIVE in Brooklyn',
description:
'Sampha is on tour with his new, flawless album Lahai. Come see the live performance outdoors in Prospect Park. Yes, there will be a grand piano 🎹',
date: new Date('2024-01-01'),
ticketPrice: 10000,
location: 'Brooklyn, NY',
});
export default async function () {
await db.insert(Event).values({
name: 'Sampha LIVE in Brooklyn',
description:
'Sampha is on tour with his new, flawless album Lahai. Come see the live performance outdoors in Prospect Park. Yes, there will be a grand piano 🎹',
date: new Date('2024-01-01'),
ticketPrice: 10000,
location: 'Brooklyn, NY',
});
}