-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
db: expose
isDbError()
utility (#10498)
* feat: expose isDbError * test: foreign key constraint error detection * fix(test); use isDbError * chore: changeset
- Loading branch information
1 parent
19e42c3
commit f0fc78c
Showing
10 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@astrojs/db": patch | ||
--- | ||
|
||
Expose `isDbError()` utility to handle database exceptions when querying. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { expect } from 'chai'; | ||
import { loadFixture } from '../../astro/test/test-utils.js'; | ||
|
||
const foreignKeyConstraintError = | ||
'LibsqlError: SQLITE_CONSTRAINT_FOREIGNKEY: FOREIGN KEY constraint failed'; | ||
|
||
describe('astro:db - error handling', () => { | ||
let fixture; | ||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: new URL('./fixtures/error-handling/', import.meta.url), | ||
}); | ||
}); | ||
|
||
describe('development', () => { | ||
let devServer; | ||
|
||
before(async () => { | ||
devServer = await fixture.startDevServer(); | ||
}); | ||
|
||
after(async () => { | ||
await devServer.stop(); | ||
}); | ||
|
||
it('Raises foreign key constraint LibsqlError', async () => { | ||
const json = await fixture.fetch('/foreign-key-constraint.json').then((res) => res.json()); | ||
expect(json.error).to.equal(foreignKeyConstraintError); | ||
}); | ||
}); | ||
|
||
describe('build', () => { | ||
before(async () => { | ||
await fixture.build(); | ||
}); | ||
|
||
it('Raises foreign key constraint LibsqlError', async () => { | ||
const json = await fixture.readFile('/foreign-key-constraint.json'); | ||
expect(JSON.parse(json).error).to.equal(foreignKeyConstraintError); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import db from '@astrojs/db'; | ||
import { defineConfig } from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
integrations: [db()], | ||
devToolbar: { | ||
enabled: false, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { column, defineDb, defineTable } from 'astro:db'; | ||
|
||
const Recipe = defineTable({ | ||
columns: { | ||
id: column.number({ primaryKey: true }), | ||
title: column.text(), | ||
description: column.text(), | ||
}, | ||
}); | ||
|
||
const Ingredient = defineTable({ | ||
columns: { | ||
id: column.number({ primaryKey: true }), | ||
name: column.text(), | ||
quantity: column.number(), | ||
recipeId: column.number(), | ||
}, | ||
indexes: { | ||
recipeIdx: { on: 'recipeId' }, | ||
}, | ||
foreignKeys: [{ columns: 'recipeId', references: () => [Recipe.columns.id] }], | ||
}); | ||
|
||
export default defineDb({ | ||
tables: { Recipe, Ingredient }, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Ingredient, Recipe, db } from 'astro:db'; | ||
|
||
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, | ||
}, | ||
]); | ||
|
||
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, | ||
}, | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "@test/error-handling", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"dev": "astro dev", | ||
"build": "astro build", | ||
"preview": "astro preview" | ||
}, | ||
"dependencies": { | ||
"@astrojs/db": "workspace:*", | ||
"astro": "workspace:*" | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/db/test/fixtures/error-handling/src/pages/foreign-key-constraint.json.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { APIRoute } from 'astro'; | ||
import { db, Ingredient, isDbError } from 'astro:db'; | ||
|
||
export const GET: APIRoute = async () => { | ||
try { | ||
await db.insert(Ingredient).values({ | ||
name: 'Flour', | ||
quantity: 1, | ||
// Trigger foreign key constraint error | ||
recipeId: 42, | ||
}); | ||
} catch (e) { | ||
if (isDbError(e)) { | ||
return new Response(JSON.stringify({ error: `LibsqlError: ${e.message}` })); | ||
} | ||
} | ||
return new Response(JSON.stringify({ error: 'Did not raise expected exception' })); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.