Commit Graph

706 Commits

Author SHA1 Message Date
Raymond Feng c609b6d7ce Allow leading slash for `path` in model settings 2015-04-24 08:23:13 -07:00
Miroslav Bajtoš 40eecd98c9 validations: treat `NaN` as a blank value
When a required number property is set to NaN, for example as a result
of coersion (`Number([1,2,3])`), the "presence" validation now correctly
reports an error.
2015-04-22 19:57:48 +02:00
Raymond Feng c338c2326d Merge pull request #561 from strongloop/feature/allow-name-mapper-for-discovery
Allow custom name mapping for discovered models
2015-04-22 08:43:09 -07:00
Raymond Feng 1e70678fa7 Allow custom name mapping for discovered models 2015-04-22 08:42:47 -07:00
Miroslav Bajtoš b642d52fbe Merge pull request #565 from strongloop/feature/validate-upsert
Validate model on updateOrCreate (upsert).
2015-04-17 17:20:55 +02:00
Raymond Feng 988a929577 Extend findById to accept an optional filter object 2015-04-16 09:06:53 -07:00
Miroslav Bajtoš 3df62444d6 Validate model on updateOrCreate (upsert).
Fix the implementation of updateOrCreate (a.k.a. upsert) to validate
the model before calling the connector.

In order to preserve backwards compatibility, validation errors are
only logged via console.warn by default.

The correct behaviour, where validation errors fail the updateOrCreate
operation, can be enabled via new model setting "validateUpsert".
2015-04-16 09:09:54 +02:00
Miroslav Bajtoš 99d4c6aa8d Add new strict mode "validate"
When a model is configured with `strict: 'validate'`,
any dynamic properties not included in the schema trigger
a validation error.
2015-04-14 08:16:10 +02:00
Partap Davis 29eb3434c7 Promisify model relation methods
When a callback is omitted from a method on a model relation that
supports promises, return that promise.  This includes all the standard
DAO methods, as well as any user-defined methods that return promises.

e.g.:
mylist.todos.create({name: 'Item 1'}) // returns Promise

This API will use native ES6 promises if available.  If not available,
or to force the use of another Promise library, you must assign the
global.Promise object.

e.g.:
global.Promise = require('bluebird')

Relations affected:

- BelongsTo
- HasOne
- HasMany
- HasManyThrough
- HasAndBelongsToMany
- ReferencesMany
- EmbedsOne

Exceptions:

The EmbedsMany relation has not been promisified, because most of the
methods return synchronous values.

The base relation getter method [e.g.: mylist.todos()] has not been
promisified, due to its default caching behavior.

New Methods:

- getAsync(condition, cb)

