Socket
Socket
Sign inDemoInstall

redux-orm

Package Overview
Dependencies
Maintainers
1
Versions
70
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redux-orm - npm Package Compare versions

Comparing version 0.1.16 to 0.1.17

36

lib/Model.js

@@ -21,2 +21,6 @@ 'use strict';

var _lodashLangIsArray = require('lodash/lang/isArray');
var _lodashLangIsArray2 = _interopRequireDefault(_lodashLangIsArray);
var _Session = require('./Session');

@@ -34,2 +38,4 @@

var _fields = require('./fields');
var _constants = require('./constants');

@@ -461,2 +467,4 @@

value: function create(userProps) {
var _this4 = this;
var idAttribute = this.idAttribute;

@@ -468,2 +476,4 @@ var props = Object.assign({}, userProps);

var m2mVals = {};
(0, _lodashObjectForOwn2['default'])(userProps, function (value, key) {

@@ -473,2 +483,11 @@ if (value instanceof Model) {

}
// If a value is supplied for a ManyToMany field,
// discard them from props and save for later processing.
if ((0, _lodashLangIsArray2['default'])(value)) {
if (_this4.fields.hasOwnProperty(key) && _this4.fields[key] instanceof _fields.ManyToMany) {
m2mVals[key] = value;
delete props[key];
}
}
});

@@ -481,3 +500,18 @@

var ModelClass = this;
return new ModelClass(props);
var instance = new ModelClass(props);
(0, _lodashObjectForOwn2['default'])(m2mVals, function (value, key) {
var _instance$key;
var ids = value.map(function (arrVal) {
if (arrVal instanceof Model) {
return arrVal.getId();
}
return arrVal;
});
(_instance$key = instance[key]).add.apply(_instance$key, _toConsumableArray(ids));
});
return instance;
}

@@ -484,0 +518,0 @@ }, {

84

lib/test/testSchema.js

@@ -19,5 +19,5 @@ 'use strict';

var _Model11 = require('../Model');
var _Model13 = require('../Model');
var _Model12 = _interopRequireDefault(_Model11);
var _Model14 = _interopRequireDefault(_Model13);

@@ -52,3 +52,3 @@ var _fields = require('../fields');

return PersonModel;
})(_Model12['default']);
})(_Model14['default']);

@@ -67,3 +67,3 @@ Person.modelName = 'Person';

return LocationModel;
})(_Model12['default']);
})(_Model14['default']);
Location.modelName = 'Location';

@@ -117,3 +117,3 @@ });

return PersonModel;
})(_Model12['default']);
})(_Model14['default']);

@@ -132,3 +132,3 @@ PersonModel.modelName = 'Person';

return LocationModel;
})(_Model12['default']);
})(_Model14['default']);

@@ -215,3 +215,3 @@ LocationModel.modelName = 'Location';

return PersonModel;
})(_Model12['default']);
})(_Model14['default']);

@@ -230,3 +230,3 @@ PersonModel.modelName = 'Person';

return LocationModel;
})(_Model12['default']);
})(_Model14['default']);

@@ -281,3 +281,3 @@ LocationModel.modelName = 'Location';

(0, _chai.expect)(aPerson.toPlain()).to.deep.equal({ id: 0, name: 'Tommi', age: 25, location: 0 });
(0, _chai.expect)(aPerson).to.be.an.instanceOf(_Model12['default']);
(0, _chai.expect)(aPerson).to.be.an.instanceOf(_Model14['default']);
var relatedLocation = aPerson.location;

@@ -321,3 +321,3 @@ (0, _chai.expect)(relatedLocation.equals(aLocation)).to.be.ok;

return PersonModel;
})(_Model12['default']);
})(_Model14['default']);

@@ -343,3 +343,3 @@ PersonModel.modelName = 'Person';

return LocationModel;
})(_Model12['default']);
})(_Model14['default']);

@@ -425,7 +425,57 @@ LocationModel.modelName = 'Location';

