Skip to content

Latest commit

 

History

History
156 lines (102 loc) · 4.75 KB

README_ONE_TO_MANY_RESOURCE_STORAGE.md

File metadata and controls

156 lines (102 loc) · 4.75 KB

ConstructorOptions

Properties

  • client MongoClient configured mongo client to use. Can be null if url is set
  • databaseName string? name of the mongodb database
  • indexes Array<Object>? indexes to be created on instantiation. Use format {key:1} for single indexes and {key1: 1, key:2} for compound indexes
  • collectionName string name of the mongodb collection used to store the resources
  • resourceName string name of the resource e.g. users, customers, topics, shipments
  • resourcePath 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 callers
  • enableTwoWayReferences string true if documents should also store references to their parents e.g. student have references to their schools

Examples

const { MongoClient } = require('mongodb')
const { OneToManyResourceStorage } = require('@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
})

GetOptions

Properties

  • withMetadata boolean true if also meta data should be returned
  • addDocumentPath 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 }

OneToManyResourceStorage

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]
}

exists

Returns true if a resource with given ids exists.

Parameters

Returns boolean

get

Returns a resource by ids.

Parameters

  • resourceIds (String | Array<String>) resource ids that will added to the resource path i.e. /users/${id}/documents/${id}
  • options Object

Returns Object

find

Find a resource by via options.match query.

Parameters

  • resourceIds (String | Array<String>) resource ids that will added to the resource path i.e. /users/${id}/documents/${id}
  • options FindOptions

Returns Object

getAll

Returns resources based on return value of findReferences.

Parameters

  • resourceIds (String | Array<String>) resource ids that will added to the resource path i.e. /users/${id}/documents/${id}
  • options GetOptions

create

Add a resource to a collection by ids.

Parameters

  • 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

update

Updates a resource by ids

Parameters

  • 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

delete

Deletes a resource by ids

Parameters

  • resourceIds (String | Array<String>) resource ids that will added to the resource path i.e. /users/${id}/documents/${id}