https://github.com/sha256/mongur
Define MongoDB models and query data using Typescript classes.
https://github.com/sha256/mongur
javascript mongodb mongoose odm orm typegoose typescript
Last synced: 5 months ago
JSON representation
Define MongoDB models and query data using Typescript classes.
- Host: GitHub
- URL: https://github.com/sha256/mongur
- Owner: sha256
- License: mit
- Created: 2023-01-19T12:22:49.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-24T15:08:30.000Z (almost 2 years ago)
- Last Synced: 2025-05-12T03:15:28.712Z (5 months ago)
- Topics: javascript, mongodb, mongoose, odm, orm, typegoose, typescript
- Language: TypeScript
- Homepage: https://mongur.dev
- Size: 501 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](https://mongur.dev)## Install
```shell
# npm
npm install mongur --save# yarn
yarn add mongur# pnpm
pnpm add mongur
```## Guides
- [Define your models](https://mongur.dev/models)
- [Write queries](https://mongur.dev/queries)
- [API reference](https://mongur.dev/reference/api)## Basic Usage
Define your models:
```typescript
import {model, Model} from "mongur"@model()
export class User extends Model() {@field()
firstName!: string;@field()
lastName!: string;
@field()
email!: string
@field()
password?: string}
```**Connect:**
```typescript
import {connection} from "mongur";
await connetion.connect("mongodb://127.0.0.1:27017/mongur")```
**Insert:**
```typescript
const user = new User({firstName: "John", lastName: "Doe", email: "john@example.com"})
await user.save()
```**Query:**
```typescript
const user = await User.find({email: "john@example.com"}).one()
```**Update:**
```typescript
await User.find({email: "john@example.com"}).update({$set: {email: "john@example.net"}})
```**Delete:**
```typescript
await User.find({email: "john@example.com"}).delete()
```## Motivation
```typescript
// Mongoose example
const User = model('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:```typescript
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.