How to copy models from a project to another project?

Is there a way to copy all models with their contents in a project to another project?
I have the access token of a project which consists of many models, and I want to copy all the models to my project.

Can we do this programatically?

Yes @henok.tesfaye sure you can!

Just instantiate two clients, one per project. Then you read from one and write in the other. Does this work for you?

Thank you @mat_jack1. Let me try this.

Sorry to bump this old thread, but could you expand upon this? An eli5 would greatly help.

Hello @Mason and welcome to the community!

I’m assuming you are doing this on an already existing project, as if you just want to duplicate a project with the same models as another project you can click on “Duplicate Project” in the dashboard:

And then selecting “Duplicate only models and fields”:

image

Here is a detailed step by step explanation of the process for an already existing project using the both clients approach:

If you are using the Dato NodeJS client you can create a client based on the API token of a project.

So lets say you have two projects, one has the Full Access API token 11111 and the second project has the Full Access API token 22222

In your NodeJS script you would initialize two different clients:

import { buildClient } from '@datocms/cma-client-node';

const clientProjectOne = buildClient({ apiToken: '11111' });
const clientProjectTwo = buildClient({ apiToken: '22222' });

Now lets say you want to copy all of the models in Project One to Project two.
First you would read all of the models on the first project:
(Inside an async function:)

 const modelsFromProjectOne = await clientProjectOne.itemTypes.list();

Now, we would loop through each model, to create a new model corresponding to it on project two:

modelsFromProjectOne.forEach((model) => {
    const newModelInProjectTwo = await clientProjectTwo.itemTypes.create(model);
});

This would however only transfer the models, and not the fields inside the models, if we’d like to transfer also the fields inside each model, than we would have to increment this loop with a couple of things, a call to get all fields from that model in project one, and then for all of the fetch fields, loop through them creating them inside the new model in project two:

modelsFromProjectOne.forEach((model) => { //looping through each model in the project
    const newModelInProjectTwo = await clientProjectTwo.itemTypes.create(model);
    const fieldsFromProjectOneModel = await clientProjectOne.fields.list(model.id);
    fieldsFromProjectOneModel.forEach((field) => { //looping through each field in the model
        const field = await client.fields.create(model.id, field);
    });
});

Then, you after running this you will have transferred all of the models from project one to project two :slight_smile:

Keep in mind that this does not take into count if the project two has already a model with the same api key as project one.
You could do a different iteration of this to detect this conflict of API keys, and update the fields in the model if the models already exists.