Permalink
Browse files

Aliases relationship functions

  • Loading branch information...
1 parent 4c13288 commit 503eac3a9e6cf6a66f09953e54c9ae3468871094 @jperkelens jperkelens committed Feb 27, 2014
Showing with 44 additions and 16 deletions.
  1. +9 −9 README.md
  2. +4 −4 lib/schema_methods.js
  3. +1 −1 package.json
  4. +30 −2 test/index.test.js
View
@@ -11,10 +11,10 @@ handy functions.
## Relationship creator functions
-* [parentOf](#relFunction)
-* [childOf](#relFunction)
-* [hasA](#relFunction)
-* [hasMany](#relFunction)
+* [parentOf/oneToMany](#relFunction)
+* [childOf/manyToOne](#relFunction)
+* [hasA/oneToOne](#relFunction)
+* [hasMany/manyToMany](#relFunction)
* [Relationship](#relationship)
Installation
@@ -101,13 +101,13 @@ __Arguments__
<a name="relFunction" />
### Relationship Creator Functions
-* parentOf - Creates a one to many relationship
-* childOf - Creates a many to one relationship
-* hasA - Creates a one to one relationship
-* hasMany - Creates a many to many relationship
+* parentOf / oneToMany - Creates a one to many relationship
+* childOf / manyToOne - Creates a many to one relationship
+* hasA / oneToOne - Creates a one to one relationship
+* hasMany / manyToMany - Creates a many to many relationship
__Arguments__
-* modelName <String> - Name of related Model
+* modelName <String> - Name of related Model
* [myPath] <String> - Name of path on this schema that refers to related Model. (If not provided, a default is used based on the model name. '_clicks' for the above example)
__Returns__
View
@@ -2,7 +2,7 @@ var Relationship = require('./relationship')
module.exports = function(schema, instance) {
- schema.parentOf = function(modelName, pathName) {
+ schema.parentOf = schema.oneToMany = function(modelName, pathName) {
var relInfo = {
schema: schema,
instance: instance,
@@ -14,7 +14,7 @@ module.exports = function(schema, instance) {
return new Relationship(relInfo)
}
- schema.childOf = function(modelName, pathName) {
+ schema.childOf = schema.manyToOne = function(modelName, pathName) {
var relInfo = {
schema: schema,
instance: instance,
@@ -25,7 +25,7 @@ module.exports = function(schema, instance) {
return new Relationship(relInfo)
}
- schema.hasA = function(modelName, pathName) {
+ schema.hasA = schema.oneToOne = function(modelName, pathName) {
var relInfo = {
schema: schema,
instance: instance,
@@ -36,7 +36,7 @@ module.exports = function(schema, instance) {
return new Relationship(relInfo)
}
- schema.hasMany = function(modelName, pathName) {
+ schema.hasMany = schema.manyToMany = function(modelName, pathName) {
var relInfo = {
schema: schema,
instance: instance,
View
@@ -1,7 +1,7 @@
{
"name": "supergoose",
"description": "Mongoose plugin for simple addons like findOrCreate",
- "version": "0.2.2",
+ "version": "0.2.3",
"keywords": [
"supergoose",
"mongodb",
View
@@ -165,7 +165,7 @@ describe('parentOf', function() {
})
it('should allow for custom paths', function() {
- ReferrerSchema.parentOf('Click', '_funsos')
+ ReferrerSchema.oneToMany('Click', '_funsos')
var path = ReferrerSchema.path('_funsos')
should.exist(path)
@@ -237,6 +237,34 @@ describe('parentOf', function() {
})
})
+ it('remove parent reference from all children if child array set to blank', function(done) {
+ ClickSchema.add({_referrer: { type: Schema.ObjectId, ref: 'Referrer' }})
+ ReferrerSchema.parentOf('Click').enforceWith('_referrer')
+ var Click = mongoose.model('Click', ClickSchema);
+ var Referrer = mongoose.model('Referrer', ReferrerSchema);
+ var id = new mongoose.Types.ObjectId()
+ var id2 = new mongoose.Types.ObjectId()
+ var refId = new mongoose.Types.ObjectId()
+
+ Referrer.create({name: 'hello', _clicks: [id, id2], _id: refId}, function(err, referrer) {
+ Click.create([
+ {_id: id, ip: '1234', _referrer: refId },
+ {_id: id2, ip: '1234', _referrer: refId }
+ ], function(err) {
+ referrer._doc._clicks = []
+ referrer.save(function(err) {
+ Click.find({}).exec(function(err, clicks) {
+ clicks.forEach(function(c) {
+ should.not.exist(c._doc._referrer)
+ })
+ done()
+ })
+ })
+ })
+ })
+ })
+
+
it('should remove child on remove if delete option set', function(done) {
ClickSchema.add({_referrer: { type: Schema.ObjectId, ref: 'Referrer' }})
ReferrerSchema.parentOf('Click').enforceWith('_referrer', {delete: true})
@@ -305,7 +333,7 @@ describe('childOf', function() {
})
it('should allow custom paths', function() {
- ClickSchema.childOf('Referrer', '_funsos')
+ ClickSchema.manyToOne('Referrer', '_funsos')
var path = ClickSchema.path('_funsos')
should.exist(path)

0 comments on commit 503eac3

Please sign in to comment.