Skip to content

Commit 89af024

Browse files
authored
improvement: identify primary key fields as such (#409)
1 parent ab52965 commit 89af024

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

src/adapters/mongoose.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ module.exports = (model, opts) => {
6464
type: getType(fieldName),
6565
};
6666

67+
if (fieldName === '_id') {
68+
field.isPrimaryKey = true;
69+
}
70+
6771
if (!field.type) { return; }
6872

6973
const ref = detectReference(fieldInfo);
@@ -191,6 +195,10 @@ module.exports = (model, opts) => {
191195
type: getTypeFromMongoose(fieldType),
192196
};
193197

198+
if (fieldName === '_id') {
199+
field.isPrimaryKey = true;
200+
}
201+
194202
if (fieldType.enumValues && fieldType.enumValues.length) {
195203
field.enums = fieldType.enumValues;
196204
}
@@ -274,6 +282,10 @@ module.exports = (model, opts) => {
274282
schema.isRequired = isRequired;
275283
}
276284

285+
if (path === '_id') {
286+
schema.isPrimaryKey = true;
287+
}
288+
277289
if (fieldInfo.options && !_.isNull(fieldInfo.options.default)
278290
&& !_.isUndefined(fieldInfo.options.default)
279291
&& !_.isFunction(fieldInfo.options.default)) {

test/tests/adapters/expected-results/deep-nested-object.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@
173173
},
174174
{
175175
"field": "_id",
176-
"type": "String"
176+
"type": "String",
177+
"isPrimaryKey": true
177178
}
178179
]
179180
}

test/tests/adapters/expected-results/deep-nested-schema.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
},
6363
{
6464
"field": "_id",
65-
"type": "String"
65+
"type": "String",
66+
"isPrimaryKey": true
6667
}
6768
]
6869
}
@@ -73,7 +74,8 @@
7374
},
7475
{
7576
"field": "_id",
76-
"type": "String"
77+
"type": "String",
78+
"isPrimaryKey": true
7779
}
7880
]
7981
}

test/tests/adapters/expected-results/real-world-schema.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
},
7474
{
7575
"field": "_id",
76+
"isPrimaryKey": true,
7677
"type": "String"
7778
}
7879
]
@@ -145,6 +146,7 @@
145146
},
146147
{
147148
"field": "_id",
149+
"isPrimaryKey": true,
148150
"type": "String"
149151
}
150152
]
@@ -173,6 +175,7 @@
173175
},
174176
{
175177
"field": "_id",
178+
"isPrimaryKey": true,
176179
"type": "String"
177180
}
178181
]
@@ -241,6 +244,7 @@
241244
},
242245
{
243246
"field": "_id",
247+
"isPrimaryKey": true,
244248
"type": "String"
245249
}
246250
]
@@ -259,6 +263,7 @@
259263
},
260264
{
261265
"field": "_id",
266+
"isPrimaryKey": true,
262267
"type": "String"
263268
}
264269
]
@@ -299,6 +304,7 @@
299304
},
300305
{
301306
"field": "_id",
307+
"isPrimaryKey": true,
302308
"type": "String"
303309
}
304310
]
@@ -377,6 +383,7 @@
377383
},
378384
{
379385
"field": "_id",
386+
"isPrimaryKey": true,
380387
"type": "String"
381388
}
382389
]
@@ -394,6 +401,7 @@
394401
},
395402
{
396403
"field": "_id",
404+
"isPrimaryKey": true,
397405
"type": "String"
398406
}
399407
]
@@ -432,7 +440,8 @@
432440
},
433441
{
434442
"field": "_id",
435-
"type": "String"
443+
"type": "String",
444+
"isPrimaryKey": true
436445
}
437446
]
438447
}

test/tests/adapters/mongoose.test.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ describe('adapters > schema-adapter', () => {
450450
{ field: 'firstName', type: 'String' },
451451
{ field: 'lastName', type: 'String' },
452452
{ field: 'createdAt', type: 'Date' },
453-
{ field: '_id', type: 'String' },
453+
{ field: '_id', type: 'String', isPrimaryKey: true },
454454
]);
455455
});
456456
});
@@ -755,4 +755,30 @@ describe('adapters > schema-adapter', () => {
755755
expect(result.fields).toHaveLength(2);
756756
});
757757
});
758+
759+
describe('isPrimaryKey flag', () => {
760+
let model;
761+
762+
beforeAll(() => {
763+
const schema = new mongoose.Schema({
764+
_id: mongoose.Schema.ObjectId,
765+
bar: String,
766+
});
767+
model = mongoose.model('Foo', schema);
768+
});
769+
770+
it('should be set to true on field _id', async () => {
771+
expect.assertions(2);
772+
773+
const result = await createSchemaAdapter(model, {
774+
mongoose,
775+
connections: [mongoose],
776+
});
777+
expect(result).toHaveProperty('fields');
778+
779+
const idField = result.fields.find((field) => field.field === '_id');
780+
781+
expect(idField.isPrimaryKey).toBe(true);
782+
});
783+
});
758784
});

0 commit comments

Comments
 (0)