Skip to content

Commit

Permalink
Update seed to use export default function() instead of top-level awa…
Browse files Browse the repository at this point in the history
…it (#10334)

* Update fixtures seed files to export default function

* Call default export when running seed files

* Add changeset
  • Loading branch information
delucis authored Mar 7, 2024
1 parent 5a9dab2 commit bad9b58
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 81 deletions.
16 changes: 16 additions & 0 deletions .changeset/bright-students-worry.md
Original file line number Diff line number Diff line change
@@ -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' });
+ }
```
7 changes: 7 additions & 0 deletions packages/db/src/core/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions packages/db/src/runtime/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
}
Expand Down
30 changes: 16 additions & 14 deletions packages/db/test/fixtures/basics/db/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
]);
}
112 changes: 57 additions & 55 deletions packages/db/test/fixtures/recipes/db/seed.ts
Original file line number Diff line number Diff line change
@@ -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,
},
]);
}
18 changes: 10 additions & 8 deletions packages/db/test/fixtures/ticketing-example/db/seed.ts
Original file line number Diff line number Diff line change
@@ -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',
});
}

0 comments on commit bad9b58

Please sign in to comment.