Skip to content

Commit

Permalink
Merge pull request #1783 from frbuceta/fix/lb4-issue-3602
Browse files Browse the repository at this point in the history
Fix issue with lb4 id different to int
  • Loading branch information
bajtos authored Nov 18, 2019
2 parents b07bdfc + b9f0284 commit 5578ab4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions test/loopback-dl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
35 changes: 35 additions & 0 deletions test/manipulation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

Expand Down

0 comments on commit 5578ab4

Please sign in to comment.