npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

astro-pocketbase-loader

v0.1.1

Published

A content loader for Astro that uses the PocketBase API

Downloads

109

Readme

astro-loader-pocketbase

NPM Version NPM Downloads GitHub License Discord

This package is a simple loader to load data from a PocketBase database into Astro using the Astro Loader API introduced in Astro 5.

Basic usage

In your content configuration file, you can use the pocketbaseLoader function to use your PocketBase database as a data source.

import { pocketbaseLoader } from "astro-loader-pocketbase";
import { defineCollection } from "astro:content";

const blog = defineCollection({
  loader: pocketbaseLoader({
    url: "https://<your-pocketbase-url>",
    collectionName: "<collection-in-pocketbase>"
  })
});

export const collections = { blog };

By default, the loader will only fetch entries that have been modified since the last build. Remember that due to the nature Astros Content Layer lifecycle, the loader will only fetch entries at build time, even when using on-demand rendering. If you want to update your deployed site with new entries, you need to rebuild it.

When running the dev server, you can trigger a reload by using s + enter.

Entries

After generating the schema (see below), the loader will automatically parse the content of the entries (e.g. transform ISO dates to Date objects, coerce numbers, etc.).

HTML content

You can also specify a field or an array of fields to use as content. This content will then be used when calling the render function of Astros content collections.

const blog = defineCollection({
  loader: pocketbaseLoader({
    ...options,
    content: "<field-in-collection>"
  })
});

Since the goal of the render function is to render the content as HTML, it's recommended to use a field of type editor (rich text) in PocketBase as content.

If you specify an array of fields, the loader will wrap the content of these fields in a <section> and concatenate them.

Images and files

PocketBase can store images and files for each entry in a collection. While the API only returns the filenames of these images and files, the loader will out of the box transform these filenames to the actual URLs of the files. This doesn't mean that the files are downloaded during the build process. But you can directly use these URLs in your Astro components to display images or link to the files.

Type generation

The loader can automatically generate types for your collection. This is useful for type checking and autocompletion in your editor. These types can be generated in two ways:

Remote schema

To use the lice remote schema, you need to provide the email and password of an admin of the PocketBase instance.

const blog = defineCollection({
  loader: pocketbaseLoader({
    ...options,
    adminEmail: "<admin-email>",
    adminPassword: "<admin-password>"
  })
});

Under the hood, the loader will use the PocketBase API to fetch the schema of your collection and generate types with Zod based on that schema.

Local schema

If you don't want to provide the admin credentials (e.g. in a public repository), you can also provide the schema manually via a JSON file.

const blog = defineCollection({
  loader: pocketbaseLoader({
    ...options,
    localSchema: "<path-to-schema-file>"
  })
});

In PocketBase you can export the schema of the whole database to a pb_schema.json file. If you provide the path to this file, the loader will use this schema to generate the types locally.

When admin credentials are provided, the loader will always use the remote schema since it's more up-to-date.

Manual schema

If you don't want to use the automatic type generation, you can also provide your own schema manually. This manual schema will always override the automatic type generation.

All options

| Option | Type | Required | Description | | ---------------- | ----------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------- | | url | string | x | The URL of your PocketBase instance. | | collectionName | string | x | The name of the collection in your PocketBase instance. | | content | string \| Array<string> | | The field in the collection to use as content. This can also be an array of fields. | | adminEmail | string | | The email of the admin of the PocketBase instance. This is used for automatic type generation and access to private collections. | | adminPassword | string | | The password of the admin of the PocketBase instance. This is used for automatic type generation and access to private collections. | | id | string | | The name of entry field to be used as a unique identifier. Defaults to entry ID. | | localSchema | string | | The path to a local schema file. This is used for automatic type generation. | | jsonSchemas | Record<string, z.ZodSchema> | | A record of Zod schemas to use for type generation of json fields. | | forceUpdate | boolean | | If set to true, the loader will fetch every entry instead of only the ones modified since the last build. |

Special cases

Private collections

If you want to access a private collection, you also need to provide the admin credentials. Otherwise, you need to make the collection public in the PocketBase dashboard.

Generally, it's not recommended to use private collections, especially when users should be able to see images or other files stored in the collection.

View collections

Out of the box, the loader also supports collections with the type view, though with some limitations. To enable incremental builds, the loader needs to know when an entry has been modified. Normal base collections have a updated field that is automatically updated when an entry is modified. Thus, view collections that don't include this field can't be incrementally built but will be fetched every time.

You can also alias another field as updated (as long as it's a date field) in your view. While this is possible, it's not recommended since it can lead to outdated data not being fetched.

JSON fields

PocketBase can store arbitrary JSON data in a json field. While this data is checked to be valid JSON, there's no schema attached or enforced. Theoretically, every entry in a collection can have a different structure in such a field. This is why by default the loader will treat these fields as unknown and will not generate types for them.

If you have your own schema for these fields, you can provide them via the jsonSchemas option. The keys of this record should be the field names of your json fields, while the values are Zod schemas. This way, the loader will also generate types for these fields and validate the data against these schemas. So be sure that the schema is correct, up-to-date and all entries in the collection adhere to it.