API versioning with versioned models and single database

890 views
Skip to first unread message

Andrew Braini

unread,
Aug 18, 2014, 9:41:48 AM8/18/14
to loopb...@googlegroups.com
Hello,

I'm looking for some guidance on how one could implement "standard" REST versioning (ex: /api/v1/{model} /api/v2/{model}) within loopback.
The models may have different structure but would have the same backing records in the database.  In our instance the database will be Mongo.

Most implementations in express seem to route to endpoints based on the /v{n} with each version implementing it's own endpoints.  Later version methods that do not alter functionality call the earlier version of the method to avoid code duplication.

Since loopback in building routes based on models what is the suggested method for loopback?  Has anyone implemented versioning in loopback?

Thanks!

Andrew B

unread,
Aug 18, 2014, 9:50:12 AM8/18/14
to loopb...@googlegroups.com
Sorry for the multi post however I just found this thread which has part of the answer.

I see an option for making versioned models with the same backing records.
A version 1 public model and version 2 would extend version 1.  However I am unsure of how properties can be excluded from the child models.

Thanks.

Raymond Feng

unread,
Aug 18, 2014, 11:37:57 AM8/18/14
to Andrew B, loopb...@googlegroups.com
1. You are not required to have v2 extend from v1. They can be two different definitions.
2. Technically you can remove base properties using custom code next to the model json, for example, in common/models/ModelV2.js,

module.exports = function(ModelV2) {

  var excludedProperties = [
    'realm',
    'emailVerified',
    'verificationToken',
    'credentials',
    'challenges',
    'lastUpdated'
  ];

  // Remove the properties from base User model that doesn't have mapped columns
  excludedProperties.forEach(function (p) {
    delete ModelV2.definition.rawProperties[p];
    delete ModelV2.definition.properties[p];
    delete ModelV2.prototype[p];
  });
};
 

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrew B

unread,
Aug 18, 2014, 11:49:38 AM8/18/14
to loopb...@googlegroups.com, arbr...@gmail.com
Thank you Raymond,

There is one remaining piece of the puzzle. How can I have both models pull from the same table/collection so that there isn't a duplication of data?

MyModel1 
{id: 1
name : bonkers}

MyModel2
{id: 1
 content : {
  name: bonkers
  descriptions: hilarity
}}

Do I need a private base that includes all fields and the MyModel1/2 transforms the data returned from the base?

Thanks!

Raymond Feng

unread,
Aug 18, 2014, 12:03:35 PM8/18/14
to Andrew B, loopb...@googlegroups.com
It seems that you need a ‘discriminator’ property in the model definition to differentiate the two versions. On top of that, we probably have to implement a feature called ‘defaultScope’ for LoopBack models:

1. You can provide a set of properties with fixed values, in your case, say: version = ‘v1’ for model v1, and version = ‘v2’ for model v2.
2. All CRUD methods will automatically patch the requests with the fixed property values. As a result, ModelV1.find will automatically add version = ‘v1’ to the where statement.
 
Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

Andrew B

unread,
Aug 18, 2014, 1:42:39 PM8/18/14
to loopb...@googlegroups.com, arbr...@gmail.com
I cannot find any documentation on defining/implementing a default scope.

I am also unsure how delineating v1 and v2 data will allow users hitting a v1 endpoint see new data entered through the v2 point.  If I understand this correctly this would break backwards compatibility as any v2 data will be filtered out of v1.

This may not be easily possible as I am expecting to project the underling base data as two different definitions.

Raymond Feng

unread,
Aug 18, 2014, 2:29:19 PM8/18/14
to Andrew B, loopb...@googlegroups.com
I guess I created some confusion :-(.

1. The ‘defaultScope’ feature has not been implemented in LoopBack.
2. Do you want to have v2 endpoint see all instances including v1? If so, you just don’t set the default scope for the v2 model.

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

Jeremy Lu

unread,
Aug 30, 2014, 6:32:39 AM8/30/14
to loopb...@googlegroups.com, arbr...@gmail.com

So what's the time frame looks like to have versioning implemented in loopback? Thanks.

Al Tsang

unread,
Aug 31, 2014, 12:20:21 PM8/31/14
to loopb...@googlegroups.com, arbr...@gmail.com
Jeremey,

Raymond and team are working on the beginning spikes of the api gateway.  We hope that the versioning functionality will be developed as a module for this use case sometime within this coming quarter, depending on the use cases prioritized for a co-dev partners.  Thanks!

Jeremy Lu

unread,
Aug 31, 2014, 7:47:35 PM8/31/14
to loopb...@googlegroups.com, arbr...@gmail.com

Hey Al, 

Thanks for the update, very looking forward to it.
Reply all
Reply to author
Forward
0 new messages