Skip to content

Commit

Permalink
Added needed scripts and README
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiel committed Jan 21, 2012
1 parent 8baa063 commit 0de0947
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Alias-Field-Mongoose-plugin</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>
4 changes: 4 additions & 0 deletions .settings/.jsdtscope
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="output" path=""/>
</classpath>
1 change: 1 addition & 0 deletions .settings/org.eclipse.wst.jsdt.ui.superType.container
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.eclipse.wst.jsdt.launching.JRE_CONTAINER
1 change: 1 addition & 0 deletions .settings/org.eclipse.wst.jsdt.ui.superType.name
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Global
83 changes: 83 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
mongoose-aliasfield
===================

This plugin let you add a `alias` key to your schema and create getter and setter for your field using that alternate name.

Plugin is intended to write short-keys for you documents on the DB but let you use long, descriptive name when reading fetched documents.
This will result in less storage needed to memorize your data having no need to remember short key meanings.


Take this schema as example:

```
var mongoose = require('mongoose);
var fieldsAliasPlugin = require('mongoose-aliasfield');

/*Describe a Schema*/
var PersonSchema = new Schema({
't' : {'type': Date, 'index': true, 'alias': 'timestamp'},
'n' : {'type' : String, 'alias': 'name'},
's' : {'type' : String, 'alias': 'surname'},
'p' : {
'a' : {'type' : String, 'alias': 'profile.address'},
'pn': {'type' : String, 'alias': 'profile.phone_number'}
}
});

/*Add field alias plugin*/
PersonSchema.plugin(fieldsAliasPlugin);

/*Person will be the model*/
[...]
```

Now that your `schema` is created you can use alias field name to describe an instance of your model

```
var person = new Person({
'timestamp' : new Date(),
'name' : 'Jhon',
'surname' : 'Doe',
'profile.address': 'Rue de Morgane',
'profile.phone_number': '051-123456 78',
});

person.save();

```

Even getters will run out of the box

```
var full_name = person.name+' '+person.surname;
```

`full_name` will be `Jhon Doe`

The only limitation in setters and getters is that you can'y use partial path for nested properties

```
/*THIS WON'T ACT AS EXPECTED!*/
var user_profile = person.profile;
```

You'll be able to obtain even an aliased description of object as i the example below

```
Person.find({'name': 'Jhon'}, function(err,people){
console.log( people.toAliasedFieldsObject() );
});

```
Your models gain a method called `toAliasedFieldsObject` which return a long-descriptive version of your docs:

```
{
'name' : 'Jhon',
'surname': 'Doe',
'profile': {
'address' : 'Rue de Morgane',
'phone_number' : '051-123456 78'
}
}
```
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* @author Fabrizio Ruggeri
*/

module.exports = require('./lib/field-alias-plugin.js');
79 changes: 79 additions & 0 deletions lib/field-alias-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Mongoose Plugin to alias fields name
* Schema gains a property: 'alias' which is long name for a property. It will be used as getter and setter
* Limitation. For deep nested properties only the entire property is get/set-table
* @param schema
* @param options
*/


/**
* Dinamically set an array-descripted property into an object
* Es: [key,to,set] => obj{key:{to:{set: value} } }
* @param {Object} document Object to enrich. Note that document will be modified
* @param {Array} property Array descripted property. If property is p='key.to.set' simply pass p.split('.')
* @param {Any} value Value to set at the end of key chain
*/
function _propertyInflector(document, property ,value){
if(property.length > 1){
document[property[0]] = document[property[0]] || {};
return _propertyInflector( document[property[0]], property.slice(1), value);
}else{
document[property[0]] = value;
return document;
}
}

/**
* Give a instance method to retrieve object using aliased field
* @param {Object}Schema schema to modify
* @returns {Function} Methods (Mongoose intended) for schema
*/
function _toAliasedFieldsObjectProvider(schema){

return function toAliasedFieldsObject(){
var document = {};
for(var p in schema.paths){
var property = schema.paths[p];
if(property.options.alias && 'string' == typeof property.options.alias && property.options.alias != ''){
var alias = property.options.alias.split('.');
_propertyInflector(document,alias, this.get(property.path) );
}else{
document[property.path] = this.get(property.path);
}
}
return document;
};
}


module.exports = exports = function fieldAliasPlugin(schema, options) {

for(path in schema.paths){
/*Set alias name only if alias property is setted in schema*/
if( schema.paths[path].options.alias && 'string' == typeof schema.paths[path].options.alias && schema.paths[path].options.alias != ''){
var aliased_property = schema.paths[path].options.alias;
var real_property = schema.paths[path].path;

//Adding getters and setters for virtual alias names
schema
.virtual(aliased_property)
.get((function(prop){
return function(){
return this.get(prop);
};
})(real_property))
.set((function(prop){
return function(value){
return this.set(prop, value);
};
})(real_property)
);
}
}

/*Adding method toAliasedFieldsObject to schema*/

schema.methods.toAliasedFieldsObject = _toAliasedFieldsObjectProvider(schema);

};
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "mongoose-aliasfield"
, "description": "Field alias support for mongoose"
, "version": "0.0.1"
, "author": "Fabrizio Ruggeri"
, "dependencies": {
"mongoose": ">=2.0.0"
}
, "keywords": [ "mongoose", "mongo", "mongodb", "alias", "field" ]
, "engines": {
"node": ">= 0.4.0"
}
, "main": "./index.js"
"repository": {
"type": "git"
, "url": "git://github.com/ramiel/Alias-Field-Mongoose-plugin.git"
}
}

0 comments on commit 0de0947

Please sign in to comment.