Skip to content

Latest commit

 

History

History
125 lines (80 loc) · 3.41 KB

README_ONE_TO_FEW_RESOURCE_STORAGE.md

File metadata and controls

125 lines (80 loc) · 3.41 KB

ConstructorOptions

Properties

  • client MongoClient configured mongo client to use. Can be null if url is set
  • databaseName string? name of the mongodb database
  • collectionName string name of the mongodb collection used to store the resources
  • resourceName string name of the resource e.g. users, customers, topics, shipments

Examples

const { MongoClient } = require('mongodb')
const { OneToFewResourceStorage } = 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 oneToFewResourceStorage = new OneToFewResourceStorage({
  client,
  collectionName: 'api_clients',
  resourceName: 'queues'
})

GetOptions

Properties

  • withMetadata boolean true if also meta data should be returned
  • projection Object MongoDB projection object e.g. { id: 0, name: 0 }

OneToFewResourceStorage

Allows to manage a list of documents in another document to e.g. store a list of students of each school. As the child documents will be embedded, it is easy to retrieve them (only one query), but harder to get all students across e.g. a country in various schools.

Universities collection

{
  name: 'University Munich',
  students: [
    {
       name: 'Stef',
       city: 'Munich
    },
    {
        name: 'Frank',
       city: 'Stuttgart
    }
  ]
}

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 GetOptions

Returns Promise<Object>

getAll

Returns all resources.

Parameters

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

Returns Promise<Array<Object>>

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

Returns ObjectId

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

Returns void

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}

Returns void