@restlessness/dao-mongo
v1.0.5
Published
This is the official [MongoDB](https://www.mongodb.com/) DAO [(Data Access Object)](https://it.wikipedia.org/wiki/Data_Access_Object) for the [restlessness](https://github.com/getapper/restlessness) framework.
Downloads
44
Readme
@restlessness/dao-mongo
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
| Name | Description | -------|-------------- | MONGO_URI | Database uri (example: mongodb://localhost:27017 | | MONGO_DB_NAME | Database 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 querystatic get dao(): MongoDao
Returns: the MongoDao object used to perform all the querygetById(_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
otherwisestatic 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 querystatic 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 theQuerySelector
save(): Promise<boolean>
Save the current object
Returns:true
if the operation is successful,false
otherwiseupdate(): Promise<boolean>
Update the current object
Returns:true
if the operation is successful,false
otherwisepatch(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
otherwiseremove<T>(): Promise<boolean>
Remove the element from the db
Returns:true
if the operation is successful,false
otherwisestatic 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 invocationfindOne(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 queryfind(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 queryinsertOne(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 detailsupdateOne(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 detailsupdateMany(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 detailsdeleteOne(collectionName: string, filter: Object): Promise<DeleteWriteOpResultObject>
collectionName: name of the collection to query
filter: query filter
Returns: delete result, see mongodb types for detailsdeleteMany(collectionName: string, filter: Object): Promise<DeleteWriteOpResultObject>
collectionName: name of the collection to query
filter: query filter
Returns: update result, see mongodb types for detailscount(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 querycreateIndex(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