Registry / database / schm-mongo

schm-mongo

JSON →
library0.4.1jsnpmunverified

A composable schema extension for schm that parses query parameters into MongoDB queries, projections, and pagination. Version 0.4.1 is the latest stable release; the project appears to be in maintenance mode with no updates since 2018. It integrates with schm to define field projections via the `fields` parser, pagination via `page` with sort/limit/skip, geospatial queries via `near`, and operator-based filtering via `query`. Unlike monolithic ODM libraries, it provides lightweight, composable schema functions that work directly with the MongoDB Node.js driver or Mongoose.

npm install schm-mongo
INSTALL
IMPORT
SIG · SCHM-MONGO
S
schm-mongo
databasejavascriptv0.4.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

query
import { query } from 'schm-mongo'
const { query } = require('schm-mongo/query')
Named export from the package root. CommonJS using require is also fine, but avoid deep imports.
fields
import { fields } from 'schm-mongo'
const fields = require('schm-mongo').fields
Correctly imported as a named export. The wrong example is actually valid but less idiomatic in ES modules.
page
import { page } from 'schm-mongo'
Named export. No common mistake beyond ensuring you import from the correct package.
near
import { near } from 'schm-mongo'
const near = require('schm-mongo').default.near
There is no default export; must use named import. Attempting default access is a common error.

Demonstrates parsing query parameters, field projections, and pagination using schm-mongo with the schm schema library.

const schema = require('schm'); const { query, fields, page } = require('schm-mongo'); const userSchema = schema({ name: String, age: { type: Number, operator: '$gte' }, }); const querySchema = schema(userSchema, query(), fields(), page()); const parsed = querySchema.parse({ name: 'Alice', age: '18', fields: ['name', 'age'], limit: '10', page: '2', }); /* parsed: { name: 'Alice', age: { '$gte': 18 }, fields: { name: 1, age: 1 }, page: { limit: 10, skip: 10, sort: undefined } } */ // Use with mongodb driver db.collection.find(parsed, parsed.fields) .limit(parsed.page.limit) .skip(parsed.page.skip);
Debug
Known issues
deprecatedPackage is no longer actively maintained. Last release 0.4.1 on 2018-03-05.
fix
Consider alternatives like @hapi/joi with custom MongoDB transformers or mongoose for full ODM support.
affects: >=0.0.0
gotchaThe `query()` parser only wraps fields that have an explicit `operator` property in the schema definition. Without operator, values are passed as-is.
fix
Ensure schema fields that should become MongoDB operators have `operator` set, e.g., `{ type: Date, operator: '$gte' }`.
affects: >=0.0.0
gotcha`fields()` defaults to including all fields if not specified. To exclude `_id`, you must explicitly pass `fields: ['-_id']`.
fix
Always specify fields to avoid unintended projection. Use `fields: ['-_id', 'name']` to hide `_id`.
affects: >=0.0.0
gotcha`page()` expects `page` and `limit` query parameters as strings and parses them to numbers. If values are missing, they default to page=1 and limit=10.
fix
Pass numeric strings like '1' and '10' as query params; the parser handles conversion.
affects: >=0.0.0
gotcha`near()` requires a geo field name. The input must be a comma-separated string of lat,lng coordinates, and the parsed output uses GeoJSON format `[lng, lat]` (note longitude first).
fix
Supply coordinates as 'lat,lng' string. Remember GeoJSON order is [longitude, latitude].
affects: >=0.0.0
Errors
Common errors & fixes
Cannot find module 'schm'
Missing required peer dependency schm.
fix
Run `npm install schm@^0.4.0` alongside schm-mongo.
TypeError: schema is not a function
Incorrect import of schm; likely using `require('schm').default` in CJS or wrong import style.
fix
Use `const schema = require('schm')` (CommonJS) or `import schema from 'schm'` (ESM).
TypeError: (0 , _schmMongo.query) is not a function
Named export not imported correctly; possibly import default instead of named.
fix
Use `import { query } from 'schm-mongo'` or `const { query } = require('schm-mongo')`.
Upgrade
Version history
0.4.1latest on npm
Audit
Dependencies
schmrequiredPeer dependency: provides the base schema parsing engine; schm-mongo requires schm ^0.4.0
Agent activity
13 hits · last 30 days
node
10
Meta
2
Resources
schm-mongo — npm install schm-mongo · libregistry