diff --git a/dist/mobx-async-store.cjs.js b/dist/mobx-async-store.cjs.js index d7dc513b..6693ac2d 100644 --- a/dist/mobx-async-store.cjs.js +++ b/dist/mobx-async-store.cjs.js @@ -15,7 +15,6 @@ var _applyDecoratedDescriptor = _interopDefault(require('@babel/runtime/helpers/ require('@babel/runtime/helpers/initializerWarningHelper'); var _typeof = _interopDefault(require('@babel/runtime/helpers/typeof')); var mobx = require('mobx'); -var moment = _interopDefault(require('moment')); var uuidv1 = _interopDefault(require('uuid/v1')); var jqueryParam = _interopDefault(require('jquery-param')); var pluralize = _interopDefault(require('pluralize')); @@ -161,6 +160,18 @@ function uniqueByReducer(key) { function uniqueBy(array, key) { return array.reduce(uniqueByReducer(key), []); } +/** + * convert a value into a date, pass Date or Moment instances thru + * untouched + * @method makeDate + * @param {*} value + * @return {Date|Moment} + */ + +function makeDate(value) { + if (value instanceof Date || value._isAMomentObject) return value; + return new Date(Date.parse(value)); +} /** * recursively walk an object and call the `iteratee` function for * each property. returns an array of results of calls to the iteratee. @@ -425,7 +436,6 @@ function _objectSpread$1(target) { for (var i = 1; i < arguments.length; i++) { * @return {Array} an array of booleans representing results of validations */ - function validateProperties(model, propertyNames, propertyDefinitions) { return propertyNames.map(function (property) { var validator = propertyDefinitions[property].validator; @@ -481,6 +491,7 @@ function stringifyIds(object) { @class Model */ + var Model = (_class = (_temp = /*#__PURE__*/ function () { @@ -998,7 +1009,7 @@ function () { if (DataType.name === 'Array' || DataType.name === 'Object') { attr = mobx.toJS(value); } else if (DataType.name === 'Date') { - attr = moment(value).toISOString(); + attr = makeDate(value).toISOString(); } else { attr = DataType(value); } @@ -2166,7 +2177,7 @@ function defaultValueForDescriptor(descriptor, DataType) { var value = descriptor.initializer(); if (DataType.name === 'Date') { - return moment(value).toDate(); + return makeDate(value); } else { return DataType(value); } @@ -2182,7 +2193,7 @@ function defaultValueForDescriptor(descriptor, DataType) { * Attributes can be defined with a default. * ``` * class Todo extends Model { - * @attribute(Date) start_time = moment() + * @attribute(Date) start_time = new Date() * } * ``` * @method attribute diff --git a/dist/mobx-async-store.esm.js b/dist/mobx-async-store.esm.js index 1daa8b33..fdffb6c0 100644 --- a/dist/mobx-async-store.esm.js +++ b/dist/mobx-async-store.esm.js @@ -9,7 +9,6 @@ import _applyDecoratedDescriptor from '@babel/runtime/helpers/applyDecoratedDesc import '@babel/runtime/helpers/initializerWarningHelper'; import _typeof from '@babel/runtime/helpers/typeof'; import { transaction, set, observable, computed, extendObservable, reaction, toJS, action } from 'mobx'; -import moment from 'moment'; import uuidv1 from 'uuid/v1'; import jqueryParam from 'jquery-param'; import pluralize from 'pluralize'; @@ -155,6 +154,18 @@ function uniqueByReducer(key) { function uniqueBy(array, key) { return array.reduce(uniqueByReducer(key), []); } +/** + * convert a value into a date, pass Date or Moment instances thru + * untouched + * @method makeDate + * @param {*} value + * @return {Date|Moment} + */ + +function makeDate(value) { + if (value instanceof Date || value._isAMomentObject) return value; + return new Date(Date.parse(value)); +} /** * recursively walk an object and call the `iteratee` function for * each property. returns an array of results of calls to the iteratee. @@ -419,7 +430,6 @@ function _objectSpread$1(target) { for (var i = 1; i < arguments.length; i++) { * @return {Array} an array of booleans representing results of validations */ - function validateProperties(model, propertyNames, propertyDefinitions) { return propertyNames.map(function (property) { var validator = propertyDefinitions[property].validator; @@ -475,6 +485,7 @@ function stringifyIds(object) { @class Model */ + var Model = (_class = (_temp = /*#__PURE__*/ function () { @@ -992,7 +1003,7 @@ function () { if (DataType.name === 'Array' || DataType.name === 'Object') { attr = toJS(value); } else if (DataType.name === 'Date') { - attr = moment(value).toISOString(); + attr = makeDate(value).toISOString(); } else { attr = DataType(value); } @@ -2160,7 +2171,7 @@ function defaultValueForDescriptor(descriptor, DataType) { var value = descriptor.initializer(); if (DataType.name === 'Date') { - return moment(value).toDate(); + return makeDate(value); } else { return DataType(value); } @@ -2176,7 +2187,7 @@ function defaultValueForDescriptor(descriptor, DataType) { * Attributes can be defined with a default. * ``` * class Todo extends Model { - * @attribute(Date) start_time = moment() + * @attribute(Date) start_time = new Date() * } * ``` * @method attribute diff --git a/docs/classes/Model.html b/docs/classes/Model.html index 6ad77439..04c6d7e8 100644 --- a/docs/classes/Model.html +++ b/docs/classes/Model.html @@ -17,7 +17,7 @@

- API Docs for: 1.0.25 + API Docs for: 1.0.26
@@ -84,7 +84,7 @@

Model Class

- Defined in: src/Model.js:196 + Defined in: src/Model.js:81
@@ -127,10 +127,6 @@

Methods

  • _takeSnapshot -
  • -
  • - attribute -
  • attributeDefinitions @@ -151,10 +147,6 @@

    Methods

  • defaultAttributes -
  • -
  • - defaultValueForDescriptor -
  • destroy @@ -223,19 +215,11 @@

    Methods

  • validate -
  • -
  • - validatePresence -
  • validateProperties
  • -
  • - validates - -
  • @@ -319,7 +303,7 @@

    _applySnapshot

    Defined in - src/Model.js:649 + src/Model.js:534

    @@ -367,7 +351,7 @@

    _listenForChanges

    Defined in - src/Model.js:532 + src/Model.js:417

    @@ -403,7 +387,7 @@

    _makeObservable

    Defined in - src/Model.js:515 + src/Model.js:400

    @@ -441,7 +425,7 @@

    _takeSnapshot

    Defined in - src/Model.js:626 + src/Model.js:511

    @@ -474,40 +458,6 @@

    Parameters:

    -
    -
    -

    attribute

    - - () - - - - - - - - -
    -

    - Defined in - src/Model.js:99 -

    - - - -
    - -
    -

    Defines attributes that will be serialized and deserialized. Takes one argument, a class that the attribute will be coerced to. -This can be a Javascript primitive or another class. id cannot be defined as it is assumed to exist. -Attributes can be defined with a default.

    -
    class Todo extends Model {
    - -
    - - - -

    attributeDefinitions

    @@ -527,7 +477,7 @@

    attributeDefinitions

    Defined in - src/Model.js:715 + src/Model.js:600

    @@ -568,7 +518,7 @@

    attributeNames

    Defined in - src/Model.js:757 + src/Model.js:642

    @@ -609,7 +559,7 @@

    attributes

    Defined in - src/Model.js:697 + src/Model.js:582

    @@ -649,7 +599,7 @@

    constructor

    Defined in - src/Model.js:200 + src/Model.js:85

    @@ -683,7 +633,7 @@

    defaultAttributes

    Defined in - src/Model.js:777 + src/Model.js:662

    @@ -705,37 +655,6 @@

    Returns:

    -
    -
    -

    defaultValueForDescriptor

    - - () - - - - - - - - -
    -

    - Defined in - src/Model.js:79 -

    - - - -
    - -
    -

    Helper method for apply the correct defaults to attributes.

    - -
    - - - -

    destroy

    @@ -755,7 +674,7 @@

    destroy

    Defined in - src/Model.js:446 + src/Model.js:331

    @@ -798,7 +717,7 @@

    dirtyAttributes

    Defined in - src/Model.js:666 + src/Model.js:551

    @@ -846,7 +765,7 @@

    dispose

    Defined in - src/Model.js:565 + src/Model.js:450

    @@ -881,7 +800,7 @@

    hasErrors

    Defined in - src/Model.js:747 + src/Model.js:622

    @@ -922,7 +841,7 @@

    hasErrors

    Defined in - src/Model.js:737 + src/Model.js:632

    @@ -963,7 +882,7 @@

    jsonapi

    Defined in - src/Model.js:794 + src/Model.js:679

    @@ -1004,7 +923,7 @@

    previousSnapshot

    Defined in - src/Model.js:617 + src/Model.js:502

    @@ -1035,7 +954,7 @@

    previousSnapshot

    Defined in - src/Model.js:606 + src/Model.js:491

    @@ -1069,7 +988,7 @@

    relationshipDefinitions

    Defined in - src/Model.js:726 + src/Model.js:611

    @@ -1110,7 +1029,7 @@

    relationshipNames

    Defined in - src/Model.js:767 + src/Model.js:652

    @@ -1148,7 +1067,7 @@

    rollback

    Defined in - src/Model.js:342 + src/Model.js:227

    @@ -1187,7 +1106,7 @@

    rollbackToPersisted

    Defined in - src/Model.js:359 + src/Model.js:244

    @@ -1228,7 +1147,7 @@

    save

    Defined in - src/Model.js:369 + src/Model.js:254

    @@ -1282,7 +1201,7 @@

    setPreviousSnapshot

    Defined in - src/Model.js:597 + src/Model.js:482

    @@ -1316,7 +1235,7 @@

    snapshot

    Defined in - src/Model.js:575 + src/Model.js:460

    @@ -1368,7 +1287,7 @@

    type

    Defined in - src/Model.js:687 + src/Model.js:572

    @@ -1417,7 +1336,7 @@

    validate

    Defined in - src/Model.js:422 + src/Model.js:307

    @@ -1460,59 +1379,6 @@

    Returns:

    -
    -
    -

    validatePresence

    - -
    - (
      -
    • - value -
    • -
    ) -
    - - - - - - - - -
    -

    - Defined in - src/Model.js:26 -

    - - - -
    - -
    -

    returns true as long as the value is not null, undefined, or ''

    - -
    - -
    -

    Parameters:

    - -
      -
    • - value - Object - - -
      - -
      - -
    • -
    -
    - - -

    validateProperties

    @@ -1544,7 +1410,7 @@

    validateProperties

    Defined in - src/Model.js:42 + src/Model.js:20

    @@ -1607,45 +1473,6 @@

    Returns:

    -
    -
    -

    validates

    - - () - - - - - - - - -
    -

    - Defined in - src/Model.js:133 -

    - - - -
    - -
    -

    Defines validations for attributes that will be applied before saving. Takes one argument, a function to validate -the attribute. The default validator is presence: not null, undefined, or ''.

    -
    function nonzero(value => value !== 0)
    -
    -class Todo extends Model {
    -  @validates
    -  @attribute(nonzero) numberOfAssignees
    -}
    -
    - -
    - - - -
    @@ -1663,7 +1490,7 @@

    _dirtyAttributes

    Defined in - src/Model.js:240 + src/Model.js:125

    @@ -1688,7 +1515,7 @@

    _dirtyRelationships

    Defined in - src/Model.js:233 + src/Model.js:118

    @@ -1713,7 +1540,7 @@

    _disposed

    Defined in - src/Model.js:225 + src/Model.js:110

    @@ -1740,7 +1567,7 @@

    endpoint

    Defined in - src/Model.js:218 + src/Model.js:103

    @@ -1766,7 +1593,7 @@

    errors

    Defined in - src/Model.js:320 + src/Model.js:205

    @@ -1796,7 +1623,7 @@

    hasUnpersistedChanges

    Defined in - src/Model.js:282 + src/Model.js:167

    @@ -1821,7 +1648,7 @@

    isDirty

    Defined in - src/Model.js:247 + src/Model.js:132

    @@ -1867,7 +1694,7 @@

    isInFlight

    Defined in - src/Model.js:303 + src/Model.js:188

    @@ -1901,7 +1728,7 @@

    isNew

    Defined in - src/Model.js:291 + src/Model.js:176

    @@ -1926,7 +1753,7 @@

    snapshots

    Defined in - src/Model.js:333 + src/Model.js:218

    @@ -1953,7 +1780,7 @@

    type

    Defined in - src/Model.js:210 + src/Model.js:95

    diff --git a/docs/classes/RelatedRecordsArray.html b/docs/classes/RelatedRecordsArray.html index 1ad366c3..09a2d134 100644 --- a/docs/classes/RelatedRecordsArray.html +++ b/docs/classes/RelatedRecordsArray.html @@ -17,7 +17,7 @@

    - API Docs for: 1.0.25 + API Docs for: 1.0.26
    diff --git a/docs/classes/Schema.html b/docs/classes/Schema.html index ccda0b4f..1a954260 100644 --- a/docs/classes/Schema.html +++ b/docs/classes/Schema.html @@ -17,7 +17,7 @@

    - API Docs for: 1.0.25 + API Docs for: 1.0.26
    diff --git a/docs/classes/Store.html b/docs/classes/Store.html index 4cc4fbfd..4e5f61f5 100644 --- a/docs/classes/Store.html +++ b/docs/classes/Store.html @@ -17,7 +17,7 @@

    - API Docs for: 1.0.25 + API Docs for: 1.0.26
    @@ -1097,8 +1097,8 @@

    findAll

    store.findAll('todos', { queryParams: { filter: { -start_time: moment(), -end_time: moment() +start_time: '2020-06-01T00:00:00.000Z', +end_time: '2020-06-02T00:00:00.000Z' } } })

    diff --git a/docs/data.json b/docs/data.json index 9a9fc606..d626330d 100644 --- a/docs/data.json +++ b/docs/data.json @@ -3,7 +3,7 @@ "name": "mobx-async-store", "description": "Asyc Data Store for mobx", "url": "https://github.com/artemis-ag/mobx-async-store", - "version": "1.0.25" + "version": "1.0.26" }, "files": { "src/decorators/attributes.js": { @@ -98,7 +98,7 @@ "plugin_for": [], "extension_for": [], "file": "src/Model.js", - "line": 196 + "line": 81 }, "Store": { "name": "Store", @@ -295,21 +295,7 @@ }, { "file": "src/Model.js", - "line": 26, - "description": "returns `true` as long as the `value` is not `null`, `undefined`, or `''`", - "itemtype": "method", - "name": "validatePresence", - "params": [ - { - "name": "value", - "description": "" - } - ], - "class": "Model" - }, - { - "file": "src/Model.js", - "line": 42, + "line": 20, "description": "Maps the passed-in property names through and runs validations against those properties", "itemtype": "method", "name": "validateProperties", @@ -338,31 +324,7 @@ }, { "file": "src/Model.js", - "line": 79, - "description": "Helper method for apply the correct defaults to attributes.", - "itemtype": "method", - "name": "defaultValueForDescriptor", - "class": "Model" - }, - { - "file": "src/Model.js", - "line": 99, - "description": "Defines attributes that will be serialized and deserialized. Takes one argument, a class that the attribute will be coerced to.\nThis can be a Javascript primitive or another class. `id` cannot be defined as it is assumed to exist.\nAttributes can be defined with a default.\n```\nclass Todo extends Model {", - "itemtype": "method", - "name": "attribute", - "class": "Model" - }, - { - "file": "src/Model.js", - "line": 133, - "description": "Defines validations for attributes that will be applied before saving. Takes one argument, a function to validate\nthe attribute. The default validator is `presence`: not `null`, `undefined`, or `''`.\n```\nfunction nonzero(value => value !== 0)\n\nclass Todo extends Model {\n `@validates`\n `@attribute`(nonzero) numberOfAssignees\n}\n```", - "itemtype": "method", - "name": "validates", - "class": "Model" - }, - { - "file": "src/Model.js", - "line": 200, + "line": 85, "description": "Initializer for model", "itemtype": "method", "name": "constructor", @@ -370,7 +332,7 @@ }, { "file": "src/Model.js", - "line": 210, + "line": 95, "description": "The type of the model. Defined on the class. Defaults to the underscored version of the class name\n(eg 'calendar_events').", "itemtype": "property", "name": "type", @@ -379,7 +341,7 @@ }, { "file": "src/Model.js", - "line": 218, + "line": 103, "description": "The canonical path to the resource on the server. Defined on the class.\nDefaults to the underscored version of the class name", "itemtype": "property", "name": "endpoint", @@ -388,7 +350,7 @@ }, { "file": "src/Model.js", - "line": 225, + "line": 110, "description": "has this object been destroyed?", "itemtype": "property", "name": "_disposed", @@ -398,7 +360,7 @@ }, { "file": "src/Model.js", - "line": 233, + "line": 118, "description": "set of relationships which have changed since last snapshot", "itemtype": "property", "name": "_dirtyRelationships", @@ -407,7 +369,7 @@ }, { "file": "src/Model.js", - "line": 240, + "line": 125, "description": "set of attributes which have changed since last snapshot", "itemtype": "property", "name": "_dirtyAttributes", @@ -416,7 +378,7 @@ }, { "file": "src/Model.js", - "line": 247, + "line": 132, "description": "True if the instance has been modified from its persisted state\n\nNOTE that isDirty does _NOT_ track changes to the related objects\nbut it _does_ track changes to the relationships themselves.\n\nFor example, adding or removing a related object will mark this record as dirty,\nbut changing a related object's properties will not mark this record as dirty.\n\nThe caller is reponsible for asking related objects about their\nown dirty state.\n\n```\nkpi = store.add('kpis', { name: 'A good thing to measure' })\nkpi.isDirty\n=> true\nkpi.name\n=> \"A good thing to measure\"\nawait kpi.save()\nkpi.isDirty\n=> false\nkpi.name = \"Another good thing to measure\"\nkpi.isDirty\n=> true\nawait kpi.save()\nkpi.isDirty\n=> false\n```", "itemtype": "property", "name": "isDirty", @@ -425,7 +387,7 @@ }, { "file": "src/Model.js", - "line": 282, + "line": 167, "description": "have any changes been made since this record was last persisted?", "itemtype": "property", "name": "hasUnpersistedChanges", @@ -434,7 +396,7 @@ }, { "file": "src/Model.js", - "line": 291, + "line": 176, "description": "True if the model has not been sent to the store", "itemtype": "property", "name": "isNew", @@ -443,7 +405,7 @@ }, { "file": "src/Model.js", - "line": 303, + "line": 188, "description": "True if the instance is coming from / going to the server\n```\nkpi = store.find('kpis', 5)\n// fetch started\nkpi.isInFlight\n=> true\n// fetch finished\nkpi.isInFlight\n=> false\n```", "itemtype": "property", "name": "isInFlight", @@ -453,7 +415,7 @@ }, { "file": "src/Model.js", - "line": 320, + "line": 205, "description": "A hash of errors from the server\n```\nkpi = store.find('kpis', 5)\nkpi.errors\n=> { authorization: \"You do not have access to this resource\" }\n```", "itemtype": "property", "name": "errors", @@ -463,7 +425,7 @@ }, { "file": "src/Model.js", - "line": 333, + "line": 218, "description": "a list of snapshots that have been taken since the record was either last persisted or since it was instantiated", "itemtype": "property", "name": "snapshots", @@ -473,7 +435,7 @@ }, { "file": "src/Model.js", - "line": 342, + "line": 227, "description": "restores data to its last snapshot state\n```\nkpi = store.find('kpis', 5)\nkpi.name\n=> \"A good thing to measure\"\nkpi.name = \"Another good thing to measure\"\nkpi.rollback()\nkpi.name\n=> \"A good thing to measure\"\n```", "itemtype": "method", "name": "rollback", @@ -481,7 +443,7 @@ }, { "file": "src/Model.js", - "line": 359, + "line": 244, "description": "restores data to its last persisted state or the oldest snapshot\nstate if the model was never persisted", "itemtype": "method", "name": "rollbackToPersisted", @@ -489,7 +451,7 @@ }, { "file": "src/Model.js", - "line": 369, + "line": 254, "description": "creates or updates a record.", "itemtype": "method", "name": "save", @@ -508,7 +470,7 @@ }, { "file": "src/Model.js", - "line": 422, + "line": 307, "description": "Checks all validations, adding errors where necessary and returning `false` if any are not valid\nDefault is to check all validations, but they can be selectively run via options:\n - attributes - an array of names of attributes to validate\n - relationships - an array of names of relationships to validate", "itemtype": "method", "name": "validate", @@ -527,7 +489,7 @@ }, { "file": "src/Model.js", - "line": 446, + "line": 331, "description": "deletes a record from the store and server", "itemtype": "method", "name": "destroy", @@ -539,7 +501,7 @@ }, { "file": "src/Model.js", - "line": 515, + "line": 400, "description": "Magic method that makes changes to records\nobservable", "itemtype": "method", "name": "_makeObservable", @@ -547,7 +509,7 @@ }, { "file": "src/Model.js", - "line": 532, + "line": 417, "description": "sets up a reaction for each top-level attribute so we can compare\nvalues after each mutation and keep track of dirty attr states\nif an attr is different than the last snapshot, add it to the\n_dirtyAttributes set\nif it's the same as the last snapshot, make sure it's _not_ in the\n_dirtyAttributes set", "itemtype": "method", "name": "_listenForChanges", @@ -555,7 +517,7 @@ }, { "file": "src/Model.js", - "line": 565, + "line": 450, "description": "call this when destroying an object to make sure that we clean up\nany event listeners and don't leak memory", "itemtype": "method", "name": "dispose", @@ -563,7 +525,7 @@ }, { "file": "src/Model.js", - "line": 575, + "line": 460, "description": "The current state of defined attributes and relationships of the instance\nReally just an alias for attributes\n```\ntodo = store.find('todos', 5)\ntodo.title\n=> \"Buy the eggs\"\nsnapshot = todo.snapshot\ntodo.title = \"Buy the eggs and bacon\"\nsnapshot.title\n=> \"Buy the eggs and bacon\"\n```", "itemtype": "method", "name": "snapshot", @@ -575,7 +537,7 @@ }, { "file": "src/Model.js", - "line": 597, + "line": 482, "description": "Sets previous snapshot to current snapshot", "itemtype": "method", "name": "setPreviousSnapshot", @@ -583,7 +545,7 @@ }, { "file": "src/Model.js", - "line": 606, + "line": 491, "description": "the latest snapshot", "itemtype": "method", "name": "previousSnapshot", @@ -591,7 +553,7 @@ }, { "file": "src/Model.js", - "line": 617, + "line": 502, "description": "the latest persisted snapshot or the first snapshot if the model was never persisted", "itemtype": "method", "name": "previousSnapshot", @@ -599,7 +561,7 @@ }, { "file": "src/Model.js", - "line": 626, + "line": 511, "description": "take a snapshot of the current model state.\nif persisted, clear the stack and push this snapshot to the top\nif not persisted, push this snapshot to the top of the stack", "itemtype": "method", "name": "_takeSnapshot", @@ -614,7 +576,7 @@ }, { "file": "src/Model.js", - "line": 649, + "line": 534, "description": "set the current attributes and relationships to the attributes\nand relationships of the snapshot to be applied. also reset errors", "itemtype": "method", "name": "_applySnapshot", @@ -629,7 +591,7 @@ }, { "file": "src/Model.js", - "line": 666, + "line": 551, "description": "a list of any property paths which have been changed since the previous\nsnapshot\n```\nconst todo = new Todo({ title: 'Buy Milk' })\ntodo.dirtyAttributes\n=> []\ntodo.title = 'Buy Cheese'\ntodo.dirtyAttributes\n=> ['title']\n```", "itemtype": "method", "name": "dirtyAttributes", @@ -641,7 +603,7 @@ }, { "file": "src/Model.js", - "line": 687, + "line": 572, "description": "shortcut to get the static", "itemtype": "method", "name": "type", @@ -653,7 +615,7 @@ }, { "file": "src/Model.js", - "line": 697, + "line": 582, "description": "current attributes of record", "itemtype": "method", "name": "attributes", @@ -665,7 +627,7 @@ }, { "file": "src/Model.js", - "line": 715, + "line": 600, "description": "Getter find the attribute definition for the model type.", "itemtype": "method", "name": "attributeDefinitions", @@ -677,7 +639,7 @@ }, { "file": "src/Model.js", - "line": 726, + "line": 611, "description": "Getter find the relationship definitions for the model type.", "itemtype": "method", "name": "relationshipDefinitions", @@ -689,7 +651,7 @@ }, { "file": "src/Model.js", - "line": 737, + "line": 622, "description": "Getter to check if the record has errors.", "itemtype": "method", "name": "hasErrors", @@ -701,7 +663,7 @@ }, { "file": "src/Model.js", - "line": 747, + "line": 632, "description": "Getter to check if the record has errors.", "itemtype": "method", "name": "hasErrors", @@ -713,7 +675,7 @@ }, { "file": "src/Model.js", - "line": 757, + "line": 642, "description": "Getter to just get the names of a records attributes.", "itemtype": "method", "name": "attributeNames", @@ -725,7 +687,7 @@ }, { "file": "src/Model.js", - "line": 767, + "line": 652, "description": "Getter to just get the names of a records relationships.", "itemtype": "method", "name": "relationshipNames", @@ -737,7 +699,7 @@ }, { "file": "src/Model.js", - "line": 777, + "line": 662, "description": "getter method to get the default attributes", "itemtype": "method", "name": "defaultAttributes", @@ -749,7 +711,7 @@ }, { "file": "src/Model.js", - "line": 794, + "line": 679, "description": "getter method to get data in api compliance format\nTODO: Figure out how to handle unpersisted ids", "itemtype": "method", "name": "jsonapi", @@ -918,7 +880,7 @@ { "file": "src/Store.js", "line": 161, - "description": "finds all of the instances of a given type. If there are instances available in the store,\nit will return those, otherwise it will trigger a fetch\n\n store.findAll('todos')\n // fetch triggered\n => [event1, event2, event3]\n store.findAll('todos')\n // no fetch triggered\n => [event1, event2, event3]\n\npassing `fromServer` as an option will always trigger a\nfetch if `true` and never trigger a fetch if `false`.\nOtherwise, it will trigger the default behavior\n\n store.findAll('todos', { fromServer: false })\n // no fetch triggered\n => []\n\n store.findAll('todos')\n // fetch triggered\n => [event1, event2, event3]\n\n // async stuff happens on the server\n store.findAll('todos')\n // no fetch triggered\n => [event1, event2, event3]\n\n store.findAll('todos', { fromServer: true })\n // fetch triggered\n => [event1, event2, event3, event4]\n\nQuery params can be passed as part of the options hash.\nThe response will be cached, so the next time `findAll`\nis called with identical params and values, the store will\nfirst look for the local result (unless `fromServer` is `true`)\n\n store.findAll('todos', {\n queryParams: {\n filter: {\n start_time: moment(),\n end_time: moment()\n }\n }\n })", + "description": "finds all of the instances of a given type. If there are instances available in the store,\nit will return those, otherwise it will trigger a fetch\n\n store.findAll('todos')\n // fetch triggered\n => [event1, event2, event3]\n store.findAll('todos')\n // no fetch triggered\n => [event1, event2, event3]\n\npassing `fromServer` as an option will always trigger a\nfetch if `true` and never trigger a fetch if `false`.\nOtherwise, it will trigger the default behavior\n\n store.findAll('todos', { fromServer: false })\n // no fetch triggered\n => []\n\n store.findAll('todos')\n // fetch triggered\n => [event1, event2, event3]\n\n // async stuff happens on the server\n store.findAll('todos')\n // no fetch triggered\n => [event1, event2, event3]\n\n store.findAll('todos', { fromServer: true })\n // fetch triggered\n => [event1, event2, event3, event4]\n\nQuery params can be passed as part of the options hash.\nThe response will be cached, so the next time `findAll`\nis called with identical params and values, the store will\nfirst look for the local result (unless `fromServer` is `true`)\n\n store.findAll('todos', {\n queryParams: {\n filter: {\n start_time: '2020-06-01T00:00:00.000Z',\n end_time: '2020-06-02T00:00:00.000Z'\n }\n }\n })", "itemtype": "method", "name": "findAll", "params": [ @@ -1552,6 +1514,25 @@ { "file": "src/utils.js", "line": 147, + "description": "convert a value into a date, pass Date or Moment instances thru\nuntouched", + "itemtype": "method", + "name": "makeDate", + "params": [ + { + "name": "value", + "description": "", + "type": "*" + } + ], + "return": { + "description": "", + "type": "Date|Moment" + }, + "class": "" + }, + { + "file": "src/utils.js", + "line": 159, "description": "recursively walk an object and call the `iteratee` function for\neach property. returns an array of results of calls to the iteratee.", "itemtype": "method", "name": "walk", @@ -1579,7 +1560,7 @@ }, { "file": "src/utils.js", - "line": 165, + "line": 177, "description": "deeply compare objects a and b and return object paths for attributes\nwhich differ. it is important to note that this comparison is biased\ntoward object a. object a is walked and compared against values in\nobject b. if a property exists in object b, but not in object a, it\nwill not be counted as a difference.", "itemtype": "method", "name": "diff", diff --git a/docs/files/src_Model.js.html b/docs/files/src_Model.js.html index 79cdd222..af61fe9c 100644 --- a/docs/files/src_Model.js.html +++ b/docs/files/src_Model.js.html @@ -17,7 +17,7 @@

    - API Docs for: 1.0.25 + API Docs for: 1.0.26
    @@ -93,9 +93,7 @@

    File: src/Model.js

    observable } from 'mobx' -import moment from 'moment' - -import { diff } from './utils' +import { diff, makeDate } from './utils' import ObjectPromiseProxy from './ObjectPromiseProxy' import schema from './schema' @@ -104,26 +102,6 @@

    File: src/Model.js

    import isObject from 'lodash/isObject' import findLast from 'lodash/findLast' -function isPresent (value) { - return value !== null && value !== undefined && value !== '' -} - -/** - * returns `true` as long as the `value` is not `null`, `undefined`, or `''` - * @method validatePresence - * @param value - */ - -function validatePresence (value) { - return { - isValid: isPresent(value), - errors: [{ - key: 'blank', - message: 'can\'t be blank' - }] - } -} - /** * Maps the passed-in property names through and runs validations against those properties * @method validateProperties @@ -161,99 +139,6 @@

    File: src/Model.js

    }) } -/** - * Helper method for apply the correct defaults to attributes. - * @method defaultValueForDescriptor - */ -function defaultValueForDescriptor (descriptor, DataType) { - if (typeof descriptor.initializer === 'function') { - const value = descriptor.initializer() - if (DataType.name === 'Date') { - return moment(value).toDate() - } else { - return DataType(value) - } - } - - if (DataType.name === 'String') return '' - if (DataType.name === 'Array') return [] - - return null -} - -/** - * Defines attributes that will be serialized and deserialized. Takes one argument, a class that the attribute will be coerced to. - * This can be a Javascript primitive or another class. `id` cannot be defined as it is assumed to exist. - * Attributes can be defined with a default. - * ``` - * class Todo extends Model { - * @attribute(Date) start_time = moment() - * } - * ``` - * @method attribute - */ -export function attribute (dataType = (obj) => obj) { - return function (target, property, descriptor) { - const { type } = target.constructor - const defaultValue = defaultValueForDescriptor(descriptor, dataType) - // Update the schema - schema.addAttribute({ - dataType, - defaultValue, - property, - type - }) - // Return custom descriptor - return { - get () { - return defaultValue - }, - set (value) { - set(target, property, value) - } - } - } -} - -/** - * Defines validations for attributes that will be applied before saving. Takes one argument, a function to validate - * the attribute. The default validator is `presence`: not `null`, `undefined`, or `''`. - * ``` - * function nonzero(value => value !== 0) - * - * class Todo extends Model { - * `@validates` - * `@attribute`(nonzero) numberOfAssignees - * } - * ``` - * @method validates - */ - - export function validates (target, property) { - let validator = validatePresence - - if (typeof target === 'function') { - validator = target - - return function (target, property) { - const { type } = target.constructor - - schema.addValidation({ - property, - type, - validator - }) - } - } else { - const { type } = target.constructor - schema.addValidation({ - property, - type, - validator - }) - } - } - /* * Defines a many-to-one relationship. Defaults to the class with camelized name of the property. * An optional argument specifies the data model, if different from the property name. @@ -908,7 +793,7 @@

    File: src/Model.js

    if (DataType.name === 'Array' || DataType.name === 'Object') { attr = toJS(value) } else if (DataType.name === 'Date') { - attr = moment(value).toISOString() + attr = makeDate(value).toISOString() } else { attr = DataType(value) } diff --git a/docs/files/src_Store.js.html b/docs/files/src_Store.js.html index 3688a64c..dfe3c6fb 100644 --- a/docs/files/src_Store.js.html +++ b/docs/files/src_Store.js.html @@ -17,7 +17,7 @@

    - API Docs for: 1.0.25 + API Docs for: 1.0.26
    @@ -283,8 +283,8 @@

    File: src/Store.js

    * store.findAll('todos', { * queryParams: { * filter: { - * start_time: moment(), - * end_time: moment() + * start_time: '2020-06-01T00:00:00.000Z', + * end_time: '2020-06-02T00:00:00.000Z' * } * } * }) diff --git a/docs/files/src_decorators_attributes.js.html b/docs/files/src_decorators_attributes.js.html index 11e87324..6d2de7ba 100644 --- a/docs/files/src_decorators_attributes.js.html +++ b/docs/files/src_decorators_attributes.js.html @@ -17,7 +17,7 @@

    - API Docs for: 1.0.25 + API Docs for: 1.0.26
    @@ -83,9 +83,9 @@

    File: src/decorators/attributes.js

    -import moment from 'moment'
     import { set } from 'mobx'
     import schema from '../schema'
    +import { makeDate } from '../utils'
     
     /**
      * returns `true` as long as the `value` is not `null`, `undefined`, or `''`
    @@ -121,7 +121,7 @@ 

    File: src/decorators/attributes.js

    if (typeof descriptor.initializer === 'function') { const value = descriptor.initializer() if (DataType.name === 'Date') { - return moment(value).toDate() + return makeDate(value) } else { return DataType(value) } @@ -139,7 +139,7 @@

    File: src/decorators/attributes.js

    * Attributes can be defined with a default. * ``` * class Todo extends Model { - * @attribute(Date) start_time = moment() + * @attribute(Date) start_time = new Date() * } * ``` * @method attribute diff --git a/docs/files/src_decorators_relationships.js.html b/docs/files/src_decorators_relationships.js.html index 199ba8b1..136714ff 100644 --- a/docs/files/src_decorators_relationships.js.html +++ b/docs/files/src_decorators_relationships.js.html @@ -17,7 +17,7 @@

    - API Docs for: 1.0.25 + API Docs for: 1.0.26
    diff --git a/docs/files/src_schema.js.html b/docs/files/src_schema.js.html index b509212b..fc7c98b8 100644 --- a/docs/files/src_schema.js.html +++ b/docs/files/src_schema.js.html @@ -17,7 +17,7 @@

    - API Docs for: 1.0.25 + API Docs for: 1.0.26
    diff --git a/docs/files/src_utils.js.html b/docs/files/src_utils.js.html index ac8fd502..63cc6cfa 100644 --- a/docs/files/src_utils.js.html +++ b/docs/files/src_utils.js.html @@ -17,7 +17,7 @@

    - API Docs for: 1.0.25 + API Docs for: 1.0.26
    @@ -229,6 +229,18 @@

    File: src/utils.js

    }) } +/** + * convert a value into a date, pass Date or Moment instances thru + * untouched + * @method makeDate + * @param {*} value + * @return {Date|Moment} + */ +export function makeDate (value) { + if (value instanceof Date || value._isAMomentObject) return value + return new Date(Date.parse(value)) +} + /** * recursively walk an object and call the `iteratee` function for * each property. returns an array of results of calls to the iteratee. diff --git a/docs/index.html b/docs/index.html index 8cc80777..fcedcfd3 100644 --- a/docs/index.html +++ b/docs/index.html @@ -17,7 +17,7 @@

    - API Docs for: 1.0.25 + API Docs for: 1.0.26
    diff --git a/package.json b/package.json index da5b6c19..50a6f1c3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@artemisag/mobx-async-store", - "version": "1.0.25", + "version": "1.0.26", "module": "dist/mobx-async-store.esm.js", "browser": "dist/mobx-async-store.cjs.js", "main": "dist/mobx-async-store.cjs.js", @@ -9,7 +9,6 @@ "jsonapi-serializer": "^3.6.4", "lodash": "^4.17.15", "mobx": "5.5.0", - "moment": "^2.24.0", "pluralize": "^8.0.0", "react-native-uuid": "^1.4.9", "uuid": "^3.3.2" @@ -38,7 +37,6 @@ "jest-fetch-mock": "^2.1.2", "mobx": "5.5.0", "mobx-react": "^5.4.3", - "moment": "^2.24.0", "react": "^16.8.6", "react-dom": "^16.8.6", "rollup": "^1.0.0", diff --git a/spec/Model.spec.js b/spec/Model.spec.js index b14ff6b6..04eedbfd 100644 --- a/spec/Model.spec.js +++ b/spec/Model.spec.js @@ -1,6 +1,5 @@ /* global fetch */ import { autorun, isObservable } from 'mobx' -import moment from 'moment' import { Model, @@ -18,8 +17,7 @@ import { exampleRelatedToManyIncludedWithNoiseResponse } from './fixtures/exampleRelationalResponses' -// YYYY-MM-DD -const timestamp = moment() +const timestamp = new Date(Date.now()) class Note extends Model { static type = 'notes' @@ -137,7 +135,8 @@ const mockTodoData = { type: 'organizations', attributes: { title: 'Do taxes', - created_at: timestamp.format('YYYY-MM-DD') + // YYYY-MM-DD + created_at: timestamp.toISOString().split('T')[0] } } } @@ -469,7 +468,7 @@ describe('Model', () => { it('a snapshot of the current attributes and relationship', async () => { const todo = new Organization({ title: 'Buy Milk' }) expect(todo.snapshot.attributes).toEqual({ - due_at: moment(timestamp).toDate(), + due_at: timestamp, tags: [], title: 'Buy Milk', options: {} @@ -479,7 +478,7 @@ describe('Model', () => { it('doesn\'t exclude falsey values', async () => { const todo = new Organization({ title: '' }) expect(todo.snapshot.attributes).toEqual({ - due_at: moment(timestamp).toDate(), + due_at: timestamp, tags: [], title: '', options: {} @@ -492,7 +491,7 @@ describe('Model', () => { const todo = new Organization({ title: 'Buy Milk' }) todo.title = 'something different' expect(todo.previousSnapshot.attributes).toEqual({ - due_at: moment(timestamp).toDate(), + due_at: timestamp, tags: [], title: 'Buy Milk', options: {} @@ -627,7 +626,7 @@ describe('Model', () => { id: '1', type: 'organizations', attributes: { - due_at: moment(timestamp).toISOString(), + due_at: timestamp.toISOString(), tags: [], title: 'Buy Milk', options: {} @@ -651,7 +650,7 @@ describe('Model', () => { id: '11', type: 'organizations', attributes: { - due_at: moment(timestamp).toISOString(), + due_at: timestamp.toISOString(), tags: [], title: 'Buy Milk', options: {} @@ -921,7 +920,7 @@ describe('Model', () => { data: { type: 'organizations', attributes: { - due_at: moment(timestamp).toDate().toISOString(), + due_at: timestamp.toISOString(), tags: [], title: 'Buy Milk', options: {} @@ -932,8 +931,9 @@ describe('Model', () => { // from the server expect(todo.id).toEqual('1') // Check that the `created_at` attribute is populated + // YYYY-MM-DD expect(todo.created_at) - .toEqual(timestamp.format('YYYY-MM-DD')) + .toEqual(timestamp.toISOString().split('T')[0]) }) it('sets hasUnpersistedChanges = false when save succeeds', async () => { diff --git a/src/Model.js b/src/Model.js index 11e91dbd..98a8b168 100644 --- a/src/Model.js +++ b/src/Model.js @@ -8,9 +8,7 @@ import { observable } from 'mobx' -import moment from 'moment' - -import { diff } from './utils' +import { diff, makeDate } from './utils' import ObjectPromiseProxy from './ObjectPromiseProxy' import schema from './schema' @@ -19,26 +17,6 @@ import isEqual from 'lodash/isEqual' import isObject from 'lodash/isObject' import findLast from 'lodash/findLast' -function isPresent (value) { - return value !== null && value !== undefined && value !== '' -} - -/** - * returns `true` as long as the `value` is not `null`, `undefined`, or `''` - * @method validatePresence - * @param value - */ - -function validatePresence (value) { - return { - isValid: isPresent(value), - errors: [{ - key: 'blank', - message: 'can\'t be blank' - }] - } -} - /** * Maps the passed-in property names through and runs validations against those properties * @method validateProperties @@ -76,99 +54,6 @@ function stringifyIds (object) { }) } -/** - * Helper method for apply the correct defaults to attributes. - * @method defaultValueForDescriptor - */ -function defaultValueForDescriptor (descriptor, DataType) { - if (typeof descriptor.initializer === 'function') { - const value = descriptor.initializer() - if (DataType.name === 'Date') { - return moment(value).toDate() - } else { - return DataType(value) - } - } - - if (DataType.name === 'String') return '' - if (DataType.name === 'Array') return [] - - return null -} - -/** - * Defines attributes that will be serialized and deserialized. Takes one argument, a class that the attribute will be coerced to. - * This can be a Javascript primitive or another class. `id` cannot be defined as it is assumed to exist. - * Attributes can be defined with a default. - * ``` - * class Todo extends Model { - * @attribute(Date) start_time = moment() - * } - * ``` - * @method attribute - */ -export function attribute (dataType = (obj) => obj) { - return function (target, property, descriptor) { - const { type } = target.constructor - const defaultValue = defaultValueForDescriptor(descriptor, dataType) - // Update the schema - schema.addAttribute({ - dataType, - defaultValue, - property, - type - }) - // Return custom descriptor - return { - get () { - return defaultValue - }, - set (value) { - set(target, property, value) - } - } - } -} - -/** - * Defines validations for attributes that will be applied before saving. Takes one argument, a function to validate - * the attribute. The default validator is `presence`: not `null`, `undefined`, or `''`. - * ``` - * function nonzero(value => value !== 0) - * - * class Todo extends Model { - * `@validates` - * `@attribute`(nonzero) numberOfAssignees - * } - * ``` - * @method validates - */ - - export function validates (target, property) { - let validator = validatePresence - - if (typeof target === 'function') { - validator = target - - return function (target, property) { - const { type } = target.constructor - - schema.addValidation({ - property, - type, - validator - }) - } - } else { - const { type } = target.constructor - schema.addValidation({ - property, - type, - validator - }) - } - } - /* * Defines a many-to-one relationship. Defaults to the class with camelized name of the property. * An optional argument specifies the data model, if different from the property name. @@ -823,7 +708,7 @@ class Model { if (DataType.name === 'Array' || DataType.name === 'Object') { attr = toJS(value) } else if (DataType.name === 'Date') { - attr = moment(value).toISOString() + attr = makeDate(value).toISOString() } else { attr = DataType(value) } diff --git a/src/Store.js b/src/Store.js index 872560b3..c42c0edf 100644 --- a/src/Store.js +++ b/src/Store.js @@ -198,8 +198,8 @@ class Store { * store.findAll('todos', { * queryParams: { * filter: { - * start_time: moment(), - * end_time: moment() + * start_time: '2020-06-01T00:00:00.000Z', + * end_time: '2020-06-02T00:00:00.000Z' * } * } * }) diff --git a/src/decorators/attributes.js b/src/decorators/attributes.js index ce567642..076dd227 100644 --- a/src/decorators/attributes.js +++ b/src/decorators/attributes.js @@ -1,6 +1,6 @@ -import moment from 'moment' import { set } from 'mobx' import schema from '../schema' +import { makeDate } from '../utils' /** * returns `true` as long as the `value` is not `null`, `undefined`, or `''` @@ -36,7 +36,7 @@ function defaultValueForDescriptor (descriptor, DataType) { if (typeof descriptor.initializer === 'function') { const value = descriptor.initializer() if (DataType.name === 'Date') { - return moment(value).toDate() + return makeDate(value) } else { return DataType(value) } @@ -54,7 +54,7 @@ function defaultValueForDescriptor (descriptor, DataType) { * Attributes can be defined with a default. * ``` * class Todo extends Model { - * @attribute(Date) start_time = moment() + * @attribute(Date) start_time = new Date() * } * ``` * @method attribute diff --git a/src/utils.js b/src/utils.js index 1c475af3..14a3921e 100644 --- a/src/utils.js +++ b/src/utils.js @@ -144,6 +144,18 @@ export function stringifyIds (object) { }) } +/** + * convert a value into a date, pass Date or Moment instances thru + * untouched + * @method makeDate + * @param {*} value + * @return {Date|Moment} + */ +export function makeDate (value) { + if (value instanceof Date || value._isAMomentObject) return value + return new Date(Date.parse(value)) +} + /** * recursively walk an object and call the `iteratee` function for * each property. returns an array of results of calls to the iteratee.