Docs
Docs
yarn add @kernex/client
npm install @kernex/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,
});
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
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,
});
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');
// Suppose you have a Note Resource defined as
// {
// _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 a new Resource entry.
Params:
data
(object): entry dataReturns:
entry
(object): created entryclient.resource('notes').create({
title: 'To-dos',
content: '1. Test Kernex CMS.\n2. Fall in love with Kernex',
isPinned: false,
});
client.resource('notes').create({
title: 'To-dos',
content: '1. Test Kernex CMS.\n2. Fall in love with Kernex',
isPinned: false,
});
Get a Resource entry by the _id
.
Params:
_id
(string): entry idReturns:
entry
(object): entryIt will throw an error if the entry doesn't exist.
client.resource('notes').get('noteId').then((note) => {
console.log(note);
});
client.resource('notes').get('noteId').then((note) => {
console.log(note);
});
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 joinobject.on
- the field name of the resource id on the current resourceobject.as
- the field name of the resultquery[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 queryskip
(number) - the number of skipped recordsUnlike 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,
});
// 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 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 iddata
(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,
});
// Pin a note
client.resource('notes').patch('note-id', {
isPinned: true,
});
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 iddata
(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(),
});
// 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 a Resource entry by the _id
.
Params:
_id
(string): entry idReturns:
entry
(object): removed entryIt 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);
});
client.resource('notes').remove('noteId').then((note) => {
console.log(note);
});