DEV Community

Cover image for Importing Ghost Blog Users to Appwrite
Joysankar Majumdar
Joysankar Majumdar

Posted on • Updated on

Importing Ghost Blog Users to Appwrite

In Appwrite 1.0, a new feature has been added to allow users to migrate from other platforms like Firebase, WordPress, Ghost, Supabase, etc. to Appwrite. So in this tutorial we are going to migrate all the users(members) from Ghost Blog to Appwrite, and this is going to be super easy!

Let's do it 🦾

Step 1: Export users(members) from Ghost Blog

Firstly, login to your Ghost admin and go to the Settings page, and then scroll down and go to the Labs section.

Image description

Now in Labs, export your content into a json file by clicking the Export button.

Image description

You'll get a json file of all your contents like this

Image description
This json file contains all the users' data. Now we need to import all the data into the Appwrite.

Step 2: Import Users to Appwrite

To import users into Appwrite, we can use any Server SDK to access the Users API. For this example, we will use the NodeJS SDK.

Newly added functions let you create a user with one of seven different hashing algorithms. Ghost uses the Bcrypt algorithm for saving passwords. So we will use Bcrypt Password in the Appwrite Server SDK to import users.

async createBcryptUser(
        userId,
        email,
        password,
        name
      );
Enter fullscreen mode Exit fullscreen mode

Going through the function parameters one by one:

  • userId: The ID to assign to the new user in Appwrite. It can be custom, or you can use the SDK's new ID.unique() function to assign a randomly generated ID.
  • email: Corresponds to the email value from the json file.
  • password: Corresponds to the passwordHash value from json file.
  • name: The name to assign to the user. Can be null.

Putting this all together, we could import all of the exported users into Appwrite like so:

const outfile = "cozblog.ghost.2022-10-24-17-31-47.json";
  const json = await fs.readFile(outfile, { encoding: "utf8" });
  const exported = JSON.parse(json).db[0].data.users;
  await Promise.all(
    exported.map(async (user) => {
      await users.createBcryptUser(
        ID.unique(),
        user.email,
        user.password,
        user.name
      );
    })
  );
Enter fullscreen mode Exit fullscreen mode

After all the user export the Appwrite dashboard looks like this

Image description

So now the whole code looks like this


const sdk = require("node-appwrite");
const { ID } = require("node-appwrite");
const fs = require('fs/promises');

let client = new sdk.Client();

const users = new sdk.Users(client);

const endPoint = "Your End Point";
const projectId = "Your Project Id";
const apiKey = "Your Api Key";
client
  .setEndpoint(endPoint)
  .setProject(projectId)
  .setKey(apiKey)
  .setSelfSigned();

const migrate = async () => {
  const outfile = "cozblog.ghost.2022-10-24-17-31-47.json";
  const json = await fs.readFile(outfile, { encoding: "utf8" });
  const exported = JSON.parse(json).db[0].data.users;
  await Promise.all(
    exported.map(async (user) => {
      await users.createBcryptUser(
        ID.unique(),
        user.email,
        user.password,
        user.name
      );
    })
  );
};

migrate()
  .then((r) => console.log("Done!"))
  .catch((e) => console.log(e));

Enter fullscreen mode Exit fullscreen mode

Hope you learn how to migrate users from WordPress to Appwrite.
Don't forget to follow me @joysankar2001

Top comments (4)

Collapse
 
gewenyu99 profile image
Vincent Ge

Did you submit this to Awesome-Appwrite for Hacktoberfest? You should!

Collapse
 
joysankar2001 profile image
Joysankar Majumdar

Already submitted 😛

Collapse
 
codercatdev profile image
Alex Patterson

Great writeup, very 👻 like.

Collapse
 
joysankar2001 profile image
Joysankar Majumdar

Thanks @codercatdev