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

feat: basic glob fixture

This commit is contained in:
bholmesdev 2024-01-22 16:11:23 -05:00 committed by Nate Moore
parent 0e428df67a
commit 846088e7bf
5 changed files with 82 additions and 0 deletions

View file

@ -0,0 +1,30 @@
import { defineConfig } from 'astro/config';
import db, { defineCollection, field } from '@astrojs/db';
import glob from 'fast-glob';
import { readFile } from 'fs/promises';
const Quote = defineCollection({
fields: {
author: field.text(),
body: field.text(),
},
async data() {
const quotes = await glob('quotes/*.json');
return Promise.all(
quotes.map(async (quote) => {
const data = JSON.parse(await readFile(quote, 'utf-8'));
return {
author: data.author,
body: data.body,
};
})
);
},
});
export default defineConfig({
db: {
collections: { Quote },
},
integrations: [db()],
});

View file

@ -0,0 +1,19 @@
{
"name": "glob",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "astro dev",
"build": "astro build",
"preview": "astro preview"
},
"dependencies": {
"@astrojs/db": "workspace:*",
"astro": "workspace:*",
"fast-glob": "^3.3.2"
},
"keywords": [],
"author": "",
"license": "ISC"
}

View file

@ -0,0 +1,4 @@
{
"author": "Erika",
"body": "Put the quote in the database."
}

View file

@ -0,0 +1,4 @@
{
"author": "Tony Sull",
"body": "All content is data, but not all data is content."
}

View file

@ -0,0 +1,25 @@
---
/// <reference types="../../.astro/db-types.d.ts" />
import { Quote, db } from 'astro:db';
const quotes = await db.select().from(Quote);
---
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
{
quotes.map((q) => (
<figure>
<blockquote>{q.body}</blockquote>
<figcaption>{q.author}</figcaption>
</figure>
))
}
</body>
</html>