Mongoose: use $type as the typeKey (#10789)

* use $type as the typeKey in mongoose

* fix and add tests
This commit is contained in:
Matteo Pagliazzi
2018-10-28 15:23:41 +01:00
committed by Sabe Jones
parent 490531cc76
commit 79c0499672
15 changed files with 383 additions and 357 deletions

View File

@@ -10,22 +10,23 @@ const defaultSchema = () => ({
// sender properties
user: String, // profile name
contributor: {type: mongoose.Schema.Types.Mixed},
backer: {type: mongoose.Schema.Types.Mixed},
contributor: {$type: mongoose.Schema.Types.Mixed},
backer: {$type: mongoose.Schema.Types.Mixed},
uuid: String, // sender uuid
userStyles: {type: mongoose.Schema.Types.Mixed},
userStyles: {$type: mongoose.Schema.Types.Mixed},
flags: {type: mongoose.Schema.Types.Mixed, default: {}},
flagCount: {type: Number, default: 0},
likes: {type: mongoose.Schema.Types.Mixed},
_meta: {type: mongoose.Schema.Types.Mixed},
flags: {$type: mongoose.Schema.Types.Mixed, default: {}},
flagCount: {$type: Number, default: 0},
likes: {$type: mongoose.Schema.Types.Mixed},
_meta: {$type: mongoose.Schema.Types.Mixed},
});
const chatSchema = new mongoose.Schema({
...defaultSchema(),
groupId: {type: String, ref: 'Group'},
groupId: {$type: String, ref: 'Group'},
}, {
minimize: false, // Allow for empty flags to be saved
typeKey: '$type', // So that we can use fields named `type`
});
chatSchema.plugin(baseModel, {
@@ -33,14 +34,15 @@ chatSchema.plugin(baseModel, {
});
const inboxSchema = new mongoose.Schema({
sent: {type: Boolean, default: false}, // if the owner sent this message
sent: {$type: Boolean, default: false}, // if the owner sent this message
// the uuid of the user where the message is stored,
// we store two copies of each inbox messages:
// one for the sender and one for the receiver
ownerId: {type: String, ref: 'User'},
ownerId: {$type: String, ref: 'User'},
...defaultSchema(),
}, {
minimize: false, // Allow for empty flags to be saved
typeKey: '$type', // So that we can use fields named `type`
});
inboxSchema.plugin(baseModel, {