Skip to content

Commit 1023baf

Browse files
committed
DBController refactoring (#1228)
* Moves transform to MongoTransform - Adds ACL query injection in MongoTransform * Removes adaptiveCollection from DatabaseController - All collections manipulations are now handled by a DBController - Adds optional flags to configure an unsafe databaseController for direct access - Adds ability to configure RestWrite with multiple writes - Moves some transfirmations to MongoTransform as they output specific code * Renames Unsafe to WithoutValidation
1 parent 51970fb commit 1023baf

17 files changed

+317
-291
lines changed

spec/transform.spec.js renamed to spec/MongoTransform.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// These tests are unit tests designed to only test transform.js.
22
"use strict";
33

4-
let transform = require('../src/transform');
4+
let transform = require('../src/Adapters/Storage/Mongo/MongoTransform');
55
let dd = require('deep-diff');
66
let mongodb = require('mongodb');
77

spec/ParseGlobalConfig.spec.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let Config = require('../src/Config');
77
describe('a GlobalConfig', () => {
88
beforeEach(done => {
99
let config = new Config('test');
10-
config.database.adaptiveCollection('_GlobalConfig')
10+
config.database.adapter.adaptiveCollection('_GlobalConfig')
1111
.then(coll => coll.upsertOne({ '_id': 1 }, { $set: { params: { companies: ['US', 'DK'] } } }))
1212
.then(() => { done(); });
1313
});
@@ -43,6 +43,35 @@ describe('a GlobalConfig', () => {
4343
});
4444
});
4545

46+
it('properly handles delete op', (done) => {
47+
request.put({
48+
url : 'http://localhost:8378/1/config',
49+
json : true,
50+
body : { params: { companies: {__op: 'Delete'}, foo: 'bar' } },
51+
headers: {
52+
'X-Parse-Application-Id': 'test',
53+
'X-Parse-Master-Key' : 'test'
54+
}
55+
}, (error, response, body) => {
56+
expect(response.statusCode).toEqual(200);
57+
expect(body.result).toEqual(true);
58+
request.get({
59+
url : 'http://localhost:8378/1/config',
60+
json : true,
61+
headers: {
62+
'X-Parse-Application-Id': 'test',
63+
'X-Parse-Master-Key' : 'test'
64+
}
65+
}, (error, response, body) => {
66+
expect(response.statusCode).toEqual(200);
67+
expect(body.params.companies).toBeUndefined();
68+
expect(body.params.foo).toBe('bar');
69+
expect(Object.keys(body.params).length).toBe(1);
70+
done();
71+
});
72+
});
73+
});
74+
4675
it('fail to update if master key is missing', (done) => {
4776
request.put({
4877
url : 'http://localhost:8378/1/config',
@@ -61,7 +90,7 @@ describe('a GlobalConfig', () => {
6190

6291
it('failed getting config when it is missing', (done) => {
6392
let config = new Config('test');
64-
config.database.adaptiveCollection('_GlobalConfig')
93+
config.database.adapter.adaptiveCollection('_GlobalConfig')
6594
.then(coll => coll.deleteOne({ '_id': 1 }))
6695
.then(() => {
6796
request.get({

spec/ParseHooks.spec.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ app.listen(12345);
1616

1717

1818
describe('Hooks', () => {
19-
19+
2020
it("should have some hooks registered", (done) => {
2121
Parse.Hooks.getFunctions().then((res) => {
2222
expect(res.constructor).toBe(Array.prototype.constructor);
@@ -26,7 +26,7 @@ describe('Hooks', () => {
2626
done();
2727
});
2828
});
29-
29+
3030
it("should have some triggers registered", (done) => {
3131
Parse.Hooks.getTriggers().then( (res) => {
3232
expect(res.constructor).toBe(Array.prototype.constructor);
@@ -59,7 +59,7 @@ describe('Hooks', () => {
5959
}).then((res) => {
6060
expect(res.functionName).toBe("My-Test-Function");
6161
expect(res.url).toBe("http://anotherurl")
62-
62+
6363
return Parse.Hooks.deleteFunction("My-Test-Function");
6464
}, (err) => {
6565
fail(err);
@@ -81,7 +81,7 @@ describe('Hooks', () => {
8181
done();
8282
})
8383
});
84-
84+
8585
it("should CRUD a trigger registration", (done) => {
8686
// Create
8787
Parse.Hooks.createTrigger("MyClass","beforeDelete", "http://someurl").then((res) => {
@@ -105,7 +105,7 @@ describe('Hooks', () => {
105105
}).then((res) => {
106106
expect(res.className).toBe("MyClass");
107107
expect(res.url).toBe("http://anotherurl")
108-
108+
109109
return Parse.Hooks.deleteTrigger("MyClass","beforeDelete");
110110
}, (err) => {
111111
fail(err);
@@ -127,7 +127,7 @@ describe('Hooks', () => {
127127
done();
128128
});
129129
});
130-
130+
131131
it("should fail to register hooks without Master Key", (done) => {
132132
request.post(Parse.serverURL+"/hooks/functions", {
133133
headers: {
@@ -141,7 +141,7 @@ describe('Hooks', () => {
141141
done();
142142
})
143143
});
144-
144+
145145
it("should fail trying to create two times the same function", (done) => {
146146
Parse.Hooks.createFunction("my_new_function", "http://url.com").then( () => {
147147
return Parse.Hooks.createFunction("my_new_function", "http://url.com")
@@ -162,7 +162,7 @@ describe('Hooks', () => {
162162
done();
163163
})
164164
});
165-
165+
166166
it("should fail trying to create two times the same trigger", (done) => {
167167
Parse.Hooks.createTrigger("MyClass", "beforeSave", "http://url.com").then( () => {
168168
return Parse.Hooks.createTrigger("MyClass", "beforeSave", "http://url.com")
@@ -181,7 +181,7 @@ describe('Hooks', () => {
181181
done();
182182
})
183183
});
184-
184+
185185
it("should fail trying to update a function that don't exist", (done) => {
186186
Parse.Hooks.updateFunction("A_COOL_FUNCTION", "http://url.com").then( () => {
187187
fail("Should not succeed")
@@ -198,7 +198,7 @@ describe('Hooks', () => {
198198
done();
199199
});
200200
});
201-
201+
202202
it("should fail trying to update a trigger that don't exist", (done) => {
203203
Parse.Hooks.updateTrigger("AClassName","beforeSave", "http://url.com").then( () => {
204204
fail("Should not succeed")
@@ -215,8 +215,8 @@ describe('Hooks', () => {
215215
done();
216216
});
217217
});
218-
219-
218+
219+
220220
it("should fail trying to create a malformed function", (done) => {
221221
Parse.Hooks.createFunction("MyFunction").then( (res) => {
222222
fail(res);
@@ -226,7 +226,7 @@ describe('Hooks', () => {
226226
done();
227227
});
228228
});
229-
229+
230230
it("should fail trying to create a malformed function (REST)", (done) => {
231231
request.post(Parse.serverURL+"/hooks/functions", {
232232
headers: {
@@ -241,16 +241,16 @@ describe('Hooks', () => {
241241
done();
242242
})
243243
});
244-
245-
244+
245+
246246
it("should create hooks and properly preload them", (done) => {
247-
247+
248248
var promises = [];
249249
for (var i = 0; i<5; i++) {
250250
promises.push(Parse.Hooks.createTrigger("MyClass"+i, "beforeSave", "http://url.com/beforeSave/"+i));
251251
promises.push(Parse.Hooks.createFunction("AFunction"+i, "http://url.com/function"+i));
252252
}
253-
253+
254254
Parse.Promise.when(promises).then(function(results){
255255
for (var i=0; i<5; i++) {
256256
// Delete everything from memory, as the server just started
@@ -263,7 +263,7 @@ describe('Hooks', () => {
263263
return hooksController.load()
264264
}, (err) => {
265265
console.error(err);
266-
fail();
266+
fail('Should properly create all hooks');
267267
done();
268268
}).then(function() {
269269
for (var i=0; i<5; i++) {
@@ -273,17 +273,17 @@ describe('Hooks', () => {
273273
done();
274274
}, (err) => {
275275
console.error(err);
276-
fail();
276+
fail('should properly load all hooks');
277277
done();
278278
})
279279
});
280-
280+
281281
it("should run the function on the test server", (done) => {
282-
282+
283283
app.post("/SomeFunction", function(req, res) {
284284
res.json({success:"OK!"});
285285
});
286-
286+
287287
Parse.Hooks.createFunction("SOME_TEST_FUNCTION", hookServerURL+"/SomeFunction").then(function(){
288288
return Parse.Cloud.run("SOME_TEST_FUNCTION")
289289
}, (err) => {
@@ -299,9 +299,9 @@ describe('Hooks', () => {
299299
done();
300300
})
301301
});
302-
302+
303303
it("should run the function on the test server", (done) => {
304-
304+
305305
app.post("/SomeFunctionError", function(req, res) {
306306
res.json({error: {code: 1337, error: "hacking that one!"}});
307307
});
@@ -322,8 +322,8 @@ describe('Hooks', () => {
322322
done();
323323
});
324324
});
325-
326-
325+
326+
327327
it("should run the beforeSave hook on the test server", (done) => {
328328
var triggerCount = 0;
329329
app.post("/BeforeSaveSome", function(req, res) {
@@ -350,7 +350,7 @@ describe('Hooks', () => {
350350
done();
351351
});
352352
});
353-
353+
354354
it("should run the afterSave hook on the test server", (done) => {
355355
var triggerCount = 0;
356356
var newObjectId;
@@ -387,4 +387,4 @@ describe('Hooks', () => {
387387
done();
388388
});
389389
});
390-
});
390+
});

spec/PushController.spec.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ describe('PushController', () => {
184184
}).then((result) => {
185185
done();
186186
}, (err) => {
187-
console.error(err);
188187
fail("should not fail");
189188
done();
190189
});
@@ -233,7 +232,6 @@ describe('PushController', () => {
233232
}).then((result) => {
234233
done();
235234
}, (err) => {
236-
console.error(err);
237235
fail("should not fail");
238236
done();
239237
});

spec/ValidationAndPasswordsReset.spec.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,10 @@ describe("Email Verification", () => {
266266
.then((user) => {
267267
return user.save();
268268
}).then((user) => {
269-
return Parse.User.requestPasswordReset("[email protected]");
269+
return Parse.User.requestPasswordReset("[email protected]").catch((err) => {
270+
fail('Should not fail requesting a password');
271+
done();
272+
})
270273
}).then(() => {
271274
expect(calls).toBe(2);
272275
done();
@@ -551,7 +554,7 @@ describe("Password Reset", () => {
551554
Parse.User.requestPasswordReset('[email protected]', {
552555
error: (err) => {
553556
console.error(err);
554-
fail("Should not fail");
557+
fail("Should not fail requesting a password");
555558
done();
556559
}
557560
});
@@ -628,7 +631,7 @@ describe("Password Reset", () => {
628631

629632
Parse.User.logIn("zxcv", "hello").then(function(user){
630633
let config = new Config('test');
631-
config.database.adaptiveCollection('_User')
634+
config.database.adapter.adaptiveCollection('_User')
632635
.then(coll => coll.find({ 'username': 'zxcv' }, { limit: 1 }))
633636
.then((results) => {
634637
// _perishable_token should be unset after reset password

src/Adapters/Storage/Mongo/MongoSchemaCollection.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import MongoCollection from './MongoCollection';
3+
import * as transform from './MongoTransform';
34

45
function mongoFieldToParseSchemaField(type) {
56
if (type[0] === '*') {
@@ -200,6 +201,10 @@ class MongoSchemaCollection {
200201
update = {'$set': update};
201202
return this.upsertSchema(className, query, update);
202203
}
204+
205+
get transform() {
206+
return transform;
207+
}
203208
}
204209

205210
// Exported for testing reasons and because we haven't moved all mongo schema format

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import MongoCollection from './MongoCollection';
22
import MongoSchemaCollection from './MongoSchemaCollection';
33
import {parse as parseUrl, format as formatUrl} from '../../../vendor/mongodbUrl';
4+
import * as transform from './MongoTransform';
45
import _ from 'lodash';
56

67
let mongodb = require('mongodb');
@@ -57,7 +58,7 @@ export class MongoStorageAdapter {
5758

5859
schemaCollection() {
5960
return this.connect()
60-
.then(() => this.adaptiveCollection(this._collectionPrefix + MongoSchemaCollectionName))
61+
.then(() => this.adaptiveCollection(MongoSchemaCollectionName))
6162
.then(collection => new MongoSchemaCollection(collection));
6263
}
6364

@@ -125,6 +126,10 @@ export class MongoStorageAdapter {
125126
.then(updateResult => this.schemaCollection())
126127
.then(schemaCollection => schemaCollection.updateSchema(className, schemaUpdate));
127128
}
129+
130+
get transform() {
131+
return transform;
132+
}
128133
}
129134

130135
export default MongoStorageAdapter;

0 commit comments

Comments
 (0)