-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffHistoryModel.js
38 lines (35 loc) · 1010 Bytes
/
diffHistoryModel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const mongoose = require('mongoose');
const { Schema } = mongoose;
const historySchemaGenerator = (paths, opts) =>
new Schema(
{
collectionName: String,
collectionId: {
type: paths?._id?.options?.type || Schema.Types.ObjectId
},
diff: {},
user: {},
reason: String,
version: { type: Number, min: 0 }
},
{
timestamps: true,
...opts
}
);
const historyModelGenerator = (schema, opts = {}) => {
const adapter = opts.adapter || mongoose;
let model;
try {
model = adapter.model(`${opts.name}`);
console.log(`Model [${opts.name}] already exists`);
} catch (e) {
model = adapter.model(
`${opts.name}`,
historySchemaGenerator(schema.paths, opts.schemaOpts)
);
console.log(`Creating model [${opts.name}]`);
}
return model;
};
module.exports = historyModelGenerator;