@positivegrid/pg-mongoose-schema 27.8.5 → 28.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,88 +0,0 @@
1
- 'use strict'
2
-
3
- const _ = require('lodash')
4
-
5
- module.exports = function (mongoose) {
6
- const Schema = mongoose.Schema
7
- const ObjectId = Schema.Types.ObjectId
8
-
9
- const PgConfigSchema = new Schema({
10
- config_key: { type: String, index: { unique: true }, required: true },
11
- config_value: { type: Schema.Types.Mixed, required: true },
12
- created_on: { type: Date, default: Date.now }
13
- }, { collection: 'jamup_positivegridconfig' })
14
-
15
- const PgLoggingSchema = new Schema({
16
- type: { type: String, required: true },
17
- log_id: { type: String, required: true },
18
- log_content: { type: Schema.Types.Mixed, required: true },
19
- user_id: { type: ObjectId, ref: 'User' },
20
- created_on: { type: Date, default: Date.now }
21
- }, { collection: 'jamup_logging' })
22
- PgLoggingSchema.index({ type: 1, log_id: 1 })
23
-
24
- const KeyValueSchema = new Schema({
25
- key: { type: String, required: true },
26
- value: { type: Schema.Types.Mixed, required: true },
27
- created_on: { type: Date, default: Date.now },
28
- expire_at: { type: Date, default: null }
29
- }, { collection: 'pg_key_value' })
30
- KeyValueSchema.index({ key: 1 })
31
- KeyValueSchema.index({ expire_at: 1 }, { expireAfterSeconds: 0 })
32
-
33
- const CrashDataSchema = new Schema({
34
- product: { type: String, required: true },
35
- crash_data: { type: Schema.Types.Mixed, default: null },
36
- email: { type: String, default: null },
37
- crash_file: { type: String, required: true },
38
- created_on: { type: Date, default: Date.now }
39
- }, { collection: 'pg_crash_data' })
40
-
41
- PgConfigSchema.static({
42
- getConfigByKey: function (config_key, cb) {
43
- this.findOne({ config_key: config_key }).exec(cb);
44
- },
45
- createOrUpdateConfig: function (key, value, doMerge, cb) {
46
- let that = this;
47
- if (_.isFunction(doMerge)) { cb = doMerge; }
48
-
49
- that.findOne({ config_key: key }, function (err, config) {
50
- if (err) {
51
- return cb(err);
52
- } else {
53
- if (config) {
54
- let newData = (_.isBoolean(doMerge) && doMerge === true)
55
- ? _.assign({}, config['config_value'], value) : value;
56
-
57
- config['config_value'] = newData;
58
- config['created_on'] = new Date();
59
- config.save(cb);
60
- } else {
61
- that.create({
62
- config_key: key,
63
- config_value: value
64
- }, cb);
65
- }
66
- }
67
- });
68
- },
69
- deleteConfig: function (key, cb) {
70
- this.findOneAndRemove({
71
- config_key: key
72
- }, function (err, doc) {
73
- if (err) {
74
- return cb(err)
75
- } else if (!doc) {
76
- return cb(new Error(`Cannot find config key ${key}`))
77
- } else {
78
- return cb(null, doc)
79
- }
80
- })
81
- }
82
- });
83
-
84
- mongoose.model('PgConfig', PgConfigSchema)
85
- mongoose.model('PgLogging', PgLoggingSchema)
86
- mongoose.model('KeyValue', KeyValueSchema)
87
- mongoose.model('CrashData', CrashDataSchema)
88
- }