Schema Cache Improvement 2 (#5616)
* schema hasClass improvement * create object improvement * destroy object * update object * hasClass test rewrite * more tests * improve signing up users
This commit is contained in:
@@ -175,20 +175,20 @@ describe_only(() => {
|
||||
|
||||
beforeEach(async () => {
|
||||
await cacheAdapter.clear();
|
||||
getSpy = spyOn(cacheAdapter, 'get').and.callThrough();
|
||||
putSpy = spyOn(cacheAdapter, 'put').and.callThrough();
|
||||
await reconfigureServer({
|
||||
cacheAdapter,
|
||||
enableSingleSchemaCache: true,
|
||||
});
|
||||
getSpy = spyOn(cacheAdapter, 'get').and.callThrough();
|
||||
putSpy = spyOn(cacheAdapter, 'put').and.callThrough();
|
||||
});
|
||||
|
||||
it('test new object', async () => {
|
||||
const object = new TestObject();
|
||||
object.set('foo', 'bar');
|
||||
await object.save();
|
||||
expect(getSpy.calls.count()).toBe(4);
|
||||
expect(putSpy.calls.count()).toBe(3);
|
||||
expect(getSpy.calls.count()).toBe(2);
|
||||
expect(putSpy.calls.count()).toBe(2);
|
||||
});
|
||||
|
||||
it('test new object multiple fields', async () => {
|
||||
@@ -200,8 +200,8 @@ describe_only(() => {
|
||||
booleanField: true,
|
||||
});
|
||||
await container.save();
|
||||
expect(getSpy.calls.count()).toBe(4);
|
||||
expect(putSpy.calls.count()).toBe(3);
|
||||
expect(getSpy.calls.count()).toBe(2);
|
||||
expect(putSpy.calls.count()).toBe(2);
|
||||
});
|
||||
|
||||
it('test update existing fields', async () => {
|
||||
@@ -214,7 +214,57 @@ describe_only(() => {
|
||||
|
||||
object.set('foo', 'barz');
|
||||
await object.save();
|
||||
expect(getSpy.calls.count()).toBe(3);
|
||||
expect(getSpy.calls.count()).toBe(2);
|
||||
expect(putSpy.calls.count()).toBe(0);
|
||||
});
|
||||
|
||||
it('test saveAll / destroyAll', async () => {
|
||||
const object = new TestObject();
|
||||
await object.save();
|
||||
|
||||
getSpy.calls.reset();
|
||||
putSpy.calls.reset();
|
||||
|
||||
const objects = [];
|
||||
for (let i = 0; i < 10; i++) {
|
||||
const object = new TestObject();
|
||||
object.set('number', i);
|
||||
objects.push(object);
|
||||
}
|
||||
await Parse.Object.saveAll(objects);
|
||||
expect(getSpy.calls.count()).toBe(11);
|
||||
expect(putSpy.calls.count()).toBe(10);
|
||||
|
||||
getSpy.calls.reset();
|
||||
putSpy.calls.reset();
|
||||
|
||||
await Parse.Object.destroyAll(objects);
|
||||
expect(getSpy.calls.count()).toBe(11);
|
||||
expect(putSpy.calls.count()).toBe(0);
|
||||
});
|
||||
|
||||
it('test saveAll / destroyAll batch', async () => {
|
||||
const object = new TestObject();
|
||||
await object.save();
|
||||
|
||||
getSpy.calls.reset();
|
||||
putSpy.calls.reset();
|
||||
|
||||
const objects = [];
|
||||
for (let i = 0; i < 10; i++) {
|
||||
const object = new TestObject();
|
||||
object.set('number', i);
|
||||
objects.push(object);
|
||||
}
|
||||
await Parse.Object.saveAll(objects, { batchSize: 5 });
|
||||
expect(getSpy.calls.count()).toBe(12);
|
||||
expect(putSpy.calls.count()).toBe(5);
|
||||
|
||||
getSpy.calls.reset();
|
||||
putSpy.calls.reset();
|
||||
|
||||
await Parse.Object.destroyAll(objects, { batchSize: 5 });
|
||||
expect(getSpy.calls.count()).toBe(12);
|
||||
expect(putSpy.calls.count()).toBe(0);
|
||||
});
|
||||
|
||||
@@ -228,7 +278,7 @@ describe_only(() => {
|
||||
|
||||
object.set('new', 'barz');
|
||||
await object.save();
|
||||
expect(getSpy.calls.count()).toBe(3);
|
||||
expect(getSpy.calls.count()).toBe(2);
|
||||
expect(putSpy.calls.count()).toBe(1);
|
||||
});
|
||||
|
||||
@@ -248,8 +298,43 @@ describe_only(() => {
|
||||
booleanField: true,
|
||||
});
|
||||
await object.save();
|
||||
expect(getSpy.calls.count()).toBe(2);
|
||||
expect(putSpy.calls.count()).toBe(1);
|
||||
});
|
||||
|
||||
it('test user', async () => {
|
||||
const user = new Parse.User();
|
||||
user.setUsername('testing');
|
||||
user.setPassword('testing');
|
||||
await user.signUp();
|
||||
|
||||
expect(getSpy.calls.count()).toBe(6);
|
||||
expect(putSpy.calls.count()).toBe(1);
|
||||
});
|
||||
|
||||
it('test allowClientCreation false', async () => {
|
||||
const object = new TestObject();
|
||||
await object.save();
|
||||
await reconfigureServer({
|
||||
cacheAdapter,
|
||||
enableSingleSchemaCache: true,
|
||||
allowClientClassCreation: false,
|
||||
});
|
||||
getSpy.calls.reset();
|
||||
putSpy.calls.reset();
|
||||
|
||||
object.set('foo', 'bar');
|
||||
await object.save();
|
||||
expect(getSpy.calls.count()).toBe(3);
|
||||
expect(putSpy.calls.count()).toBe(1);
|
||||
|
||||
getSpy.calls.reset();
|
||||
putSpy.calls.reset();
|
||||
|
||||
const query = new Parse.Query(TestObject);
|
||||
await query.get(object.id);
|
||||
expect(getSpy.calls.count()).toBe(3);
|
||||
expect(putSpy.calls.count()).toBe(0);
|
||||
});
|
||||
|
||||
it('test query', async () => {
|
||||
@@ -266,6 +351,45 @@ describe_only(() => {
|
||||
expect(putSpy.calls.count()).toBe(0);
|
||||
});
|
||||
|
||||
it('test query include', async () => {
|
||||
const child = new TestObject();
|
||||
await child.save();
|
||||
|
||||
const object = new TestObject();
|
||||
object.set('child', child);
|
||||
await object.save();
|
||||
|
||||
getSpy.calls.reset();
|
||||
putSpy.calls.reset();
|
||||
|
||||
const query = new Parse.Query(TestObject);
|
||||
query.include('child');
|
||||
await query.get(object.id);
|
||||
|
||||
expect(getSpy.calls.count()).toBe(4);
|
||||
expect(putSpy.calls.count()).toBe(0);
|
||||
});
|
||||
|
||||
it('query relation without schema', async () => {
|
||||
const child = new Parse.Object('ChildObject');
|
||||
await child.save();
|
||||
|
||||
const parent = new Parse.Object('ParentObject');
|
||||
const relation = parent.relation('child');
|
||||
relation.add(child);
|
||||
await parent.save();
|
||||
|
||||
getSpy.calls.reset();
|
||||
putSpy.calls.reset();
|
||||
|
||||
const objects = await relation.query().find();
|
||||
expect(objects.length).toBe(1);
|
||||
expect(objects[0].id).toBe(child.id);
|
||||
|
||||
expect(getSpy.calls.count()).toBe(2);
|
||||
expect(putSpy.calls.count()).toBe(0);
|
||||
});
|
||||
|
||||
it('test delete object', async () => {
|
||||
const object = new TestObject();
|
||||
object.set('foo', 'bar');
|
||||
@@ -275,7 +399,7 @@ describe_only(() => {
|
||||
putSpy.calls.reset();
|
||||
|
||||
await object.destroy();
|
||||
expect(getSpy.calls.count()).toBe(3);
|
||||
expect(getSpy.calls.count()).toBe(2);
|
||||
expect(putSpy.calls.count()).toBe(0);
|
||||
});
|
||||
|
||||
|
||||
@@ -181,31 +181,26 @@ describe('rest query', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('query existent class when disabled client class creation', done => {
|
||||
it('query existent class when disabled client class creation', async () => {
|
||||
const customConfig = Object.assign({}, config, {
|
||||
allowClientClassCreation: false,
|
||||
});
|
||||
config.database
|
||||
.loadSchema()
|
||||
.then(schema => schema.addClassIfNotExists('ClientClassCreation', {}))
|
||||
.then(actualSchema => {
|
||||
const schema = await config.database.loadSchema();
|
||||
const actualSchema = await schema.addClassIfNotExists(
|
||||
'ClientClassCreation',
|
||||
{}
|
||||
);
|
||||
expect(actualSchema.className).toEqual('ClientClassCreation');
|
||||
return rest.find(
|
||||
|
||||
await schema.reloadData({ clearCache: true });
|
||||
// Should not throw
|
||||
const result = await rest.find(
|
||||
customConfig,
|
||||
auth.nobody(customConfig),
|
||||
'ClientClassCreation',
|
||||
{}
|
||||
);
|
||||
})
|
||||
.then(
|
||||
result => {
|
||||
expect(result.results.length).toEqual(0);
|
||||
done();
|
||||
},
|
||||
() => {
|
||||
fail('Should not throw error');
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it('query with wrongly encoded parameter', done => {
|
||||
|
||||
@@ -929,6 +929,7 @@ describe('SchemaController', () => {
|
||||
.then(schema => {
|
||||
return schema
|
||||
.addClassIfNotExists('NewClass', {})
|
||||
.then(() => schema.reloadData({ clearCache: true }))
|
||||
.then(() => {
|
||||
schema
|
||||
.hasClass('NewClass')
|
||||
|
||||
@@ -181,30 +181,25 @@ describe('rest create', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('handles create on existent class when disabled client class creation', done => {
|
||||
it('handles create on existent class when disabled client class creation', async () => {
|
||||
const customConfig = Object.assign({}, config, {
|
||||
allowClientClassCreation: false,
|
||||
});
|
||||
config.database
|
||||
.loadSchema()
|
||||
.then(schema => schema.addClassIfNotExists('ClientClassCreation', {}))
|
||||
.then(actualSchema => {
|
||||
const schema = await config.database.loadSchema();
|
||||
const actualSchema = await schema.addClassIfNotExists(
|
||||
'ClientClassCreation',
|
||||
{}
|
||||
);
|
||||
expect(actualSchema.className).toEqual('ClientClassCreation');
|
||||
return rest.create(
|
||||
|
||||
await schema.reloadData({ clearCache: true });
|
||||
// Should not throw
|
||||
await rest.create(
|
||||
customConfig,
|
||||
auth.nobody(customConfig),
|
||||
'ClientClassCreation',
|
||||
{}
|
||||
);
|
||||
})
|
||||
.then(
|
||||
() => {
|
||||
done();
|
||||
},
|
||||
() => {
|
||||
fail('Should not throw error');
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it('handles user signup', done => {
|
||||
|
||||
@@ -432,6 +432,15 @@ class DatabaseController {
|
||||
return this.loadSchema(options);
|
||||
}
|
||||
|
||||
loadSchemaIfNeeded(
|
||||
schemaController: SchemaController.SchemaController,
|
||||
options: LoadSchemaOptions = { clearCache: false }
|
||||
): Promise<SchemaController.SchemaController> {
|
||||
return schemaController
|
||||
? Promise.resolve(schemaController)
|
||||
: this.loadSchema(options);
|
||||
}
|
||||
|
||||
// Returns a promise for the classname that is related to the given
|
||||
// classname through the key.
|
||||
// TODO: make this not in the DatabaseController interface
|
||||
@@ -477,7 +486,8 @@ class DatabaseController {
|
||||
update: any,
|
||||
{ acl, many, upsert }: FullQueryOptions = {},
|
||||
skipSanitization: boolean = false,
|
||||
validateOnly: boolean = false
|
||||
validateOnly: boolean = false,
|
||||
validSchemaController: SchemaController.SchemaController
|
||||
): Promise<any> {
|
||||
const originalQuery = query;
|
||||
const originalUpdate = update;
|
||||
@@ -486,7 +496,9 @@ class DatabaseController {
|
||||
var relationUpdates = [];
|
||||
var isMaster = acl === undefined;
|
||||
var aclGroup = acl || [];
|
||||
return this.loadSchema().then(schemaController => {
|
||||
|
||||
return this.loadSchemaIfNeeded(validSchemaController).then(
|
||||
schemaController => {
|
||||
return (isMaster
|
||||
? Promise.resolve()
|
||||
: schemaController.validatePermission(className, aclGroup, 'update')
|
||||
@@ -547,7 +559,8 @@ class DatabaseController {
|
||||
update[updateOperation] &&
|
||||
typeof update[updateOperation] === 'object' &&
|
||||
Object.keys(update[updateOperation]).some(
|
||||
innerKey => innerKey.includes('$') || innerKey.includes('.')
|
||||
innerKey =>
|
||||
innerKey.includes('$') || innerKey.includes('.')
|
||||
)
|
||||
) {
|
||||
throw new Parse.Error(
|
||||
@@ -620,7 +633,8 @@ class DatabaseController {
|
||||
}
|
||||
return sanitizeDatabaseResult(originalUpdate, result);
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Collect all relation-updating operations from a REST-format update.
|
||||
@@ -753,12 +767,14 @@ class DatabaseController {
|
||||
destroy(
|
||||
className: string,
|
||||
query: any,
|
||||
{ acl }: QueryOptions = {}
|
||||
{ acl }: QueryOptions = {},
|
||||
validSchemaController: SchemaController.SchemaController
|
||||
): Promise<any> {
|
||||
const isMaster = acl === undefined;
|
||||
const aclGroup = acl || [];
|
||||
|
||||
return this.loadSchema().then(schemaController => {
|
||||
return this.loadSchemaIfNeeded(validSchemaController).then(
|
||||
schemaController => {
|
||||
return (isMaster
|
||||
? Promise.resolve()
|
||||
: schemaController.validatePermission(className, aclGroup, 'delete')
|
||||
@@ -811,7 +827,8 @@ class DatabaseController {
|
||||
throw error;
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Inserts an object into the database.
|
||||
@@ -820,7 +837,8 @@ class DatabaseController {
|
||||
className: string,
|
||||
object: any,
|
||||
{ acl }: QueryOptions = {},
|
||||
validateOnly: boolean = false
|
||||
validateOnly: boolean = false,
|
||||
validSchemaController: SchemaController.SchemaController
|
||||
): Promise<any> {
|
||||
// Make a copy of the object, so we don't mutate the incoming data.
|
||||
const originalObject = object;
|
||||
@@ -836,8 +854,9 @@ class DatabaseController {
|
||||
null,
|
||||
object
|
||||
);
|
||||
|
||||
return this.validateClassName(className)
|
||||
.then(() => this.loadSchema())
|
||||
.then(() => this.loadSchemaIfNeeded(validSchemaController))
|
||||
.then(schemaController => {
|
||||
return (isMaster
|
||||
? Promise.resolve()
|
||||
@@ -1173,7 +1192,8 @@ class DatabaseController {
|
||||
pipeline,
|
||||
readPreference,
|
||||
}: any = {},
|
||||
auth: any = {}
|
||||
auth: any = {},
|
||||
validSchemaController: SchemaController.SchemaController
|
||||
): Promise<any> {
|
||||
const isMaster = acl === undefined;
|
||||
const aclGroup = acl || [];
|
||||
@@ -1186,7 +1206,8 @@ class DatabaseController {
|
||||
op = count === true ? 'count' : op;
|
||||
|
||||
let classExists = true;
|
||||
return this.loadSchema().then(schemaController => {
|
||||
return this.loadSchemaIfNeeded(validSchemaController).then(
|
||||
schemaController => {
|
||||
//Allow volatile classes if querying with Master (for _PushStatus)
|
||||
//TODO: Move volatile classes concept into mongo adapter, postgres adapter shouldn't care
|
||||
//that api.parse.com breaks when _PushStatus exists in mongo.
|
||||
@@ -1233,7 +1254,9 @@ class DatabaseController {
|
||||
? Promise.resolve()
|
||||
: schemaController.validatePermission(className, aclGroup, op)
|
||||
)
|
||||
.then(() => this.reduceRelationKeys(className, query, queryOptions))
|
||||
.then(() =>
|
||||
this.reduceRelationKeys(className, query, queryOptions)
|
||||
)
|
||||
.then(() =>
|
||||
this.reduceInRelation(className, query, schemaController)
|
||||
)
|
||||
@@ -1332,7 +1355,8 @@ class DatabaseController {
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
deleteSchema(className: string): Promise<void> {
|
||||
|
||||
@@ -646,7 +646,7 @@ export default class SchemaController {
|
||||
fields: SchemaFields = {},
|
||||
classLevelPermissions: any,
|
||||
indexes: any = {}
|
||||
): Promise<void> {
|
||||
): Promise<void | Schema> {
|
||||
var validationError = this.validateNewClass(
|
||||
className,
|
||||
fields,
|
||||
@@ -667,11 +667,6 @@ export default class SchemaController {
|
||||
})
|
||||
)
|
||||
.then(convertAdapterSchemaToParseSchema)
|
||||
.then(res => {
|
||||
return this._cache.clear().then(() => {
|
||||
return Promise.resolve(res);
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
if (error && error.code === Parse.Error.DUPLICATE_VALUE) {
|
||||
throw new Parse.Error(
|
||||
@@ -1285,6 +1280,9 @@ export default class SchemaController {
|
||||
|
||||
// Checks if a given class is in the schema.
|
||||
hasClass(className: string) {
|
||||
if (this.schemaData[className]) {
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
return this.reloadData().then(() => !!this.schemaData[className]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +69,10 @@ function RestWrite(
|
||||
|
||||
// The timestamp we'll use for this whole operation
|
||||
this.updatedAt = Parse._encode(new Date()).iso;
|
||||
|
||||
// Shared SchemaController to be reused to reduce the number of loadSchema() calls per request
|
||||
// Once set the schemaData should be immutable
|
||||
this.validSchemaController = null;
|
||||
}
|
||||
|
||||
// A convenient method to perform all the steps of processing the
|
||||
@@ -101,7 +105,8 @@ RestWrite.prototype.execute = function() {
|
||||
.then(() => {
|
||||
return this.validateSchema();
|
||||
})
|
||||
.then(() => {
|
||||
.then(schemaController => {
|
||||
this.validSchemaController = schemaController;
|
||||
return this.setRequiredFieldsIfNeeded();
|
||||
})
|
||||
.then(() => {
|
||||
@@ -614,7 +619,9 @@ RestWrite.prototype._validateUserName = function() {
|
||||
.find(
|
||||
this.className,
|
||||
{ username: this.data.username, objectId: { $ne: this.objectId() } },
|
||||
{ limit: 1 }
|
||||
{ limit: 1 },
|
||||
{},
|
||||
this.validSchemaController
|
||||
)
|
||||
.then(results => {
|
||||
if (results.length > 0) {
|
||||
@@ -645,7 +652,9 @@ RestWrite.prototype._validateEmail = function() {
|
||||
.find(
|
||||
this.className,
|
||||
{ email: this.data.email, objectId: { $ne: this.objectId() } },
|
||||
{ limit: 1 }
|
||||
{ limit: 1 },
|
||||
{},
|
||||
this.validSchemaController
|
||||
)
|
||||
.then(results => {
|
||||
if (results.length > 0) {
|
||||
@@ -854,11 +863,16 @@ RestWrite.prototype.destroyDuplicatedSessions = function() {
|
||||
if (!user.objectId) {
|
||||
return;
|
||||
}
|
||||
this.config.database.destroy('_Session', {
|
||||
this.config.database.destroy(
|
||||
'_Session',
|
||||
{
|
||||
user,
|
||||
installationId,
|
||||
sessionToken: { $ne: sessionToken },
|
||||
});
|
||||
},
|
||||
{},
|
||||
this.validSchemaController
|
||||
);
|
||||
};
|
||||
|
||||
// Handles any followup logic
|
||||
@@ -1361,7 +1375,15 @@ RestWrite.prototype.runDatabaseOperation = function() {
|
||||
return defer.then(() => {
|
||||
// Run an update
|
||||
return this.config.database
|
||||
.update(this.className, this.query, this.data, this.runOptions)
|
||||
.update(
|
||||
this.className,
|
||||
this.query,
|
||||
this.data,
|
||||
this.runOptions,
|
||||
false,
|
||||
false,
|
||||
this.validSchemaController
|
||||
)
|
||||
.then(response => {
|
||||
response.updatedAt = this.updatedAt;
|
||||
this._updateResponseWithData(response, this.data);
|
||||
@@ -1391,7 +1413,13 @@ RestWrite.prototype.runDatabaseOperation = function() {
|
||||
|
||||
// Run a create
|
||||
return this.config.database
|
||||
.create(this.className, this.data, this.runOptions)
|
||||
.create(
|
||||
this.className,
|
||||
this.data,
|
||||
this.runOptions,
|
||||
false,
|
||||
this.validSchemaController
|
||||
)
|
||||
.catch(error => {
|
||||
if (
|
||||
this.className !== '_User' ||
|
||||
|
||||
12
src/rest.js
12
src/rest.js
@@ -102,6 +102,7 @@ function del(config, auth, className, objectId) {
|
||||
enforceRoleSecurity('delete', className, auth);
|
||||
|
||||
let inflatedObject;
|
||||
let schemaController;
|
||||
|
||||
return Promise.resolve()
|
||||
.then(() => {
|
||||
@@ -151,8 +152,10 @@ function del(config, auth, className, objectId) {
|
||||
return;
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
var options = {};
|
||||
.then(() => config.database.loadSchema())
|
||||
.then(s => {
|
||||
schemaController = s;
|
||||
const options = {};
|
||||
if (!auth.isMaster) {
|
||||
options.acl = ['*'];
|
||||
if (auth.user) {
|
||||
@@ -166,12 +169,12 @@ function del(config, auth, className, objectId) {
|
||||
{
|
||||
objectId: objectId,
|
||||
},
|
||||
options
|
||||
options,
|
||||
schemaController
|
||||
);
|
||||
})
|
||||
.then(() => {
|
||||
// Notify LiveQuery server if possible
|
||||
config.database.loadSchema().then(schemaController => {
|
||||
const perms = schemaController.getClassLevelPermissions(className);
|
||||
config.liveQueryController.onAfterDelete(
|
||||
className,
|
||||
@@ -179,7 +182,6 @@ function del(config, auth, className, objectId) {
|
||||
null,
|
||||
perms
|
||||
);
|
||||
});
|
||||
return triggers.maybeRunTrigger(
|
||||
triggers.Types.afterDelete,
|
||||
auth,
|
||||
|
||||
Reference in New Issue
Block a user