0.0.4 • Published 1 year ago

mongur v0.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Mongur

Define MongoDB models and query data using Typescript/ES6 classes.

Note: It needs a lot more tests before it's ready for use in production.

Documentation

https://mongur.dev

Install

# npm 
npm install mongur --save

# yarn
yarn add mongur

# pnpm
pnpm add mongur

Guides

Basic Usage

Define your models:

import {model, Model} from "mongur"

@model()
export class User extends Model<User>() {

  @field()
  firstName!: string;

  @field()
  lastName!: string;
  
  @field()
  email!: string
  
  @field()
  password?: string

}

Connect:

import {connection, MongoClient} from "mongur";

connection.client = new MongoClient("mongodb://127.0.0.1:27017/mongur", {monitorCommands: true})
await connetion.connect()

Insert:

const user = new User({firstName: "John", lastName: "Doe", email: "john@example.com"})
await user.save()

Query:

const user = await User.find({email: "john@example.com"}).one()

Update:

await User.find({email: "john@example.com"}).update({$set: {email: "john@example.net"}})

Delete

await User.find({email: "john@example.com"}).delete()

Motivation

// Mongoose example
const User = model<IUser>('User', userSchema);
// Typegoose example
const User = getModelForClass(ModelClass);

Here, in both cases User is not a type, it's a value. Therefore, it cannot be used to specify type in the code. For example, you cannot define a function like this:

function doSomething(user: User) { // Error, 'User' refers to a value, but is being used as a type here

}

Many people counter this problem by writing an interface with the same fields (Declaration Merging). Others just use any type instead. Mongur solves this problem by defining schema using pure class and using the same class for querying data.

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago