odmongo
Barebones MongoDB ODM.
Last updated 5 months ago by goto-bus-stop .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install odmongo 
SYNC missed versions from official npm registry.

odmongo

A barebones MongoDB ODM designed for use with modern ES syntax.

It provides a very small wrapper that you can extend in various places to build yourself a nice DB library.

Usage

In short:

const joi = require('joi')
const { Connection, Model } = require('odmongo')

// Set up a connection instance
const connection = new Connection()

// Create some Model classes
class User extends Model {
  validate () {
    return joi.validate(this.fields, User.schema)
  }
}
User.schema = joi.object({
  username: joi.string().required()
})
User.collection = 'users'

// Register models on the connection instance
connection.define({ User })

// Connect!
await connection.connect('mongodb://localhost:27017/my_db')

// Query existing documents
for await (const user of connection.models.User.find()) {
  console.log(user.fields.username)
}

// Create new documents
const user = new connection.models.User({
  username: 'Me'
})
try {
  await user.save()
  console.log('saved')
} catch (err) {
  console.error('Could not save the new user:', err.message)
}

API

See the documentation in API.md.

License

Apache-2.0

Current Tags

  • 1.0.0-beta.1                                ...           latest (5 months ago)

2 Versions

  • 1.0.0-beta.1                                ...           5 months ago
  • 1.0.0-beta.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (1)
Dev Dependencies (3)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |