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:
parent
0e428df67a
commit
846088e7bf
5 changed files with 82 additions and 0 deletions
30
packages/db/test/fixtures/glob/astro.config.ts
vendored
Normal file
30
packages/db/test/fixtures/glob/astro.config.ts
vendored
Normal 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()],
|
||||
});
|
19
packages/db/test/fixtures/glob/package.json
vendored
Normal file
19
packages/db/test/fixtures/glob/package.json
vendored
Normal 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"
|
||||
}
|
4
packages/db/test/fixtures/glob/quotes/erika.json
vendored
Normal file
4
packages/db/test/fixtures/glob/quotes/erika.json
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"author": "Erika",
|
||||
"body": "Put the quote in the database."
|
||||
}
|
4
packages/db/test/fixtures/glob/quotes/tony.json
vendored
Normal file
4
packages/db/test/fixtures/glob/quotes/tony.json
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"author": "Tony Sull",
|
||||
"body": "All content is data, but not all data is content."
|
||||
}
|
25
packages/db/test/fixtures/glob/src/pages/index.astro
vendored
Normal file
25
packages/db/test/fixtures/glob/src/pages/index.astro
vendored
Normal 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>
|
Loading…
Reference in a new issue