Skip to content
Guide/createQubee

createQubee

createQubee() builds the three objects the library needs and returns them sharing one store.

import { createQubee, STRAPI_DRIVER } from '@qubeejs/core';
const { builder, paginator, store } = createQubee({ driver: STRAPI_DRIVER });

Sharing a store is the point: a response parsed by the paginator teaches the builder where it is, so totalPages() and lastPage() start answering instead of throwing.

builder.setResource('articles');
const page = paginator.paginate(body);
builder.currentPage(); // 2 — set by the response
builder.totalPages(); // 6
createQubee({
baseUrl: 'https://example.com/api', // prefixed to every URI
driver: POSTGREST_DRIVER,
pagination: PaginationModeEnum.RANGE, // wire-level mechanism; PostgREST only
request: { limit: 'perPage' }, // rename emitted query parameters
response: { data: 'rows' }, // where to read pagination metadata
});
Option Type Purpose
driver DriverDefinition The driver to use, built-in or your own — required
pagination PaginationModeEnum Query string or Range header. Defaults to query
request QueryBuilderConfig Override emitted parameter names
response PaginationConfig Override where metadata is read from
baseUrl string Prefixed to every generated URI, as setBaseUrl() does. reset() clears it

createQubee({ driver: 'strapi' }) would read nicer, but the factory would then have to import DRIVERS — and a static import bundles all twenty drivers into every consumer, whether they use one or not.

minified gzipped
hand-wiring the three classes 17,703 B 5,334 B
createQubee 17,846 B 5,398 B
createQubee({ driver: DRIVERS[id] }) 68,070 B 13,597 B

Taking a definition costs 143 bytes over wiring by hand and keeps the registry out of your bundle. Runtime selection is still one line — you just opt into the cost yourself:

import { createQubee, DRIVERS } from '@qubeejs/core';
const { builder } = createQubee({ driver: DRIVERS[config.driver] });

Each call gets its own store, so two builders never interfere:

const articles = createQubee({ driver: STRAPI_DRIVER });
const authors = createQubee({ driver: STRAPI_DRIVER });
articles.builder.setResource('articles').setPage(3);
authors.store.getSnapshot().page; // 1 — untouched

The factory is a convenience, not a requirement. Build the three yourself when they should not share a store, or when a DI container owns their lifecycle:

const store = new QubeeStore();
const builder = new QueryBuilder(store, STRAPI_DRIVER.createRequestStrategy('query'));
const paginator = new Paginator(
store,
STRAPI_DRIVER.createResponseStrategy(),
STRAPI_DRIVER.createResponseOptions({}),
);