Skip to content

Instantly share code, notes, and snippets.

@kalyco
Last active February 5, 2018 22:11
Show Gist options
  • Save kalyco/f314f968c66f6f4eb613713ca92acf77 to your computer and use it in GitHub Desktop.
Save kalyco/f314f968c66f6f4eb613713ca92acf77 to your computer and use it in GitHub Desktop.
Loopback 2 --> 3 Guide

Loopback 2.x --> 3.0

1. Package-Lock.json:

Change "loopback-datasource-juggler" to "strong-error-handler": “^1.0.1”,

npm install

2. Update use of REST error handler

npm install —save strong-error-handler

Strong-error-handler — error handler middleware package

Changes needed:

• change error.status to error.statusCode

• change use of loopback#errorhandler shortcut to require(’strong-error-handler')

• remove any use of errorHandler.disableStackTrace

• enable strong-error-handler by adding it to the final:after phase in your middleware config file: server/middleware.json:

...
"final:after": {
  "strong-error-handler": {
    "params": {
      "debug": false,
      "log": true
    }
  }
}  

• In your config.json file, disable remoting.rest.handleErrors. This will instruct Loopback to respect your middleware-based configuration of the error handler:

{
  ...
  "remoting": {
    ...
    "rest": {
      ...
      "handleErrors": false
    }
  }
}

3. Replace LoopBack middleware "getter" properties

loopback.compress --> require('compression')
loopback.cookieParser --> require('cookie-parser')
loopback.cookieSession --> require('cookie-session')
loopback.csrf --> require('csurf')
loopback.directory --> require('serve-index')
loopback.loopback.errorHandler --> require('errorhandler')
loopback.favicon --> require('serve-favicon')
loopback.logger --> require('morgan')
loopback.methodOverride --> require('method-override')
loopback.responseTime --> require('response-time')
loopback.session --> require('express-session')
loopback.timeout --> require('connect-timeout')
loopback.vhost --> require('vhost')

4. Update Models

As of LB 3.0 PUT updates no loner perform a partial update, but replace the model instance as a whole. If you rely on partial updates, you'll need to update it.

{
  "name": "MyModel",
  "replaceOnPut": false,
  ...
}

In the longterm, switch from PUT to PATCH endpoints, as replaceOnPut will likely be removed at some point in the future.

Updated method:

replaceOrCreate --> POST /customers/replaceOrCreate
replaceById --> POST customers/:id/replace
updateOrCreate --> PATCH /customers
prototype.updateAttributes --> PATCH /customers/:id

Remove use of undefined mixins
Applying an undefined mixin to a loopback model now throws an error.

Update remote model definitions
In 3.0 the isStatic property no longer indicates that a remote method is static. Rather, if the method name starts with prototype, then the remote method isan instance method, otherwise (by default) it's a static method.

As a result, you may see many deprecation warnings after upgrading to 3.0, To eliminate:
• For static methods, remove the isStatic: true property • For instance methods, remove isStatic: false and add the prototype. prefix to the method name

Version 2.x:

{
  “methods”: {
    “isStatic”: true,
    “http”: { “path”: “/static” }
  },
  “instanceMethod”: {
    “isStatic”: false,
    “http”: { “path”: “/instance” }
   }
}

Version 3.0:

{
  “methods”: {
    “staticMethod”: {
    “http”: { “path”: “/static” }
  },
  “prototype.instanceMethod”: {
    “http”: { “path”: “/instance” }
   }
}

Note: Methods are typically defined either in:
Model Javascript file: common/models/my-model.json
Model JSON file: common/models/my-model.js
Check both when looking for the source of the deprecation warning.

Remove dots from model property names
Property names containing a dot (customer.name) were deprecated in 2.x. Loopback throws an error for them. Use a different character instead (customer_name etc)

Rename models called "File" Strong remoting has a built-in file type

5. Update models derived from PersistedModel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment