Remult

A CRUD framework for full stack TypeScript

README

Remult

A CRUD framework for full-stack TypeScript

CircleCI GitHub license npm version npm downloads Join Discord Twitter URL

Getting Started | Documentation | Example Apps


Video thumbnail

Watch code demo on YouTube here (14 mins)


What is Remult?


Remult is a full-stack CRUD framework that uses your TypeScript entities
as a single source of truth for your API, frontend type-safe API client and
backend ORM.

:zap: Zero-boilerplate CRUD API routes with paging, sorting, and filtering for Express / Fastify / Next.js / NestJS / Koa / others...
:ok_hand: Fullstack type-safety for API queries, mutations and RPC, without code generation
:sparkles: Input validation, defined once, runs both on the backend and on the frontend for best UX
:lock: Fine-grained code-based API authorization
:relieved: Incrementally adoptable
:rocket: Production ready

Status


Remult is production-ready and, in fact, used in production apps since 2018.
However, we’re keeping the major version at zero so we can use community
feedback to finalize the v1 API.

Motivation


Full-stack web development is (still) too complicated. Simple CRUD, a common
requirement of any business application, should be simple to build, maintain,
and extend when the need arises.

Remult abstracts away repetitive, boilerplate, error-prone, and poorly designed
code on the one hand, and enables total flexibility and control on the other.
Remult helps building fullstack apps using only TypeScript code you can easily
follow and safely refactor, and fits nicely into any existing or new project
by being minimalistic and completely unopinionated regarding the developer’s
choice of other frameworks and tools.

Other frameworks tend to fall into either too much abstraction (no-code,
low-code, BaaS) or partial abstraction (MVC frameworks, GraphQL, ORMs, API
generators, code generators), and tend to be opinionated regarding the
development tool-chain, deployment environment, configuration/conventions or
DSL. Remult attempts to strike a better balance.

Installation


The _remult_ package is one and the same for both the frontend bundle and the
backend. Install it once for a monolith project or per-repo in a monorepo.

  1. ```sh
  2. npm i remult
  3. ```

Usage


Define model classes


  1. ```ts
  2. // shared/product.ts

  3. import { Entity, Fields } from "remult";

  4. @Entity("products", {
  5.   allowApiCrud: true,
  6. })
  7. export class Product {
  8.   @Fields.string()
  9.   name = "";

  10.   @Fields.number()
  11.   unitPrice = 0;
  12. }
  13. ```

Setup API backend using an Express middleware


  1. ```ts
  2. // backend/index.ts

  3. import express from "express";
  4. import { remultExpress } from "remult/remult-express";
  5. import { Product } from "../shared/product";

  6. const port = 3001;
  7. const app = express();

  8. app.use(remultExpress({
  9.   entities: [Product],
  10. }));

  11. app.listen(port, () => {
  12.   console.log(`Example API listening at http://localhost:${port}`);
  13. });
  14. ```

:rocket: API Ready


  1. ```sh
  2. > curl http://localhost:3001/api/products

  3. [{"name":"Tofu","unitPrice":5}]
  4. ```

Find and manipulate data in type-safe frontend code


  1. ```ts
  2. // frontend/code.ts

  3. import { remult } from "remult";
  4. import { Product } from "../shared/product";

  5. async function increasePriceOfTofu(priceIncrease: number) {
  6.   const productsRepo = remult.repo(Product);

  7.   const product = await productsRepo.findFirst({ name: "Tofu" }); // filter is passed through API request all the way to the db
  8.   product.unitPrice += priceIncrease;
  9.   productsRepo.save(product); // mutation request updates the db with no boilerplate code
  10. }
  11. ```

..._exactly_ the same way as in backend code


  1. ```ts
  2. @BackendMethod({ allowed: Allow.authenticated })
  3. static async increasePriceOfTofu(priceIncrease: number) {
  4.   const productsRepo = remult.repo(Product);

  5.   const product = await productsRepo.findFirst({ name: 'Tofu' }); // use Remult in the backend as an ORM
  6.   product.unitPrice += priceIncrease;
  7.   productsRepo.save(product);
  8. }
  9. ```

:ballot_box_with_check: Data validation and constraints - defined once


  1. ```ts
  2. import { Entity, Fields, Validators } from "remult";

  3. @Entity("products", {
  4.   allowApiCrud: true,
  5. })
  6. export class Product {
  7.   @Fields.string({
  8.     validate: Validators.required,
  9.   })
  10.   name = "";

  11.   @Fields.string<Product>({
  12.     validate: (product) => {
  13.       if (product.description.trim().length < 50) {
  14.         throw "too short";
  15.       }
  16.     },
  17.   })
  18.   description = "";

  19.   @Fields.number({
  20.     validate: (_, field) => {
  21.       if (field.value < 0) {
  22.         field.error = "must not be less than 0"; // or: throw "must not be less than 0";
  23.       }
  24.     },
  25.   })
  26.   unitPrice = 0;
  27. }
  28. ```

Enforced in frontend:


  1. ```ts
  2. const product = productsRepo.create();

  3. try {
  4.   await productsRepo.save(product);
  5. } catch (e: any) {
  6.   console.error(e.message); // Browser console will display - "Name: required"
  7. }
  8. ```

Enforced in backend:


  1. ```sh
  2. > curl http://localhost:3001/api/products -H "Content-Type: application/json" -d "{""unitPrice"":-1}"

  3. {"modelState":{"unitPrice":"must not be less than 0","name":"required"},"message":"Name: required"}
  4. ```

:lock: Secure the API with fine-grained authorization


  1. ```ts
  2. @Entity<Article>("Articles", {
  3.   allowApiRead: true,
  4.   allowApiInsert: (remult) => remult.authenticated(),
  5.   allowApiUpdate: (remult, article) => article.author.id == remult.user.id,
  6. })
  7. export class Article {
  8.   @Fields.string({ allowApiUpdate: false })
  9.   slug = "";

  10.   @Field(() => Profile, { allowApiUpdate: false })
  11.   author!: Profile;

  12.   @Fields.string()
  13.   content = "";
  14. }
  15. ```

What about complex CRUD?


While simple CRUD shouldn’t require any backend coding, using Remult means
having the ability to handle any complex scenario by controlling the backend in
numerous ways:

- Backend computed (read-only) fields - from simple
  complex data lookups or even direct db access (SQL)
- Custom side-effects with
  (before/after saving/deleting)
- Backend only updatable fields (e.g. “last updated at”)
- Many-to-one relations with
- Roll-your-own type-safe endpoints with
- Roll-your-own low-level endpoints (Express, Fastify, koa, others…)

Getting started


The best way to learn Remult is by following a tutorial of a simple Todo web app
with a Node.js Express backend.


Documentation


The documentation covers the main features of Remult.
However, it is still a work-in-progress.

Example Apps


- Fullstack TodoMVC example with React and Express.

- CRM demo with a React +
  MUI front-end and Postgres database.

Contributing


Contributions are welcome. See CONTRIBUTING.md.

- :speech_balloon: Any feedback or suggestions? Start a
- :muscle: Want to help out? Look for "help wanted" labeled
  issues.
- :star: Give this repo a star.

License


Remult is MIT Licensed.