New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

model-mixin

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

model-mixin

Helper for writing mixins to Mongoose models, Backbone models and plain ole JavaScript objects

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

model-mixin

NPM version

Build Status

About

At Good Eggs we are on a quest for a ubiquitous domain model implementation that works across frameworks in both the browser and in Node.js. When you have an instance of a domain model in our applications, we want it to have the same interface and behavior, regardless of where it is running.

In this quest we've been using a strategy in our apps at Good Eggs that achieves a similar end with a different strategy. We write mixins that add behavior to models from different frameworks. So if you have reference to a User model in Mongoose or Backbone or as a JavaScript object from a JSON API, it has the same interface backed by the same code. It's not the final answer but yields a ton of immediate benefit in minimizing code duplication and sharing behavior across browser and server.

Usage

Write a mixin

var modelMixin = require('model-mixin');

var UserMixin = modelMixin({
  methods: {
    uppercaseName: function() {
      return this.get('name').toUpperCase();
    }
   },

  statics: {
    version: function() {
      return '1.0.0';
    }
  }
});

module.exports = UserMixin;

Mix in to Backbone model

var User = Backbone.Model.extend({});
UserMixin(User);

var user = new User({name: 'Noah'});
console.log user.uppercaseName(); // NOAH
console.log(User.version()); // 1.0.0

Mix in to Mongoose model

var mongoose = require('mongoose');
var UserMixin = require('./user_mixin');

var schema = new mongoose.Schema({
  name: String
});
UserMixin(schema);
var User = mongoose.model('User', schema);

var user = new User({name: 'Noah'});
console.log(user.uppercaseName()); // NOAH
console.log(User.version()); // 1.0.0

Mix in to JavaScript object

var UserMixin = require('./user_mixin');

var user = {name: 'Noah'};
UserMixin(user);

console.log(user.uppercaseName()); // NOAH

Code of Conduct

Code of Conduct for contributing to or participating in this project.

License

MIT

Module scaffold generated by generator-goodeggs-npm

FAQs

Package last updated on 20 Jun 2014

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc