npm.io
2.2.0 • Published 1 week ago

@rapiq/adapter-memory

Licence
MIT
Version
2.2.0
Deps
1
Size
91 kB
Vulns
0
Weekly
0
Stars
33

rapiq

@rapiq/adapter-memory

Evaluate a rapiq Query against plain JavaScript objects & arrays.
The in-memory sibling of @rapiq/adapter-sql and @rapiq/adapter-typeorm: same AST,
compiled into plain functions instead of SQL.

npm version types License: MIT

Documentation · Monorepo · npm


Part of rapiq. Typed REST queries: build, transport, validate, execute. Same visitor-pattern surface as the SQL/TypeORM adapters, but the visitors compile the AST into plain functions (predicate, comparator, projector, slicer), so the same query filters a database and an array.

  • Compile once, run many: the AST is turned into closures up front; apply them across large collections without re-walking the tree.
  • No database required: perfect for tests, mock servers, edge/serverless caches, or filtering data you already hold in memory.
  • Backend-parity semantics: matches @rapiq/adapter-sql for positive operators and the complement law for negations (ne / nin / not* match null & missing), same-element binding for dotted paths over arrays.
  • Whole-query or à la carte: applyQuery runs everything, or compile just the filters / sorts / fields / pagination on their own.
import { and, eq, gte } from '@rapiq/core';
import { compileFilters } from '@rapiq/adapter-memory';

const matches = compileFilters(and(eq('name', 'Peter'), gte('age', 18)));

matches({ name: 'Peter', age: 28 }); // true
matches({ name: 'Peter', age: 16 }); // false

Installation

npm install @rapiq/core @rapiq/adapter-memory

Usage

Apply a whole query to a collection:

import { defineQuery } from '@rapiq/core';
import { applyQuery } from '@rapiq/adapter-memory';

const query = defineQuery<User>({
    filters: { age: { $gte: 18 } },
    sorts: { name: 'ASC' },
    pagination: { limit: 10 },
    fields: ['id', 'name'],
});

const { data, total, pagination } = applyQuery(query, users);

Or compile individual parameters:

import {
    compileFields,
    compileFilters,
    compilePagination,
    compileSorts,
} from '@rapiq/adapter-memory';

const predicate = compileFilters(query.filters);    // (input) => boolean
const comparator = compileSorts(query.sorts);       // (a, b) => number
const projector = compileFields(query.fields);      // (input) => projected
const slicer = compilePagination(query.pagination); // (data) => data page

The rapiq family

Package Purpose
@rapiq/core Query AST, typed build layer & schema system (the shared foundation)
@rapiq/parser-simple Parse plain object/array input (the "simple" dialect)
@rapiq/parser-expression Parse filter expressions like and(eq(name,'John'), gte(age,'18'))
@rapiq/parser-mongo Parse MongoDB-style filter documents like { age: { $gte: 18 } }
@rapiq/codec-url URL query-string transport codec
@rapiq/adapter-sql Dialect-agnostic SQL fragment adapter (pg, mysql, sqlite, mssql, oracle)
@rapiq/adapter-typeorm Apply a query to a TypeORM SelectQueryBuilder
@rapiq/adapter-prisma Serialize a query into a Prisma argument object
@rapiq/adapter-drizzle Serialize a query into a Drizzle relational query config
@rapiq/adapter-memory Evaluate a query against in-memory objects & arrays

Documentation

To find out more, head over to the documentation.

License

Published under the MIT License.

Keywords