Storage

Setup different storages for your CMS

The function cms.storage() allows to register different storages to save the site data. When a storage is registered, it must have a name that will be used later in your documents, collections and uploads.

LumeCMS supports several types of storages.

File system

Tip

Lume automatically configure the src storage pointing to the src folder. So you don't need to setup a file system storage.

To register a filesystem storage, import the class and register it with a name. For example, let's register a file system storage under the name "my_files", pointing to the ./files folder:

import createCMS from "@lumeland/cms";
import Fs from "@lumeland/cms/storage/fs.ts";

const cms = createCMS();
const storage = Fs.create(process.cwd() + "/files");

cms.storage("src", storage);

export default cms;

The my_files storage is now available to read and write content by the CMS. Because the file system is the most common way to read and write data, the example above can be simplified as following:

import createCMS from "@lumeland/cms";

const cms = createCMS();

cms.storage("src", "files");

export default cms;

As you can see, if you define a string as the storage, LumeCMS assume it's a folder name, so the file system storage is used automatically. Note also that you don't need to prepend process.cwd() to the folder name, because the value of root option (which by default is process.cwd()) is used as the base path.

GitHub

Allows to write and read values directly from a GitHub repository using the GitHub API. It's useful if you want to allow people without GitHub accounts or permission to edit the site data.

import createCMS from "@lumeland/cms";
import GitHub from "@lumeland/cms-github-storage";

const cms = createCMS();

const token = "xxx"; // A personal access token
const storage = GitHub.create("username/repo", token);

cms.storage("src", storage);

export default cms;

If you want to set a different root path:

GitHub.create("username/repo/path/to/root", token);

Generate a GitHub access token

  1. Log into GitHub and make sure you have permissions to manage the repository.
  2. Go to Fine-grained tokens and press Generate new token.
  3. Set a name and expiration date for the token.
  4. In Repository access section, select Only select repositories and add the repository that you want to manage.
  5. Open Repository permissions and select Read and write access level in the Contents section.
  6. Press Generate token.

GitLab

It's similar to GitHub but for GitLab:

import createCMS from "@lumeland/cms";
import GitLab from "@lumeland/cms/storage/gitlab.ts";

const cms = createCMS();

const token = "xxx"; // A personal access token
const storage = GitLab.create("username/repo", token);

cms.storage("src", storage);

export default cms;

Generate a GitLab access token

  1. Log into GitLab and make sure you have permissions to manage the repository.
  2. Go to Personal access tokens and press Add new token.
  3. Set a name and expiration date for the token.
  4. In Select scopes section, select api
  5. Press Create token.

Codeberg

It's similar to GitHub but for Codeberg:

import createCMS from "@lumeland/cms";
import Codeberg from "@lumeland/cms/storage/codeberg.ts";

const cms = createCMS();

const token = "xxx"; // A personal access token
const storage = Codeberg.create("username/repo", token);

cms.storage("src", storage);

export default cms;

Generate a Codeberg access token

  1. Log into Codeberg and make sure you have permissions to manage the repository.
  2. Go to Access tokens.
  3. Set a token name.
  4. Open Select permissions and in the repository selector, select "Read and write"
  5. Press Generate token.

Sqlite

Use a Sqlite database which is natively supported by Node. You can pass the DatabaseSync instance or a path to the sqlite file:

import createCMS from "@lumeland/cms";
import { Sqlite } from "@lumeland/cms/storage/sqlite.ts";

const cms = createCMS();

cms.storage("db", Sqlite.create("my-database.db"));

export default cms;

All tables MUST have the id field used to identify the rows.

To create a collection in Sqlite:

cms.collection({
  name: "posts",
  store: "db:posts(title)",
  fields: [...]
});

The store must have the table name (posts) and the field name used to list the elements (in the example, we use the field title). If it's not specified, the id field is used.

Memory

It creates an in-memory storage. Useful for testing purposes.

import createCMS from "@lumeland/cms";
import Memory from "@lumeland/cms/storage/memory.ts";

const cms = createCMS();

const storage = Memory.create();

cms.storage("src", storage);

export default cms;