0.2.0 • Published 4 years ago

better-mongoose v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Better Mongoose

This project is a wrapper for mongoose. It allows you to use decorators in your typescript to implement mongoose. Everything passed through your decorators get's sent directly to mongoose.

Getting Started

# install package
npm install better-mongoose --save

Sample

import { ObjectID } from 'mongodb';
import {
  Schema,
  Property,
  connect,
  getModel,
  getCollection,
} from 'better-mongoose';

// connect to database using better-mongoose (mongoose under the hood)
connect('mongodb://root:myPassword@localhost:27017/?ssl=false');

// create your structure
@Schema(/* Mongoose schema options here */)
export class User {
  @Property(/* Mongoose Property options here */)
  username: string;

  @Property({
    length: 50,
  })
  firstname: string;

  @Property()
  lastname: string;
}

// find single user using object id
async function FindUser() {
  const userModel = await getModel(User);
  const findResult = await userModel.findOne({
    $where: function () {
      return this._id === new ObjectID(id);
    },
  });
}

// get paginated users using model
async function GetUsers() {
  const userModel = await getModel(User);
  const findResult = await userModel
    .find()
    .skip(limit * page)
    .limit(limit);
}

// create user using model
async function CreateUser() {
  const userModel = await getModel(User);
  const insertResult = await userModel.create({
    username: 'john',
    firstname: 'john',
    lastname: 'Doe',
  });
}

supports arrays and sub-collections

@Schema()
export class Tool {
  @Property()
  name: string;
}

export class Details {
  @Property()
  address: string;
}

@Schema()
export class User {
  @Property()
  username: string;
  
  @Property()
  tools: SubCollection[];

  @Property()
  details: Details;
}
0.2.0

4 years ago

0.1.5

4 years ago

0.1.4

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.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