Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions spec/schemas.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
18 changes: 18 additions & 0 deletions src/Routers/SchemasRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,34 @@ function classNameMismatchResponse(bodyClass, pathClass) {
);
}

function injectDefaultSchema(schema) {
if (Array.isArray(schema)) {
let schemas = schema.map((s) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than have a function that accepts a schema or an array of schemas, can we have one that only accepts a single schema, and do the map outside the function if it needs it?

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

function getOneSchema(req) {
const className = req.params.className;
return req.config.database.schemaCollection()
.then(collection => collection.findSchema(className))
.then(schema => injectDefaultSchema(schema))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be more concise here too if you feel like it:

.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,
};