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.
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} from "mongur";
await connetion.connect("mongodb://127.0.0.1:27017/mongur")
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.