Gathering detailed insights and metrics for relay-mongoose
Gathering detailed insights and metrics for relay-mongoose
Gathering detailed insights and metrics for relay-mongoose
Gathering detailed insights and metrics for relay-mongoose
mongoose-relay-paginate
Uses mongoose to create a relay compatible cursor based pagination.
graphql-relay-mongoose
A library to help construct connection with edges and cursors for a graphql-js server supporting react-relay with mongoose ODM.
global-relay-id-mongoose
Mongoose helpers to add relay compatible object identification to your GraphQL Schema.
relay-mongoose-connection
Mongoose connection for Relay
npm install relay-mongoose
Typescript
Module System
TypeScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
22 Commits
1 Watchers
10 Branches
1 Contributors
Updated on Jan 28, 2023
Latest Version
0.5.0
Package Id
relay-mongoose@0.5.0
Unpacked Size
21.21 kB
Size
6.16 kB
File Count
9
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
1
21
relay-mongoose
is a small library with a few utilities to make implementing relay compliant server easier.
It provides a class that enhanced the base mongoose.Model
with a few relay helpers.
EnhancedModel
Use mongoose loadClass()
method as described here.
1import * as mongoose from 'mongoose'; 2import { EnhancedDocument, EnhancedModel } from 'relay-mongoose'; 3 4export interface TestSchemaInterface extends EnhancedDocument { 5 field: string; 6} 7const testSchema = new mongoose.Schema({ 8 field: String 9}); 10testSchema.loadClass(EnhancedModel); 11 12// Due to the loadClass method implicitly adding new virtual methods to our model 13// To preserve new and old model types for convenience of development the model has to be casted to any 14const TestModel: EnhancedModel<TestSchemaInterface> = mongoose.model('test', testSchema) as any;
You can now use the added findConnections
method:
1/* test collection 2[ 3 { _id: '1', field: '1' }, 4 { _id: '2', field: '2' }, 5 { _id: '3', field: '3' }, 6] 7*/ 8 9const result = await TestModel.findConnections({}, {}); 10 11/* result 12{ 13 edges: [ 14 { node: { id: 'dGVzdC4x', field: '1' }, cursor: 'dGVzdC4x' }, 15 { node: { id: 'dGVzdC4y', field: '2' }, cursor: 'dGVzdC4y' }, 16 { node: { id: 'dGVzdC4z', field: '3' }, cursor: 'dGVzdC4z' }, 17 ], 18 pageInfo: { 19 hasNextPage: false, 20 hasPreviousPage: false, 21 startCursor: 'dGVzdC4x', 22 endCursor: 'dGVzdC4z' 23 } 24} 25*/
Note that the id returned in each node is already a base64 encoded string returned from document.relayId
utility.
relayId
As shown in the previous example, all documents returned by findConnections
have a modified id which is
the base64 encoded concatenation of model name and object ID by a dot.
For example consider the following document in the test
collection:
{ _id: '5ea4730dfdea900007758b00', field: 'something' }
Before encoding, relayId
value is test.5ea4730dfdea900007758b00
. The string is then base64 encoded
to mask the collection name in the ID. This value can be decoded by the fromRelayId()
utility in relay-mongoose
.
This allows to easily write a node
query resolver like this:
1import * as mongoose from 'mongoose'; 2import { fromRelayId } from 'relay-mongoose'; 3 4const Query = { 5 node: (obj, { id }) => { 6 const { modelName, objectId } = fromRelayId(id); 7 return mongoose.models[modelName].findById(objectId); 8 } 9};
findConnections
standalone functionThere is also a standalone findConnections
function to which as the first argument you can pass a mongoose
query for more flexibility. For example:
1import * as mongoose from 'mongoose'; 2import { EnhancedModel, findConnections } from 'relay-mongoose'; 3 4const TestModel: EnhancedModel<any> = mongoose.model('test', testSchema) as any; 5 6const firstQuery = TestModel.find({ condition: 2 }).populate(...); 7const paginatedData = await findConnections(firstQuery, { first: 10 });
Please note that the query passed as the first argument has to have extended the EnhancedModel
class
as relayId
property is needed for correct mapping.
EnhancedModel.findConnections
1async findConnections<T extends Document>( 2 conditions: FilterQuery<T>, 3 paginationArgs: PaginationArgs, 4 projection?: any | null 5 ): Promise<ConnectionDocuments<T>>
conditions
, projection
: the same as mongoose find
signature arguments.
paginationArgs
: pagination arguments as defined by relay specification
1export type Cursor = string; 2 3export interface PaginationArgs { 4 first?: number; 5 last?: number; 6 before?: Cursor; 7 after?: Cursor; 8} 9 10export interface Edge<T> { 11 cursor: Cursor; 12 node: T; 13} 14 15export interface ConnectionDocuments<T> { 16 edges: Edge<T>[]; 17 pageInfo: { 18 startCursor?: Cursor; 19 endCursor?: Cursor; 20 hasNextPage: boolean; 21 hasPreviousPage: boolean; 22 }; 23}
EnhancedModel.relayId
The base64 encoded concatenation of model name and stringified object ID of a document. Returns a string.
fromRelayId
1type fromRelayId = (id: string) => { modelName: string | null; objectId: string | null };
The reverse operation of EnhancedModel.relayId
. Returns null
if invalid id is provided.
findConnections
1export const findConnections = async <DocType extends EnhancedDocument, QueryHelpers = {}>( 2 documentQuery: DocumentQuery<DocType | DocType[] | null, any, QueryHelpers> & QueryHelpers, 3 { before, after, first, last }: PaginationArgs, 4 projection?: any | null 5): Promise<ConnectionDocuments<DocType>>
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
project is archived
Details
Reason
Found 0/17 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
28 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More