Skip to content

Commit c22bafb

Browse files
committed
Sets the defaultSchemas keys in the SchemaCollection (#1421)
* Sets the defaultSchemas keys in the SchemaCollection * Moves defaultSchema injection logic to router * maps outside the function * fixes test * conciseness
1 parent bc96f0b commit c22bafb

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

spec/schemas.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,35 @@ describe('schemas', () => {
326326
});
327327
});
328328

329+
it('responds with all fields when getting incomplete schema', done => {
330+
config.database.schemaCollection().then((schema) => {
331+
return schema.addSchema('_User');
332+
}).then(() => {
333+
request.get({
334+
url: 'http://localhost:8378/1/schemas/_User',
335+
headers: masterKeyHeaders,
336+
json: true
337+
}, (error, response, body) => {
338+
expect(body).toEqual({
339+
className: '_User',
340+
fields: {
341+
objectId: {type: 'String'},
342+
updatedAt: {type: 'Date'},
343+
createdAt: {type: 'Date'},
344+
username: {type: 'String'},
345+
password: {type: 'String'},
346+
authData: {type: 'Object'},
347+
email: {type: 'String'},
348+
emailVerified: {type: 'Boolean'},
349+
ACL: {type: 'ACL'}
350+
},
351+
classLevelPermissions: defaultClassLevelPermissions
352+
});
353+
done();
354+
});
355+
})
356+
});
357+
329358
it('lets you specify class name in both places', done => {
330359
request.post({
331360
url: 'http://localhost:8378/1/schemas/NewClass',

src/Routers/SchemasRouter.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,28 @@ function classNameMismatchResponse(bodyClass, pathClass) {
1414
);
1515
}
1616

17+
function injectDefaultSchema(schema) {
18+
let defaultSchema = Schema.defaultColumns[schema.className];
19+
if (defaultSchema) {
20+
Object.keys(defaultSchema).forEach((key) => {
21+
schema.fields[key] = defaultSchema[key];
22+
});
23+
}
24+
return schema;
25+
}
26+
1727
function getAllSchemas(req) {
1828
return req.config.database.schemaCollection()
1929
.then(collection => collection.getAllSchemas())
30+
.then(schemas => schemas.map(injectDefaultSchema))
2031
.then(schemas => ({ response: { results: schemas } }));
2132
}
2233

2334
function getOneSchema(req) {
2435
const className = req.params.className;
2536
return req.config.database.schemaCollection()
2637
.then(collection => collection.findSchema(className))
38+
.then(injectDefaultSchema)
2739
.then(schema => ({ response: schema }))
2840
.catch(error => {
2941
if (error === undefined) {

src/Schema.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,4 +848,5 @@ export {
848848
schemaAPITypeToMongoFieldType,
849849
buildMergedSchemaObject,
850850
systemClasses,
851+
defaultColumns,
851852
};

0 commit comments

Comments
 (0)