A new method "getAsync()" has been added to all relations except
EmbedsMany, which always fetches from the datasource rather than from
the cache.  It takes an optional "where" condition (except for HasOne
and BelongsTo) and an optional callback.  If the callback is omitted,
a Promise is returned.
2015-04-07 11:41:07 +02:00
Raymond Feng 16cc870f57 Merge pull request #544 from strongloop/feature/nesting-doc-query
Allow nesting properties to be queried for memory connector
2015-04-02 08:47:10 -07:00
Miroslav Bajtoš cd7bc46efb Deprecate property names containing a dot 2015-04-02 09:49:04 +02:00
Miroslav Bajtoš a00b91db79 Fix test for "after save" called on save/CREATE
Fix the test to correctly trigger the code path where the connector
decides whether a new record is created or an existing one is updated.
2015-04-01 18:25:45 +02:00
Fabien Franzen 17c8576097 Save parent model of embedded relations 2015-04-01 16:59:21 +02:00
Raymond Feng 8d1e782199 Merge pull request #546 from fabien/feature/scope-update-all
Implement scope.updateAll
2015-03-30 14:35:47 -07:00
Raymond Feng 44e5ad1fef Merge pull request #549 from fabien/feature/hookstate-from-options
Allow passing in hookState from options
2015-03-30 14:35:34 -07:00
Miroslav Bajtoš d18fa04ee9 Merge pull request #548 from strongloop/fix/persistUndefinedAsNull-tests-and-SQL
Fix persistUndefinedAsNull tests w/ SQL connectors
2015-03-30 19:28:40 +02:00
Fabien Franzen 1ab3d74ab5 Pass options in operation hooks context. 2015-03-30 18:03:42 +02:00
Raymond Feng 7c7f04955d Merge pull request #543 from strongloop/feature/fix-test-case
Fix the test cases so that they be run with the mssql connector
2015-03-30 08:25:10 -07:00
Miroslav Bajtoš 517ea875ec Merge pull request #500 from 0angelic0/master
Remove and clear Operation Hook observers
2015-03-30 11:26:57 +02:00
Miroslav Bajtoš b66d9fbc54 Fix persistUndefinedAsNull tests w/ SQL connectors
Check whether the connector enables strict mode and don't assert on
dynamic properties in such case.
2015-03-30 10:45:55 +02:00
Fabien Franzen 7e55ef18f8 Implement scope.updateAll 2015-03-28 21:25:24 +01:00
Raymond Feng 137d096cfb Allow nesting properties to be queried for memory connector
See https://github.com/strongloop/loopback/issues/517
2015-03-27 16:05:12 -07:00
Raymond Feng d192b2ea05 Fix the test cases so that they be run with the mssql connector
For SQL server, if the PK is an identity, the id cannot be set for
insert.
2015-03-27 14:18:17 -07:00
Miroslav Bajtoš 9fd4c00225 Add model setting "persistUndefinedAsNull"
When the setting "persistUndefinedAsNull" is true,
the model will use `null` instead of `undefined` in
all property values.

 - Known optional model properties are set to `null` when no value
   was provided.

 - When setting model properties, `undefined` is always converted
   to `null`. This applies to both known (model-defined) properties
   and additional (custom, dynamic) properties.

 - The instance method `toObject()` converts `undefined` to `null` too.
   Because `toJSON()` calls `toObject()` under the hood, the change
   applies to `toJSON()` too.
2015-03-27 18:25:26 +01:00
Simon Ho 311ab293d2 Clean up delete and update tests 2015-03-24 21:30:40 -07:00
Simon Ho f7540edf8a Clean up wording in update/delete tests 2015-03-24 12:15:35 -07:00
Simon Ho 8467243e63 Fix wording in update test 2015-03-24 11:47:13 -07:00
Fabien Franzen aa5943f6b0 Properly support embedsMany destroyAll 2015-03-24 15:39:55 +01:00
Simon Ho 3f97586455 Clean up update/delete manipulation tests 2015-03-23 14:41:38 -07:00
Miroslav Bajtoš 35b549543d test: fix test failure in MySQL connector
Fix a test using a string Person.id value to use a numeric value
instead, in order to support connectors that use numeric ids by default.
2015-03-23 09:18:46 +01:00
Miroslav Bajtoš 1fbaf4e382 Improve test failure messages
Replace

    foo.count.should.equal(X)
    Uncaught TypeError: Cannot read property 'should' of undefined

