Skip to content
Luna
Esc
↑↓navigate↵open⌘Jpreview
On this page

Define a luna

Define a luna type with defineLuna, choose its key and make it reachable from your app.

A luna type is the code for one kind of thing in your app, such as a list, a board or a user. Each luna is one of those things, with its own key and its own data.

Define a luna type

Call defineLuna with a name, a key, the tables each luna keeps and its handlers.

import { defineLuna, t, table } from "luna";

export const List = defineLuna({
  name: "List",
  key: { id: t.string() },
  schema: {
    items: table({ id: t.id(), text: t.string() }),
  },
  handlers: (h) => ({
    add: h({ args: { text: t.string() } }, (ctx, { text }) => {
      ctx.db.items.insert({ text });
    }),
    all: h({ query: true }, (ctx) => ctx.db.items.find({ orderBy: "id" })),
  }),
});

Every list in the app is a List luna. The list called groceries and the list called chores each have their own items table.

Name it

The name is how callers reach the luna type, so it appears in every URL. It starts with a capital letter and holds letters, digits and underscores, up to 64 characters.

Two luna types in one app cannot share a name.

Choose its key

The key says which luna a call is for. Each key field is one of these types.

Type Holds
t.string() Text, such as a name or an id you made
t.int() A whole number
t.uuid() A UUID
t.bytes() Bytes, written as base64, URL-safe in a URL
t.enum([...]) One of the values you list

A key can have several fields. Fields are given in the order you declare them.

import { defineLuna, t } from "luna";

export const Card = defineLuna({
  name: "Card",
  key: { board: t.string(), card: t.int() },
  handlers: (h) => ({
    show: h({ query: true }, (ctx) => ctx.key),
  }),
});

A luna type with no key has exactly one luna, which suits settings that the whole app shares.

Inside a handler, ctx.key holds the key. A handler argument cannot have the same name as a key field.

Choose key fields with care, because they cannot change after the first deploy.

Call it over HTTP

Each key field is one segment of the URL, then the handler’s name.

curl -X POST "$LUNA_URL/Card/launch/7/show" -H "luna-key: $LUNA_KEY"

A luna type with no key is called as $LUNA_URL/<TYPE>/<HANDLER>. Encode a key that holds / or % with encodeURIComponent, so it stays one segment.

Make it public

A luna type is private unless you say otherwise. Only other lunas and your own server, with a secret token, can call a private type. A handler’s HTTP route can let others in, as Serve HTTP routes explains.

Set visibility: "public" to let apps call it with a publishable key.

export const List = defineLuna({
  name: "List",
  visibility: "public",
  key: { id: t.string() },
  // ...
});

Export it from your config

luna.config.ts names the module whose exports are your luna types. Every luna type it exports is deployed, and other exports are ignored.

import { defineConfig } from "luna";

export default defineConfig({ lunas: "./lunas/index.ts" });
export { List } from "./list.ts";
export { Card } from "./card.ts";

Keep the config and your lunas in a folder called luna at the top of your project. luna dev and luna deploy find it there.

Run code as it wakes and sleeps

Hooks run at points in a luna’s life. They live under hooks.

export const List = defineLuna({
  name: "List",
  key: { id: t.string() },
  hooks: {
    onCreate: (ctx) => console.log("new list", ctx.key.id),
    onWake: (ctx) => console.log("awake", ctx.key.id),
  },
  handlers: (h) => ({}),
});

The hooks of a luna’s life are onCreate, onWake, onSleep, onDestroy and onMigrate. Connections add onBeforeConnect, onConnect, onDisconnect and onMessage, and sandboxes add onSshConnect. A hook cannot be called over HTTP.

Let it sleep sooner or later

A luna sleeps after 30 seconds without calls. Set idleTimeout to change that, as milliseconds or a duration such as "5m".

export const List = defineLuna({
  name: "List",
  key: { id: t.string() },
  idleTimeout: "5m",
  handlers: (h) => ({}),
});

Other options

defineLuna takes more options, each covered with its feature. defineLuna lists them all.

Option Feature
stateless Types with no key and no tables
schema Tables
crons Schedules
on Events
streams Streams
pushes Realtime
blobs Files
isolates, sandboxes, internet Running code
limits The memory and CPU each luna may use

Next steps

Was this page helpful?