Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 20, 2022 08:17 pm GMT

Keystone.js 101


Photo by Bicanski on Pixnio

Introduction

Some time ago I was working on small project consisting website and app to collect research data. Its a kind of project I am typically working at for my clients. In most cases my stack for that kind of project consists of WordPress as headless CMS, Next.js front, and custom-built Node.js app for data collecting. But often I feel this is kind of overkill. Especially in that last case. So I decided to look for better solution, best in the way to solve CMS and data collecting app in one. After brief research Ive decided to stick to Keystone.js, it has all I need. But also it required a bit of customization (more about in previous articles, here and here). These customizations were so interesting Ive decided to write about them, but when I was writing last article I realized that they were pretty niche and specialized. And theres hardly any introduction to Keystone.js in its newest version, released at the end of last year. And thats why Ive decided to take brake from writing about custom fields and components and concentrate on simple, beginner-friendly introduction to Keystone.

New version of Keystone.js was released at the end of 2021. Basically its CMS system build with Express.js backend and admin UI in Next.js. But also its features packed including types safe Prisma ORM and TypeScript to elevate developer experience as stated in release blog post:

We extended Keystones foundations in Node, Express, and JavaScript, by adding Prisma to take care of the database layer and NextJS to power Keystones Admin UI. We also introduced a strongly-typed developer experience to make Keystone **the most advanced TypeScript Headless CMS**.

Thanks to usage of Prisma Keystone can generate migrations every time theres update in data schema. It definitely allows co concentrate on building application business logic without concern about low level implementation details. In some cases it may not be what we want but in general its nice to have it working out of the box. There may be some performance issues due to ORMs issue with 1+N queries, but it can be mitigated easily. Additionally, we have here built-in support for GraphQL what is always pleasant.

Ok, all that features are rally impressive, but what use case is prefect for Keystone? First its CMS, so managing content for websites, landing pages or blogs with custom frontends are usages where it shines. But not only, due to its wide possibilities of creating data structure, it can be used in many more cases. E.g., e-commercethe challenge is only in designing right models for cart, and products, additional functionalities like payment processing can be added using built in extensibility of underlying Express.js.

Installation

I think Its the high time to get our hands dirty and start our own Keystone instance.

Dependencies

Basically only thing we require to have is Node.js installed. Out of the box Keystone comes with SQLite, so theres no need for additional database. But always we can switch to PostgreSQL, for now I am gonna stick to first option in sake of simplicity.

Lets start

To begin with we only require one command. Open your terminal and type:

$ npx create-keystone-app

First, CLI will ask for name of our app, lets try simple-keystone-blog:

npx create-keystone-app output

Next, as prompted cd into that folder (assuming You are using Linux or Mac, if You need, use Windows equivalent commands), and start project with yarn dev and open http://localhost:3000 in browser.

Here You can set initial user account and login into admin dashboard. Out of the box there are two lists already created. User and Post, so we have all the basics to start writing a blog. Also, we can access all the data via GraphQL API or use sandbox accessible under http://localhost:3000/api/graphql.

Files structure

Ok, lets have a look on the files structure in our project:

Files structure

First two folders contains auto-generated views for admin UI, third dependencies to NPM packages. keystone.db, schema.graphql and schema.prisma are database itself, GraphQL and ORM configuration, each one is also auto-generated, based on our changes. Last three important files are keystone.ts, auth.ts and schema.ts. First contains whole configuration of our projectdatabase configuration and imported from other two auth (session) settings and lists definitions.

Auth settings work really nice out of the box, and I wont focus on them If You are curious of them check the docs. Much more interesting are lists settings, lets have a look on them.

Schemas

This file exports object of type of Lists, which contains configurations for all the lists visible (and invisible) in admin UI. There are three defined here: User, Post and Tag. Each one calls function list with additional configuration object. This object can have up to six properties, but only one is mandatoryfields. It holds list of all the fields in that list (with their additional config). Amount of field types here is not huge, but its enough for all basic needs (more), it contains fields for all data types, files and images, relations to other lists and rich text editor. Rest of the props holds more advance configuration options, like display options in ui prop, side effects associated with this list in hooks prop, additional database or GraphQL config in db and graphql. Additionally, some of this config properties can be used directly on the level of each field to extend their functionality.

All this config options gives us wide possibility to build complex schemas for each list including side effects allowing to integrate external tools and extend it further. In topic of extensibility there are two additional things worth mention. On top of all the fields in main config object (keystone.ts) we can modify setting of underlying Express.js server. Options here are pretty basic, except oneextendExpressApp. Basically this option allows us to add more REST endpoints or middleware to our backend. Second important option is extendGraphqlSchema, generally it does the same job as previous one, but allows adding custom GraphQL resolvers.

Summary

I know Ive barely scratched the surface of what really Keystone.js can do, but on the other hand I hope Ive showed the real potential in it. At first glance it looks like pretty basic CMS with limited options. But when we dig deeper there are tons of possibilities and after careful planing and clear requirements it may turn out that we dont need to build another custom system. Instead, use something nice as a base and extend it.

Thats the third article in series about Keystone.js. In this one Ive decided to take step back, and get to basics. I hope its enough to convince You to give it a try. Next week, Im getting back to series and custom components, this time we are going to extend JSON field and use it as data for building pages navigation menu.


Original Link: https://dev.to/eabald/keystonejs-101-3kbo

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To