From 46de3bbabb2a208e7b200ea6616444fd20981998 Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Thu, 7 Apr 2016 19:45:03 -0400 Subject: [PATCH 1/5] Sets the defaultSchemas keys in the SchemaCollection --- spec/schemas.spec.js | 32 +++++++++++++++++++ .../Storage/Mongo/MongoSchemaCollection.js | 7 ++++ src/Schema.js | 1 + 3 files changed, 40 insertions(+) diff --git a/spec/schemas.spec.js b/spec/schemas.spec.js index e2d1955c08..5b90148241 100644 --- a/spec/schemas.spec.js +++ b/spec/schemas.spec.js @@ -326,6 +326,38 @@ describe('schemas', () => { }); }); + it('responds with all fields when you create a _User', done => { + request.post({ + url: 'http://localhost:8378/1/schemas', + headers: masterKeyHeaders, + json: true, + body: { + className: "_User", + fields: { + foo: {type: 'Number'}, + } + } + }, (error, response, body) => { + expect(body).toEqual({ + className: '_User', + fields: { + ACL: {type: 'ACL'}, + createdAt: {type: 'Date'}, + updatedAt: {type: 'Date'}, + objectId: {type: 'String'}, + foo: {type: 'Number'}, + username: { type: 'String' }, + password: { type: 'String' }, + authData: { type: 'Object' }, + email: { type: 'String' }, + emailVerified: { type: 'Boolean' }, + }, + classLevelPermissions: defaultClassLevelPermissions + }); + done(); + }); + }); + it('lets you specify class name in both places', done => { request.post({ url: 'http://localhost:8378/1/schemas/NewClass', diff --git a/src/Adapters/Storage/Mongo/MongoSchemaCollection.js b/src/Adapters/Storage/Mongo/MongoSchemaCollection.js index 2f5adbbfb4..b02e401e18 100644 --- a/src/Adapters/Storage/Mongo/MongoSchemaCollection.js +++ b/src/Adapters/Storage/Mongo/MongoSchemaCollection.js @@ -1,5 +1,6 @@ import MongoCollection from './MongoCollection'; +import { defaultColumns } from '../../../Schema'; function mongoFieldToParseSchemaField(type) { if (type[0] === '*') { @@ -34,6 +35,12 @@ function mongoSchemaFieldsToParseSchemaFields(schema) { obj[fieldName] = mongoFieldToParseSchemaField(schema[fieldName]) return obj; }, {}); + let defaultSchema = defaultColumns[schema]; + if (defaultSchema) { + Object.keys(defaultSchema).forEach((key) => { + response[key] = defaultSchema[key]; + }); + } response.ACL = {type: 'ACL'}; response.createdAt = {type: 'Date'}; response.updatedAt = {type: 'Date'}; diff --git a/src/Schema.js b/src/Schema.js index 8311ef482e..f2f188cb57 100644 --- a/src/Schema.js +++ b/src/Schema.js @@ -848,4 +848,5 @@ export { schemaAPITypeToMongoFieldType, buildMergedSchemaObject, systemClasses, + defaultColumns, }; From 2f8f4172e3ec7e95b4a4542f7c1121c1b126e7d8 Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Thu, 7 Apr 2016 19:58:36 -0400 Subject: [PATCH 2/5] Moves defaultSchema injection logic to router --- .../Storage/Mongo/MongoSchemaCollection.js | 7 ------- src/Routers/SchemasRouter.js | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/Adapters/Storage/Mongo/MongoSchemaCollection.js b/src/Adapters/Storage/Mongo/MongoSchemaCollection.js index b02e401e18..2f5adbbfb4 100644 --- a/src/Adapters/Storage/Mongo/MongoSchemaCollection.js +++ b/src/Adapters/Storage/Mongo/MongoSchemaCollection.js @@ -1,6 +1,5 @@ import MongoCollection from './MongoCollection'; -import { defaultColumns } from '../../../Schema'; function mongoFieldToParseSchemaField(type) { if (type[0] === '*') { @@ -35,12 +34,6 @@ function mongoSchemaFieldsToParseSchemaFields(schema) { obj[fieldName] = mongoFieldToParseSchemaField(schema[fieldName]) return obj; }, {}); - let defaultSchema = defaultColumns[schema]; - if (defaultSchema) { - Object.keys(defaultSchema).forEach((key) => { - response[key] = defaultSchema[key]; - }); - } response.ACL = {type: 'ACL'}; response.createdAt = {type: 'Date'}; response.updatedAt = {type: 'Date'}; diff --git a/src/Routers/SchemasRouter.js b/src/Routers/SchemasRouter.js index 3babd66828..f267d6f3ce 100644 --- a/src/Routers/SchemasRouter.js +++ b/src/Routers/SchemasRouter.js @@ -14,9 +14,26 @@ function classNameMismatchResponse(bodyClass, pathClass) { ); } +function injectDefaultSchema(schema) { + if (Array.isArray(schema)) { + let schemas = schema.map((s) => { + return injectDefaultSchema(s); + }); + return schemas; + } + let defaultSchema = Schema.defaultColumns[schema.className]; + if (defaultSchema) { + Object.keys(defaultSchema).forEach((key) => { + schema.fields[key] = defaultSchema[key]; + }); + } + return schema; +} + function getAllSchemas(req) { return req.config.database.schemaCollection() .then(collection => collection.getAllSchemas()) + .then(schemas => injectDefaultSchema(schemas)) .then(schemas => ({ response: { results: schemas } })); } @@ -24,6 +41,7 @@ function getOneSchema(req) { const className = req.params.className; return req.config.database.schemaCollection() .then(collection => collection.findSchema(className)) + .then(schema => injectDefaultSchema(schema)) .then(schema => ({ response: schema })) .catch(error => { if (error === undefined) { From dd48684dd5d694522879933a37c828a59a69ba4c Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Thu, 7 Apr 2016 21:36:51 -0400 Subject: [PATCH 3/5] maps outside the function --- src/Routers/SchemasRouter.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Routers/SchemasRouter.js b/src/Routers/SchemasRouter.js index f267d6f3ce..6bcb3f7320 100644 --- a/src/Routers/SchemasRouter.js +++ b/src/Routers/SchemasRouter.js @@ -15,12 +15,6 @@ function classNameMismatchResponse(bodyClass, pathClass) { } function injectDefaultSchema(schema) { - if (Array.isArray(schema)) { - let schemas = schema.map((s) => { - return injectDefaultSchema(s); - }); - return schemas; - } let defaultSchema = Schema.defaultColumns[schema.className]; if (defaultSchema) { Object.keys(defaultSchema).forEach((key) => { @@ -33,7 +27,11 @@ function injectDefaultSchema(schema) { function getAllSchemas(req) { return req.config.database.schemaCollection() .then(collection => collection.getAllSchemas()) - .then(schemas => injectDefaultSchema(schemas)) + .then(schemas => { + return schemas.map((schema) => { + return injectDefaultSchema(schema); + }) + }) .then(schemas => ({ response: { results: schemas } })); } From edd87c90a4e4f975958750c81d631ce19e93e9ab Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Thu, 7 Apr 2016 21:48:04 -0400 Subject: [PATCH 4/5] fixes test --- spec/schemas.spec.js | 55 +++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/spec/schemas.spec.js b/spec/schemas.spec.js index 5b90148241..568dd2b866 100644 --- a/spec/schemas.spec.js +++ b/spec/schemas.spec.js @@ -326,36 +326,33 @@ describe('schemas', () => { }); }); - it('responds with all fields when you create a _User', done => { - request.post({ - url: 'http://localhost:8378/1/schemas', - headers: masterKeyHeaders, - json: true, - body: { - className: "_User", - fields: { - foo: {type: 'Number'}, - } - } - }, (error, response, body) => { - expect(body).toEqual({ - className: '_User', - fields: { - ACL: {type: 'ACL'}, - createdAt: {type: 'Date'}, - updatedAt: {type: 'Date'}, - objectId: {type: 'String'}, - foo: {type: 'Number'}, - username: { type: 'String' }, - password: { type: 'String' }, - authData: { type: 'Object' }, - email: { type: 'String' }, - emailVerified: { type: 'Boolean' }, - }, - classLevelPermissions: defaultClassLevelPermissions + it('responds with all fields when getting incomplete schema', done => { + config.database.schemaCollection().then((schema) => { + return schema.addSchema('_User'); + }).then(() => { + request.get({ + url: 'http://localhost:8378/1/schemas/_User', + headers: masterKeyHeaders, + json: true + }, (error, response, body) => { + expect(body).toEqual({ + className: '_User', + fields: { + objectId: {type: 'String'}, + updatedAt: {type: 'Date'}, + createdAt: {type: 'Date'}, + username: {type: 'String'}, + password: {type: 'String'}, + authData: {type: 'Object'}, + email: {type: 'String'}, + emailVerified: {type: 'Boolean'}, + ACL: {type: 'ACL'} + }, + classLevelPermissions: defaultClassLevelPermissions + }); + done(); }); - done(); - }); + }) }); it('lets you specify class name in both places', done => { From ba3c20cbea27c1c3b1445fff3691d62cd610a1bc Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Thu, 7 Apr 2016 21:55:28 -0400 Subject: [PATCH 5/5] conciseness --- src/Routers/SchemasRouter.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Routers/SchemasRouter.js b/src/Routers/SchemasRouter.js index 6bcb3f7320..23ad4b56f8 100644 --- a/src/Routers/SchemasRouter.js +++ b/src/Routers/SchemasRouter.js @@ -27,11 +27,7 @@ function injectDefaultSchema(schema) { function getAllSchemas(req) { return req.config.database.schemaCollection() .then(collection => collection.getAllSchemas()) - .then(schemas => { - return schemas.map((schema) => { - return injectDefaultSchema(schema); - }) - }) + .then(schemas => schemas.map(injectDefaultSchema)) .then(schemas => ({ response: { results: schemas } })); } @@ -39,7 +35,7 @@ function getOneSchema(req) { const className = req.params.className; return req.config.database.schemaCollection() .then(collection => collection.findSchema(className)) - .then(schema => injectDefaultSchema(schema)) + .then(injectDefaultSchema) .then(schema => ({ response: schema })) .catch(error => { if (error === undefined) {