it('correctly handles ManyToMany relations', function () {
var BookModel = (function (_Model9) {
_inherits(BookModel, _Model9);
function BookModel() {
_classCallCheck(this, BookModel);
_get(Object.getPrototypeOf(BookModel.prototype), 'constructor', this).apply(this, arguments);
}
return BookModel;
})(_Model14['default']);
BookModel.modelName = 'Book';
BookModel.fields = {
genres: new _fields.ManyToMany('Genre')
};
var GenreModel = (function (_Model10) {
_inherits(GenreModel, _Model10);
function GenreModel() {
_classCallCheck(this, GenreModel);
_get(Object.getPrototypeOf(GenreModel.prototype), 'constructor', this).apply(this, arguments);
}
return GenreModel;
})(_Model14['default']);
GenreModel.modelName = 'Genre';
var schema = new _Schema2['default']();
schema.register(BookModel, GenreModel);
var initialState = schema.getDefaultState();
var _schema$withMutations2 = schema.withMutations(initialState);
var Book = _schema$withMutations2.Book;
var Genre = _schema$withMutations2.Genre;
Genre.create({ name: 'Fiction' });
Genre.create({ name: 'Non-Fiction' });
Genre.create({ name: 'Business' });
var book = Book.create({ name: 'A Phenomenal Novel', genres: [0, 1] });
(0, _chai.expect)(initialState.BookGenres.items).to.have.length(2);
(0, _chai.expect)(book._fields.genres).to.be.undefined;
});
it('Correctly defined OneToOne', function () {
var schema = new _Schema2['default']();
var UserModel = (function (_Model9) {
_inherits(UserModel, _Model9);
var UserModel = (function (_Model11) {
_inherits(UserModel, _Model11);

@@ -464,8 +514,8 @@ function UserModel() {

return UserModel;
})(_Model12['default']);
})(_Model14['default']);
UserModel.modelName = 'User';
var ProfileModel = (function (_Model10) {
_inherits(ProfileModel, _Model10);
var ProfileModel = (function (_Model12) {
_inherits(ProfileModel, _Model12);

@@ -498,3 +548,3 @@ function ProfileModel() {

return ProfileModel;
})(_Model12['default']);
})(_Model14['default']);

@@ -501,0 +551,0 @@ ProfileModel.modelName = 'Profile';

{
"name": "redux-orm",
"version": "0.1.16",
"version": "0.1.17",
"description": "Simple ORM to manage and query your state trees",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

import forOwn from 'lodash/object/forOwn';
import isArray from 'lodash/lang/isArray';

@@ -6,2 +7,3 @@ import Session from './Session';

import QuerySet from './QuerySet';
import {ManyToMany} from './fields';
import {CREATE, UPDATE, DELETE, ORDER} from './constants';

@@ -314,2 +316,4 @@ import {match} from './utils';

const m2mVals = {};
forOwn(userProps, (value, key) => {

@@ -319,2 +323,11 @@ if (value instanceof Model) {

}
// If a value is supplied for a ManyToMany field,
// discard them from props and save for later processing.
if (isArray(value)) {
if (this.fields.hasOwnProperty(key) && this.fields[key] instanceof ManyToMany) {
m2mVals[key] = value;
delete props[key];
}
}
});

@@ -327,3 +340,16 @@

const ModelClass = this;
return new ModelClass(props);
const instance = new ModelClass(props);
forOwn(m2mVals, (value, key) => {
const ids = value.map(arrVal => {
if (arrVal instanceof Model) {
return arrVal.getId();
}
return arrVal;
});
instance[key].add(...ids);
});
return instance;
}

@@ -330,0 +356,0 @@

@@ -288,2 +288,26 @@ import {expect} from 'chai';

it('correctly handles ManyToMany relations', () => {
class BookModel extends Model {}
BookModel.modelName = 'Book';
BookModel.fields = {
genres: new ManyToMany('Genre'),
};
class GenreModel extends Model {}
GenreModel.modelName = 'Genre';
const schema = new Schema();
schema.register(BookModel, GenreModel);
const initialState = schema.getDefaultState();
const {Book, Genre} = schema.withMutations(initialState);
Genre.create({name: 'Fiction'});
Genre.create({name: 'Non-Fiction'});
Genre.create({name: 'Business'});
const book = Book.create({name: 'A Phenomenal Novel', genres: [0, 1]});
expect(initialState.BookGenres.items).to.have.length(2);
expect(book._fields.genres).to.be.undefined;
});
it('Correctly defined OneToOne', () => {

@@ -290,0 +314,0 @@ const schema = new Schema();

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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