Skip to content

Commit

Permalink
Sets the defaultSchemas keys in the SchemaCollection (#1421)
Browse files Browse the repository at this point in the history
* Sets the defaultSchemas keys in the SchemaCollection

* Moves defaultSchema injection logic to router

* maps outside the function

* fixes test

* conciseness
  • Loading branch information
flovilmart committed Apr 8, 2016
1 parent bc96f0b commit c22bafb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
29 changes: 29 additions & 0 deletions spec/schemas.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,35 @@ describe('schemas', () => {
});
});

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();
});
})
});

it('lets you specify class name in both places', done => {
request.post({
url: 'http://localhost:8378/1/schemas/NewClass',
Expand Down
12 changes: 12 additions & 0 deletions src/Routers/SchemasRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,28 @@ function classNameMismatchResponse(bodyClass, pathClass) {
);
}

function injectDefaultSchema(schema) {
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 => schemas.map(injectDefaultSchema))
.then(schemas => ({ response: { results: schemas } }));
}

function getOneSchema(req) {
const className = req.params.className;
return req.config.database.schemaCollection()
.then(collection => collection.findSchema(className))
.then(injectDefaultSchema)
.then(schema => ({ response: schema }))
.catch(error => {
if (error === undefined) {
Expand Down
1 change: 1 addition & 0 deletions src/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -848,4 +848,5 @@ export {
schemaAPITypeToMongoFieldType,
buildMergedSchemaObject,
systemClasses,
defaultColumns,
};

0 comments on commit c22bafb

Please sign in to comment.