Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
70 changes: 60 additions & 10 deletions backend/helpers/getModelDescriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,68 @@ const formatSchemaPath = (path, schemaType) => `- ${path}: ${formatSchemaTypeIns
formatRef(schemaType) +
(schemaType.schema ? formatNestedSchema(schemaType) : '');

const listModelPaths = Model => [
...Object.entries(Model.schema.paths).map(
const indentLines = (value, spaces = 2) => value.split('\n').map(line => `${' '.repeat(spaces)}${line}`).join('\n');

const normalizeFunctionSource = fn => {
const source = fn.toString();
const lines = source.split('\n');

if (lines.length <= 1) {
return source;
}

const [firstLine, ...rest] = lines;
const indentLengths = rest
.filter(line => line.trim().length > 0)
.map(line => line.match(/^\s*/)[0].length);
const minIndent = indentLengths.length > 0 ? Math.min(...indentLengths) : 0;
const normalizedRest = rest.map(line => line.slice(Math.min(line.length, minIndent)));

return [firstLine, ...normalizedRest].join('\n');
};

const formatFieldSection = Model => {
const fieldLines = Object.entries(Model.schema.paths).map(
([path, schemaType]) => formatSchemaPath(path, schemaType)
),
...Object.entries(Model.schema.virtuals).filter(([path, virtual]) => virtual.options?.ref).map(
);
return fieldLines.length ? ['Fields:', ...fieldLines] : [];
};

const formatVirtualSection = Model => {
const virtualLines = Object.entries(Model.schema.virtuals).filter(([path, virtual]) => virtual.options?.ref).map(
([path, virtual]) => `- ${path}: Virtual (ref: ${virtual.options.ref})`
)
].join('\n');
);
return virtualLines.length ? ['Virtuals:', ...virtualLines] : [];
};

const formatMethodSection = Model => {
const methodEntries = Object.entries(Model.schema.methods || {});
if (!methodEntries.length) {
return [];
}

return ['Methods:', ...methodEntries.flatMap(([name, fn]) => [`- ${name}:`, indentLines(normalizeFunctionSource(fn), 2)])];
};

const formatStaticSection = Model => {
const staticEntries = Object.entries(Model.schema.statics || {});
if (!staticEntries.length) {
return [];
}

return ['Statics:', ...staticEntries.flatMap(([name, fn]) => [`- ${name}:`, indentLines(normalizeFunctionSource(fn), 2)])];
};

const getModelDescriptions = db => Object.values(db.models).filter(Model => !Model.modelName.startsWith('__Studio')).map(Model => {
const sections = [
`${Model.modelName} (collection: ${Model.collection.collectionName})`,
...formatFieldSection(Model),
...formatVirtualSection(Model),
...formatMethodSection(Model),
...formatStaticSection(Model)
];

const getModelDescriptions = db => Object.values(db.models).filter(Model => !Model.modelName.startsWith('__Studio')).map(Model => `
${Model.modelName} (collection: ${Model.collection.collectionName})
${listModelPaths(Model)}
`.trim()).join('\n\n');
return sections.join('\n');
}).join('\n\n');

module.exports = getModelDescriptions;
43 changes: 43 additions & 0 deletions test/helpers.getModelDescriptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('getModelDescriptions', function() {
result,
dedent(`
User (collection: users)
Fields:
- name: String
- age: Number
- email: String
Expand All @@ -57,6 +58,7 @@ describe('getModelDescriptions', function() {
result,
dedent(`
Book (collection: books)
Fields:
- title: String
- author: ObjectId (ref: User)
- _id: ObjectId
Expand All @@ -82,9 +84,11 @@ describe('getModelDescriptions', function() {
result,
dedent(`
User (collection: users)
Fields:
- name: String
- _id: ObjectId
- __v: Number
Virtuals:
- books: Virtual (ref: Book)
`)
);
Expand Down Expand Up @@ -117,11 +121,13 @@ describe('getModelDescriptions', function() {
result,
dedent(`
User (collection: users)
Fields:
- name: String
- _id: ObjectId
- __v: Number

Book (collection: books)
Fields:
- title: String
- author: ObjectId (ref: User)
- _id: ObjectId
Expand All @@ -147,6 +153,7 @@ describe('getModelDescriptions', function() {
result,
dedent(`
Book (collection: books)
Fields:
- title: String
- tags: String[]
- authors: Subdocument[]
Expand All @@ -159,4 +166,40 @@ describe('getModelDescriptions', function() {
`)
);
});

it('should include methods and statics with their source code', function() {
conn = mongoose.createConnection();
const UserSchema = new Schema({ name: String });
UserSchema.methods.greet = function(prefix) {
return `${prefix} ${this.name}`;
};
UserSchema.statics.findByName = function(name) {
return this.findOne({ name });
};

conn.model('User', UserSchema, 'users');

const result = getModelDescriptions(conn);

assert.strictEqual(
result,
dedent(`
User (collection: users)
Fields:
- name: String
- _id: ObjectId
- __v: Number
Methods:
- greet:
function(prefix) {
return \`\${prefix} \${this.name}\`;
}
Statics:
- findByName:
function(name) {
return this.findOne({ name });
}
`)
);
});
});