client
MongoClient configured mongo client to use. Can be null if url is setdatabaseName
string? name of the mongodb databaseindexes
Array<object>? indexes to be created on instantiation. Use format {key:1} for single indexes and {key1: 1, key:2} for compound indexescollectionName
string name of the mongodb collection used to store the resourcesresourceName
string name of the resource e.g. users, customers, topics, shipmentsresourcePath
string? slash separated path describing the hierarchy e.g. universities/teachers/subjects/exams.hiddenResourcePath
string? slash separated path describing which path elements should not be returned to callersenableTwoWayReferences
string true if documents should also store references to their parents e.g. student have references to their schools
import { MongoClient } from 'mongodb'
import { OneToManyResourceStorage } from '@discue/mongodb-resource-client'
const client = new MongoClient(url, {
serverApi: { version: '1', strict: true, deprecationErrors: true }, // https://www.mongodb.com/docs/manual/reference/stable-api/
})
const oneToManyResourceStorage = new OneToManyResourceStorage({
client,
collectionName: 'api_clients',
resourceName: 'listeners'
enableTwoWayReferences: true
})
withMetadata
boolean true if also meta data should be returnedaddDocumentPath
boolean true if $path propety should be added to documents e.g.$path=/countries/1/cities/2/companies
projection
object MongoDB projection object e.g. { id: 0, name: 0 }
Manages relationships between entities in a more decoupled way by keep storing entities in separate collections and using references to establish an relationship between both. This way students can be queried independently of an university, while all studies of a university can still be looked up via the stored reference.
The references between both collections are kept up-to-date. Deleting a document, causes the reference to be deleted in the other entity. Adding a document causes a reference to be updated, too.
Students collection
{
id: 1828391,
name: 'Miles Morales',
},
{
id: 4451515,
name: 'Bryan Jenkins',
}
Universities collection
{
name: 'University Munich',
students: [1828391]
}
{
name: 'University Stuttgart',
students: [4451515]
}
Returns true if a resource with given ids exists.
Returns boolean
Returns a resource by ids.
resourceIds
(string | Array<string>) resource ids that will added to the resource path i.e. /users/${id}/documents/${id}options
object
Returns object
Find a resource by via options.match query.
resourceIds
(string | Array<string>) resource ids that will added to the resource path i.e. /users/${id}/documents/${id}options
FindOptions
Returns object
resourceIds
(string | Array<string>) resource ids that will added to the resource path i.e. /users/${id}/documents/${id}options
GetOptions
Add a resource to a collection by ids.
resourceIds
(string | Array<string>) resource ids that will added to the resource path i.e. /users/${id}/documents/${id}resource
object the resource to be stored
Updates a resource by ids
resourceIds
(string | Array<string>) resource ids that will added to the resource path i.e. /users/${id}/documents/${id}update
object values that should be updated
Deletes a resource by ids