Ir al contenido

Programmatic API

Esta página aún no está disponible en tu idioma.

The programmatic API lets you start and stop a yRest server from inside your test suite or any Node.js script — no CLI, no separate process. Import createYrestServer, pass a config object, and use the running server URL in your tests.

import { createYrestServer } from "@yrest/cli";
const server = createYrestServer({ file: "./tests/db.yml", port: 3070 });
await server.start();
// run tests against http://localhost:3070
await server.stop();
OptionTypeDefaultDescription
filestringPath to the YAML database file
portnumber3070Port to listen on
hoststring"localhost"Host to bind
basestring""URL prefix for all routes
readonlybooleanfalseReject all write operations
delaynumber0Simulated latency in ms
snapshotbooleanfalseEnable snapshot/reset endpoints

server.collection<T>(name) returns a typed CRUD handle for the named collection:

import type { User } from "./db.types"; // from yrest types codegen
const users = server.collection<User>("users");
users.findAll(); // User[]
users.findById(1); // User | undefined
users.create({ name: "Ana" }); // User
users.update(1, { name: "Ana M." });
users.delete(1);