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}`; 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) => { export const REFERENCE_DNE_ERROR = (columnName: string) => {
return `Column ${bold( return `Column ${bold(
columnName columnName

View file

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

View file

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

View file

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

View file

@ -1,10 +1,12 @@
import { Event, db } from 'astro:db'; import { Event, db } from 'astro:db';
await db.insert(Event).values({ export default async function () {
name: 'Sampha LIVE in Brooklyn', await db.insert(Event).values({
description: name: 'Sampha LIVE in Brooklyn',
'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 🎹', description:
date: new Date('2024-01-01'), '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 🎹',
ticketPrice: 10000, date: new Date('2024-01-01'),
location: 'Brooklyn, NY', ticketPrice: 10000,
}); location: 'Brooklyn, NY',
});
}