cockpit-orm
v0.4.0
Published
ORM for Cockpit Headless CMS
Downloads
3
Readme
Cockpit ORM
Create a representations of you collections and entries to manage data without manually fetching the Cockpit API.
npm install cockpit-orm
# or
yarn add cockpit-orm
Entry
The Entry class is for mapping one single entry of a collection.
- Create a new Entry class by extending
Entry
. - Use the schema given by Cockpit so your local entry knows the collection name and fields.
- Use a authenticated Cockpt SDK instance that will be called internally.
- Set an optional
slugField
to use a handy slug that will identify this Entry instead of_id
. When theslugField
is undefined the_id
field will be used.
import { Entry } from "cockpit-orm";
import schema from "../schemas/Portfolioitems.json";
import cockpit from "../"; // Initiated CockpitSDK instance.
class Post extends Entry {
static cockpit = cockpit;
static slugField = "title_slug";
static schema = {
// Cockpit collection schema.
name: "post",
fields: [{ name: "title", options: { slug: true } }, { name: "body" }]
};
}
Sync new Post by slug.
async () => {
const post = new Post();
post.setSlug("hello-work");
await post.sync(); // Fetches cockpit and fill all other fields.
post.get("title");
post.get("body");
};
Add new entry.
const post = new Post({ title: "Hello" });
post.get("title"); // Hello
post.has("body"); // false
post.save(); // POST to cockpit
Update entry.
async () => {
const post = new Post();
post.setSlug("hello-work");
await post.sync(); // Fetches cockpit and fill all other fields.
post.set("title", "New title");
post.set("body", "New body");
await post.save(); // POST to cockpit
};
Collection
The Collection
class is for fetching the whole collection entries.
import { Collection } from "cockpit-orm";
import Entry from "./Entry";
import cockpit from "../"; // Initiated CockpitSDK instance.
export default class PostCollection extends Collection {
static collectionName = "Posts";
static slugField = "title_slug";
static cockpit = cockpit;
}
Get entries
const posts = new PostCollection();
const entries = posts.getEntries();
Custom methods
export default class PostCollection extends Collection {
//...
getPublished(fields) {
return this.getObject(fields, { filter: { published: true } });
}
}
const entries = new PostCollection().getPublished();