Skip to content

Commit

Permalink
fix(db): validate column type before column schema (#10409)
Browse files Browse the repository at this point in the history
* fix(db): validate column type before column schema

* add changeset

* Add test for text foreign keys

---------

Co-authored-by: Matthew Phillips <matthew@skypack.dev>
  • Loading branch information
lilnasy and matthewp authored Mar 13, 2024
1 parent c9cde7e commit 96c8bca
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-tools-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---

Fixes an issue where one table schema could not reference text fields of another table schema.
2 changes: 1 addition & 1 deletion packages/db/src/core/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export const jsonColumnSchema = z.object({
}),
});

export const columnSchema = z.union([
export const columnSchema = z.discriminatedUnion("type", [
booleanColumnSchema,
numberColumnSchema,
textColumnSchema,
Expand Down
8 changes: 8 additions & 0 deletions packages/db/test/basics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,13 @@ describe('astro:db', () => {
const themeDark = $($('.themes-list .theme-dark')[0]).text();
expect(themeDark).to.equal('dark mode');
});

it('text fields an be used as references', async () => {
const html = await fixture.fetch('/login').then((res) => res.text());
const $ = cheerioLoad(html);

expect($('.session-id').text()).to.equal('12345');
expect($('.username').text()).to.equal('Mario');
});
});
});
3 changes: 3 additions & 0 deletions packages/db/test/fixtures/basics/astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ import { defineConfig } from 'astro/config';
// https://astro.build/config
export default defineConfig({
integrations: [db()],
devToolbar: {
enabled: false,
}
});
18 changes: 17 additions & 1 deletion packages/db/test/fixtures/basics/db/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ const Author = defineTable({
},
});

const User = defineTable({
columns: {
id: column.text({ primaryKey: true, optional: false }),
username: column.text({ optional: false, unique: true }),
password: column.text({ optional: false }),
},
});

const Session = defineTable({
columns: {
id: column.text({ primaryKey: true, optional: false }),
expiresAt: column.number({ optional: false, name: "expires_at" }),
userId: column.text({ optional: false, references: () => User.columns.id, name: "user_id" }),
},
});

export default defineDb({
tables: { Author, Themes },
tables: { Author, Themes, User, Session },
});
12 changes: 11 additions & 1 deletion packages/db/test/fixtures/basics/db/seed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { asDrizzleTable } from '@astrojs/db/utils';
import { Themes as ThemesConfig } from './theme';
import { Author, db } from 'astro:db';
import { Author, User, Session, db } from 'astro:db';

const Themes = asDrizzleTable('Themes', ThemesConfig);
export default async function () {
Expand All @@ -18,5 +18,15 @@ export default async function () {
{ name: 'Bjorn' },
{ name: 'Sarah' },
]),
db
.insert(User)
.values([
{ id: 'mario', username: 'Mario', password: 'itsame' }
]),
db
.insert(Session)
.values([
{ id: '12345', expiresAt: new Date().valueOf(), userId: 'mario' }
]),
]);
}
18 changes: 18 additions & 0 deletions packages/db/test/fixtures/basics/src/pages/login.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
import { db, Session, User, eq } from 'astro:db';
const users = await db.select().from(User);
const sessions = await db.select()
.from(Session)
.innerJoin(User, eq(Session.userId, User.id));
---

<h2>Sessions</h2>
<ul class="sessions-list">
{sessions.map(({ Session, User }) => (
<li>
<div class="session-id">{Session.id}</div>
<div class="username">{User.username}</div>
</li>
))}
</ul>

0 comments on commit 96c8bca

Please sign in to comment.