0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-06 22:10:10 -05:00

feat(test): recipes example

This commit is contained in:
bholmesdev 2024-01-25 11:49:44 -05:00
parent 4864057f39
commit 253ad9bc2d
4 changed files with 96 additions and 0 deletions

View file

@ -0,0 +1,46 @@
import { defineConfig } from 'astro/config';
import db, { defineCollection, field } from '@astrojs/db';
const Recipe = defineCollection({
fields: {
id: field.number({ primaryKey: true }),
title: field.text(),
description: field.text(),
},
});
const Ingredient = defineCollection({
fields: {
id: field.number({ primaryKey: true }),
name: field.text(),
quantity: field.number(),
recipeId: field.text(),
},
});
export default defineConfig({
integrations: [db()],
db: {
collections: { Recipe, Ingredient },
async data({ db, Recipe, Ingredient }) {
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 });
await db.insert(Ingredient).values({ name: 'Eggs', quantity: 2, recipeId: pancakes.id });
await db.insert(Ingredient).values({ 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 });
await db.insert(Ingredient).values({ name: 'Eggs', quantity: 2, recipeId: pizza.id });
await db.insert(Ingredient).values({ name: 'Milk', quantity: 1, recipeId: pizza.id });
await db.insert(Ingredient).values({ name: 'Tomato Sauce', quantity: 1, recipeId: pizza.id });
},
},
});

View file

@ -0,0 +1,16 @@
{
"name": "@test/recipes",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@astrojs/db": "workspace:*",
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,25 @@
---
/// <reference path="../../.astro/db-types.d.ts" />
import { Recipe, Ingredient, db, eq } from 'astro:db';
const ingredientsByRecipe = await db
.select({
name: Ingredient.name,
recipeName: Recipe.title,
})
.from(Ingredient)
.innerJoin(Recipe, eq(Ingredient.recipeId, Recipe.id));
console.log(ingredientsByRecipe);
---
<h2>Shopping list</h2>
<ul>
{
ingredientsByRecipe.map(({ name, recipeName }) => (
<li>
{name} ({recipeName})
</li>
))
}
</ul>

View file

@ -3876,6 +3876,15 @@ importers:
specifier: ^3.3.2
version: 3.3.2
packages/db/test/fixtures/recipes:
dependencies:
'@astrojs/db':
specifier: ^0.1.2
version: link:../../..
astro:
specifier: ^4.2.4
version: link:../../../../astro
packages/db/test/fixtures/ticketing-example:
dependencies:
'@astrojs/check':