Skip to content

docs: FileDataAdapter recipe (ADR-0095 fulfillment) #629

Description

@SisyphusZheng

Summary

Provide a documented FileDataAdapter recipe showing build-time data loading from JSON/YAML files, fulfilling ADR-0095's recipe promise. The framework core must not import node:fs — this is a user-space pattern.

Competitive context

No mainstream framework bundles a file-data adapter internally. Astro uses content collections (build-time); SvelteKit/Next.js leave data loading to user code. openElement's ADR-0095 correctly defers this to recipe level but provides zero examples.

Proposed deliverable

  1. Recipe doc (docs/integrations/file-data-adapter.md):
import { readFileSync } from 'node:fs';
import type { DataAdapter } from '@openelement/element';

export class FileDataAdapter<T> implements DataAdapter<T> {
  readonly name = 'file';
  #dir: string;

  constructor(dir: string) { this.#dir = dir; }

  async get(key: string): Promise<T | undefined> {
    try {
      const raw = readFileSync(join(this.#dir, `${key}.json`), 'utf-8');
      return JSON.parse(raw) as T;
    } catch { return undefined; }
  }

  async keys(): Promise<string[]> {
    return readdirSync(this.#dir)
      .filter(f => f.endsWith('.json'))
      .map(f => f.replace('.json', ''));
  }
}
  1. Usage in loader:
const db = new FileDataAdapter<Product>('./data/products');

export const loader = async ({ params }) => {
  return { product: await db.get(params.id) };
};

Non-goals

  • No @openelement/file-adapter package
  • No watch/hot-reload (build-time only)
  • No database migration

Target version

0.43.0 (documentation)

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions