Skip to content

Commit

Permalink
README restyles
Browse files Browse the repository at this point in the history
  • Loading branch information
goulash1971 committed Jun 8, 2011
1 parent dfe0c08 commit 969a33c
Showing 1 changed file with 42 additions and 44 deletions.
86 changes: 42 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,56 @@ to be dereferenced from the models.

The extension provides the following types:

- 'DBRef' : this is the schema type that can be used for database references
- `DBRef` : this is the schema type that can be used for database references

The extension provides the following plugins:

- 'resolveDBRefs' : used to create getter/setter methods that resolve DBRefs
- `resolveDBRefs` : used to create getter/setter methods that resolve DBRefs

The extension provides the following utilities:

- 'fetch' : fetches the object referenced by a DBRef value
- `fetch` : fetches the object referenced by a DBRef value


### Installation
npm install mongoose-dbref

### Setup
To install all of the types, plugins and utilities provided by the extension into a mongoose instance:
var mongoose = require("mongoose");

// Create a connection to your database
var db = mongoose.createConnection("mongodb://localhost/sampledb");

// Access the mongoose-dbref module and install everything
var dbref = require("mongoose-dbref");
var utils = dbref.install(mongoose);
var mongoose = require("mongoose");
// Create a connection to your database
var db = mongoose.createConnection("mongodb://localhost/sampledb");
// Access the mongoose-dbref module and install everything
var dbref = require("mongoose-dbref");
var utils = dbref.install(mongoose);

To just install the types provided by the extension (either all types or a list of named types):
var mongoose = require("mongoose");
var mongoose = require("mongoose");

// Create a connection to your database
var db = mongoose.createConnection("mongodb://localhost/sampledb");
// Create a connection to your database
var db = mongoose.createConnection("mongodb://localhost/sampledb");

// Access the mongoose-dbref module and install everything
var dbref = require("mongoose-dbref");
var utils = dbref.loadTypes(mongoose);
// Access the mongoose-dbref module and install everything
var dbref = require("mongoose-dbref");
var utils = dbref.loadTypes(mongoose);

To just install the plugins provided by the extension (either all plugins or list of named plugins):
var mongoose = require("mongoose");

// Create a connection to your database
var db = mongoose.createConnection("mongodb://localhost/sampledb");

// Access the mongoose-dbref module and install everything
var dbref = require("mongoose-dbref");
var utils = dbref.loadTypes(mongoose);
var mongoose = require("mongoose");
// Create a connection to your database
var db = mongoose.createConnection("mongodb://localhost/sampledb");
// Access the mongoose-dbref module and install everything
var dbref = require("mongoose-dbref");
var utils = dbref.loadTypes(mongoose);

### Using the types
Once you have loaded the types, or installed the whole extension, you can begin to use them.

#### 'DBRef'
The 'DBRef' type is a top-level schema type that can be used to identfied a field as holding
#### `DBRef`
The `DBRef` type is a top-level schema type that can be used to identfied a field as holding
a MongoDb database reference. You use the type as you would any other standard type.
var DBRef = mongoose.SchemaTypes.DBRef;

Expand All @@ -67,22 +67,22 @@ a MongoDb database reference. You use the type as you would any other standard
description: String,
cost: Number
});

var OrderSchema = new Schema({
poNumber: String,
lineItems: [DBRef]
});

This will create two schema - 'OrderSchema' that has a field ('lineItems') which can hold
multiple references, and 'LineItemSchema' that has a field 'order' with a single reference.
This will create two schema - `OrderSchema` that has a field (`lineItems`) which can hold
multiple references, and `LineItemSchema` that has a field 'order' with a single reference.

All of the 'standard' options ('required', 'index' etc) can be applied to these fields.
All of the 'standard' options (`required`, `index` etc) can be applied to these fields.

### Using the plugins
Once you have installed the plugins, or installed the whole extension, you can begin to use them.

#### 'resolveDBRefs'
The 'resolveDBRefs' plugin can be used to be used to automatically install *getter* and *setter*
#### `resolveDBRefs`
The `resolveDBRefs` plugin can be used to be used to automatically install *getter* and *setter*
methods for fields where a 'resolve' option has been set.

These methods will map DBRef values to/from objects via the database connection of the owning
Expand All @@ -100,8 +100,8 @@ This will create two methods:
- `setOrder(value)` - this will cast the `value` (a model) to a DBRef value

In addition if the 'cache' option is set, then the object resolved from teh DBRef value will be
cached in a cache property ('$order' for the 'order' field) and the getter method signature
will be changed to 'getOrder(callback, force)'. The additional, optional, parameter 'force'
cached in a cache property (`$order` for the `order` field) and the getter method signature
will be changed to `getOrder(callback, force)`. The additional, optional, parameter 'force'
can be used to by pass any cached value.

This plugin can be installed on a mongoose instance or on individual schema, but the "owning"
Expand All @@ -110,16 +110,16 @@ mongoose instance for the schema must always be specified.
### Using the utilities
Once you have installed the utilities, or installed the whole extension, you can begin to use them.

#### 'utils.fetch'
This utility function can be used to resolve a given 'DBRef' value when supplied with a mongoose
#### `utils.fetch`
This utility function can be used to resolve a given `DBRef` value when supplied with a mongoose
database connection and a callback function.
var mongoose = require("mongoose");
var mongoose = require("mongoose");
var db = mongoose.createConnection("mongodb://localhost/sampledb");

var utils = require("mongoose-dbref").utils;

var utils = require("mongoose-dbref").utils;
var LineItem = db.model('LineItem');

LineItem.findById("4dee1f473abd4fbc61000001",
function(err, doc) {
utils.fetch(db, doc.order,
Expand All @@ -144,5 +144,3 @@ MIT License
---
### Author
Stuart Hudson

0 comments on commit 969a33c

Please sign in to comment.