Registry / database / relay-mongodb-connection

relay-mongodb-connection

JSON →
library2.1.0jsnpmunverified

A library that provides Relay-compatible cursor-based pagination for MongoDB cursors, mongoose queries, and aggregations. Version 2.1.0 is the latest stable release, with a simple API similar to graphql-relay's connectionFromArray. It automatically applies skip/limit to the cursor to fetch only necessary documents. Differentiators: supports both native MongoDB driver and mongoose, optional mapper function, and does not require pre-sorting or building edges array in memory.

npm install relay-mongodb-connection
INSTALL
IMPORT
SIG · RELAY-MONGODB-CONN
R
relay-mongodb-connection
databasejavascriptv2.1.0
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.

connectionFromMongoCursor
import connectionFromMongoCursor from 'relay-mongodb-connection'
const connectionFromMongoCursor = require('relay-mongodb-connection')
Default export of the package for MongoDB cursors
connectionFromMongooseQuery
import { connectionFromMongooseQuery } from 'relay-mongodb-connection'
import connectionFromMongooseQuery from 'relay-mongodb-connection'
Named export for mongoose query objects
connectionFromMongooseAggregate
import { connectionFromMongooseAggregate } from 'relay-mongodb-connection'
import connectionFromMongooseAggregate from 'relay-mongodb-connection'
Named export for mongoose aggregate pipelines

Shows how to use connectionFromMongoCursor with graphql-relay's connectionArgs and connectionDefinitions.

import connectionFromMongoCursor from 'relay-mongodb-connection'; import { connectionArgs, connectionDefinitions } from 'graphql-relay'; import { GraphQLObjectType, GraphQLString } from 'graphql'; const { connectionType: UserConnection } = connectionDefinitions({ nodeType: GraphQLString }); const UserType = new GraphQLObjectType({ name: 'User', fields: () => ({ friends: { type: UserConnection, args: connectionArgs, resolve: async (parent, args) => { const cursor = db.collection('users').find({}); return connectionFromMongoCursor(cursor, args); } } }) });
Debug
Known issues
gotchaThe cursor must be a MongoDB Cursor object, not a Promise or array. Passing something else will cause runtime errors.
fix
Ensure you pass the result of .find() or .aggregate() directly, not a thenable.
affects: *
gotchaThe library assumes the cursor documents have an _id field to generate Relay cursors. If your collection uses a different identifier, provide a mapper function that returns an object with an _id property.
fix
Add a mapper function: connectionFromMongoCursor(cursor, args, doc => ({ ...doc, _id: doc.customId }))
affects: *
deprecatedCommonJS require() may not work with ESM-only code in the library if Node.js version < 12 or without transpilation.
fix
Use import statements or ensure your bundler handles ESM.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: connectionFromMongoCursor is not a function
Using require() on the default export incorrectly, e.g., const connectionFromMongoCursor = require('relay-mongodb-connection')
fix
Use import connectionFromMongoCursor from 'relay-mongodb-connection' or use require('relay-mongodb-connection').default
Error: cursor.skip is not a function
Passing a Promise instead of a MongoDB Cursor object
fix
Await the promise before calling connectionFromMongoCursor
TypeError: Cannot read property 'after' of undefined
connectionArgs not spread properly; args object is undefined
fix
Spread connectionArgs in the field definition: args: { ...connectionArgs }
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies
graphql-relayrequiredProvides types such as connectionArgs and connectionDefinitions used for Relay pagination.
Agent activity
9 hits · last 30 days
node
8
Meta
1
Resources
relay-mongodb-connection — npm install relay-mongodb-connection · libregistry