Commit Graph

1480 Commits

Author SHA1 Message Date
ebarault cfd3cdf535 Overall review of polymorphic relations
In #1298, the spec/doc for polymorphic relations was reviewed

**hasX relation**
- `type`: **hasMany**
- `as`: redefines **this** relation's name (optional)
- `model`: **modelTo**
- `polymorphic`:
  - typeOf `polymorphic` === `String`
    - matching **belongsTo** relation name
      - `foreignKey` is generated as `polymorphic + 'Id'`,
      - `discriminator` is generated as `polymorphic + 'Type'`
  - typeOf `polymorphic` === `Object`
    - `as`: **DEPRECATED** should display a warning,
            replaced by `selector`
    - `selector`: should match **belongsTo** relation name if the
                  latter is defined with {polymorphic: true}
      - (required) if both foreignKey and discriminator
        are **NOT** provided
      - (extraneous) if both foreignKey and discriminator
        are provided
    - `foreignKey`:  A property of modelTo, representing the fk to
       modelFrom's id.
      - generated by default as `selector + 'Id'`
    - `discriminator`: A property of modelTo, representing the actual
                       modelFrom to be looked up and defined
                       dynamically
      - generated by default as `selector + 'Type'`

---

**belongsTo relation**
- `type`: **belongsTo**
- `as`: redefines **this** relation's name (optional)
- `model`: **NOT EXPECTED**: should throw an error at
               relation validation
- `polymorphic`:
  - typeOf `polymorphic` === `Boolean`
      - `foreignKey` is generated as `relationName + 'Id'`,
      - `discriminator` is generated as `relationName + 'Type'`
  - typeOf `polymorphic` === `Object`
    - `as`: **DEPRECATED**: should display a warning,
            replaced by `selector`
    - `selector`:
      - (required) if both foreignKey and discriminator
        are **NOT** provided
      - (extraneous) if both foreignKey and discriminator
        are provided
    - `foreignKey`: A property of modelTo, representing the fk to
                    modelFrom's id.
      - generated by default as `selector + 'Id'`
    - `discriminator`: A property of modelTo, representing the actual
                       modelFrom to be looked up and defined
                       dynamically
      - generated by default as `selector + 'Type'`
2017-05-12 21:33:39 +02:00
rashmihunt 52f8fb3416 handle excludeBaseProperties 2017-05-10 12:03:42 -07:00
ebarault 79068d142b configurable model merge
The PR superseeds the existing deepMerge algorithm used to merge
settings of parent and child models with a new algorithm that allows
to specify the way each setting is merged or mixed-in.

This configuration of this algorithm uses a merge policy specification.
The `getMergePolicy()` helper of BaseModelClass can be used to ease
model merge configuration.

Next is presented the expected merge behaviour for each option.
NOTE: This applies to top-level settings properties

- Any
  - `{replace: true}` (default): child replaces the value from parent
  - assignin `null` on child setting deletes the inherited setting

- Arrays
  - `{replace: false}`: unique elements of parent and child cumulate
  - `{rank: true}` adds the model inheritance rank to array
    elements of type Object {} as internal property `__rank`

- Object {}:
  - `{replace: false}`: deep merges parent and child objects
  - `{patch: true}`: child replaces inner properties from parent

The recommended merge policy is returned by getMergePolicy()
when calling the method with option `{configureModelMerge: true}`.

The legacy built-in merge policy is returned by `getMergePolicy()`
when avoiding option `configureModelMerge`.
NOTE: it also delivers ACLs ranking in addition to the legacy
behaviour as well as fixes for settings `description` and `relations`

`getMergePolicy()` can be customized using model's setting
`configureModelMerge` as follows:
```
{
  // ..
  options: {
    configureModelMerge: {
      // merge options
    }
  }
  // ..
}
```

`getMergePolicy()` method can also be extended programmatically as
follows:
```
myModel.getMergePolicy = function(options) {
  const origin = myModel.base.getMergePolicy(options);
  return Object.assign({}, origin, {
    // new/overriding options
  });
};
```
2017-05-10 02:33:45 +02:00
Loay e3ce44a286 Merge pull request #818 from fullcube/fullcube/validatesUniquenessOf-case-insensitive
Fullcube/validates uniqueness of case insensitive
2017-05-05 10:41:42 -04:00
Loay 87dd44dd59 Fix assertion errors 2017-05-04 10:38:06 -04:00
Loay 84b55d8160 Update modelbaseclass api docs 2017-05-03 21:03:36 -04:00
Bram Borggreve c3629633de
Add caseInsensitive opt to validatesUniquenessOf 2017-05-01 17:26:16 -05:00
Kevin Delisle 5e80837424 Add DateString type
New type that preserves string input as a string, but ensures that
the string is a valid Date.

