About Commodo Data Models

How to create commodo Data Models and Implement using MongoDB.

Hi @shravan,

thanks for the question.

Here’s a link to the crash course - https://docs.webiny.com/docs/api-development/commodo/crash-course.

Other than that, if that doesn’t help, can you be more specific? Can you explain what are you trying to achieve?


How to implement below code in to my website in webiny. There is no clear documentation regarding MondoDB connections.

import { withStorage, withName, withFields, string, number, boolean } from “@webiny/commodo”;
import { MongoDBDriver, withId } from “@commodo/fields-storage-mongodb”;

const Dog = flow(
name: string(),
age: number(),
drools: boolean()
withStorage({ driver: new MongoDbDriver({ connection: … }) }),

You are right @shravan, there isn’t unfortunately yet.

But we are actually working on this as we speak. We are working on a simple scaffold (active PR) command that will setup everything for you, so you will be able to immediately jump to the code, without setting up the MongoDB connection yourself.

Also note that the crash course talks about Commodo library itself, and that a separate “How to use with Webiny” page is in the works as well.

If you’d like, I can update you as soon this article is up (I think a day/two should be enough), so you can continue your work.

While waiting for the docs, here’s a reference to one of our packages that uses models.
It should be enough for you to see how it works and how you can setup your own models the same way:

1 Like

How data models can be loaded into our app?
How to configure graphQL for them?

Yesterday, another user had a similar question, so you might want to check it out.

BTW the docs are still not yet complete, mainly because we want to first finish the scaffolding tool that will be a part of our CLI.

You can watch issues #733 and #741 to stay updated on the progress. We decided we want to finish this first, then create docs based on these newly added scaffolding features. Will be less docs, which means easier for the user to get started.

So in the meantime, take a look at the existing question, our answers, and linked docs. If you have any questions, feel free to ask.

And of course, again, if you’d like me to notify you once docs are up, just let me know.

Thank you!

driver: new MongoDbDriver({ database: new MongoDbDriver({ (A) }) })

What I have to write code or add code in place of (A) OR am I have to mention connection string here to load data models.

The answer to your question can be found in #741. The issue basically describes how you can structure your models, with good examples, and also explains how the driver is set.

So, the key file is api/pets/src/models.ts, in which we define the base model factory function, which we use as a base for all of our models (Pet and Trick).

Note that compose function that is imported from @webiny/commodo is not yet available (until the upcoming release). You can use flow function from lodash.flow package to achieve the same thing.

Can you try checking out the issue, and then let me know if you have any questions? I’d be glad to answer them!

Let me know when you get a chance.

P.S. Personally, I’m super excited about the new scaffolding CLI feature we’re working on at the moment. It will make things so much easier! :slightly_smiling_face: