Commit Graph

698 Commits

Author SHA1 Message Date
Amir Jafarian eb8f398c6a Docuemtation for `replace*` methods 2016-05-20 16:29:25 -04:00
Amir Jafarian 5c3d021fe7 Make the doc clear for `findORCreate` cb 2016-05-17 17:50:13 -04:00
David Cheung e723d8b641 Throw error upon extending unknown model
Create Model now uses findModel to retrieve base instead of getModel
and throws error upon base model not found
2016-05-10 18:00:56 -04:00
Ryan Graham 6964914bab
update copyright statements 2016-05-03 15:50:21 -07:00
Miroslav Bajtoš 4d7154a31a app: send port:0 instead of port:undefined
Node v6 no longer supports port:undefined, this commit is fixing
app.listen() to correctly send port:0 when no port is specified.
2016-05-03 16:20:07 +02:00
Miroslav Bajtoš 87bbf4502a Remove "loopback.autoAttach()"
The method was deprecated since LoopBack 2.0, there is no need to keep
it around in 3.0.
2016-05-03 14:01:39 +02:00
Simon Ho d16c789638 Fixes for emit `remoteMethodDisabled` PR
See https://github.com/strongloop/loopback/pull/2266#issuecomment-215689358
2016-04-29 16:42:13 -07:00
Supasate Choochaisri 0ca24389b2 Add new feature to emit a `remoteMethodDisabled` event when disabling a remote method.
Signed-off-by: Supasate Choochaisri <supasate.c@gmail.com>
2016-04-28 09:03:54 +07:00
Tim Needham 159aaf5afa Fix typo in Model.nestRemoting
Prevent apps from crashing when using `Model.nestRemoting` without
`{ hooks: false }` option.

Note that it is not possible to reproduce this bug using our current
Mocha test suite, because other tests modify the global state in such
way that the bug no longer occurs.
2016-04-19 16:51:52 +02:00
Rand McKinney 58f32dd70e Update loopback.js
Update comment line that was creating invalid info in API docs.
2016-04-18 09:42:56 -07:00
Rand McKinney 88d41f84fd Merge pull request #2155 from sghung/master
Fix inconsistencies in JSDoc
2016-04-06 16:01:31 -07:00
Miroslav Bajtoš f9702b0ace Use eslint with loopback config
Drop jshint and jscs in favour of eslint.

Fix style violations.

While we are at this, reduce the max line length from 150 to 100.
2016-04-06 10:45:30 +02:00
Jue Hou eb09681f21 promise docs
Add promise jsdoc in loopback
2016-04-04 12:35:35 -04:00
sghung@ca.ibm.com d69ab1e411 Update JSDoc 2016-04-01 14:00:01 -04:00
Candy b5b900e0ff Remove constraint making isStatic required 2016-03-30 11:16:32 -04:00
Simon Ho b4e230389d Merge pull request #1780 from linguofeng/patch-1
Update persisted-model.js
2016-03-22 16:10:22 -07:00
sghung@ca.ibm.com 3302391ac7 Fix inconsistencies in JSDoc 2016-03-17 23:55:12 -04:00
Miroslav Bajtoš 93d487ffc0 Improve error message on connector init error 2016-02-26 14:46:26 +01:00
Sam Roberts b9acace932 application: correct spelling of "cannont" 2016-02-18 20:36:07 -08:00
Miroslav Bajtoš 804c71d7c6 Remove "loopback.DataModel"
The model was just a temporary alias to simplify migration of code
based on <=2.0.0-beta3
2016-01-25 10:17:45 +01:00
Chris Coggburn 7252c7686c Correct JSDoc findOrCreate() callback in PersistedModel
Update PersistedModel.findOrCreate() JSDoc to reflect the callback accepts an additional created boolean parameter.
2016-01-21 20:40:19 -07:00
Amir Jafarian 08a2786b04 Checkpoint speedup 2016-01-09 01:56:13 -05:00
Jue Hou 889c561ed3 Always use bluebird as promise library
Replace `global.Promise` with `bluebird`
2016-01-08 13:58:12 -05:00
Simon Ho e8179e119d Fix failing tests
JSCS is detecting improper whitespaces lib/persisted-model.js.
2015-12-31 14:04:09 -08:00
Rand McKinney 1af03613cc Update persisted-model.js
- Correct doc for findOrCreate.
- Fix indentation
- Remove quotes in example `where`
- Fix typo
- Fix spacing in notional where clause
2015-12-22 17:08:19 -08:00
Amir Jafarian 4aac2776a8 Fix bulkUpdate to not trigger rectifyAll
Fix `getIdFromWhereByModelId()` to correctly detect the situation
when "bulkUpdate" performs a write operation using a where filter
containing both id attribute but also all other model attributes.

This should significantly improve the performance of change replication,
because the cost of running rectifyAll is very high.
2015-12-22 10:44:28 +01:00
Miroslav Bajtoš 5e4bf1dd25 PersistedModel: log rectify/rectifyAll triggers
Add debug logging to identify what operations trigger rectify and
rectifyAll calls.
2015-12-17 14:13:47 +01:00
Rand McKinney 17abd8ac76 Update model.js
*Add doc comments for nestRemoting
*change cb to filterCallback in nestRemoting
2015-12-04 19:24:03 -05:00
Richard Pringle 03c8d044ad Fix typo in description of persistedModel.updateAttributes() 2015-11-11 16:54:21 -05:00
Miroslav Bajtoš 36cd5a7a78 lib/registry: fix findModel for model ctor
Fix `registry.findModel(arg)` to support the case when `arg` is already
a model constructor.
2015-11-09 16:01:43 +01:00
linguofeng 4717fb3dbd Update persisted-model.js
`find` to `findById`
2015-10-28 12:48:36 +08:00
Rand McKinney 32085475ed Fix typo in doc comment 2015-10-21 13:47:36 -07:00
Simon Ho 26af1472e7 Check configs for shared method settings 2015-09-28 16:27:48 -07:00
Miroslav Bajtoš c87bf9a1da Merge pull request #1681 from strongloop/fix/replication-perf-on-save
Fix perf of rectification after updateAttributes
2015-09-18 17:05:14 +02:00
Miroslav Bajtoš 89326c7630 Fix perf of rectification after updateAttributes
Improve the id-detection algorithm in the "after save" hook
to correctly handle "updateAttributes" as a single-model change
and DO NOT trigger full "rectify all" scan.
2015-09-17 19:23:00 +02:00
Rand McKinney 8d02e42cf3 Update persisted-model.js
Remove trailing whitespace.
2015-09-09 16:08:20 -07:00
Rand McKinney 3821824c97 Update persisted-model.js
Fix links in API docs to docs for where filter
2015-09-09 15:46:53 -07:00
Richard Walker 65ca318392 Create stack-removing errorhandler middleware
Create a wrapper around express' errorhandler that removes the stack
trace from error responses when "options.includeStack" is false.
2015-09-03 08:38:24 +02:00
Miroslav Bajtoš bab1d5dae8 Merge pull request #1632 from gausie/patch-2
Allow EJS templates to use includes
2015-08-31 18:15:16 +02:00
Samuel Gaus ecc98fd29c Allow EJS templates to use includes
Simple change that allows templates to use includes.
2015-08-27 16:37:04 +01:00
Christoph c2eb8e754d fix typo "PeristedModel" 2015-08-14 01:49:27 +02:00
Pradnya Baviskar 64a1dbadc8 Promisify 'PersistedModel - replication' 2015-08-12 09:22:53 +02:00
Raymond Feng c44a19dc73 Allow methods filter for middleware config 2015-08-05 10:30:57 -07:00
Miroslav Bajtoš 98784e2b4b Don't load Bluebird for createPromiseCallback
The decision which Promise implementation to use should be made by
LoopBack user, not by the framework.

This commit moves Bluebird reference from lib/utils.js to
test/support.js.
2015-08-04 11:04:12 +02:00
Miroslav Bajtoš aa5c9e3628 Merge pull request #1493 from PradnyaBaviskar/issue418-userModel
Promisify User model
2015-08-04 10:32:44 +02:00
Raymond Feng 0f9dd93aba Fix jsdocs for methods with where argument 2015-07-28 08:29:43 -07:00
Pradnya Baviskar dc987a59a9 Promisify User model 2015-07-14 13:01:46 +05:30
Ritchie Martori 9422175510 Add link to createChangeStream docs 2015-07-13 09:40:22 -07:00
Ritchie Martori cdddb08a67 Merge pull request #1445 from strongloop/feature/subscribe
Add PersistedModel.subscribe()
2015-07-09 13:35:49 -07:00
Ritchie Martori 40c5707a36 Add PersistedModel.createChangeStream() 2015-07-09 13:34:01 -07:00
Ritchie Martori 503eb948b7 Remove trailing whitespace from jsdoc 2015-07-08 16:53:51 -07:00
Rand McKinney ae7c12552a Update model.js
Updated JSdoc for remoteMethod
2015-07-07 11:01:50 -07:00
Miroslav Bajtoš 3115e5055f Auto-configure models required by `app.enableAuth`
Modify `app.enableAuth` to automaticaly setup all required models
that are not attached to the app nor a datasource.

Users wishing to use this option must provide the name of the
data-source to use for these models.

Example usage:

    var app = loopback();
    app.dataSource('db', { connector: 'memory' });
    app.enableAuth({ dataSource: 'db' });

    app.use(loopback.rest());
    app.listen(3000);
2015-06-01 12:19:28 +02:00
Miroslav Bajtoš 0ccc1e2b73 Add loadBuiltinModels flag to loopback(options)
When creating an application with a local registry, the default
behaviour is to define only two core models Model & PersistedModel.

The new flag `loadBuiltinModels` modifies this behaviour and instructs
loopback to define all builtin models in the local registry too.
2015-06-01 12:19:28 +02:00
Raymond Feng 77bcc09107 Merge branch 'master' of https://github.com/esco/loopback into esco-master 2015-05-29 16:44:18 -07:00
Ritchie Martori 805e0e0c1c Merge pull request #1370 from strongloop/fix/check-access-ordering
Use the new remoting.authorization hook for check access
2015-05-12 16:06:47 -07:00
Ritchie Martori 9cde2a45be Use the new remoting.authorization hook for check access 2015-05-12 15:33:05 -07:00
Miroslav Bajtoš f93b69ed0a Define remote methods via model settings/config
Process `settings.methods` and `config.methods` as a key-value map
where the key is the method name and the value is an object describing
the method in the format expected by strong-remoting.

Example: a static method `Model.create`

    "methods": {
      "create": {
        "isStatic": true,
        "accepts": {
           "arg": "data", "type": "Car",
           "http": { "source": "body" }
        },
        "returns": { "arg": "data", "type": "Car", "root": true }
      }
    }

This patch is based on the code proposed by @mrfelton in #1163.
2015-05-07 07:42:08 +02:00
Rand McKinney ce26141d85 un-document _findLayerByHandler 2015-05-04 12:23:21 -07:00
Miroslav Bajtoš cad616ed22 Remove trailing whitespace added by 242bcec 2015-05-04 18:19:13 +02:00
Rand McKinney 242bcec539 Update model.js
Add `extend()` method to API docs.
2015-04-28 10:06:14 -07:00
Miroslav Bajtoš b6b76d538c Fix regression in Model.getApp()
This patch fixes the method Model.getApp() that started to report
the following error after b61fae5 was landed:

assert.js:88
  throw new assert.AssertionError({
        ^
AssertionError: undefined == true
    at loopback/loopback/lib/model.js:392:7
    at EventEmitter.<anonymous> (loopback/loopback/lib/model.js:222:9)
    at EventEmitter.g (events.js:257:16)
    at emitOne (events.js:77:13)
    at EventEmitter.emit (events.js:166:7)
    at EventEmitter.app.model (loopback/loopback/lib/application.js:157:9)
2015-04-28 12:49:57 +02:00
Raymond Feng f798882e9a Allow dataSource === false 2015-04-24 08:32:17 -07:00
Miroslav Bajtoš f130b7aa01 Rework global registry to be per-module-instance
Fix a regression introduced by b917075 where two loopback projects,
each one using a different instance of loopback module, were
sharing the global registry and thus not working correctly.

The issue was discovered by unit-tests in loopback-workspace.
2015-04-17 19:26:05 +02:00
Miroslav Bajtoš 4d0a824757 Add back loopback properties like modelBuilder
The commit b917075 accidentally removed a couple of properties,
this commit is bringing them back:

 - loopback.modelRegistry
 - loopback.defaultDataSources

A unit-test was added to prevent this kind of regressions in the future.
2015-04-17 18:05:25 +02:00
Raymond Feng 3dcde7994b Merge pull request #1306 from strongloop/feature/allow-filter-on-findById
Expose the `filter` argument for findById
2015-04-16 15:12:50 -07:00
Raymond Feng 2bf69a1171 Expose the `filter` argument for findById 2015-04-16 15:02:41 -07:00
Edmond Lau ef7c1439b6 fixed the missing '.' in various description fields. 2015-04-15 09:47:38 -04:00
Miroslav Bajtoš bd973def63 Merge pull request #1294 from strongloop/feature/conflict-resolution-access-control
Conflict resolution and Access control
2015-04-14 09:36:40 +02:00
Miroslav Bajtoš cf2acb3cd2 Conflict resolution and Access control
Add end-to-end unit-tests verifying enforcement of access control during
conflict resolution.

Implement two facade methods providing REST API for Change methods used
by conflict resolution:

    PersistedModel.findLastChange
    GET /api/{model.pluralName}/{id}/changes/last

    PersistedModel.updateLastChange
    PUT /api/{model.pluralName}/{id}/changes/last

By providing these two methods on PersistedModel, replication users
don't have to expose the Change model via the REST API. What's even
more important, these two methods use the same set of ACL rules
as other (regular) PersistedModel methods.

Rework `Conflict.prototype.changes()` and `Conflict.prototype.resolve()`
to use these new facade methods.

Implement a new method `Conflict.prototype.swapParties()` that provides
better API for the situation when a conflict detected in Remote->Local
replication should be resolved locally (i.e. in the replication target).
2015-04-14 08:23:24 +02:00
Raymond Feng b4c487b191 Fix the typo 2015-04-10 11:03:55 -07:00
Miroslav Bajtoš 28acffd7dd Fix PersistedModel._defineChangeModel
Correctly handle the case when the model is attached multiple times
during the lifecycle, this happens because `loopback.createModel`
always makes an attempt to auto-attach.
2015-04-08 11:55:03 +02:00
Miroslav Bajtoš 9c5fe088e3 AccessControl for change replication
1) Add integration tests running change replication over REST to verify
that access control at model level is correctly enforced.

2) Implement a new access type "REPLICATE" that allows principals
to create new checkpoints, even though they don't have full WRITE
access to the model. Together with the "READ" permission, these
two types allow principals to replicate (pull) changes from the server.

Note that anybody having "WRITE" access type is automatically
granted "REPLICATE" type too.

3) Add a new model option "enableRemoteReplication" that exposes
replication methods via strong remoting, but does not configure
change rectification. This option should be used the clients
when setting up Remote models attached to the server via the remoting
connector.
2015-04-07 19:53:58 +02:00
Miroslav Bajtoš b61fae58f6 Merge pull request #1272 from strongloop/feature/after-remote-error-hook
Model.afterRemoteError hook
2015-04-07 09:47:51 +02:00
Ritchie Martori b9170751bc Add support for app level Model isolation
- `loopback.registry` is now a true global registry
 - `app.registry` is unique per app object
 - `Model.registry` is set when a Model is created using any registry method
 - `loopback.localRegistry` and `loopback({localRegistry: true})` when set to `true` this will create a `Registry` per `Application`. It defaults to `false`.
2015-04-03 11:48:45 -07:00
Miroslav Bajtoš dd83be99f0 Implement ModelCtor.afterRemoteError 2015-04-03 10:31:03 +02:00
Miroslav Bajtoš a71c8253e2 Code cleanup, add Model._runWhenAttachedToApp 2015-04-03 10:06:49 +02:00
Miroslav Bajtoš c72c134d80 Refactor Model and PersistedModel registration
Modify the files to export a model factory function accepting
a `registry` argument. This is a preparation step for per-application
models - see #1212.
2015-04-03 09:26:19 +02:00
Miroslav Bajtoš 8493ede19e Add missing error handlers to checkpoints() 2015-04-02 14:14:32 +02:00
Rand McKinney 8a1fe0b744 Fix where param format 2015-04-01 11:29:35 -07:00
Miroslav Bajtoš 63e2f4b134 Improve error handling in replication
Deprecate `Change.handleError`, it was used inconsistenly for a subset
of possible errors only. Rework all `Change` methods to always report
all errors to the caller via the callback.

Rework `PersistedModel` to report change-tracking errors via the
existing method `PersistedModel.handleChangeError`. This method
can be customized on a per-model basis to provide different error
handling.

The default implementation emits `error` event on the model class,
users can attach an event listener that can provide a custom error
handler.

NOTE: Unhandled `error` events crash the application by default.
2015-03-30 11:07:53 +02:00
Miroslav Bajtoš 2aa09ba574 Add `loopback.runInContext`
Refactor the core implementation of current context from
server/middleware/context.js into server/current-context.js.

Expose new public API:
 - loopback.runInContext
 - loopback.createContext
2015-03-27 19:12:17 +01:00
Miroslav Bajtoš ade5d5ea21 Merge pull request #1246 from strongloop/fix/browser-bundle-size
Import subset of underscore.string scripts only
2015-03-27 08:51:59 +01:00
Miroslav Bajtoš 2ca621e597 Document the new third callback arg of replicate() 2015-03-26 19:10:00 +01:00
Miroslav Bajtoš caf53f72c1 Fix API doc for updateAll/deleteAll
Based on changes made in
  https://github.com/strongloop/loopback/issues/1167
  https://github.com/strongloop/loopback-datasource-juggler/pull/540
2015-03-26 19:06:56 +01:00
Esco Obong 1993338c0b Merge branch 'master' of https://github.com/strongloop/loopback 2015-03-25 16:45:58 -04:00
Miroslav Bajtoš 7528cbb712 Import subset of underscore.string scripts only
Require individual methods like `classify` instead of the whole module.
This reduces the size of the browser bundle from ~27kb down to ~2kb.
2015-03-25 14:02:16 +01:00
Miroslav Bajtoš cadb5e4524 Use `ctx.instance` provided by "after delete" hook
Use the recently added context property `ctx.instance` to improve
the accuracy of the algorithm detecting whether a single or
multiple models were deleted.
2015-03-23 11:49:47 +01:00
Miroslav Bajtoš 87940a4b58 Detect 3rd-party changes made during replication
Modify `Change.diff()` to include current data revision in each
delta reported back. The current data revision is stored in
`delta.prev`.

Modify `PersistedModel.bulkUpdate()` to check that the current data
revision matches `delta.prev` and report a conflict if a third party
has modified the database under our hands.

Fix `Change` implementation and tests so that they are no longer
attempting to create instances with duplicate ids.
(This used to work because the memory connector was silently
converting such requests to updateOrCreate/findOrCreate.)
2015-03-20 08:19:59 +01:00
Esco Obong 8cc558a991 consolidate Role methods roles, applications, and users into one, add query param to allow for pagination and restricting fields 2015-03-13 11:50:30 -04:00
Miroslav Bajtoš 76d9244448 Prevent more kinds of false replication conflicts
Rework the Change model to merge changes made within the same
Checkpoint.

Rework `replicate()` to run multiple iteration until there were no
changes replicated. This ensures that the target model is left in
a clean state with no pending changes associated with the latest
(current) checkpoint.
2015-03-05 14:15:02 +01:00
Miroslav Bajtoš b381c5df7e Add more debug logs to replication 2015-03-04 15:00:53 +01:00
Miroslav Bajtoš 628e3a30ca Return new checkpoints in callback of replicate()
Extend `PersistedModel.replicate` to pass the newly created checkpoints
as the third callback argument.

The typical usage of these values is to pass them as the `since`
argument of the next `replicate()` call.

    global.since = -1;

    function sync(cb) {
      LocalModel.replicate(
        since,
        RemoteModel,
        function(err, conflicts, cps)
          if (err) return cb(err);
          if (!conflicts.length) {
            since = cps;
            return cb();
          }
          // resolve conflicts and try again
        });
    }
2015-03-03 19:37:11 +01:00
Miroslav Bajtoš 53ebddfa9f Create a remote checkpoint during replication too
Before this change, in the case of a one-way replication, the remote
checkpoint was never updated, thus the "diff" step had to check
all changes made through the whole history.

This commit fixes the problem by creating a new remote checkpoint
at the same time when a local checkpoint is created.

It is important to create the new checkpoint before the replication is
started to prevent a race condition where a remote change can end up
being associated with an already replicated checkpoint.
2015-03-03 19:37:11 +01:00
Miroslav Bajtoš 2dc230b7cf Replication: fix checkpoint-related race condition
Rework the "replicate()" to create a new source checkpoint as the first
step, so that any changes made in parallel will be associated with
the next checkpoint.

Before this commit, there was a race condition where a change might
end up being associated with the already-replicated checkpoint and thus
not picked up by the next replication run.
2015-03-03 19:37:11 +01:00
Miroslav Bajtoš 2f9cf115c3 Support different "since" for source and target
Modify `PersistedModel.replicate` to allow consumers to provide
different "since" values to be used for the local and remote changes.

Make the "since" filters consistent and include the "since" value
in the range via `gte`. Before this commit, the local query used
`gt` and the remote query used `gte`.
2015-03-03 14:40:59 +01:00
Miroslav Bajtoš 610767b6c8 Change tracking requires a string id set to GUID
Print a deprecation warning when a persisted model is tracking changes
but does not have a client-generated unique string id property (GUID).
2015-03-02 18:16:18 +01:00
Raymond Feng 9a0267e87a Add a workaround to avoid conflicts with NewRelic 2015-02-25 09:47:18 -08:00
Miroslav Bajtoš 3d977f3e68 Merge pull request #1116 from strongloop/fix/change-detection
Fix change detection & tracking
2015-02-24 08:24:27 +01:00
crandmck a82b33ec5c Add docs for settings per #1069 2015-02-23 13:13:52 -08:00