Socket
Socket
Sign inDemoInstall

p-odm

Package Overview
Dependencies
3
Maintainers
1
Versions
57
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    p-odm

ODM mongodb library for node.js


Version published
0
Maintainers
1
Install size
1.91 MB
Created
Weekly downloads
 

Readme

Source

ODM

ODM is a new, innovative and easy way to use MongoDB documents, as Models, in your code. It uses the JSON schema standard for validating the documents.

Quick Start

Connect

odm.connect('mongodb://127.0.0.1:27017/simple');

Define a model

var Person = odm.model("persons", {
  "type" : "object",
  "properties": {
    "name": {"type": "string"},
  }
});

Embedding other documents

We embed an address model in person:

// Address, to be embedded on Person
var Address = odm.model({
  "id": "Simple#Address",
  "type" : "object",
  "properties": {
    "lines": {
      "type": "array",
      "items": {"type": "string"}
    },
    "zip": {"type": "string"},
    "city": {"type": "string"},
    "country": {"type": "string"}
  }
});

The changed person model:

var Person = odm.model("persons", {
  "type" : "object",
  "properties": {
    "name": {"type": "string"},
    "address": {"$ref": "Simple#Address"}
  }
});

Methods

Class methods

ODM#parse(jsonString)

Parses a JSON string to a JSON document. It is aware of ISO Dates and ObjectIds and coverts them on the fly.

Model#findOne(query, fields, options, callback)

Finds one document or fields, satisfying query.

Person.findOne({'name': 'Barack Obama'}, function (error, document) {
  if (error) {
    console.log("error", error);
  }
  console.log(document);
});
Model#findById(id, fields, options, callback)

Finds one document by id, returining fields.

Person.findById("4ff3fcf14335e9d6ba000001", function (error, document) {
  if (error) {
    console.log("error", error);
  }
  console.log(document);
});
Model#find(query, fields, options, callback)

Finds all documents or fields, satisfying query.

Person.find({'name': 'Barack Obama'}, function (error, documents) {
  if (error) {
    console.log("error", error);
  }
  console.log(documents);
});
Model#findAll(fields, options, callback)

Finds all documents or fields.

Person.findAll(function (error, documents) {
  if (error) {
    console.log("error", error);
  }
  console.log(documents);
});
Model#remove(query, options, callback)

Removes all documents satisfying query.

Model#update(query, document, options, callback)

Update all documents satisfying query, with document.

Model#loadDbRef(id/ids, options, callback)

Loads one Id or array of ids, it is similar to a simple find, however the number of results and order is the same as the array argument

Model#ensureIndex(fieldOrSpec, options, callback)

Adds an index and will also add a findByXXX method, where XXX is the name of the fieldOrSpec

Instance methods

Model.validate(verbose)

Returns true or false, or all errors in case verbose is true.

Model.save(options, callback)

Saves the instance model.

p.save(function (error, id) {
  if (error) {
    console.log("error", error);
  } 
  console.log(id);
});
Model.update(query, document, options, callback)

Update the instance model.

Model.insert(options, callback)

Insert the instance model.

Model.remove(options, callback)

Remove the instance model.

Keywords

FAQs

Last updated on 18 Mar 2013

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc