Using Admin SDK Script to Export Models from One Space/Organization to Another

How to Use Admin SDK Script for Exporting Models Between Spaces/Organizations

Prerequisites:

  1. Admin SDK installed.
  2. Necessary permissions for both source and destination spaces/organizations.
  3. Private API key of source and destination space.

Example Script:

const { createAdminApiClient } = require("@builder.io/admin-sdk");

// Admin SDK for the source space
const adminSDKSource = createAdminApiClient("source-space-private-api-key");

// Admin SDK for the target space
const adminSDKTarget = createAdminApiClient("target-space-private-api-key");

// Function to get data models from the source space
async function getDataModels() {
  const models = await adminSDKSource.query({
    models: {
      name: true,
      id: true,
      fields: true,
      kind: true,
    },
  }).then((res) => res.data.models);
  
  return models.filter(model => model.kind === 'data');
}

// Function to recursively map fields, including subfields if present
function mapFields(fields) {
  return fields.map(field => {
    const mappedField = {
      type: field.type,
      "@type": "@builder.io/core:Field",
      required: field.required,
      hideFromFieldsEditor: field.hideFromFieldsEditor,
      name: field.name,
      helperText: field.helperText,
    };

    if (field.subFields && Array.isArray(field.subFields)) {
      mappedField.subFields = mapFields(field.subFields);
    }

    return mappedField;
  });
}

// Function to add data models to the target space
async function addDataModelToTarget(targetModel) {
  try {
    const response = await adminSDKTarget.chain.mutation.addModel({
      body: {
        designerVersion: 1,
        name: targetModel.name,
        kind: "data",
        ownerId: "Target Space public API Key", // You might want to replace this with the correct ownerId for the target space
        examplePageUrl: "http://localhost:3000/",
        helperText: targetModel.helperText || "Data model added from source space",
        pathPrefix: "/",
        fields: mapFields(targetModel.fields), // Use the recursive mapping function here
        // Add other relevant fields if needed
      },
    }).execute({
      id: true,
      name: true,
      kind: true,
    });

    console.log("Create model", response);
  } catch (error) {
    console.error("Error creating model:", error);
  }
}

(async () => {
  const dataModels = await getDataModels();

  for (const model of dataModels) {

    console.log("Processing model:", model.name);
    await addDataModelToTarget(model);
  }

  console.log("Finished processing all data models.");
})();

Notes:

  • This script demonstrates how to export data models. Modify the script accordingly to export all types of models.
  • This example maps core fields. If you have reference fields or other types of fields, you might need to alter the script to handle them appropriately.

Resources

  1. Builder Admin API - Builder.io
  2. Admin GraphQL Schema - Builder.io
  3. builder/packages/admin-sdk at main · BuilderIO/builder · GitHub