# InMemoryBackend

**Category:** Guides

## Design

### Overview

A mocking utility which acts as a collection server API.

`Wix Patterns` itself uses it for its internal tests.


```tsx
import { InMemoryBackend } from '@wix/patterns/testkit/backend';
```

---

### Example

```tsx
import { InMemoryBackend } from '@wix/patterns/testkit/backend';
import { Chance } from 'chance';
import { escapeRegExp } from 'lodash';

export function createBackend() {
  const chance = new Chance();

  return new InMemoryBackend({
    total: 1000,
    delay: { min: 100, max: 700 },
    createOne: (index) => ({
      id: chance.guid(),
      name: `${index} ${chance.name()}`,
      level: chance.pickone([
        'Beginner',
        'Amateur',
        'Semi-Pro',
        'Professional',
        'World Class',
        'Legendary',
        'Ultimate',
      ]),
      image: 'https://picsum.photos/200',
    }),
    predicate: ({ search, filters }) => {
      const rgx = new RegExp(escapeRegExp(search), 'i');

      return (item) => {
        if (search && !rgx.test(item.name)) {
          return false;
        }

        if (filters.level && filters.level.indexOf(item.level) === -1) {
          return false;
        }

        return true;
      };
    },
    itemKey: (item) => item.id,
  });
}
```

## API

### Props

| Prop | Type | Required | Default | Description |
|------|------|----------|---------|-------------|
| `createOne` | `(index: number) => T` | Yes | - | A callback function for each item creation |
| `itemKey` | `(item: T) => string` | Yes | - | A callback function that accepts an item and returns a unique ID of it. typically `item.id`. |
| `predicate` | `((query: BackendQuery<FiltersMap>) => (item: T) => boolean)` | No | - | A function that accepts a subset of [ComputedQuery](./?path=/story/common-types--computedquery) sent by the client and returns another function that accepts an item and should return a boolean that indicates whether the item passes the query filtering or not. |
| `orderBy` | `((query: BackendQuery<FiltersMap>) => Order<T>[])` | No | - | A function that accepts a subset of [ComputedQuery](./?path=/story/common-types--computedquery) sent by the client and returns another function that accepts an item and should returns a list of rules to order the items by |
| `projection` | `((query: BackendQuery<FiltersMap>) => (item: T) => T)` | No | - | A function that accepts a subset of [ComputedQuery](./?path=/story/common-types--computedquery) sent by the client and returns a list of fields to project from the item |
| `delay` | `{ min: number; max: number; }` | No | `{ min: 0, max: 5 }` | Simulates API latency |
| `total` | `number` | Yes | - | Total number of items to store in the backend |
| `enableTotal` | `boolean` | No | - | Enable total for the backend. Defaults to "true" |

