0.3.2 • Published 4 years ago

ormon v0.3.2

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

Ormon

Object ~Relation~ Document Mapper for Mongodb

Quick Start

// import ormon
const { OrmonEngine, OrmonModel, MongoDriver } = require("ormon")

// connect to the database
OrmonEngine.init(new MongoDriver("mongodb://localhost:27017/db"))

// Create a model
class Post extends OrmonModel {}

// Bind the model to a collection
OrmonEngine.bind("posts", Post)

await Promise.all([
  // We're talking about mongo, you can initilize your object with anything ;)
  (new Post({title: "first", body: "My first article"})).save(), // Call save to save, duh.
  (new Post({title: "second", body: "My second article"})).save(),
  (new Post({title: "third", body: "My third article"})).save(),
])

// Retrieve the 1st post
const first = await Post.findOne({title: "first"})

console.log(first.$state.body) // My second article
//                ^-------------- You can access any property from the state

// Deletes the post
await first.delete()

// list only articles with a `t` in the title
const articles = await Post.findMany({title: /t/}, null, d => d.format())
// do not apply any sort / limit filters -----------^           ^-------- do convert each document into clean obj

console.log(articles) // [{title: "third", body: "My third article", id: "349080932840"}]

// Save handles update as well
articles[0].$state.title = "first"
await articles[0].save()

const articles = await Post.findMany({title: /t/}, null, d => d.format())
console.log(articles) // [{title: "first", body: "My third article", id: "349080932840"}]

Not just for storing...

The point of having a class that acts as a model is that one can extends its behavior:

const { OrmonEngine, OrmonModel } = require("ormon")

class User extends OrmonModel {
  /** Sets the password of the user and hashes it
   *
   *  @param {String} data The password to hash
   */
  set password(data) { this.$state.password = sha384(data) }

  /** Checks if the provided password matches the user's
   *
   *  @param {String} password The password to test
   *  @return {Boolean} Whether both password matched
   */
  authenticate(password) { return this.$state.password === sha384(password) }
}

OrmonEngine.bind("users", User)

const user = await User.findOne({username: req.body.username })
if (!user || !user.authenticate(req.body.password))
  return res.status(401).json({code: "Unauthorized", message: "We don't know who you are!"})

Contributing

Pre-requisits

One needs docker and docker-compose + the right to run commands as current user. Mostly (actually only) for test purposes

Yarn commands

  • test: Runs unit tests and cover (actually runs docker with mongo and then runs tests)
    • test:docker: Tests runs within the docker container
    • test:ci: Runs test and report coverage to codecov
  • lint: Runs eslint (yarn lint --fix to fix some errors)
  • doc: Generates the documentation
0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.0

5 years ago