@dhyasama/totem-models 6.9.0 → 6.10.0

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.
package/index.js CHANGED
@@ -63,6 +63,7 @@ var bootstrap = function(mongoose, config) {
63
63
  require('./lib/List.js')(mongoose, config);
64
64
  require('./lib/Message.js')(mongoose, config);
65
65
  require('./lib/News.js')(mongoose, config);
66
+ require('./lib/Snapshot.js')(mongoose, config);
66
67
  require('./lib/Webhook.js')(mongoose, config);
67
68
 
68
69
  };
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+
3
+ module.exports = function(mongoose, config) {
4
+
5
+ var Schema = mongoose.Schema;
6
+
7
+ var Snapshot = new Schema({
8
+
9
+ createdOn: { type: Date, default: Date.now },
10
+ createdBy: { type: String, required: true, trim: true },
11
+ collectionName: { type: String, required: true, trim: true },
12
+ doc: { type: mongoose.Schema.Types.Mixed, required: true }
13
+
14
+ });
15
+
16
+ Snapshot.statics.getByDocId = function getByDocId(docId, cb) {
17
+
18
+ var self = this;
19
+
20
+ self
21
+ .find({ 'doc._id': docId })
22
+ .sort({ changedOn: -1 })
23
+ .exec(cb);
24
+
25
+ };
26
+
27
+ Snapshot.statics.upsert = function(snapshot, cb) {
28
+ snapshot.save(cb);
29
+ };
30
+
31
+ mongoose.model('Snapshot', Snapshot, '_Snapshots');
32
+
33
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-models",
3
- "version": "6.9.0",
3
+ "version": "6.10.0",
4
4
  "author": "Jason Reynolds",
5
5
  "license": "UNLICENSED",
6
6
  "description": "Models for Totem platform",
@@ -0,0 +1,149 @@
1
+ "use strict";
2
+
3
+ var
4
+
5
+ should = require("should"),
6
+ config = require('./config')['test'],
7
+ mongoose = require('mongoose'),
8
+ clearDB = require('mocha-mongoose')(config.db.uri, {noClear: true}),
9
+ Organization = mongoose.model('Organization'),
10
+ Person = mongoose.model('Person'),
11
+ Snapshot = mongoose.model('Snapshot');
12
+
13
+ var org, person;
14
+
15
+ describe('Snapshot', function() {
16
+
17
+ before(function(done) {
18
+ if (mongoose.connection.db) return done();
19
+ mongoose.connect(config.db.uri, done);
20
+ });
21
+
22
+ before(function(done) {
23
+ clearDB(done);
24
+ });
25
+
26
+ before(function(done) {
27
+
28
+ org = new Organization();
29
+ org.name = 'Big Org';
30
+ org.slug = 'big-org';
31
+
32
+ Organization.upsert(org, 'test', function(err, result) {
33
+
34
+ should.not.exist(err);
35
+ should.exist(result);
36
+ org = result;
37
+
38
+ return done();
39
+
40
+ });
41
+
42
+ });
43
+
44
+ before(function(done) {
45
+
46
+ person = new Person();
47
+ person.name = {
48
+ 'first': 'Big',
49
+ 'last': 'Person'
50
+ };
51
+ person.slug = 'big-person';
52
+
53
+ Person.upsert(person, 'test', function(err, result) {
54
+
55
+ should.not.exist(err);
56
+ should.exist(result);
57
+ person = result;
58
+
59
+ return done();
60
+
61
+ });
62
+
63
+ });
64
+
65
+ it('creates a snapshot of an organization', function(done) {
66
+
67
+ var snapshot = new Snapshot({
68
+ createdBy: 'testy-tester',
69
+ collectionName: 'Organization',
70
+ doc: org
71
+ });
72
+
73
+ Snapshot.upsert(snapshot, function(err, result) {
74
+
75
+ should.not.exist(err);
76
+ should.exist(result);
77
+ should.exist(result.doc);
78
+ should.exist(result.doc._id);
79
+ should.equal(result.doc._id, org._id);
80
+
81
+ return done();
82
+
83
+ });
84
+
85
+
86
+
87
+
88
+ });
89
+
90
+ it('gets snapshots of an organization', function(done) {
91
+
92
+ Snapshot.getByDocId(org._id, function(err, result) {
93
+
94
+ should.not.exist(err);
95
+ should.exist(result);
96
+ result.length.should.equal(1);
97
+
98
+ return done();
99
+
100
+ });
101
+
102
+
103
+
104
+
105
+ });
106
+
107
+ it('creates a snapshot of a person', function(done) {
108
+
109
+ var snapshot = new Snapshot({
110
+ createdBy: 'testy-tester',
111
+ collectionName: 'Person',
112
+ doc: person
113
+ });
114
+
115
+ Snapshot.upsert(snapshot, function(err, result) {
116
+
117
+ should.not.exist(err);
118
+ should.exist(result);
119
+ should.exist(result.doc);
120
+ should.exist(result.doc._id);
121
+ should.equal(result.doc._id, person._id);
122
+
123
+ return done();
124
+
125
+ });
126
+
127
+
128
+
129
+
130
+ });
131
+
132
+ it('gets snapshots of a person', function(done) {
133
+
134
+ Snapshot.getByDocId(person._id, function(err, result) {
135
+
136
+ should.not.exist(err);
137
+ should.exist(result);
138
+ result.length.should.equal(1);
139
+
140
+ return done();
141
+
142
+ });
143
+
144
+
145
+
146
+
147
+ });
148
+
149
+ });