Additionally, provides a .toDate function to provide the Date
object representation of the string.
2017-05-01 12:28:59 -04:00
Loay ef61c8618a Update api documents 2017-04-27 16:28:43 -04:00
Kevin Delisle f7fe187120 Datasource documentation tune-up 2017-04-25 15:51:42 -04:00
paulussup e9ff88f453 Fix/geo null (#1334)
* fix check for null

* add tests

* fix for early return

* Allow check for null and non-existent value

Some connectors uses a non existent prop instead of allowing null
Modified test case to look if null exists or the prop is non existent

* Check for null value with geo near query

* Apply requested changes

* change test to two users and simplify

* check error first

* Fix simple query test case with null value

* BDD for connectors w//o null support
2017-04-24 19:28:11 -04:00
Diana Lau a488a71dbd replace exception thrown for invalid dates 2017-04-21 15:57:11 -04:00
Loay f1d10b47ce Fix forceId bug for updateOrCreate 2017-04-18 13:14:35 -04:00
Eric Barault 64f64caaff handle deep geo-near queries (#1314)
* handle deep geo-near queries (#1216)

    
a dedicated mongKey is added in geo.nearFilter for mongoDB
fixes geo min distance tests as filter now expects an array

* Fix for string geoPoints

* Add geo point handle for ibmdb connectors

* Handle geo-point type for cassandra connector
2017-04-13 17:05:05 -04:00
Kevin Delisle 00fe8fd59e Merge pull request #1313 from strongloop/revert-1216-fix/geo.nearFilter-deep
Revert ""Near" fix for where clauses"
2017-04-06 18:29:48 -04:00
Sakib Hasan befea83a76 Revert "handle deep geo-near queries (#1216)"
This reverts commit 6f88cf1930.
2017-04-06 17:45:04 -04:00
Joshua Chaitin-Pollak bb3812fbfb Allow `after save` hook to see count of records changed (#1231)
* `after save` hook allows count of records changed

* Fix PR linter
2017-04-06 12:02:34 -04:00
Corentin H 6f88cf1930 handle deep geo-near queries (#1216)
a dedicated mongKey is added in geo.nearFilter for mongoDB
fixes geo min distance tests as filter now expects an array
2017-04-06 08:25:04 -04:00
destillat fc2f66c514 Fix model def column name method (#1224) 2017-04-05 15:12:27 -04:00
Jonathan Sheely 21b08af7a3 Added notify flag for create and upsert (#1277)
* Added notify flag for create and upsert

* Code styling

* More code styling

* Added unit test for notify

* Fix PR linter
2017-04-05 13:23:26 -04:00
Waldemar Zahn ef143dc5eb Custom Table Names on rels (#1303)
hasAndBelongsToMany relation
2017-04-05 12:42:21 -04:00
Sakib Hasan 6a962583b0 Support multiple fk relations (#1308) 2017-04-04 18:23:43 -04:00
Thaer Abbas 57ead01624 #1261 Property name "constructor" is not allowed in 'Model' data (#1284)
* check if data has a constructor prop

* test data with no constructor

* test non function constructor

* test non function ctor message

* cleanup
2017-04-04 13:35:24 -04:00
Raymond Feng 1f995ec674 Use dataSource.connect to avoid duplicate connects 2017-04-04 10:19:58 -07:00
Raymond Feng 0a408476ec Fix in-mem connector file operation racing condition 2017-04-04 08:25:15 -07:00
Vincent Wen 8c93710963 make geo nearFilter support minDistance (#987) 2017-03-26 10:04:16 -04:00
Mikhail 68b93e1074 Disallow regexp string in arrays for coerce (#1279)
Disallow regexp arrays for coerce
2017-03-22 20:29:01 -04:00
Clark Wang c99441247c Fix - `_targetClass` on scope function (#1280)
Fix `_targetClass` on scope function when using hasManyThrough
relation with customized relation names and foreignKey/keyThrough.

This bug is cause by `_targetClass` uses the camel-case of
`relationName`(e.g.: if `relationName` is `bbb`, `targetClass`
would be `Bbb`), which is not exists.

This will also suppress "not exposed" warnings when generating
angular sdk, and generate end-points for this scope.
2017-03-22 17:06:36 -04:00
Nick Oikonomou b3a7bc521d Fixes #1275. `Include` filter transforms fields property into array. (#1276)
* Fixes#1275 Transform *fields* property into array

`Include` filter takes into consideration string property
'fields' and transforms it into an array containing this string.

* Added error handling for `include` filter.

* ExecTasksWithInterLeave now contains a try-catch block
 in order to catch any unexpected errors.

* LinkManyToMany now checks if *modelToIdName* exists on
 *target* before continuing.

* Added unit test for *include* with string fields
2017-03-22 10:03:36 -04:00
Dimitris 77c4cd7b01 Included models from include operations do not change defined `strict` model option (#1259)
* Fixes #1252

* Allowed setting of dynamic relation property

* Fixed tests to also consider other connectors
2017-03-20 19:48:57 -04:00
Dimitris 2bfc769c4d Using a filter with exclusion of a non existent property, removes an existing one (#1257)
* #fixes 1256

* Added missing check for returned error

* Fixed test to check proper fields in each doc
2017-03-20 19:18:48 -04:00
Sakib Hasan 3c7e398f6f Clean version of PR 1272 (#1273)
Able to filter out the properties given to a model instead
Of throwing an error
2017-03-20 18:10:01 -04:00
Diana Lau eb2cb315e6 FindOrCreate missing error callback 2017-03-20 10:32:09 -04:00
Dimitris 6f8ecfdf5c Fixes #1230 coerceArray converts empty Objects (#1269) 2017-03-20 10:05:11 -04:00
Kevin Delisle 248aaf0541 dao: catch errors on Model creation in find 2017-03-16 17:26:53 -04:00
Kevin Delisle a6c5802940 dao: catch sync errors on setAttributes
Wrap inst.setAttributes in try-catch to prevent synchronous errors
from crashing the application.
2017-03-16 17:26:53 -04:00
Loay 46318d2094 Update error message 2017-03-16 11:02:08 -04:00
jannyHou 7373e1ae31 Doc:Add option for discoverModelDefinitions 2017-03-14 21:51:30 -04:00
Rémi Bèges 11c2a19ad2 Add tests for validatesExclusionOf (#1248) 2017-03-08 10:24:18 -05:00
Rémi Bèges 9afaa9a66e Fix id update error message formatting
Error is raised when trying to update an id property while forceId
is set to true
2017-02-15 18:26:32 +01:00
Loay 9541bb5f15 Add proper statusCode for duplicate 2017-02-08 23:52:14 -05:00
Miroslav Bajtoš e8a977f55b Fix datasource to report connector-loading errors
Before this change, when resolving full connector path, all errors were
ignored. As a result, when the connector was installed but not
correctly built (e.g. loopback-connector-db2 which uses a native addon),
a very confusing message was reported by LoopBack.

In this commit, I am fixing the code handling `require()` errors
to ignore only MODULE_NOT_FOUND errors that contain the name
of the required module.
2017-02-03 20:29:11 +01:00
Miroslav Bajtoš d809260799 Upgrade eslint-config, fix new violations 2017-02-01 12:45:56 +01:00
Miroslav Bajtoš 121afda79b Fix option propagation in relation methods
- EmbedsMany.prototype.remove
 - EmbedsMany.prototype.add
2017-01-31 16:10:56 +01:00
Matteo Padovano 733ad1a024 Refactor logic of options.allowExtendedOperators
Implement logic to allowExtendedOperators options
per request, per Model and Datasource.
2017-01-30 00:05:56 +01:00
Loay 44a5a7ad81 Fix forceId validation error 2017-01-26 16:52:19 -05:00
Raymond Feng 0c76bd21b9 Upgrade dependencies to remove npm install warnings 2017-01-17 09:45:51 -08:00
Heath Morrison 2377792c22 Coerce array-like objects into arrays
The query-string parser used by express
https://github.com/ljharb/qs#parsing-arrays
limits the size of arrays that are created from query strings to 20
items. Arrays larger than that are converted to objects using numeric
indices.

This commit fixes the coercion algorithm used by queries to
treat number-indexed objects as arrays. We still maintain a strict
understanding of an "array-like object" to limit the opportunity for
subtle bugs. In particular, the presence of non-index keys is an
indication that the object was not intended to be interpreted as
an array.
2017-01-10 14:46:41 +01:00
Simon Ho 2320df1227 Refactor flush to deleteAll
- Rename `flush` to `deleteAll`
- Add `delete`
- Detect `delete/deleteAll` before running downstream test suites
- Fall back to unoptimized `deleteAll` when connector does not support
  `deleteAll` but supports `delete`
- Return 501 for connectors not supporting `delete` or `deleteAll`
2017-01-09 14:53:34 -08:00
Miroslav Bajtoš 454bf93aaf Upgrade eslint-config to 7.x
Also upgrade eslint itself to 3.x.
2017-01-06 13:33:54 +01:00
Brian Schemp cd94be2fb8 Throw error when model relation name is trigger
Defining a model relation with the name "trigger" causes the model not
able to insert records. No error is thrown when a model relation with
the name "trigger" is defined. Adding a check for the model relation
name "trigger" will now throw an error.
2017-01-02 17:09:14 +01:00
Simon Ho f38709b7cb Add flush operation to KVAO
Used to delete all keys (and values) associated to the current model.
2016-12-30 18:06:36 -08:00
Miroslav Bajtoš 0d862890e0 Fix HasOne.update to propagate options arg 2016-12-20 11:40:22 +01:00
Miroslav Bajtoš 779cf6a370 Honour allowExtendedOperators in "DAO.find"
Modify the coercion of filter.where to hounour "allowExtendedOperators"
and don't coerce property values of type object (extended operators).
2016-12-06 10:28:40 +01:00
Miroslav Bajtoš 0f7a567f18 Upgrade eslint & config to latest
- eslint ^3.11.1
 - eslint-config-loopback: ^6.0.0
 - fix linter errors (mostly no-undef)
2016-12-05 15:14:09 +01:00
Amirali Jafarian dd92b8d7e8 Merge pull request #1175 from BostjanPisler/patch-1
Fixed example for creating ValidationError
2016-12-03 10:59:12 -05:00
jannyHou 6c8e806bc8 Apply hasManyThrough filter on target model 2016-12-01 10:08:27 -05:00
Boštjan Pišler 93a739d612 Fixed example for creating ValidationError 2016-11-29 16:04:53 +01:00
Amir Jafarian 54d0f5b53d Add support for `loaded` hook
* Add support for loaded hook for DAO.find with near filter
2016-10-28 11:46:46 -04:00
Heath Morrison 266e9f5a9a Continue _coerce after logical operators 2016-10-25 23:09:46 +03:00
CerealGuy 662458dc2c Fix validateNumericality, nullCheck & add tests
validateNumericality didn't test if attributes value is a number
only if it's type is number.
Further nullCheck had a wrong testing order. It first checked if
value is null, later if blank. Also null check only used two equals,
not three. We don't use blank() anymore, testing if variable is
undefined should be fine too.
Added tests covering validateNumericality.
2016-10-15 10:59:22 +02:00
Nick Duffy a23076d3a8 Add ilike and nilike operators
Closes #633
2016-10-12 18:31:31 -06:00
Amir Jafarian e42b1327aa Fix JSDoc issue 2016-10-11 12:15:35 -04:00
Simon Ho b089b0d786 Fix linting errors
Trailing spaces in comments causing linter to fail. Errors introduced
at commit 9a1ef08495.
2016-09-29 19:28:02 -07:00
Rand McKinney 9a1ef08495 Update validations.js
Add doc of allowNull options property per #372.
2016-09-28 16:43:17 -07:00
Tim De Pauw be1d71f626 Support {defaultFn: 'shortid'} 2016-09-22 16:07:34 -07:00
Amirali Jafarian 66f27b9b83 Merge pull request #1094 from strongloop/newInstance_loade_updateAttributes
Add 'isNewInstance' for updateAttributes
2016-09-21 09:23:57 -04:00
David Cheung 373e038301 Merge pull request #1084 from strongloop/strict-mode-cleanup
[SEMVER-MAJOR] Strict mode cleanup
2016-09-20 08:24:31 -04:00
Amir Jafarian fec7742a69 Add 'isNewInstance' for updateAttributes
* Add 'isNewInstance' to loaded hook for updateAttributes
2016-09-19 16:56:15 -04:00
David Cheung 805db78e19 Strict mode now always return validationError
- Deprecation of strict:validate and strict:throw
- When strict mode is enabled, it will now always
return validation error (previous strict:validate)
2016-09-19 10:27:24 -04:00
Simon Ho 6796fac7f1 Add docs for KVAO 2016-09-16 17:03:33 -07:00
pponugo a39c0236bb Support nested queries for arrays
Enhance the built-in memory connector to correctly support nested
queries for arrays in addition to objects.

E.g. if "friends" is an array of objects containing "name", then
{ where: { "friends.name": "Jane" } } should match records containing
a friend called "Jane".
2016-09-07 13:33:57 +02:00
Simon Ho 96cd8ff56b Remove expired item before executing expire
The expire feature is falsely returning 204 instead of 404 because it is
not removing expired items before execution.
2016-09-01 21:18:40 -07:00
Amir Jafarian 2830062259 Disable `strict` for a few files
*Disable `strict` for a few files to disappear warnings
2016-08-26 09:54:31 -04:00
Amir Jafarian c17a490708 Fix CI introduced by `use strict` 2016-08-25 09:49:02 -04:00
Amir Jafarian aec678d252 Fix manually 2016-08-22 19:20:12 -04:00
Amir Jafarian aa6d28268b Auto-update by eslint --fix 2016-08-22 15:12:25 -04:00
Simon Ho 5b46484277 Merge pull request #1056 from strongloop/globalize-kv-mem-connector
Globalize KeyValue Memory connector
2016-08-19 12:58:31 -07:00
Miroslav Bajtoš 3b653a192b kvao: implement key filter 2016-08-18 09:59:56 +02:00
Miroslav Bajtoš 01ce7df60f kvao: add iterateKeys() and keys()
Add a core implementation of KVAO.iterateKeys() which returns an
AsyncIterator, inspired by
 - https://github.com/tc39/proposal-async-iteration
 - https://www.npmjs.com/package/async-iterators

This way we can safely iterate even large sets of data.

Also add KVAO.keys(), a sugar API converting the result of iterateKeys()
into a single array.
2016-08-18 09:59:56 +02:00
Simon Ho 8b28cb3993 Globalize KeyValue Memory connector 2016-08-17 18:22:17 -07:00
Sonali Samantaray 37541dd178 upsertWithWhere feature support in juggler DAO 2016-08-16 18:06:01 +05:30
Simon Ho 1c20cc83aa Add TTL for KeyValue related features 2016-08-11 17:12:36 -07:00
Miroslav Bajtoš b01df10eae Merge pull request #1039 from strongloop/fix/kv-memory-reference-error
kv-memory: fix crash in regular cleanup
2016-08-10 14:18:11 +02:00
Amir-61 1e57d4f316 Merge pull request #1034 from strongloop/err_connector_not_implements_replaceById
Return error if the connector does not implement
2016-08-09 14:01:58 -04:00
Amir-61 954c0df0c6 Merge pull request #1035 from strongloop/globalization_fixup
Fixup globalization
2016-08-09 10:44:06 -04:00
Amir Jafarian 327c785347 Return error if the connector does not implement
* Return error if the connector does not implement `replaceById`
2016-08-09 10:37:15 -04:00
Janny c4b2921977 Merge pull request #982 from strongloop/fix/auto-increment-db
forceId=true with auto-increment db
2016-08-09 10:02:36 -04:00
Miroslav Bajtoš 4978cd8089 kv-memory: fix crash in regular cleanup
Fix bug in "_setupRegularCleanup()" where the interval callback
was trying to access an object that has been garbage-collected
in the meantime.
2016-08-09 15:35:23 +02:00
jannyHou 8935b978f3 forceId=true with auto-increment db 2016-08-08 16:26:26 -04:00
Amir Jafarian d181fd0671 Fixup globalization 2016-08-08 15:34:41 -04:00
Miroslav Bajtoš 9ced20fdcc kvao: return 404 when expiring unknown key 2016-08-08 17:22:33 +02:00
Miroslav Bajtoš f15b4e2c86 Implement KeyValue API and memory connector
Models attached to a KeyValue connector get the following *static*
methods:

    Color.set(key, value);
    Color.set(key, value, ttl);
    Color.set(key, value, { ttl: ttl });

    Color.get(key);

    Color.expire(key, ttl);
2016-08-08 10:15:34 +02:00
Richard Pringle 0b62dd94ea Disallow bulk updateOrCreate. 2016-08-05 11:54:06 +02:00
Amir-61 c2a683bca4 Merge pull request #1007 from horiaradu/master
Optimize related model queries
2016-08-03 16:24:32 -04:00
Amir Jafarian a5429dc026 Update globalization 2016-08-03 14:48:35 -04:00
Amir Jafarian f2f9b4a6f8 Use g.f instead of utils.format 2016-08-03 10:47:40 -04:00
Horia Radu c3f62991c3 Optimize related model queries
No longer query for the related/included model when no results are
obtained in the first query.

fix:
https://github.com/strongloop/loopback-datasource-juggler/issues/1006
2016-08-01 20:23:08 +03:00
Amir Jafarian 4ce5b5d3a0 Support for globalization 2016-07-28 15:20:18 -04:00
Rand McKinney f1ed1ee023 Update include.js
Change doc comments to remove spurious API doc entry per #1008
2016-07-25 08:48:47 -07:00