Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(db): validate column type before column schema #10409

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Loading