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

mobx-decorated-models

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mobx-decorated-models - npm Package Compare versions

Comparing version 0.6.5 to 0.7.0

package-lock.json

415

dist/build.full.js
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('serializr'), require('mobx')) :
typeof define === 'function' && define.amd ? define(['exports', 'serializr', 'mobx'], factory) :
(factory((global['mobx-decorated-models'] = global['mobx-decorated-models'] || {}),global.serializr,global.mobx));
}(this, (function (exports,serializr,mobx) { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('babel-runtime/core-js/map'), require('babel-runtime/core-js/object/assign'), require('serializr'), require('babel-runtime/core-js/object/keys'), require('babel-runtime/core-js/object/is-extensible'), require('mobx')) :
typeof define === 'function' && define.amd ? define(['exports', 'babel-runtime/core-js/map', 'babel-runtime/core-js/object/assign', 'serializr', 'babel-runtime/core-js/object/keys', 'babel-runtime/core-js/object/is-extensible', 'mobx'], factory) :
(factory((global['mobx-decorated-models'] = global['mobx-decorated-models'] || {}),global._Map,global._Object$assign,global.serializr,global._Object$keys,global._Object$isExtensible,global.mobx));
}(this, (function (exports,_Map,_Object$assign,serializr,_Object$keys,_Object$isExtensible,mobx) { 'use strict';
_Map = _Map && 'default' in _Map ? _Map['default'] : _Map;
_Object$assign = _Object$assign && 'default' in _Object$assign ? _Object$assign['default'] : _Object$assign;
_Object$keys = _Object$keys && 'default' in _Object$keys ? _Object$keys['default'] : _Object$keys;
_Object$isExtensible = _Object$isExtensible && 'default' in _Object$isExtensible ? _Object$isExtensible['default'] : _Object$isExtensible;
function getModelschema(model) {

@@ -15,3 +20,3 @@ if (model.$schema) {

configurable: true,
value: new Map()
value: new _Map()
});

@@ -21,9 +26,5 @@ return model.$schema;

var ModelsMap = new Map();
const ModelsMap = new _Map();
function defaultModelLookup(_ref) {
var property = _ref.property,
_ref$model = _ref.model,
model = _ref$model === undefined ? property : _ref$model;
function defaultModelLookup({ property, model = property }) {
if (typeof model === 'function') {

@@ -34,8 +35,8 @@ return model;

}
var modelLookup = defaultModelLookup;
let modelLookup = defaultModelLookup;
var defaultRememberModel = function defaultRememberModel(model) {
const defaultRememberModel = model => {
ModelsMap[model.identifiedBy || model.name] = model;
};
var rememberModel = defaultRememberModel;
let rememberModel = defaultRememberModel;

@@ -47,3 +48,3 @@ function registerModel(model) {

function findModel(model, property) {
return modelLookup({ model: model, property: property });
return modelLookup({ model, property });
}

@@ -68,86 +69,11 @@

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
var toConsumableArray = function (arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
return arr2;
} else {
return Array.from(arr);
}
};
function setupModel(_ref) {
var attrs = _ref.attrs,
Klass = _ref.modelClass,
array = _ref.array,
defaultAttributes = _ref.defaultAttributes,
inverseOf = _ref.inverseOf,
parentModel = _ref.parentModel,
parentModelProp = _ref.parentModelProp;
function setupModel({
attrs, modelClass: Klass, array, defaultAttributes,
inverseOf, parentModel, parentModelProp
}) {
if (!attrs) {
return attrs;
}
var isObject = (typeof attrs === 'undefined' ? 'undefined' : _typeof(attrs)) === 'object';
var canWrite = Object.isExtensible(attrs);
const isObject = typeof attrs === 'object';
const canWrite = _Object$isExtensible(attrs);

@@ -159,3 +85,3 @@ if (isObject && canWrite) {

}
Object.keys(defaultAttributes).forEach(function (key) {
_Object$keys(defaultAttributes).forEach(key => {
if (!attrs[key]) {

@@ -170,3 +96,3 @@ attrs[key] = defaultAttributes[key];

if (parentModelProp) {
attrs[inverseOf + '_association_name'] = parentModelProp;
attrs[`${inverseOf}_association_name`] = parentModelProp;
}

@@ -176,3 +102,3 @@ }

var model = Klass && typeof Klass === 'function' && !(attrs instanceof Klass) ? new Klass(attrs) : attrs;
const model = Klass && typeof Klass === 'function' && !(attrs instanceof Klass) ? new Klass(attrs) : attrs;

@@ -186,10 +112,10 @@ if (inverseOf) {

var PendingLookups = [];
const PendingLookups = [];
function objectSerializer() {
return {
serializer: function serializer(obj, prop, parent) {
serializer(obj, prop, parent) {
return isSerializable(parent, prop) ? obj : serializr.SKIP;
},
deserializer: function deserializer(json, cb) {
deserializer(json, cb) {
cb(null, json);

@@ -201,3 +127,3 @@ }

function addReference(parentModel, propName, options, cb) {
var model = findModel(options.model, propName);
const model = findModel(options.model, propName);
if (model) {

@@ -207,15 +133,9 @@ serializr.getDefaultModelSchema(parentModel).props[propName] = cb(model, options, propName);

serializr.getDefaultModelSchema(parentModel).props[propName] = objectSerializer();
PendingLookups.push({ parentModel: parentModel, propName: propName, options: options, cb: cb });
PendingLookups.push({ parentModel, propName, options, cb });
}
}
var dateSerializer = function dateSerializer() {
return serializr.custom(function (date) {
return date ? date.toJSON() : null;
}, function (json) {
return json ? new Date(json) : null;
});
};
const dateSerializer = () => serializr.custom(date => date ? date.toJSON() : null, json => json ? new Date(json) : null);
var CustomSerializerTypes = {
const CustomSerializerTypes = {
date: dateSerializer,

@@ -230,8 +150,6 @@ object: objectSerializer,

}
var fns = (CustomSerializerTypes[options.type] || defaultSerializer)();
const fns = (CustomSerializerTypes[options.type] || defaultSerializer)();
if (options.writeOnly) {
return {
serializer: function serializer() {
return serializr.SKIP;
},
serializer: () => serializr.SKIP,
deserializer: fns.deserializer

@@ -243,31 +161,25 @@ };

var Fields = {
identifier: function identifier$$1(options) {
return getSerializer(options, serializr.identifier);
},
field: function field(options) {
return getSerializer(options, serializr.primitive);
},
session: function session(options) {
return getSerializer(Object.assign({}, options, { writeOnly: true }), serializr.primitive);
}
const Fields = {
identifier: options => getSerializer(options, serializr.identifier),
field: options => getSerializer(options, serializr.primitive),
session: options => getSerializer(_Object$assign({}, options, { writeOnly: true }), serializr.primitive)
};
var Associations = {
hasMany: function hasMany(modelKlass, options) {
var defaultSerializer = serializr.list(serializr.object(modelKlass));
return Object.assign(defaultSerializer, options, {
const Associations = {
hasMany(modelKlass, options) {
const defaultSerializer = serializr.list(serializr.object(modelKlass));
return _Object$assign(defaultSerializer, options, {
model: modelKlass
});
},
belongsTo: function belongsTo(modelKlass, options, propName) {
var defaultSerializer = serializr.object(serializr.getDefaultModelSchema(modelKlass));
var serializerFn = modelKlass.serialize || defaultSerializer.serializer;
return Object.assign(options, {
belongsTo(modelKlass, options, propName) {
const defaultSerializer = serializr.object(serializr.getDefaultModelSchema(modelKlass));
const serializerFn = modelKlass.serialize || defaultSerializer.serializer;
return _Object$assign(options, {
model: modelKlass,
deserializer: function deserializer(value, cb, context) {
var setter = function setter(err, model) {
deserializer(value, cb, context) {
const setter = (err, model) => {
if (!err && options.inverseOf) {
markNonserializable(model, options.inverseOf);
model[options.inverseOf + '_association_name'] = propName;
model[`${options.inverseOf}_association_name`] = propName;
model[options.inverseOf] = context.target;

@@ -280,6 +192,6 @@ }

} else {
setter(null, setupModel(Object.assign({}, options, { attrs: value, modelClass: modelKlass })));
setter(null, setupModel(_Object$assign({}, options, { attrs: value, modelClass: modelKlass })));
}
},
serializer: function serializer(belongsTo, name, parent) {
serializer(belongsTo, name, parent) {
if (!isSerializable(parent, name)) {

@@ -294,8 +206,8 @@ return serializr.SKIP;

var MixedInInstanceMethods = {
serialize: function serialize$$1() {
var schema = serializr.getDefaultModelSchema(this.constructor);
const MixedInInstanceMethods = {
serialize() {
const schema = serializr.getDefaultModelSchema(this.constructor);
return serializr.serialize(schema, this);
},
update: function update$$1(json, callback) {
update(json, callback) {
return serializr.update(serializr.getDefaultModelSchema(this.constructor), this, json, callback);

@@ -305,4 +217,4 @@ }

var MixedInClassMethods = {
deserialize: function deserialize$$1(json, callback) {
const MixedInClassMethods = {
deserialize(json, callback) {
return serializr.deserialize(serializr.getDefaultModelSchema(this), json, callback);

@@ -313,12 +225,9 @@ }

function decorateModel(model) {
Object.assign(model.prototype, MixedInInstanceMethods);
Object.assign(model, MixedInClassMethods);
_Object$assign(model.prototype, MixedInInstanceMethods);
_Object$assign(model, MixedInClassMethods);
var schema = getModelschema(model);
const schema = getModelschema(model);
registerModel(model);
var serializeSchema = {};
schema.forEach(function (_ref, name) {
var type = _ref.type,
options = _ref.options;
const serializeSchema = {};
schema.forEach(({ type, options }, name) => {
if (Fields[type]) {

@@ -329,6 +238,3 @@ serializeSchema[name] = Fields[type](options, name);

serializr.createModelSchema(model, serializeSchema);
schema.forEach(function (_ref2, name) {
var type = _ref2.type,
options = _ref2.options;
schema.forEach(({ type, options }, name) => {
if (Associations[type] || options.model) {

@@ -338,12 +244,7 @@ addReference(model, name, options, Associations[type] || Associations.belongsTo);

});
for (var i = PendingLookups.length - 1; i >= 0; i -= 1) {
var _PendingLookups$i = PendingLookups[i],
parentModel = _PendingLookups$i.parentModel,
propName = _PendingLookups$i.propName,
options = _PendingLookups$i.options,
cb = _PendingLookups$i.cb;
var referencedModel = findModel(options.model, propName);
for (let i = PendingLookups.length - 1; i >= 0; i -= 1) {
const { parentModel, propName, options, cb } = PendingLookups[i];
const referencedModel = findModel(options.model, propName);
if (referencedModel) {
var parentModelSchema = serializr.getDefaultModelSchema(parentModel);
const parentModelSchema = serializr.getDefaultModelSchema(parentModel);
parentModelSchema.props[propName] = cb(referencedModel, options, propName);

@@ -356,3 +257,3 @@ PendingLookups.splice(i, 1);

function identifiedBy(modelId) {
return function (model) {
return model => {
Object.defineProperty(model, 'identifiedBy', { value: modelId, writable: false });

@@ -364,16 +265,7 @@ return decorateModel(model);

function unresolvedAssociations() {
return PendingLookups.map(function (_ref3) {
var model = _ref3.parentModel,
property = _ref3.propName;
return { model: model, property: property };
});
return PendingLookups.map(({ parentModel: model, propName: property }) => ({ model, property }));
}
function onCollectionChangeInterceptor(_ref, parentModel, parentModelProp) {
var modelClass = _ref.modelClass,
model = _ref.model,
defaultAttributes = _ref.defaults,
inverseOf = _ref.inverseOf;
return function (change) {
function onCollectionChangeInterceptor({ modelClass, model, defaults: defaultAttributes, inverseOf }, parentModel, parentModelProp) {
return change => {
if (!change.newValue) {

@@ -386,11 +278,11 @@ change.newValue = {};

if (change.type === 'splice') {
for (var i = 0; i < change.added.length; i += 1) {
for (let i = 0; i < change.added.length; i += 1) {
change.added[i] = setupModel({
attrs: change.added[i],
array: change.object,
modelClass: modelClass,
defaultAttributes: defaultAttributes,
inverseOf: inverseOf,
parentModel: parentModel,
parentModelProp: parentModelProp });
modelClass,
defaultAttributes,
inverseOf,
parentModel,
parentModelProp });
}

@@ -401,7 +293,7 @@ } else if (change.type === 'update') {

array: change.object,
modelClass: modelClass,
defaultAttributes: defaultAttributes,
inverseOf: inverseOf,
parentModel: parentModel,
parentModelProp: parentModelProp });
modelClass,
defaultAttributes,
inverseOf,
parentModel,
parentModelProp });
}

@@ -413,3 +305,3 @@ return change;

function createCollection(options, parentModel, parentModelProp) {
var ary = mobx.observable.array([]);
const ary = mobx.observable.array([]);
if (options && options.model) {

@@ -425,16 +317,10 @@ ary.intercept(onCollectionChangeInterceptor(options, parentModel, parentModelProp));

function onPropertySet(change, _ref) {
var modelClass = _ref.modelClass,
defaultAttributes = _ref.defaultAttributes,
inverseOf = _ref.inverseOf,
parentModel = _ref.parentModel,
parentModelProp = _ref.parentModelProp;
function onPropertySet(change, { modelClass, defaultAttributes, inverseOf, parentModel, parentModelProp }) {
change.newValue = setupModel({
attrs: change.newValue,
modelClass: modelClass,
defaultAttributes: defaultAttributes,
inverseOf: inverseOf,
parentModel: parentModel,
parentModelProp: parentModelProp
modelClass,
defaultAttributes,
inverseOf,
parentModel,
parentModelProp
});

@@ -444,22 +330,18 @@ return change;

function onHasManySet(change, _ref2) {
var modelClass = _ref2.modelClass,
defaultAttributes = _ref2.defaultAttributes,
inverseOf = _ref2.inverseOf,
parentModel = _ref2.parentModel,
parentModelProp = _ref2.parentModelProp;
function onHasManySet(change, {
modelClass, defaultAttributes, inverseOf, parentModel, parentModelProp
}) {
if (change.type !== 'update' || !change.newValue) {
return change;
}
var array = mobx.observable.array(change.newValue);
for (var i = 0; i < array.length; i += 1) {
const array = mobx.observable.array(change.newValue);
for (let i = 0; i < array.length; i += 1) {
array[i] = setupModel({
attrs: array[i],
array: array,
modelClass: modelClass,
defaultAttributes: defaultAttributes,
inverseOf: inverseOf,
parentModel: parentModel,
parentModelProp: parentModelProp
array,
modelClass,
defaultAttributes,
inverseOf,
parentModel,
parentModelProp
});

@@ -475,10 +357,8 @@ }

var Initializers = {
object: function object$$1() {
return mobx.observable.object({});
},
const Initializers = {
object: () => mobx.observable.object({}),
array: createCollection
};
var TypeInitializers = {
const TypeInitializers = {
hasMany: createCollection

@@ -488,3 +368,3 @@ };

function getInitializer(type, options, propName) {
var fn = TypeInitializers[type] || Initializers[options.type];
const fn = TypeInitializers[type] || Initializers[options.type];
if (!fn) {

@@ -499,30 +379,23 @@ return undefined;

function installModelInterceptor(interceptingFn, _ref3) {
var target = _ref3.target,
property = _ref3.property;
var schema = getModelschema(target.constructor);
addLazyInitializer(target, function (model) {
var schemaProps = schema.get(property).options;
mobx.intercept(model, property, function (change) {
return interceptingFn(change, {
inverseOf: schemaProps.inverseOf,
modelClass: schemaProps.model,
parentModel: model,
parentModelProp: property,
defaultAttributes: schemaProps.defaults
});
});
function installModelInterceptor(interceptingFn, { target, property }) {
const schema = getModelschema(target.constructor);
addLazyInitializer(target, model => {
const schemaProps = schema.get(property).options;
mobx.intercept(model, property, change => interceptingFn(change, {
inverseOf: schemaProps.inverseOf,
modelClass: schemaProps.model,
parentModel: model,
parentModelProp: property,
defaultAttributes: schemaProps.defaults
}));
});
}
function addAttribute(type, setAttributeFn, target, property, descriptor) {
var options = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : {};
getModelschema(target.constructor).set(property, { name: property, type: type, options: options });
var initializer = getInitializer(type, options, property);
function addAttribute(type, setAttributeFn, target, property, descriptor, options = {}) {
getModelschema(target.constructor).set(property, { name: property, type, options });
const initializer = getInitializer(type, options, property);
if (initializer) {
descriptor.initializer = initializer;
}
var definition = mobx.observable(target, property, descriptor);
const definition = mobx.observable(target, property, descriptor);
if (type === 'belongsTo' && !options.model) {

@@ -533,3 +406,3 @@ options.model = property;

installModelInterceptor(setAttributeFn, {
target: target, property: property, descriptor: descriptor, model: options.model
target, property, descriptor, model: options.model
});

@@ -541,46 +414,14 @@ }

function buildAttributeDecorator(type, args, setAttributeFn) {
if (_typeof(args[0]) === 'object' && args.length === 1) {
return function (target, property, descriptor) {
return addAttribute(type, setAttributeFn, target, property, descriptor, args[0]);
};
if (typeof args[0] === 'object' && args.length === 1) {
return (target, property, descriptor) => addAttribute(type, setAttributeFn, target, property, descriptor, args[0]);
}
return addAttribute.apply(undefined, [type, setAttributeFn].concat(toConsumableArray(args), [{}]));
return addAttribute(type, setAttributeFn, ...args, {});
}
var hasMany = function hasMany() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
const hasMany = (...args) => buildAttributeDecorator('hasMany', args, onHasManySet);
const belongsTo = (...args) => buildAttributeDecorator('belongsTo', args, onPropertySet);
const field = (...args) => buildAttributeDecorator('field', args, onPropertySet);
const session = (...args) => buildAttributeDecorator('session', args, onPropertySet);
const identifier$1 = (...args) => buildAttributeDecorator('identifier', args, onPropertySet);
return buildAttributeDecorator('hasMany', args, onHasManySet);
};
var belongsTo = function belongsTo() {
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
return buildAttributeDecorator('belongsTo', args, onPropertySet);
};
var field = function field() {
for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
args[_key3] = arguments[_key3];
}
return buildAttributeDecorator('field', args, onPropertySet);
};
var session = function session() {
for (var _len4 = arguments.length, args = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
args[_key4] = arguments[_key4];
}
return buildAttributeDecorator('session', args, onPropertySet);
};
var identifier$1 = function identifier$$1() {
for (var _len5 = arguments.length, args = Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
args[_key5] = arguments[_key5];
}
return buildAttributeDecorator('identifier', args, onPropertySet);
};
exports.registerModel = registerModel;

@@ -587,0 +428,0 @@ exports.findModel = findModel;

@@ -0,2 +1,6 @@

import _Map from 'babel-runtime/core-js/map';
import _Object$assign from 'babel-runtime/core-js/object/assign';
import { SKIP, createModelSchema, custom, deserialize, getDefaultModelSchema, identifier, list, object, primitive, serialize, update } from 'serializr';
import _Object$keys from 'babel-runtime/core-js/object/keys';
import _Object$isExtensible from 'babel-runtime/core-js/object/is-extensible';
import { intercept, observable } from 'mobx';

@@ -12,3 +16,3 @@

configurable: true,
value: new Map()
value: new _Map()
});

@@ -18,9 +22,5 @@ return model.$schema;

var ModelsMap = new Map();
const ModelsMap = new _Map();
function defaultModelLookup(_ref) {
var property = _ref.property,
_ref$model = _ref.model,
model = _ref$model === undefined ? property : _ref$model;
function defaultModelLookup({ property, model = property }) {
if (typeof model === 'function') {

@@ -31,8 +31,8 @@ return model;

}
var modelLookup = defaultModelLookup;
let modelLookup = defaultModelLookup;
var defaultRememberModel = function defaultRememberModel(model) {
const defaultRememberModel = model => {
ModelsMap[model.identifiedBy || model.name] = model;
};
var rememberModel = defaultRememberModel;
let rememberModel = defaultRememberModel;

@@ -44,3 +44,3 @@ function registerModel(model) {

function findModel(model, property) {
return modelLookup({ model: model, property: property });
return modelLookup({ model, property });
}

@@ -65,86 +65,11 @@

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
var toConsumableArray = function (arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
return arr2;
} else {
return Array.from(arr);
}
};
function setupModel(_ref) {
var attrs = _ref.attrs,
Klass = _ref.modelClass,
array = _ref.array,
defaultAttributes = _ref.defaultAttributes,
inverseOf = _ref.inverseOf,
parentModel = _ref.parentModel,
parentModelProp = _ref.parentModelProp;
function setupModel({
attrs, modelClass: Klass, array, defaultAttributes,
inverseOf, parentModel, parentModelProp
}) {
if (!attrs) {
return attrs;
}
var isObject = (typeof attrs === 'undefined' ? 'undefined' : _typeof(attrs)) === 'object';
var canWrite = Object.isExtensible(attrs);
const isObject = typeof attrs === 'object';
const canWrite = _Object$isExtensible(attrs);

@@ -156,3 +81,3 @@ if (isObject && canWrite) {

}
Object.keys(defaultAttributes).forEach(function (key) {
_Object$keys(defaultAttributes).forEach(key => {
if (!attrs[key]) {

@@ -167,3 +92,3 @@ attrs[key] = defaultAttributes[key];

if (parentModelProp) {
attrs[inverseOf + '_association_name'] = parentModelProp;
attrs[`${inverseOf}_association_name`] = parentModelProp;
}

@@ -173,3 +98,3 @@ }

var model = Klass && typeof Klass === 'function' && !(attrs instanceof Klass) ? new Klass(attrs) : attrs;
const model = Klass && typeof Klass === 'function' && !(attrs instanceof Klass) ? new Klass(attrs) : attrs;

@@ -183,10 +108,10 @@ if (inverseOf) {

var PendingLookups = [];
const PendingLookups = [];
function objectSerializer() {
return {
serializer: function serializer(obj, prop, parent) {
serializer(obj, prop, parent) {
return isSerializable(parent, prop) ? obj : SKIP;
},
deserializer: function deserializer(json, cb) {
deserializer(json, cb) {
cb(null, json);

@@ -198,3 +123,3 @@ }

function addReference(parentModel, propName, options, cb) {
var model = findModel(options.model, propName);
const model = findModel(options.model, propName);
if (model) {

@@ -204,15 +129,9 @@ getDefaultModelSchema(parentModel).props[propName] = cb(model, options, propName);

getDefaultModelSchema(parentModel).props[propName] = objectSerializer();
PendingLookups.push({ parentModel: parentModel, propName: propName, options: options, cb: cb });
PendingLookups.push({ parentModel, propName, options, cb });
}
}
var dateSerializer = function dateSerializer() {
return custom(function (date) {
return date ? date.toJSON() : null;
}, function (json) {
return json ? new Date(json) : null;
});
};
const dateSerializer = () => custom(date => date ? date.toJSON() : null, json => json ? new Date(json) : null);
var CustomSerializerTypes = {
const CustomSerializerTypes = {
date: dateSerializer,

@@ -227,8 +146,6 @@ object: objectSerializer,

}
var fns = (CustomSerializerTypes[options.type] || defaultSerializer)();
const fns = (CustomSerializerTypes[options.type] || defaultSerializer)();
if (options.writeOnly) {
return {
serializer: function serializer() {
return SKIP;
},
serializer: () => SKIP,
deserializer: fns.deserializer

@@ -240,31 +157,25 @@ };

var Fields = {
identifier: function identifier$$1(options) {
return getSerializer(options, identifier);
},
field: function field(options) {
return getSerializer(options, primitive);
},
session: function session(options) {
return getSerializer(Object.assign({}, options, { writeOnly: true }), primitive);
}
const Fields = {
identifier: options => getSerializer(options, identifier),
field: options => getSerializer(options, primitive),
session: options => getSerializer(_Object$assign({}, options, { writeOnly: true }), primitive)
};
var Associations = {
hasMany: function hasMany(modelKlass, options) {
var defaultSerializer = list(object(modelKlass));
return Object.assign(defaultSerializer, options, {
const Associations = {
hasMany(modelKlass, options) {
const defaultSerializer = list(object(modelKlass));
return _Object$assign(defaultSerializer, options, {
model: modelKlass
});
},
belongsTo: function belongsTo(modelKlass, options, propName) {
var defaultSerializer = object(getDefaultModelSchema(modelKlass));
var serializerFn = modelKlass.serialize || defaultSerializer.serializer;
return Object.assign(options, {
belongsTo(modelKlass, options, propName) {
const defaultSerializer = object(getDefaultModelSchema(modelKlass));
const serializerFn = modelKlass.serialize || defaultSerializer.serializer;
return _Object$assign(options, {
model: modelKlass,
deserializer: function deserializer(value, cb, context) {
var setter = function setter(err, model) {
deserializer(value, cb, context) {
const setter = (err, model) => {
if (!err && options.inverseOf) {
markNonserializable(model, options.inverseOf);
model[options.inverseOf + '_association_name'] = propName;
model[`${options.inverseOf}_association_name`] = propName;
model[options.inverseOf] = context.target;

@@ -277,6 +188,6 @@ }

} else {
setter(null, setupModel(Object.assign({}, options, { attrs: value, modelClass: modelKlass })));
setter(null, setupModel(_Object$assign({}, options, { attrs: value, modelClass: modelKlass })));
}
},
serializer: function serializer(belongsTo, name, parent) {
serializer(belongsTo, name, parent) {
if (!isSerializable(parent, name)) {

@@ -291,8 +202,8 @@ return SKIP;

var MixedInInstanceMethods = {
serialize: function serialize$$1() {
var schema = getDefaultModelSchema(this.constructor);
const MixedInInstanceMethods = {
serialize() {
const schema = getDefaultModelSchema(this.constructor);
return serialize(schema, this);
},
update: function update$$1(json, callback) {
update(json, callback) {
return update(getDefaultModelSchema(this.constructor), this, json, callback);

@@ -302,4 +213,4 @@ }

var MixedInClassMethods = {
deserialize: function deserialize$$1(json, callback) {
const MixedInClassMethods = {
deserialize(json, callback) {
return deserialize(getDefaultModelSchema(this), json, callback);

@@ -310,12 +221,9 @@ }

function decorateModel(model) {
Object.assign(model.prototype, MixedInInstanceMethods);
Object.assign(model, MixedInClassMethods);
_Object$assign(model.prototype, MixedInInstanceMethods);
_Object$assign(model, MixedInClassMethods);
var schema = getModelschema(model);
const schema = getModelschema(model);
registerModel(model);
var serializeSchema = {};
schema.forEach(function (_ref, name) {
var type = _ref.type,
options = _ref.options;
const serializeSchema = {};
schema.forEach(({ type, options }, name) => {
if (Fields[type]) {

@@ -326,6 +234,3 @@ serializeSchema[name] = Fields[type](options, name);

createModelSchema(model, serializeSchema);
schema.forEach(function (_ref2, name) {
var type = _ref2.type,
options = _ref2.options;
schema.forEach(({ type, options }, name) => {
if (Associations[type] || options.model) {

@@ -335,12 +240,7 @@ addReference(model, name, options, Associations[type] || Associations.belongsTo);

});
for (var i = PendingLookups.length - 1; i >= 0; i -= 1) {
var _PendingLookups$i = PendingLookups[i],
parentModel = _PendingLookups$i.parentModel,
propName = _PendingLookups$i.propName,
options = _PendingLookups$i.options,
cb = _PendingLookups$i.cb;
var referencedModel = findModel(options.model, propName);
for (let i = PendingLookups.length - 1; i >= 0; i -= 1) {
const { parentModel, propName, options, cb } = PendingLookups[i];
const referencedModel = findModel(options.model, propName);
if (referencedModel) {
var parentModelSchema = getDefaultModelSchema(parentModel);
const parentModelSchema = getDefaultModelSchema(parentModel);
parentModelSchema.props[propName] = cb(referencedModel, options, propName);

@@ -353,3 +253,3 @@ PendingLookups.splice(i, 1);

function identifiedBy(modelId) {
return function (model) {
return model => {
Object.defineProperty(model, 'identifiedBy', { value: modelId, writable: false });

@@ -361,16 +261,7 @@ return decorateModel(model);

function unresolvedAssociations() {
return PendingLookups.map(function (_ref3) {
var model = _ref3.parentModel,
property = _ref3.propName;
return { model: model, property: property };
});
return PendingLookups.map(({ parentModel: model, propName: property }) => ({ model, property }));
}
function onCollectionChangeInterceptor(_ref, parentModel, parentModelProp) {
var modelClass = _ref.modelClass,
model = _ref.model,
defaultAttributes = _ref.defaults,
inverseOf = _ref.inverseOf;
return function (change) {
function onCollectionChangeInterceptor({ modelClass, model, defaults: defaultAttributes, inverseOf }, parentModel, parentModelProp) {
return change => {
if (!change.newValue) {

@@ -383,11 +274,11 @@ change.newValue = {};

if (change.type === 'splice') {
for (var i = 0; i < change.added.length; i += 1) {
for (let i = 0; i < change.added.length; i += 1) {
change.added[i] = setupModel({
attrs: change.added[i],
array: change.object,
modelClass: modelClass,
defaultAttributes: defaultAttributes,
inverseOf: inverseOf,
parentModel: parentModel,
parentModelProp: parentModelProp });
modelClass,
defaultAttributes,
inverseOf,
parentModel,
parentModelProp });
}

@@ -398,7 +289,7 @@ } else if (change.type === 'update') {

array: change.object,
modelClass: modelClass,
defaultAttributes: defaultAttributes,
inverseOf: inverseOf,
parentModel: parentModel,
parentModelProp: parentModelProp });
modelClass,
defaultAttributes,
inverseOf,
parentModel,
parentModelProp });
}

@@ -410,3 +301,3 @@ return change;

function createCollection(options, parentModel, parentModelProp) {
var ary = observable.array([]);
const ary = observable.array([]);
if (options && options.model) {

@@ -422,16 +313,10 @@ ary.intercept(onCollectionChangeInterceptor(options, parentModel, parentModelProp));

function onPropertySet(change, _ref) {
var modelClass = _ref.modelClass,
defaultAttributes = _ref.defaultAttributes,
inverseOf = _ref.inverseOf,
parentModel = _ref.parentModel,
parentModelProp = _ref.parentModelProp;
function onPropertySet(change, { modelClass, defaultAttributes, inverseOf, parentModel, parentModelProp }) {
change.newValue = setupModel({
attrs: change.newValue,
modelClass: modelClass,
defaultAttributes: defaultAttributes,
inverseOf: inverseOf,
parentModel: parentModel,
parentModelProp: parentModelProp
modelClass,
defaultAttributes,
inverseOf,
parentModel,
parentModelProp
});

@@ -441,22 +326,18 @@ return change;

function onHasManySet(change, _ref2) {
var modelClass = _ref2.modelClass,
defaultAttributes = _ref2.defaultAttributes,
inverseOf = _ref2.inverseOf,
parentModel = _ref2.parentModel,
parentModelProp = _ref2.parentModelProp;
function onHasManySet(change, {
modelClass, defaultAttributes, inverseOf, parentModel, parentModelProp
}) {
if (change.type !== 'update' || !change.newValue) {
return change;
}
var array = observable.array(change.newValue);
for (var i = 0; i < array.length; i += 1) {
const array = observable.array(change.newValue);
for (let i = 0; i < array.length; i += 1) {
array[i] = setupModel({
attrs: array[i],
array: array,
modelClass: modelClass,
defaultAttributes: defaultAttributes,
inverseOf: inverseOf,
parentModel: parentModel,
parentModelProp: parentModelProp
array,
modelClass,
defaultAttributes,
inverseOf,
parentModel,
parentModelProp
});

@@ -472,10 +353,8 @@ }

var Initializers = {
object: function object$$1() {
return observable.object({});
},
const Initializers = {
object: () => observable.object({}),
array: createCollection
};
var TypeInitializers = {
const TypeInitializers = {
hasMany: createCollection

@@ -485,3 +364,3 @@ };

function getInitializer(type, options, propName) {
var fn = TypeInitializers[type] || Initializers[options.type];
const fn = TypeInitializers[type] || Initializers[options.type];
if (!fn) {

@@ -496,30 +375,23 @@ return undefined;

function installModelInterceptor(interceptingFn, _ref3) {
var target = _ref3.target,
property = _ref3.property;
var schema = getModelschema(target.constructor);
addLazyInitializer(target, function (model) {
var schemaProps = schema.get(property).options;
intercept(model, property, function (change) {
return interceptingFn(change, {
inverseOf: schemaProps.inverseOf,
modelClass: schemaProps.model,
parentModel: model,
parentModelProp: property,
defaultAttributes: schemaProps.defaults
});
});
function installModelInterceptor(interceptingFn, { target, property }) {
const schema = getModelschema(target.constructor);
addLazyInitializer(target, model => {
const schemaProps = schema.get(property).options;
intercept(model, property, change => interceptingFn(change, {
inverseOf: schemaProps.inverseOf,
modelClass: schemaProps.model,
parentModel: model,
parentModelProp: property,
defaultAttributes: schemaProps.defaults
}));
});
}
function addAttribute(type, setAttributeFn, target, property, descriptor) {
var options = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : {};
getModelschema(target.constructor).set(property, { name: property, type: type, options: options });
var initializer = getInitializer(type, options, property);
function addAttribute(type, setAttributeFn, target, property, descriptor, options = {}) {
getModelschema(target.constructor).set(property, { name: property, type, options });
const initializer = getInitializer(type, options, property);
if (initializer) {
descriptor.initializer = initializer;
}
var definition = observable(target, property, descriptor);
const definition = observable(target, property, descriptor);
if (type === 'belongsTo' && !options.model) {

@@ -530,3 +402,3 @@ options.model = property;

installModelInterceptor(setAttributeFn, {
target: target, property: property, descriptor: descriptor, model: options.model
target, property, descriptor, model: options.model
});

@@ -538,47 +410,15 @@ }

function buildAttributeDecorator(type, args, setAttributeFn) {
if (_typeof(args[0]) === 'object' && args.length === 1) {
return function (target, property, descriptor) {
return addAttribute(type, setAttributeFn, target, property, descriptor, args[0]);
};
if (typeof args[0] === 'object' && args.length === 1) {
return (target, property, descriptor) => addAttribute(type, setAttributeFn, target, property, descriptor, args[0]);
}
return addAttribute.apply(undefined, [type, setAttributeFn].concat(toConsumableArray(args), [{}]));
return addAttribute(type, setAttributeFn, ...args, {});
}
var hasMany = function hasMany() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
const hasMany = (...args) => buildAttributeDecorator('hasMany', args, onHasManySet);
const belongsTo = (...args) => buildAttributeDecorator('belongsTo', args, onPropertySet);
const field = (...args) => buildAttributeDecorator('field', args, onPropertySet);
const session = (...args) => buildAttributeDecorator('session', args, onPropertySet);
const identifier$1 = (...args) => buildAttributeDecorator('identifier', args, onPropertySet);
return buildAttributeDecorator('hasMany', args, onHasManySet);
};
var belongsTo = function belongsTo() {
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
return buildAttributeDecorator('belongsTo', args, onPropertySet);
};
var field = function field() {
for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
args[_key3] = arguments[_key3];
}
return buildAttributeDecorator('field', args, onPropertySet);
};
var session = function session() {
for (var _len4 = arguments.length, args = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
args[_key4] = arguments[_key4];
}
return buildAttributeDecorator('session', args, onPropertySet);
};
var identifier$1 = function identifier$$1() {
for (var _len5 = arguments.length, args = Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
args[_key5] = arguments[_key5];
}
return buildAttributeDecorator('identifier', args, onPropertySet);
};
export { registerModel, findModel, rememberModelUsing, lookupModelUsing, getModelschema as getModelSchema, identifiedBy, unresolvedAssociations, field, session, belongsTo, hasMany, identifier$1 as identifier, createCollection };
//# sourceMappingURL=build.module.js.map
{
"name": "mobx-decorated-models",
"version": "0.6.5",
"version": "0.7.0",
"description": "Decorators to make using Mobx for model type structures easier",

@@ -30,4 +30,4 @@ "main": "dist/build.full.js",

"babel": "^6.5.2",
"babelrc-rollup": "^3.0.0",
"babel-eslint": "^7.2.3",
"babel-jest": "^20.0.3",
"babel-loader": "^7.0.0",

@@ -38,2 +38,3 @@ "babel-plugin-add-module-exports": "^0.2.1",

"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.22.0",

@@ -49,3 +50,4 @@ "babel-preset-stage-2": "^6.22.0",

"serializr": "^1.1"
}
},
"dependencies": {}
}
import babel from 'rollup-plugin-babel';
import babelrc from 'babelrc-rollup';
const pkg = require('./package.json');
export default {
entry: 'index.js',
plugins: [
babel(babelrc()),
babel({
babelrc: false,
runtimeHelpers: true,
exclude: 'node_modules/**',
plugins: [
'transform-decorators-legacy',
'transform-class-properties',
'external-helpers',
'transform-runtime',
],
}),
],

@@ -13,0 +20,0 @@ external: [

@@ -162,6 +162,13 @@ import { autorun } from 'mobx';

it('can use a model to typecast', () => {
const boat = Ship.deserialize({ id: 1, cargoCount: 3 });
const boat = Ship.deserialize({ name: 'Boaty', cargoCount: 3 });
boat.registration = '8550W';
expect(boat.registration).toBeInstanceOf(Registration);
expect(boat.serialize()).toMatchObject({ registration: '8550W' });
expect(boat.name).toEqual('Boaty');
const customBoat = Ship.deserialize({
registration: [1, 2, 3],
});
expect(customBoat.registration).toBeInstanceOf(Registration);
expect(customBoat.registration.id).toEqual([1, 2, 3]);
});

@@ -168,0 +175,0 @@

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