Skip to content

Commit bad9b58

Browse files
authored
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
1 parent 5a9dab2 commit bad9b58

File tree

6 files changed

+117
-81
lines changed

6 files changed

+117
-81
lines changed

.changeset/bright-students-worry.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
"@astrojs/db": minor
3+
---
4+
5+
Changes the seed file format to require exporting a default function instead of running seed code at the top level.
6+
7+
To migrate a seed file, wrap your existing code in a default function export:
8+
9+
```diff
10+
// db/seed.ts
11+
import { db, Table } from 'astro:db';
12+
13+
+ export default async function() {
14+
await db.insert(Table).values({ foo: 'bar' });
15+
+ }
16+
```

packages/db/src/core/errors.ts

+7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ export const SEED_ERROR = (error: string) => {
3737
return `${red(`Error while seeding database:`)}\n\n${error}`;
3838
};
3939

40+
export const SEED_DEFAULT_EXPORT_ERROR = (fileName: string) => {
41+
return (
42+
red('Error while seeding database:') +
43+
`\n\nMissing default function export in ${bold(fileName)}`
44+
);
45+
};
46+
4047
export const REFERENCE_DNE_ERROR = (columnName: string) => {
4148
return `Column ${bold(
4249
columnName

packages/db/src/runtime/queries.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
FOREIGN_KEY_REFERENCES_EMPTY_ERROR,
88
FOREIGN_KEY_REFERENCES_LENGTH_ERROR,
99
REFERENCE_DNE_ERROR,
10+
SEED_DEFAULT_EXPORT_ERROR,
1011
SEED_ERROR,
1112
} from '../core/errors.js';
1213
import type {
@@ -35,19 +36,25 @@ export async function seedLocal({
3536
}: {
3637
db: SqliteDB;
3738
tables: DBTables;
38-
fileGlob: Record<string, () => Promise<void>>;
39+
fileGlob: Record<string, () => Promise<{ default?: () => Promise<void> }>>;
3940
}) {
4041
await recreateTables({ db, tables });
4142
for (const fileName of SEED_DEV_FILE_NAME) {
4243
const key = Object.keys(fileGlob).find((f) => f.endsWith(fileName));
4344
if (key) {
44-
await fileGlob[key]().catch((e) => {
45+
try {
46+
const mod = await fileGlob[key]();
47+
if (!mod.default) {
48+
throw new Error(SEED_DEFAULT_EXPORT_ERROR(key));
49+
}
50+
await mod.default();
51+
} catch (e) {
4552
if (e instanceof LibsqlError) {
4653
throw new Error(SEED_ERROR(e.message));
4754
}
4855
throw e;
49-
});
50-
return;
56+
}
57+
break;
5158
}
5259
}
5360
}

packages/db/test/fixtures/basics/db/seed.ts

+16-14
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ import { asDrizzleTable } from '@astrojs/db/utils';
22
import { Themes as ThemesConfig } from './theme';
33
import { Author, db } from 'astro:db';
44

5-
const Themes = asDrizzleTable('Themes', ThemesConfig);
5+
export default async function () {
6+
const Themes = asDrizzleTable('Themes', ThemesConfig);
67

7-
await db
8-
.insert(Themes)
9-
.values([{ name: 'dracula' }, { name: 'monokai', added: new Date() }])
10-
.returning({ name: Themes.name });
11-
await db
12-
.insert(Author)
13-
.values([
14-
{ name: 'Ben' },
15-
{ name: 'Nate' },
16-
{ name: 'Erika' },
17-
{ name: 'Bjorn' },
18-
{ name: 'Sarah' },
19-
]);
8+
await db
9+
.insert(Themes)
10+
.values([{ name: 'dracula' }, { name: 'monokai', added: new Date() }])
11+
.returning({ name: Themes.name });
12+
await db
13+
.insert(Author)
14+
.values([
15+
{ name: 'Ben' },
16+
{ name: 'Nate' },
17+
{ name: 'Erika' },
18+
{ name: 'Bjorn' },
19+
{ name: 'Sarah' },
20+
]);
21+
}
+57-55
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,62 @@
11
import { Ingredient, Recipe, db } from 'astro:db';
22

3-
const pancakes = await db
4-
.insert(Recipe)
5-
.values({
6-
title: 'Pancakes',
7-
description: 'A delicious breakfast',
8-
})
9-
.returning()
10-
.get();
3+
export default async function () {
4+
const pancakes = await db
5+
.insert(Recipe)
6+
.values({
7+
title: 'Pancakes',
8+
description: 'A delicious breakfast',
9+
})
10+
.returning()
11+
.get();
1112

12-
await db.insert(Ingredient).values([
13-
{
14-
name: 'Flour',
15-
quantity: 1,
16-
recipeId: pancakes.id,
17-
},
18-
{
19-
name: 'Eggs',
20-
quantity: 2,
21-
recipeId: pancakes.id,
22-
},
23-
{
24-
name: 'Milk',
25-
quantity: 1,
26-
recipeId: pancakes.id,
27-
},
28-
]);
13+
await db.insert(Ingredient).values([
14+
{
15+
name: 'Flour',
16+
quantity: 1,
17+
recipeId: pancakes.id,
18+
},
19+
{
20+
name: 'Eggs',
21+
quantity: 2,
22+
recipeId: pancakes.id,
23+
},
24+
{
25+
name: 'Milk',
26+
quantity: 1,
27+
recipeId: pancakes.id,
28+
},
29+
]);
2930

30-
const pizza = await db
31-
.insert(Recipe)
32-
.values({
33-
title: 'Pizza',
34-
description: 'A delicious dinner',
35-
})
36-
.returning()
37-
.get();
31+
const pizza = await db
32+
.insert(Recipe)
33+
.values({
34+
title: 'Pizza',
35+
description: 'A delicious dinner',
36+
})
37+
.returning()
38+
.get();
3839

39-
await db.insert(Ingredient).values([
40-
{
41-
name: 'Flour',
42-
quantity: 1,
43-
recipeId: pizza.id,
44-
},
45-
{
46-
name: 'Eggs',
47-
quantity: 2,
48-
recipeId: pizza.id,
49-
},
50-
{
51-
name: 'Milk',
52-
quantity: 1,
53-
recipeId: pizza.id,
54-
},
55-
{
56-
name: 'Tomato Sauce',
57-
quantity: 1,
58-
recipeId: pizza.id,
59-
},
60-
]);
40+
await db.insert(Ingredient).values([
41+
{
42+
name: 'Flour',
43+
quantity: 1,
44+
recipeId: pizza.id,
45+
},
46+
{
47+
name: 'Eggs',
48+
quantity: 2,
49+
recipeId: pizza.id,
50+
},
51+
{
52+
name: 'Milk',
53+
quantity: 1,
54+
recipeId: pizza.id,
55+
},
56+
{
57+
name: 'Tomato Sauce',
58+
quantity: 1,
59+
recipeId: pizza.id,
60+
},
61+
]);
62+
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Event, db } from 'astro:db';
22

3-
await db.insert(Event).values({
4-
name: 'Sampha LIVE in Brooklyn',
5-
description:
6-
'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 🎹',
7-
date: new Date('2024-01-01'),
8-
ticketPrice: 10000,
9-
location: 'Brooklyn, NY',
10-
});
3+
export default async function () {
4+
await db.insert(Event).values({
5+
name: 'Sampha LIVE in Brooklyn',
6+
description:
7+
'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 🎹',
8+
date: new Date('2024-01-01'),
9+
ticketPrice: 10000,
10+
location: 'Brooklyn, NY',
11+
});
12+
}

0 commit comments

Comments
 (0)