Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function createFileSchema(bucket) {
// obtain collection name from bucket
const collection = bucket.collectionName;
// gridfs files collection compactible schema
// see https://docs.mongodb.com/manual/core/gridfs/#the-files-collection
const FileSchema = new Schema({
length: { type: Number, index: true },
chunkSize: { type: Number, index: true },
uploadDate: { type: Date, index: true },
md5: { type: String, trim: true, index: true, searchable: true },
filename: { type: String, trim: true, index: true, searchable: true },
contentType: { type: String, trim: true, index: true, searchable: true },
aliases: { type: [String], sort: true, index: true, searchable: true },
metadata: { type: Mixed },
}, { collection, id: false, emitIndexErrors: true });
// expose timestamps as virtuals
FileSchema.virtual('createdAt').get(function () { return this.uploadDate; });
FileSchema.virtual('updatedAt').get(function () { return this.uploadDate; });
// allow virtuals to be included in toJSON and toObject
FileSchema.set('toJSON', { virtuals: true });