Skip to main content

Overview

Codehooks: The complete yet simple backend platformโ€‹

You can create Codehooks projects using the command line or the web-based admin UI. Each project can have many spaces, which are complete self-contained environments with their own database, settings and code.

The codehooks-js library is the foundation for all Codehooks JavaScript development. This library contains a set of 6 specific APIs which simplifies and accelerates backend development. Click on each topic to learn more.

API development
REST, CRUD or custom APIs
NoSQL & Key-Value
Code triggered by CRON
Long running processes
File System
Serve static content
Data Aggregation
Groups & sums
Codehooks CLI tool

To deploy code (and much more), you use the Codehooks CLI tool:

npm install codehooks -g

Code editing and deployment will also soon be available in the web based UI.

API developmentโ€‹

Get a flying start with an automatic CRUD REST API

Consider a Codehooks project, for example myproject-fafb. Under the hood, Codehooks deploys an automatic REST API which is made up of the following really short - but complete application code:
import {app} from 'codehooks-js'
// Use Crudlify to create a REST API for any database collection
app.crudlify()
// bind to serverless runtime
export default app.init();
It's just 3 lines of code ๐Ÿ”ฅ, and it delivers a fully secure CRUD REST API with a NoSQL database for JSON queries and updates. Client apps can instantly connect to your data API, for example from a JavaScript app like the example code snippet shown below:
const URL = 'https://myproject-fafb.api.codehooks.io/dev/orders';
response = await fetch(URL+'?status=OPEN', /* options */)
const result = await response.json()

NoSQL and Key-Value Datastoresโ€‹

Easy database API inspired by MongoDB

Sometimes a standard CRUD REST API isn't enough. With custom JavaScript functions you can create logic that integrates directly to the NoSQL datastore. No need to think about drivers, versions, protocol or security, the database is just there, available as an intrinsic object in every serverless function.
import {app, datastore} from 'codehooks-js'
// REST API route
app.get('/sales', async (req, res) => {
const conn = await datastore.open();
const query = { status: "OPEN" };
const options = { sort: { date: 1 } }
conn.getMany('orders', query, options).json(res); // stream JSON to client
})
// bind to serverless runtime
export default app.init();

Speed up your application with a Key-Value cache

The Codehooks data engine includes a simple key-value datastore. A key-value datastore has many use-cases, and is ideal for caching due to the swift data access and simple, maintenance-friendly architecture. A simple example is shown below, where a string is stored for 60 seconds with the ttl option.
import {app, datastore} from 'codehooks-js'
// REST API route
app.post('/cacheit', async (req, res) => {
const conn = await datastore.open();
const opt = {ttl: 60*1000};
await conn.set('my_cache_key', '1 min value', opt);
res.send('Look dad, I have my own cache now!');
})
// bind to serverless runtime
export default app.init();

Background Jobsโ€‹

CRON expressions + your function keeps running

Use simple CRON expressions to automate and manage the execution of routine tasks. You can schedule a job to run at multiple, disparate times. For example, you can schedule a task to run at 2 AM on Sundays and 4 PM on Wednesdays with the same CRON job.

In the simple example below, the CRON expression */5 * * * * will trigger a JavaScript function each 5 minutes.

import app from 'codehooks-js'
// Trigger a function with a CRON expression
app.job('*/5 * * * *', (_, job) => {
console.log('Hello every 5 minutes')
// Do stuff ...
job.end()
});
export default app.init();

Queuesโ€‹

Scale workloads with queues and worker functions

Use persistent queues and "worker" functions to create robust, efficient, and highly responsive back-end systems. Queues scales processing across multiple nodes and will restore and continue working in case of errors and system failures.
Check out the example below, where a REST API request starts a complex task by adding it to the Queue for later processing.
import { app, Datastore } from 'codehooks-js'

// register a Queue task worker
app.worker('ticketToPDF', (req, res) => {
const { email, ticketid } = req.body.payload;
//TODO: implement code to fetch ticket data, produce PDF
// and send email with attachment using Mailgun, Sendgrid or Amazon SES
// ...and implement error handling
res.end(); // done processing queue item
})

// REST API
app.post('/createticket', async (req, res) => {
const { email, ticketid } = req.body;
const conn = await Datastore.open();
await conn.enqueue("ticketToPDF", { email, ticketid });
res.status(201).json({"message": `Check email ${email}`});
})

export default app.init();

File System & Blob storageโ€‹

Serve any document or file content

Codehooks has a built-in file system to support streaming file uploads and downloads. You can serve files via a static fileserver or via JavaScript functions using the File API. The example CLI command below shows how to upload a directory of files to the Blob storage dev space (environment) of the project. You can publish documents and files in two ways. Use the file-upload CLI command for mass uploads of files/directories:
$ coho file-upload --projectname 'myproject-fafc' --space 'dev' --src '/somedir/' --target '/myblobs'
Or deploy file content located as part of your server code with the deploy command:
$ coho deploy
The example below shows how to serve both deployed files and Blob storage content.
import {app} from 'codehooks-js'
// serve the assets directory of the deployed project source code
app.static({directory: "/assets"})

// serve a direcory of Blob storage files
app.storage({route:"/documents", directory: "/myblobs"})

// CRUD REST API
crudlify(app)
// bind to serverless runtime
export default app.init();

Data Aggregationโ€‹

Easy to use data aggregation format

Transform your live data streams into insights with just a few lines of code. Codehooks has a simple but powerful JSON-based aggregation expression language to specify data transformations. Groups and sums can be nested to create both simple and advanced data outputs. The aggregation API can process input from any JSON data-stream, for example the query output from the NoSQL datastore. ๐Ÿ”ฅ
import { app, Datastore, aggregation } from 'codehooks-js'
... // collapsed code
const spec = {
$group: {
$field: "month",
$max: ["sales", "winback"],
$sum: "sales"
}
}
const db = await datastore.open();
const dbstream = db.getMany('salesData');
const result = await aggregation(dbstream, spec)
... // collapsed code
The example aggreagation transformation produces JSON output similar to the example below.
{
"oct": {
"sales": {
"max": 1477.39,
"sum": 1234.05
},
"winback": {
"max": 22.0
}
},
"nov": {
"sales": {
"max": 2357.00,
"sum": 5432.00
},
"winback": {
"max": 91.0
}
},
}

Next Stepsโ€‹

Read this far? Great ๐Ÿ™Œ

It's time to get your feet wet ๐Ÿ’ฆ. Why don't you start thinking about how codehooks' toolbox can help you build your solution. You can get started with the quickstart for developers or just sign up and create your project and space using the web UI. You can easily upload some CSV data and immediately test your new API.

The documentation or tutorials are great places to start. If you need more help, feel free to contact us on the chat.