1.0.5 • Published 2 years ago

@restlessness/dao-mongo v1.0.5

Weekly downloads
65
License
MIT
Repository
github
Last release
2 years ago

@restlessness/dao-mongo npm version

This is the official MongoDB DAO (Data Access Object) for the restlessness framework.

This package relies on the plugin serverless-mongo-proxy to create a database proxy.

Installation

From you restlessness project root run:

$ # Install node package
$ npm i @restlessness/dao-mongo
$
$ # Add it to the project using the restlessness cli
$ restlessness add-dao @restlessness/dao-mongo

The add-dao command installs the serverless-mongo-proxy plugin and adds proper permissions to allow lambda invocation.

Environment variables

NameDescription
MONGO_URIDatabase uri (example: mongodb://localhost:27017
MONGO_DB_NAMEDatabase name

The add-dao command also adds parametric environment variables to the .env files present in the project.

Example .env.production file:

MONGO_URI=${MONGO_URI_PRODUCTION}

Then is possible to set the variable by setting MONGO_URI_PRODUCTION or setting MONGO_URI directly.

Usage

The restlessness web-interface allows creating models based on the exported MongoBase class, which already has convenience methods for db query.

Example:

// src/models/User/index.ts

import { MongoBase } from '@restlessness/dao-mongo';

export default class User extends MongoBase {
  ['constructor']: typeof MyModel
  name: string
  surname: string

  static get collectionName() {
    return 'user';
  }

  // ...
};

All convenience methods can be accessed from the extended class:

// ...
const user = new User();
await user.getById(id);
console.log(user.name, user.surname);

user.name = 'Arthur';
user.surname = 'Dent';
await user.save();
// ...

The package also exports a mongoDao object (of type MongoDao) that can be used to directly run a query.

Example:

// src/endpoints/get-user/handler.ts

import { mongoDao } from '@restlessness/dao-mongo';
// ...
const user = { name: 'Arthur', surname: 'Dent' };
const result = await mongoDao.insertOne('my-collection', user);
// ...

Hooks

The package uses the beforeEndpoint hook to extend the yup validation by defining the yup.objectId validation object.

Documentation

MongoBase class

Methods:
  • static get collectionName()\ Returns: the collection name used for all the query

  • static get dao(): MongoDao\ Returns: the MongoDao object used to perform all the query

  • getById(_id: ObjectId): Promise<boolean>\ Get the resource by assigning it to the current object\ _id: the id of the resource to be queried\ Returns: true if an entry with the provided id has been found, false otherwise

  • static getList<T>(query: QuerySelector<T> = {}, limit: number = 10, skip: number = 0, sortBy: string = null, asc: boolean = true): Promise<T[]>\ Get a list of elements satisfying the provided query\ query: query constraint\ limit: maximum number of elements returned\ skip: number of element to be skipped when returning the list\ sortBy: sorting key\ asc: ascending or descending order\ Returns: list of element that satisfy the query

  • static getCounter<T>(query: QuerySelector<T> = {}): Promise<number>\ Get the number of element that satisfy the provided query\ query: query constraint\ Returns: number of element that satisfy the QuerySelector

  • save(): Promise<boolean>\ Save the current object\ Returns: true if the operation is successful, false otherwise

  • update(): Promise<boolean>\ Update the current object\ Returns: true if the operation is successful, false otherwise

  • patch(fields: any): Promise<boolean>\ Update the specified fields for the current object fields: fields to the updated\ Returns: true if the operation is successful, false otherwise

  • remove<T>(): Promise<boolean>\ Remove the element from the db\ Returns: true if the operation is successful, false otherwise

  • static createIndex(keys: string | any, options: IndexOptions): Promise<boolean>\ keys: keys on which the index will be created\ options: index options\ Returns: true if the operation is successful, false otherwise

mongoDao object

Methods:
  • invokeProxy(request: ProxyRequest): Promise<InvocationResponse>\ Invoke the database proxy lambda, which will execute the query and return the result\ request: object representing the requested query, see serverless-mongo-proxy for details\ Returns: result of invocation

  • findOne(collectionName: string, filter: Object, options?: FindOneOptions): Promise<any>\ collectionName: name of the collection to query\ filter: query filter\ options: see mongodb types for details\ Returns: the object satisfying the query

  • find(collectionName: string, filter: Object, options?: FindOneOptions): Promise<any>\ collectionName: name of the collection to query\ filter: query filter\ options: see mongodb types for details\ Returns: the objects satisfying the query

  • insertOne(collectionName: string, object): Promise<InsertOneWriteOpResult<null>>\ collectionName: name of the collection to query\ object: the object to be inserted into the db\ Returns: insert result, see mongodb types for details

  • updateOne(collectionName: string, filter: Object, object, options?: UpdateOneOptions): Promise<UpdateWriteOpResult>\ collectionName: name of the collection to query\ filter: query filter\ object: the object to be updated\ options: see mongodb types for details\ Returns: update result, see mongodb types for details

  • updateMany(collectionName: string, filter: Object, object, options?: UpdateManyOptions): Promise<UpdateWriteOpResult>\ collectionName: name of the collection to query\ filter: query filter\ object: the object to be updated\ options: see mongodb types for details\ Returns: update result, see mongodb types for details

  • deleteOne(collectionName: string, filter: Object): Promise<DeleteWriteOpResultObject>\ collectionName: name of the collection to query\ filter: query filter\ Returns: delete result, see mongodb types for details

  • deleteMany(collectionName: string, filter: Object): Promise<DeleteWriteOpResultObject>\ collectionName: name of the collection to query\ filter: query filter\ Returns: update result, see mongodb types for details

  • count(collectionName: string, filter: Object, options?: MongoCountPreferences): Promise<number>\ collectionName: name of the collection to query\ filter: query filter\ options: see mongodb types for details\ Returns: number of element satisfying the query

  • createIndex(collectionName: string, keys: string | any, options: IndexOptions): Promise<string>\ collectionName: name of the collection to query\ keys: keys on which the index will be created\ options: see mongodb types for details Returns: identifier of the created index

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.6.0

2 years ago

0.5.10

3 years ago

0.5.8

3 years ago

0.5.7

3 years ago

0.5.6

3 years ago

0.5.5

3 years ago

0.5.4

3 years ago

0.5.3

4 years ago

0.5.0

4 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.4

4 years ago

0.1.5

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.12

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago