Programs access most of Membrane’s functionality by importing the "membrane"
module. This module is specific to each program and it’s regenerated every time
you change its schema or its
connections.
You can explore it by cmd+clicking on "membrane" to open the generated
membrane.d.ts. You’ll see the types documented on this page, plus any
additional types specific to that program’s schema.
Exported objects
state
Use this object to store data. Durable state is a core concept of Membrane.
Learn more about effectively using state by reading the
concepts section and the Managing State guide.
Example usage:
nodes
Every connection can be accessed via the nodes
object.
Values in nodes have accessors that match the node’s
schema so autocomplete works as expected.
From a node, you can access other reachable nodes, actions and events.
Example usage:
import {
const nodes: {
readonly user: github.User;
readonly clock:Clock;
readonly process:Process;
}
Contains the graph references (grefs) that this program has been given access to.
nodes } from"membrane"
asyncfunction
functionrun():Promise<void>
run() {
// This program has a connection named `user` to the GitHub driver
// so we can query it, invoke actions, subscribe to events, etc.
const
const bio:string
bio = await
const nodes: {
readonly user: github.User;
readonly clock:Clock;
readonly process:Process;
}
Contains the graph references (grefs) that this program has been given access to.
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without calling require('console').
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = newconsole.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Schedules the action to be invoked at a specific time. The invocation time can
be either a Date object or a UNIX timestamp (seconds since January 1st, 1970).
Schedules a timer to invoke the action after a specified delay
@param ― seconds The number of seconds to wait before invoking the action
$invokeIn(60*10)
}
$cron
Schedules a cron-like timer that invokes an action at configured times. Unlike
traditional cronjob expressions, Membrane has a resolution of seconds instead of
the traditional minutes, so expression has an extra slot to specify at which
second the action should be invoked.
Signature:
$cron(expression: string): void;
Cron expression syntax:
// At second zero
// | At minute zero
// | | At 5pm
// | | | Every day of month
// | | | | Every month
// | | | | | Only on Fridays
// | | | | | |
const everyFridayAt5pm = "0 0 17 * * Fri"
Valid values for each slot are:
Slot
Valid values
second
0-59
minute
0-59
hour
0-23
day of month
1-31
month
1-12 or Jan, Feb, Mar, Apr, etc.
day of week
0-7 or Sun, Mon, Tue, Wed, etc.
(both 0 and 7 represent Sunday)
Each slot can also accept * to mean “every possible value”.
Note that the time zone is always UTC.
Example usage:
import {
const nodes: {
readonly email: email.Root;
readonly clock:Clock;
readonly process:Process;
}
Contains the graph references (grefs) that this program has been given access to.
nodes,
const root:Root
A gref to the root of this program.
root } from"membrane"
exportasyncfunction
functionrun():Promise<void>
run() {
// Every Friday at 5pm UTC
const root:Root
A gref to the root of this program.
root.
handles.Root.everyFriday: any
everyFriday.
any
$cron("0 0 17 * * Fri")
}
exportasyncfunction
functioneveryFriday():Promise<void>
everyFriday() {
await
const nodes: {
readonly email: email.Root;
readonly clock:Clock;
readonly process:Process;
}
Contains the graph references (grefs) that this program has been given access to.
nodes.
email: email.handles.Root
email.
email.handles.Root.send: any
Sends an email to the configured address
Note: remember to await this action invocation. Or, chain another Action method: .$invoke(), .$invokeAt(), .$invokeIn(), .$cron()
All nodes except for scalars implement this interface. This includes
connections available in the nodes object, as well as
nodes in the program itself (e.g. root, root.status, etc).
Interface
Implemented by
Field<T>
fields that return an object type
ListField<T[]>
fields that return a list types
Where T is the result(s) of querying that node (a type with all of its inner
fields).
// membrane.d.ts
interface Field<T> extendsGref {
$query(query:string):Promise<T>;
}
interface ListField<T> extendsGref {
$query:(q:string)=>Promise<T[]>;
}
$query
Queries a node using GraphQL.
Signature:
$query(query: string): Promise<T>;
Example usage:
import {
const nodes: {
readonly user: github.User;
readonly clock:Clock;
readonly process:Process;
}
Contains the graph references (grefs) that this program has been given access to.
nodes } from"membrane"
asyncfunction
functionrun():Promise<void>
run() {
const {
const name:string|undefined
name,
const bio:string|undefined
bio } = await
const nodes: {
readonly user: github.User;
readonly clock:Clock;
readonly process:Process;
}
Contains the graph references (grefs) that this program has been given access to.
Returns the arguments from the last part of the gref. Only the last element of
the gref is considered. Consider using $argsAt if you need
arguments from the middle of the path.
Returns an empty object if there are no arguments.
Signature:
$args(): Record<string, any>;
Example usage:
import {
const nodes: {
readonly user: github.User;
readonly clock:Clock;
readonly process:Process;
}
Contains the graph references (grefs) that this program has been given access to.
nodes } from"membrane"
asyncfunction
functionrun():Promise<void>
run() {
const
const repo: github.handles.Repository
repo = await
const nodes: {
readonly user: github.User;
readonly clock:Clock;
readonly process:Process;
}
Contains the graph references (grefs) that this program has been given access to.
Collection of Github repositories owned by the user
repos.
github.handles.RepositoryCollection.one: (args: {
name:string;
})=> github.handles.Repository
Retrieve a single repository by name
one({
name: string
name: "docs" })
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without calling require('console').
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = newconsole.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
console.assert() writes a message if value is falsy or omitted. It only
writes a message and does not otherwise affect execution. The output always
starts with "Assertion failed". If provided, message is formatted using
util.format().
Returns a gref with the last part of the path removed. This does nothing if the
path is empty.
$pop(): Field<any>;
const gref = root.a.b
const parent = gref.$pop()
console.assert(parent===root.a)
NodeEvent
All events implement this interface. This is not called Event to avoid
conflicts with other Event types in node.js and browser environments. To
unsubscribe from events, use the global unsubscribe function.
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without calling require('console').
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = newconsole.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
An object that can hold any data which transparently persists across program updates.
To give this object a type, export a type named State from your program's index.ts.
For example:
export type State = {
birthdays: Record<string, Date>;
}
In Membrane you can use state to efficiently store any object that persist across
invocations and updates.
Data stored in state doesn't necessarily have to be serializable, any JavaScript value can be kept in it,
including functions, promises, dates, etc.
state,
const root:Root
A gref to the root of this program.
root } from"membrane"
const state:State
An object that can hold any data which transparently persists across program updates.
To give this object a type, export a type named State from your program's index.ts.
For example:
export type State = {
birthdays: Record<string, Date>;
}
In Membrane you can use state to efficiently store any object that persist across
invocations and updates.
Data stored in state doesn't necessarily have to be serializable, any JavaScript value can be kept in it,
including functions, promises, dates, etc.
state.
count: number
count??=0
exportfunction
functionstatus():string
status() {
return`Count ${
const state:State
An object that can hold any data which transparently persists across program updates.
To give this object a type, export a type named State from your program's index.ts.
For example:
export type State = {
birthdays: Record<string, Date>;
}
In Membrane you can use state to efficiently store any object that persist across
invocations and updates.
Data stored in state doesn't necessarily have to be serializable, any JavaScript value can be kept in it,
including functions, promises, dates, etc.
state.
count: number
count}`
}
exportfunction
functionincrement():void
increment() {
const state:State
An object that can hold any data which transparently persists across program updates.
To give this object a type, export a type named State from your program's index.ts.
For example:
export type State = {
birthdays: Record<string, Date>;
}
In Membrane you can use state to efficiently store any object that persist across
invocations and updates.
Data stored in state doesn't necessarily have to be serializable, any JavaScript value can be kept in it,
including functions, promises, dates, etc.
Returns a promise that resolves after a given number of seconds. You can await
this promise to temporarily pause the execution of a function. Thanks to
Membrane’s durable runtime, you can pause for an arbitrarily long time.
Signature:
functionsleep(seconds:number):Promise<void>;
Example usage:
exportasyncfunction
functionrun():Promise<void>
run() {
// Print the time every 10 seconds
for (let
let i:number
i = 0;
let i:number
i<3;
let i:number
i++) {
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without calling require('console').
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = newconsole.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Converts a time to a string by using the current or specified locale.
@param ― locales A locale string, array of locale strings, Intl.Locale object, or array of Intl.Locale objects that contain one or more language or locale tags. If you include more than one locale string, list them in descending order of priority so that the first entry is the preferred locale. If you omit this parameter, the default locale of the JavaScript runtime is used.
@param ― options An object that contains one or more properties that specify comparison options.
toLocaleTimeString())
await
var sleep:(seconds:number) => Promise<any>
Returns a promise that resolves after the provided number of seconds
@param ― seconds the number of seconds to sleep for
@returns ― a promise that resolves once the timer has been set
sleep(10)
}
}
Note that, even though you can sleep for an arbitrarily long time, once a
function is executing, you can’t change it. When building long-running
workflows, it’s best to use $invokeIn or $invokeAt to
schedule workflow steps as separate actions.
Contains the graph references (grefs) that this program has been given access to.
nodes,
const root:Root
A gref to the root of this program.
root,
const state:State
An object that can hold any data which transparently persists across program updates.
To give this object a type, export a type named State from your program's index.ts.
For example:
export type State = {
birthdays: Record<string, Date>;
}
In Membrane you can use state to efficiently store any object that persist across
invocations and updates.
Data stored in state doesn't necessarily have to be serializable, any JavaScript value can be kept in it,
including functions, promises, dates, etc.
state } from"membrane"
asyncfunction
functionsetup():Promise<void>
setup() {
// Gref to a repo
const
const repo: github.handles.Repository
repo =
const nodes: {
readonly user: github.User;
readonly clock:Clock;
readonly process:Process;
}
Contains the graph references (grefs) that this program has been given access to.
Collection of Github repositories owned by the user
repos.
github.handles.RepositoryCollection.one: (args: {
name:string;
})=> github.handles.Repository
Retrieve a single repository by name
one({
name: string
name: "docs" })
// Subscribe to an event
const state:State
An object that can hold any data which transparently persists across program updates.
To give this object a type, export a type named State from your program's index.ts.
For example:
export type State = {
birthdays: Record<string, Date>;
}
In Membrane you can use state to efficiently store any object that persist across
invocations and updates.
Data stored in state doesn't necessarily have to be serializable, any JavaScript value can be kept in it,
including functions, promises, dates, etc.
var unsubscribe:(subscription:Subscription) => Promise<any>
Unsubscribes from a previously subscribed event
@param ― subscription the subscription to unsubscribe from. The result of a previous call to $subscribe
unsubscribe(
const state:State
An object that can hold any data which transparently persists across program updates.
To give this object a type, export a type named State from your program's index.ts.
For example:
export type State = {
birthdays: Record<string, Date>;
}
In Membrane you can use state to efficiently store any object that persist across
invocations and updates.
Data stored in state doesn't necessarily have to be serializable, any JavaScript value can be kept in it,
including functions, promises, dates, etc.
state.
subscription: number
subscription)
}
// Handle the event
exportasyncfunction
functionhandleIssueOpened(_:any, { event }: {
event:any;
}):Promise<void>
handleIssueOpened(
_: any
_, {
event: any
event }) {
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without calling require('console').
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = newconsole.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).