Skip to content

Commit 1847662

Browse files
committed
Merge branch 'SleepWalker-fix-helpers-51'
2 parents 09e3927 + abf6dd7 commit 1847662

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/__tests__/datasource.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ describe('Mongoose', () => {
7070
expect(isCollectionOrModel(undefined)).toBe(false)
7171
})
7272

73+
test('mongoose class-based components', () => {
74+
/**
75+
* @see https://github.com/GraphQLGuide/apollo-datasource-mongodb/issues/51
76+
*/
77+
78+
const ClassModel = mongoose.model(
79+
class ClassModel extends mongoose.Model {},
80+
new Schema({ name: 'string' })
81+
)
82+
83+
expect(isModel(ClassModel)).toBe(true)
84+
expect(isCollectionOrModel(ClassModel)).toBe(true)
85+
})
86+
7387
test('getCollectionName', () => {
7488
expect(getCollection(userCollection).collectionName).toBe('users')
7589
expect(getCollection(UserModel).collectionName).toBe('users')

src/helpers.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
const TYPEOF_COLLECTION = 'object'
22

3-
export const isModel = x => Boolean(x && x.name === 'model')
3+
export const isModel = x =>
4+
Boolean(
5+
typeof x === 'function' &&
6+
x.prototype &&
7+
/**
8+
* @see https://github.com/Automattic/mongoose/blob/b4e0ae52a57b886bc7046d38332ce3b38a2f9acd/lib/model.js#L116
9+
*/
10+
x.prototype.$isMongooseModelPrototype
11+
)
412

513
export const isCollectionOrModel = x =>
614
Boolean(x && (typeof x === TYPEOF_COLLECTION || isModel(x)))

0 commit comments

Comments
 (0)