From b9f0284a2834b2cd57ddab0e5df47c7a0993a4e8 Mon Sep 17 00:00:00 2001 From: Francisco Buceta Date: Fri, 15 Nov 2019 16:15:38 +0100 Subject: [PATCH] Fix generated string id's MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Miroslav Bajtoš --- lib/datasource.js | 2 +- test/loopback-dl.test.js | 10 ++++++++++ test/manipulation.test.js | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/datasource.js b/lib/datasource.js index f7530118..4538eeaf 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -719,7 +719,7 @@ DataSource.prototype.setupDataAccess = function(modelClass, settings) { // Check if the id property should be generated const idName = modelClass.definition.idName(); const idProp = modelClass.definition.rawProperties[idName]; - if (idProp && idProp.generated && this.connector.getDefaultIdType) { + if (idProp && idProp.generated && idProp.useDefaultIdType !== false && this.connector.getDefaultIdType) { // Set the default id type from connector's ability const idType = this.connector.getDefaultIdType() || String; idProp.type = idType; diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index 8d575fc0..6426206b 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -648,6 +648,16 @@ describe('DataSource define model', function() { done(); }); + it('injects id with useDefaultIdType to false', function(done) { + const ds = new ModelBuilder(); + + const User = ds.define('User', {id: {type: String, generated: true, id: true, useDefaultIdType: false}}); + assert.deepEqual(User.definition.properties.id, + {type: String, id: true, generated: true, updateOnly: true, useDefaultIdType: false}); + + done(); + }); + it('disables idInjection if the value is false', function(done) { const ds = new ModelBuilder(); diff --git a/test/manipulation.test.js b/test/manipulation.test.js index c6015b19..737a0df6 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -2662,6 +2662,41 @@ describe('manipulation', function() { null, // databases representing `undefined` as `null` (e.g. SQL) ]); }); + + it('preserves custom type of auto-generated id property', async () => { + // NOTE: This test is trying to reproduce the behavior observed + // when using property defined as follows: + // {type: 'string', generated: true, mongodb: {dataType: 'ObjectID'}} + // We want to test that behavior for all connectors, which is tricky, + // because not all connectors support autogenerated string PK values. + + const User = db.define('UserWithStringId', { + id: { + type: String, + id: true, + useDefaultIdType: false, + // `useDefaultIdType` is applied only when `generated: true` + generated: true, + }, + name: String, + }, {forceId: false}); + + // disable `generated: true` because many SQL databases cannot + // auto-generate string ids + User.definition.properties.id.generated = false; + User.definition.rawProperties.id.generated = false; + await db.automigrate(User.modelName); + + const userId = 'custom user id'; + + const createdUser = await User.create({id: userId, name: 'testUser'}); + // strict equality check + createdUser.id.should.equal(userId); + + const foundUser = await User.findById(userId); + // strict equality check + foundUser.id.should.equal(userId); + }); }); });