Skip to content

Commit eecf9ea

Browse files
committed
feat: add support for Map type (added in Mongoose 5.1.0)
closes #186
1 parent 3b610f4 commit eecf9ea

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

src/__mocks__/userModel.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ const UserSchema: SchemaType<any> = new Schema(
9494
type: Schema.Types.Decimal128,
9595
},
9696

97+
mapField: {
98+
type: Map,
99+
of: String,
100+
},
101+
102+
mapFieldDeep: {
103+
subField: {
104+
type: Map,
105+
of: String,
106+
},
107+
},
108+
97109
// createdAt, created via option `timastamp: true` (see bottom)
98110
// updatedAt, created via option `timastamp: true` (see bottom)
99111
},

src/__tests__/fieldConverter-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,5 +310,11 @@ describe('fieldConverter', () => {
310310
it('should work with Decimal128', () => {
311311
expect(tc.getFieldTypeName('salary')).toBe('BSONDecimal');
312312
});
313+
314+
it('should work with Map', () => {
315+
expect(tc.getFieldTypeName('mapField')).toBe('JSON');
316+
expect(tc.getFieldTypeName('mapFieldDeep')).toBe('UserMapFieldDeep');
317+
expect(tc.getFieldOTC('mapFieldDeep').getFieldTypeName('subField')).toBe('JSON');
318+
});
313319
});
314320
});

src/fieldsConverter.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ export function dotPathsToEmbedded(fields: MongooseFieldMapT): MongooseFieldMapT
6969
const dotIdx = fieldName.indexOf('.');
7070
if (dotIdx === -1) {
7171
result[fieldName] = fields[fieldName];
72+
} else if (fieldName.substr(dotIdx, 3) === '.$*') {
73+
// skip { type: Map of: String }
74+
// do not add this field to result
7275
} else {
7376
// create pseudo sub-model
7477
const name = fieldName.substr(0, dotIdx);

0 commit comments

Comments
 (0)