with

    foo.should.have.property('count', X)`
    Uncaught AssertionError: expected 0 to have property count
2015-03-23 08:44:53 +01:00
Miroslav Bajtoš f7dc6fab90 Fix regression in prototype.save
ffcaa4e7 added a "data" argument to the callback function, which
shadowed the original data with the data returned by a connector.
Not all connectors are returning a data object though, in which case
the model instance ("this" object) is updated with wrong values.

This commit fixes the problem by renaming the second callback argument
to "unusedData".
2015-03-23 08:34:21 +01:00
Fabien Franzen 8199767a8a Implement scope.findOne 2015-03-21 13:44:06 +01:00
Raymond Feng 5d0b745f31 Merge pull request #520 from fabien/feature/scope-find-by-id
Implement scope.findById
2015-03-20 12:00:56 -07:00
Raymond Feng 2f0561a7a8 Merge pull request #514 from fabien/feature/relation-methods
Enable defineMethod for singular relations
2015-03-20 11:57:45 -07:00
Raymond Feng 4ac227b052 Merge pull request #522 from strongloop/feature/enhance-id-comparison-for-updateAttributes
Enhance id comparision for updateAttributes
2015-03-20 11:06:57 -07:00
Raymond Feng 6a40a8a5e5 Enhance id comparision for updateAttributes
string/number and MongoDB ObjectID equality are now allowed.
2015-03-20 11:06:24 -07:00
Miroslav Bajtoš 84d538c1ca Merge pull request #515 from strongloop/feature/isNewInstance
Add ctx.isNewInstance for "save" hooks
2015-03-20 18:33:55 +01:00
Fabien Franzen 54781f376e Enable custom methods on singular relations 2015-03-20 18:15:58 +01:00
Fabien Franzen 34acc6c50a Implement scope.findById 2015-03-20 16:37:46 +01:00
Simon Ho 04f35b31b4 Memory connector returns updated records count
The memory connector now includes the number of updated items in its callback
when updating records.
2015-03-19 15:40:14 -07:00
Raymond Feng a8d8556dcc Merge pull request #499 from clarkorz/fix/merge-mixins
fix #429 Multiple Models can't mixin same class
2015-03-19 13:21:22 -07:00
Miroslav Bajtoš ffcaa4e76e Add ctx.isNewInstance for "save" hooks
"before save" hooks provide "ctx.isNewInstance" whenever "ctx.instance"
is set. Possible values:
 - true for all CREATE operations
 - false for all UPDATE operations
 - undefined for "prototype.save"

"after save" hooks provide "ctx.isNewInstance" whenever "ctx.instance"
is set. Possible values:
 - true after all CREATE operations
 - false after all UPDATE operations
 - undefined after "updateOrCreate" and "save"

Note: both "updateOrCreate" and "prototype.updateAttributes"
don't provide `ctx.instance` to "before save" hooks, therefore
`ctx.isNewInstance` it not provided either.
2015-03-19 18:05:24 +01:00
Miroslav Bajtoš d731252941 deleteAll returns number of deleted records
The number is returned as `data.ctx` to `cb(null, data)`.
2015-03-19 17:54:22 +01:00
Miroslav Bajtoš ae3dc3cec2 Merge pull request #510 from strongloop/fix/duplicate-create
Reject CREATE with a duplicate id
2015-03-19 17:37:01 +01:00
Raymond Feng 01fb5db550 Merge pull request #509 from dbarbeau/master
Enable "between" filter for memory db connector
2015-03-16 10:41:38 -07:00
Miroslav Bajtoš 6f11c2d717 DAO: Fix updateOrCreate to set persisted:true
Before this commit, the following code would not work:

    Change.updateOrCreate({...}, function(err, ch) {
      // somewhere later, modify "ch" and save the changes
      ch.save(cb);
    });
2015-03-16 18:26:42 +01:00
Miroslav Bajtoš 902772f1af Reject CREATE with a duplicate id
Modify the memory connector to reject requests to create a record
with a duplicate id.

Add a unit-test to verify this behaviour across all connectors.
2015-03-16 18:26:42 +01:00
Daniel B. Vasquez 3de94cb9bd add tests for between in memory connector 2015-03-16 18:26:33 +01:00
Raymond Feng f91399a307 Fix the test case so that at least one property is to be changed 2015-03-16 09:26:03 -07:00
Raymond Feng 04348a1168 Make sure id properties cannot be changed 2015-03-16 09:25:38 -07:00
0angelic0 befe30f14a Add abilities to remove and clear observers - Operation Hooks. 2015-03-11 18:08:35 +07:00
Fabien Franzen 8605da3ac6 Improve instance-level operation hooks
"before delete" and "after delete" hooks receive `ctx.instance`
when a single model is being deleted.

"before save" hook receives `ctx.currentInstance` when triggered
by `prototype.updateAttributes()`.

Note that "after save" hook triggered by `prototype.updateAttributes()`
already provides `ctx.instance`.
2015-03-11 09:35:46 +01:00
Clark Wang 90e169c1a6 fix #429 Multiple Models can't mixin same class
Signed-off-by: Clark Wang <clark.wangs@gmail.com>
2015-03-11 16:08:50 +08:00
Raymond Feng 2a24273b4f Fix the test case 2015-03-10 21:26:36 -07:00
Raymond Feng 159be756ac Merge pull request #494 from strongloop/feature/allow-submodel-to-hide-base-properties
Allow submodel to hide base properties
2015-03-09 13:16:09 -07:00
Fabien Franzen 18b58558cc Reformat notifyObserversOf context argument 2015-03-05 15:53:34 +01:00
Fabien Franzen f42859f2e5 Implement operation hooks' context 2015-03-05 11:55:04 +01:00
Raymond Feng 33d3239b44 Allow submodel to hide base properties 2015-03-03 10:27:22 -08:00
Raymond Feng 8042eeb3b1 Make sure inclusion filter is applied to the target model
See https://github.com/strongloop/loopback/issues/1076
2015-02-28 10:53:18 -08:00
Partap Davis 1e6c453191 Add Promises to DAO
When a callback is omitted from a DAO method, return a Promise that
resolves to the value normally passed to the callback of that method.
If a callback is provided, behave normally.

This API will use native ES6 promises if available.  If not available,
or to force the use of another Promise library, you must assign the
global.Promise object.

e.g.:
global.Promise = require('bluebird')

Class methods affected:

- create
- updateOrCreate / upsert
- findOrCreate
- exists
- find
- findOne
- findById
- findByIds
- remove / deleteAll / destroyAll
- removeById / deleteById / destroyById
- count
- update / updateAll

Prototype methods affected:

- save
- delete / remove / destroy
- updateAttribute
- updateAttributes
- reload

Exceptions / edge cases:

- create() used to return the data object that was passed in, even if
  no callback was provided.  Now, if a callback is provided, it will
  return the data object, otherwise it will return a Promise.

- If create() is provided an array of data objects for creation, it
  will continue to always return the array. This batch creation mode
  does not support promises.

- findOrCreate() has a callback of the form: cb(err, instance, created),
  with the extra parameter indicating whether the instance was created
  or not. When called with its promise variant, the resolver will
  receive a single array parameter: [instance, created]
2015-02-26 13:45:37 -07:00
rudzon 3953cc3e2f test for updateAll 2015-02-24 13:56:57 +02:00
Raymond Feng 9fd3865f32 Make sure models are migrated to avoid conflicts 2015-02-20 21:44:49 -08:00
Raymond Feng a42e3b3d30 Add err checks 2015-02-20 21:14:48 -08:00
Raymond Feng 527d5f7a81 Fix findByIds test cases 2015-02-20 21:14:41 -08:00
Raymond Feng 195752eaad Fix the idType so that it works with both MongoDB and RDBs 2015-02-20 16:39:48 -08:00
Raymond Feng 841f2d8f14 Tidy up tests so that they will work with RDBs 2015-02-20 16:10:25 -08:00
Raymond Feng 4c99fbabba Merge pull request #459 from strongloop/feature/fix-test-date
Fix createdAt type so that it won't overflow SQL server int
2015-02-20 12:08:07 -08:00
Miroslav Bajtoš ba0f3c1616 Add a new property option `defaultFn`
The property allows developers to specify that the default value
should be retrieved via a named function.

Only two built-in functions are supported at the moment:

  "guid", "uuid" - generate a new GUID/UUID
  "now" - use the current date and time

Support for custom (user-provided) functions is not implemented yet.
2015-02-20 20:08:25 +01:00
Raymond Feng a713f25d61 Fix the null/undefined check 2015-02-20 09:55:09 -08:00
Raymond Feng 02503e7635 Fix createdAt type so that it won't overflow SQL server int 2015-02-20 09:33:54 -08:00
Miroslav Bajtoš c939efe1e7 Merge pull request #445 from PradnyaBaviskar/issue292
Add $now as shortcut default value for date property
2015-02-17 19:46:05 +01:00
Miroslav Bajtoš 76ebdcb91b ModelBaseClass: promise mode for notifyObserversOf
Support both promise and callback styles in
ModelBaseClass.notifyObserversOf.

When there is no callback supplied, the method returns a promise that
is resolved (or rejected) with the result.
2015-02-17 18:19:21 +01:00
Miroslav Bajtoš a2836fbb56 ModelBaseClass: support promise-based observers
Allow the observer functions passed to `ModelBaseClass.observe`
to return a promise instead of calling the callback.
2015-02-17 17:31:42 +01:00
Bryan Clark 98fcf6b52c use lodash to update the findBelongsTo which now returns an array of matches 2015-02-16 15:36:51 -08:00
Pradnya Baviskar aba7d0dc10 Add $now as shortcut default value for date property 2015-02-16 11:32:24 +05:30
Miroslav Bajtoš 46ff76dda0 Fix `deleteById(id)` and other test failures 2015-02-13 09:34:40 -08:00
Raymond Feng bcbb9c580d Merge branch 'featrue/support-connector-findOrCreate' of https://github.com/clarkorz/loopback-datasource-juggler into clarkorz-featrue/support-connector-findOrCreate 2015-02-11 20:49:51 -08:00
Raymond Feng a8f3d21b65 Merge pull request #436 from strongloop/feature/fix-issue-293
Make sure base properties/settings are merged into the submodel
2015-02-11 16:13:00 -08:00
Raymond Feng 9bac813f59 Merge pull request #435 from strongloop/feature/add-options-to-crud-methods
Add an optional `options` argument to all CRUD methods
2015-02-11 10:31:18 -08:00
Raymond Feng 760ac97902 Add an optional `options` argument to all CRUD methods 2015-02-10 23:57:05 -08:00
Denis Bardadym 32e534732c Remove workaround for perfomance degradation 2015-02-10 13:28:47 +03:00
Raymond Feng 919d32c4dc Change equal to eql to support mongodb ObjectID 2015-02-08 11:14:51 -08:00
Raymond Feng ecf84bf802 Enhance the coercion for boolean/date types
See https://github.com/strongloop/loopback-connector-mongodb/issues/90
2015-02-08 10:54:42 -08:00
Raymond Feng cd2bd34619 Make sure base properties/settings are merged into the submodel
See https://github.com/strongloop/loopback-datasource-juggler/issues/293
2015-02-07 11:15:28 -08:00
Clark Wang 0bef56efc0 support optimized findOrCreate
Signed-off-by: Clark Wang <clark.wangs@gmail.com>

remove undefined for creating data in findOrCreate

Signed-off-by: Clark Wang <clark.wangs@gmail.com>

getLastGeneratedUid instead of force an id

Signed-off-by: Clark Wang <clark.wangs@gmail.com>
2015-02-07 10:23:04 +08:00
Raymond Feng e9c966227d Merge pull request #420 from aol-nnov/fkCustomDbType
Create model foreign key matching type of opposite part of relation (even if it has a custom field type)
2015-02-05 10:28:04 -08:00
Raymond Feng c9e78f6bd2 Fix the perf around should.not.equal for complex objects 2015-02-05 08:22:17 -08:00
Miroslav Bajtoš 2e2e01cc5c Merge pull request #430 from PradnyaBaviskar/issue709
Return 400 when client provides an incorrect value
2015-02-05 12:42:54 +01:00
Pradnya Baviskar be37fb4388 Return 400 when client provides an incorrect value 2015-02-05 16:58:42 +05:30
Andrey Loukhnov 3ce6a2618f testcase for #420 2015-02-05 10:12:34 +03:00
Miroslav Bajtoš 04083678c0 Relax "id" checks in test/manipulation.test.js
Use `eql` instead of `equal` to support non-scalar id values
used by MongoDB connector.
2015-02-04 19:20:56 +01:00
Miroslav Bajtoš 4889443ed0 Merge pull request #422 from strongloop/feature/fix-mongodb-issue-87
Remove undefined properties for create
2015-02-04 19:11:02 +01:00
Miroslav Bajtoš 7a7fcb2e34 test: undefined property values are preserved 2015-02-04 09:08:28 +01:00
Miroslav Bajtoš 3593127ef3 Update to `should` to the latest version 4.6.3 2015-02-03 11:44:15 +01:00
Raymond Feng 1f0f4b0f02 Relax the id equality test for mongodb object ids 2015-02-02 10:51:41 -08:00
Miroslav Bajtoš 9298919ec0 Merge pull request #418 from strongloop/feature/rename-query-to-access-hook
Rename hook "query" to "access"
2015-02-02 19:04:47 +01:00
Raymond Feng 4793546353 Merge pull request #400 from chrene/protected
Add support for protected properties.
2015-02-02 09:04:51 -08:00
Raymond Feng bba1ce0768 Merge pull request #405 from clarkorz/fix/id-for-compositeIds
fix id property for composite ids
2015-02-02 08:59:40 -08:00
Raymond Feng fd97b06bcc Merge pull request #416 from strongloop/feature/fix-lb-1058
Fix id type issue for update
2015-02-02 08:58:10 -08:00