Skip to content
On this page

CRUD stores

Vroom will generate a store for each model you provide. They will all be saved on vroom.stores and can be used for creating, updating and deleting items. For fetching items use the built in components e.g. FetchList

Creating an item

typescript
const bookStore = vroom.stores.book();

// This will call POST /books
const book = await bookStore.create({
  title: "The Hobbit",
});

console.log(book.id); // "1"

Updating an item

typescript
const bookStore = vroom.stores.book();

// This will call PATCH /books/1
const book = await bookStore.update("1", {
  title: "The Hobbit, or There And Back Again",
});

console.log(book.title); // "The Hobbit, or There And Back Again"

Deleting an item

typescript
const bookStore = vroom.stores.book();

// This will call DELETE /books/1
bookStore.delete("1");

API naming

As default the key you give the model, when setting up vroom will define the api routes

typescript
const vroom = createVroom({
  models: {
    todo: defineModel({
      //...
    }),
  },
});

In this example todo will be used as the base for all api routes with naive pluralising +'s'. Vroom will automatically callers for the following api routes

MethodPathDescription
GET/todosList todos
POST/todosCreate a todo
GET/todos/:idGet a single todo
PATCH/todos/:idUpdate a todo
DELETE/todos/:idDelete a todo

Setting plural name

By default Vroom will try to pluralise your model name by adding an s. You can customise the plural name like so:

typescript
const vroom = createVroom({
  models: {
    category: defineModel({
      plural: "categories",
    }),
  },
});
MethodPathDescription
GET/categoriesList categories
POST/categoriesCreate a category
GET/categories/:idGet a single category
PATCH/categories/:idUpdate a category
DELETE/categories/:idDelete a category

Custom path

Overwrites the path of the base endpoint completely

typescript
const vroom = createVroom({
  models: {
    todo: defineModel({
      path: "/v2/todos",
    }),
  },
});
MethodPathDescription
GET/v2/todosList todos
POST/v2/todosCreate a todo
GET/v2/todos/:idGet a single todo
PATCH/v2/todos/:idUpdate a todo
DELETE/v2/todos/:idDelete a todo

Omitting actions

You might not always need or want all actions, so you can specify which actions to create by providing an array of actions

typescript
const vroom = createVroom({
  models: {
    todo: defineModel({
      //...
      only: ["index", "read"], // options: index, create, read, update, delete
    }),
  },
});

Released under the MIT License.