Skip to content

TypeScript code blocks ​

Supersimple's TypeScript blocks let you run TypeScript or JavaScript code, including accessing data from other blocks in the same exploration.

TIP

Code blocks are a convenient way for engineers to share the results of more advanced work with the rest of their teams, without needing others to set up full coding environments.

Usage ​

Create a new TypeScript block via the "New block" button, and choose TypeScript from the Code dropdown.

Blocks run on Node.js 22. TypeScript is executed directly without a build step, so any TypeScript syntax works, and plain JavaScript is accepted as well. Types are not checked before running; a type error only matters if it also fails at run time.

Using data from other blocks ​

You can access data from other blocks in your exploration using getBlock from the auto-included supersimple module. Pass it a data model name or a block title:

typescript
import { getBlock } from 'supersimple';

const rows = await getBlock('User').all();

Rows are plain objects keyed by field key:

typescript
import { getBlock } from 'supersimple';

const rows = await getBlock('Event').all();

rows
  .filter((row) => row.type === 'signup')
  .map((row) => ({ user: row.user_id, at: row.created_at }));

getBlock doesn't load anything by itself. Rows are fetched only when your code asks for them, in pages. Choose how to read a block:

typescript
import { getBlock } from 'supersimple';

const block = getBlock('Event');

await block.all(); // every row

await block.page(3, { size: 500 }); // one page only: rows 1,500 to 1,999

for await (const page of block.pages({ size: 500 })) {
  // every page in turn
  console.log(page.length);
}

for await (const row of block) {
  // every row in turn, without holding them all in memory
  console.log(row.type);
}

block.fields; // the columns, each with its key, name and Supersimple type
  • Pages count from 0, and a page past the end is empty.
  • A page has 2,000 rows by default, which is also the most it can have.
  • Rows always come back in the same order, so pages never overlap or skip rows.
  • Reading is async, like other I/O: await the methods and loop with for await. block.fields is a plain property.

all() fetches every page and keeps all the rows in memory. Looping over the block or its pages holds one page at a time, so prefer a loop for blocks too large to keep in memory.

Code written earlier as const { fields, rows } = getBlock('User') still works: rows reads the same rows as await block.all(), synchronously.

Packages ​

Node.js built-in modules are available with the usual node: imports. To use packages from npm, declare them with a // deps comment line and import them as usual:

typescript
// deps lodash@4.17.21 date-fns@4.1.0
import _ from 'lodash';
import { formatISO } from 'date-fns';
import { getBlock } from 'supersimple';

const rows = await getBlock('Order').all();

_.orderBy(rows, ['total'], ['desc']).slice(0, 10);

Each package needs an exact version, <package>@<version>, and you can list several on one line or use one line per package. Packages are installed the first time a block uses them and reused by later runs.

Outputting data ​

Similarly to Jupyter notebooks, the last expression in a block is automatically outputted as the block's result. You can use top-level await, so the last expression can also be the awaited result of an asynchronous call.

  • An array of objects becomes a Supersimple table, with column types inferred from the values.
  • Strings, numbers and booleans are shown as text.
  • Other objects are shown as formatted JSON.

Anything written with console.log or console.error appears in the block's log rather than in the output.

To render custom output, return an object with an explicit type:

typescript
({ type: 'html', htmlString: '<h1>Hello</h1>' });
typescript
({ type: 'image', base64ImageString: 'data:image/png;base64,...' });

Limits ​

  • A block can run for up to 9 minutes, reading data included.
  • Very wide rows, such as long text or JSON fields, can make a page too large to send. Reading then fails with "ask for fewer rows at a time": read that block in smaller pages, such as block.pages({ size: 500 }).
  • A run can fetch at most 10,000 pages of rows, which is 20 million rows at the default page size. Every way of reading a block counts, including all().