Logo

Sign InGet Started

Docs

Docs

Typescript/Javascript client

Getting Started

yarn add @kernex/client

Initialize the client

Initialize the client.

TIP: We generate Typescript types for all the APIs automatically. Go to your app> Code, and just copy-paste them into your project.

import kernexClient from '@kernex/client';
    
// Get your app api key from your app > API Keys section
const KERNEX_APP_API_KEY = 'your_api_key';
// Get your app url from your app > Info > Base Endpoint
const KERNEX_APP_URL = 'your_app_url';
const client = kernexClient({
  appUrl: KERNEX_APP_URL,
  appApiKey: KERNEX_APP_API_KEY,
});

From now on, we will reference the initialized client as client

Typescript Types

The client allows you to provide the Typescript types for your resources, so that it will return the right Typescript types.

import kernexClient from '@kernex/client';

// Suppose you have a Note Resource defined as
interface Note {
  _id: string;
  createdAt: Date | string;
  updatedAt: Date | string;
  title: string;
  content: string;
  isPinned: string;
}

// Specify the resources, where the key is the resource slug, and the value is the Resource definition
type Resources = {
  'notes': Note;
};

// Get your app api key from your app > API Keys section
const KERNEX_APP_API_KEY = 'your_api_key';
// Get your app url from your app > Info > Base Endpoint
const KERNEX_APP_URL = 'your_app_url';
const client = kernexClient<Resources>({
  appUrl: KERNEX_APP_URL,
  appApiKey: KERNEX_APP_API_KEY,
});

API Resource

In order to perform CRUD operations against your Resources, you have to use the resource function from the client

A Resource has methods that allow you to create, read, update, and remove entries.

// Suppose you have a Note Resource defined as
interface Note {
  _id: string;
  createdAt: Date | string;
  updatedAt: Date | string;
  title: string;
  content: string;
  isPinned: boolean;
}

// To initialize a Note Resource:
const notes = client.resource('notes');

Create Resource

Create a new Resource entry.

Params:

  • data(object): entry data

Returns:

  • entry(object): created entry
client.resource('notes').create({
  title: 'To-dos',
  content: '1. Test Kernex CMS.\n2. Fall in love with Kernex',
  isPinned: false,             
});

Get Resource

Get a Resource entry by the _id.

Params:

  • _id(string): entry id

Returns:

  • entry(object): entry

It will throw an error if the entry doesn't exist.

client.resource('notes').get('noteId').then((note) => {
  console.log(note);
});

Find Resources

Find resources.

Params:

  • query (object): filter query.
    • query.$limit (number, optional): the number of records to return. By default it will return 10 records. The maximum limit is 100.
    • query.$skip (number, optional): the number of records to skip. By default, the value is 0 (zero).
    • query.$select (array of strings, optional): the list of fields to include in the response. We recommend you to query only for the fields you need. By default, it will return all the fields. Also, the _id field will be returned ALWAYS, even if you don't include it in the $select array.
    • query.$sort (object, optional): an object where the keys are resource field names, and the values are either1 or-1, where1 will sort the entries by the specified field in ASCENDING order, and-1 will sort the entries by the specified field in DESCENDING order.
    • query.$join (array of objects, optional): join a list of related resources, where each object is defined as:
      • object.resource - the slug of the resource to join
      • object.on - the field name of the resource id on the current resource
      • object.as - the field name of the result
  • query[field: string] (any value, optional): field filter. Use this to filter the entries by this specific field.

Returns:

  • response: a paginated response, with the following properties:
    • data(array of entries) - the list of the entries matching your filters.
    • total(number) - the total number of records matching your filters.
    • limit(number) - the limit specified in your query
    • skip(number) - the number of skipped records

Unlike the get method, it will not throw an error if no entries are found.

// Find the first 5 pinned notes, sorted by the update date (most recently updated notes first)
client.resource('notes').find({
  $limit: 5,
  $skip: 0,
  $sort: {
    updatedAt: -1,
  },
  isPinned: true,
});

Patch Resource

Patch a Resource entry. Patching will update the specified fields in the data, while not changing the unspecified ones. If you want to update the entry entirely, then use the update method

Params:

  • _id (string): entry id
  • data(object): partial entry data.

Returns:

  • entry(object): patched entry. It will return the entire entry, with all the fields, even the ones not specified in patch data.
// Pin a note
client.resource('notes').patch('note-id', {
  isPinned: true,
});

Update Resource

Update a Resource entry. Updating will update the entry entirely. If you want to update only some fields, then use the patch method.

RECOMMENDATION: we recommend you to use the patch method instead of update.

Params:

  • _id (string): entry id
  • data(object): entry data.

Returns:

  • entry(object): updated entry. It will return the entire entry.
// Unpin a note
client.resource('notes').update('note-id', {
  title: 'To-dos',
  content: '1. Test Kernex CMS.\n2. Fall in love with Kernex',
  isPinned: false,  
  createdAt: new Date(),
  updatedAt: new Date(),
});

Remove Resource

Remove a Resource entry by the _id.

Params:

  • _id(string): entry id

Returns:

  • entry(object): removed entry

It will throw an error if the entry doesn't exist, or if it wasn't possible to remove it.

client.resource('notes').remove('noteId').then((note) => {
  console.log(note);
});