crud-mongoose-connector
TypeScript icon, indicating that this package has built-in type declarations

0.2.2 • Public • Published

Crud Mongoose Connector

Description

Create an object with CRUD operations from a mongoose model.

Installation

npm i --save crud-mongoose-connector

Usage

import mongoose from 'mongoose';
import { mongooseCrudConnector, MongooseCrudModel } from 'crud-mongoose-connector';
 
interface TestModel {
  _id: string;
  name: string;
  anotherField?: Date;
}
 
const testSchema = new mongoose.Schema({
  anotherField: Date,
  name: { type: String, required: true },
});
const testModel = mongoose.model(testSchema, 'test');
 
const testCRUD: MongooseCrudModel<TestModel= mongooseCrudConnector(testModel);
 
// then you can use testCRUD to create / read / update / delete
testCRUD.create({ data: { name: 'obj1', anotherField: new Date() }}); // returns a Promise with the newly created object
testCRUD.read({ id: 'example-uuid' }); // returns a Promise with ONE object or throws an error if no object found
testCRUD.read({ data: { name: 'obj1' } }); // returns a Promise with an array of all found object corresponding to the query
testCRUD.read({ id: 'example-uuid', data: { name: 'obj1' } }); // data is ignored if id is provided
testCRUD.update({ id: 'example-uuid', date: { name: 'obj2' }}); // returns a Promise with the updated element
testCRUD.delete({ id: 'example-uuid' }); // returns a Promise with the deleted element

Notes

You can use mongoose-plugin-events with this library to have the model emit the create/update/delete events.

Readme

Keywords

none

Package Sidebar

Install

npm i crud-mongoose-connector

Weekly Downloads

1

Version

0.2.2

License

MIT

Unpacked Size

32.5 kB

Total Files

17

Last publish

Collaborators

  • chipp972