npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-aliasfield

v0.3.7

Published

Field alias support for mongoose

Downloads

120

Readme

mongoose-aliasfield

gitcheese.com Build Status Dependency Status devDependency Status Coverage Status

Discover on Ramiel's creations

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.

Note

IMPORTANT: Starting from version 4.10.0 of mongoose, a feature similar to the one provided by this plugin has been developed in the core library. Even if the implementation are not exactly the same, I prefer to deprecate this plugin in favor of the core implementation. For this reason this plugin won't install anymore alongside a recent version of mongoose. If something is missing in the core implementation you should open a request on the mongoose github page.

Installation

To install it in your node.js project

npm install mongoose-aliasfield

or add it to your package.json dependencies

Schema Example

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't 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.findOne({'n': 'Jhon'}, function(err,person){
	console.log( person.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'
	}
}

The same is applyable to an array of results

Person.find({}, function(err,people){
	people = people.map(function(p){
		return p.toAliasedFieldsObject();
	});
});

Utilities

Transform between representations

toOriginalFieldsObject

Sometimes you want to do some operation but you have just the aliased representation of an instance. Consider the following example:

var PersonSchema = new Schema({
    n : {type : String, required : true, alias: 'name'},
    a : {
        s: {type: String, alias: 'address.street' },
        d: {type: Date, alias: 'address.date'}
    },
    likes: {type: Number}
    
});
PersonSchema.plugin(fieldsAliasPlugin);
this.Person = mongoose.model('person', PersonSchema);

let's say you want to do an update

var data = {
	name: 'John',
	address: {
		street: 'Avenue ...',
		date: new Date()
	},
    likes: 5
}
Person.update({name: 'John'}, data, function(){
	...
});

This won't work because mongoose is not able to understand the aliases.
Your model has a static method which help you to move from an aliased representation of your data to the one you have on the database.
You can write

Person.update({name: 'John'}, Person.toOriginalFieldsObject(data), function(){
    ...
});

Edge case: you cannot transform mixed representation. All the properties which have an alias must be represented with the alias.
In our example this won't work:

var data = {
    name: 'John',
    address: {
        s: 'Avenue ...',
        d: new Date()
    },
    likes: 5
}
Person.toOriginalFieldsObject(data); // This will result in an invalid object

here we are mixing address (aliased) and s (not aliased), which is not permitted. toOriginalFieldsObject can be expensive, so use it only if you're forced to.

toOriginalFieldFromAlias

You can transform even a single field. Given the same schema as before

    const flatten = Person.toOriginalFieldFromAlias('address.street');
    // flatten is
    // 'a.s'

Author

Fabrizio 'ramiel' Ruggeri