महीनों तक टालने से लेकर 182k users migrate करने तक: आख़िरकार मैंने अपना app Supabase पर कैसे shift किया।
182,000 users को नए backend पर ले जाना कोई छोटा काम नहीं है, ख़ासकर जब आप full-time job के साथ अकेले mobile dev हों। लेकिन मैंने कर दिखाया। मैंने अपने app OSM Tactics को एक उलझे हुए NestJS + MongoDB setup से Supabase पर shift किया, जो इस समय backend की दुनिया में सबसे चर्चित topic है। इस article में मैं आपको पूरा सफ़र बताऊँगा: मैंने Supabase क्यों चुना, किन challenges का सामना किया और migration कैसे पूरा किया।
Supabase ही क्यों?
जब मेरे app का backend पुराना लगने लगा, तो मैंने कुछ बेहतर ढूँढना शुरू किया, और Supabase पर मेरी नज़र पड़ी। यह एक open-source चीज़ है जो Firebase जैसी है लेकिन शुरू करने में आसान। यह आपको एक आसान API के साथ PostgreSQL database, real-time features और login features, सब तैयार देता है। मेरे जैसे mobile apps बनाने वाले के लिए यह एकदम सही है, मैं complicated server के झंझट में नहीं पड़ना चाहता। Supabase project लगभग पाँच मिनट में चालू हो जाता है, और docs बहुत सीधे हैं, इसलिए backend pro न होते हुए भी मुझे कभी खोया हुआ महसूस नहीं हुआ।
लेकिन Supabase सिर्फ़ आसान नहीं है, यह बड़ा भी हो रहा है। यह अभी Firebase जितना बड़ा नहीं है, लेकिन ढेरों developers इसे इस्तेमाल कर रहे हैं, और community तेज़ी से बढ़ रही है। इसमें वह सब है जो मुझे चाहिए: data रखने के लिए database, login tools, files upload करने की जगह, और छोटे functions भी जिन्हें मैं पूरे server के बिना चला सकता हूँ। Firebase छुपाता है कि वह कैसे काम करता है, लेकिन Supabase PostgreSQL इस्तेमाल करता है, जो एक पुराना, भरोसेमंद database है जिस पर बहुत लोग भरोसा करते हैं। इसका मतलब है कि मैं चीज़ें अपनी मर्ज़ी से बदल सकता हूँ और बाद में बिना फँसे इसे बड़ा कर सकता हूँ। साथ ही, online developers का एक बड़ा group है जो GitHub जैसी जगहों पर tips, tricks और fixes share करता है। मुझे पता था कि अगर मैं कहीं अटका, तो Google कर सकता हूँ या किसी से पूछ सकता हूँ। मेरे जैसे अकेले coder के लिए, जिसके पास समय कम है, Supabase सही चुनाव लगा: यह सरल है लेकिन मेरे app को संभालने लायक मज़बूत भी।
बड़े बदलाव से पहले का पुराना setup

Supabase पर जाने से पहले, बात करते हैं कि मैं क्या इस्तेमाल कर रहा था। मेरा app OSM Tactics Android और iOS के लिए एक mobile game है जहाँ लोग tactic strategies बनाते हैं; अगर आपको यह सब पसंद है तो यह काफ़ी मज़ेदार है। Backend मैंने ख़ुद NestJS से बनाया था, जो Node.js का एक framework है और मुझे पसंद था क्योंकि यह TypeScript के साथ काम करता है और चीज़ों को व्यवस्थित रखता है। यह MongoDB से बात करता था, एक database जहाँ मैं सारी user info, game data और बाकी चीज़ें रखता था। मैं यह setup लगभग चार साल से चला रहा था।
_Migration से पहले mongodb का आख़िरी snapshot, Osm Tactics उर्फ़ “Osm Guide” _
लेकिन दिक्कत यह थी: मैं full time job वाला अकेला इंसान हूँ, और इस backend को ज़िंदा रखना मुझे थका रहा था। शुरू में NestJS और MongoDB ठीक थे, मैं उन्हें संभाल लेता था। लेकिन जैसे जैसे users बढ़े, चीज़ें बिगड़ने लगीं। मुझे यह पक्का करने में बहुत ज़्यादा समय लगाना पड़ता था कि server crash न हो, security holes ठीक करने पड़ते थे और उसे काफ़ी तेज़ रखना पड़ता था। Security सबसे बुरा हिस्सा था: मुझे थोड़ा Node.js आता है, लेकिन मैं mobile app वाला हूँ, backend expert नहीं। यह किसी तरह चल तो रहा था, लेकिन एक पुरानी car जैसा था जो बार बार खराब होती रहती है। मैं इसे हमेशा जोड़-तोड़ कर नहीं चला सकता था; मुझे कुछ नया चाहिए था जो मुझे बाल नोचने पर मजबूर न करे।
छलांग: migrate करने का फ़ैसला

सब कुछ shift करने का फ़ैसला जल्दी नहीं हुआ, इसमें बहुत समय लगा क्योंकि मैं इसे टालता रहा। OSM Tactics ढेरों features वाला कोई विशाल app नहीं है, लेकिन 182k users का मतलब संभालने के लिए बहुत सारा data है। MongoDB से, जहाँ data खुला और बेतरतीब होता है, Supabase के PostgreSQL पर जाना, जहाँ सब कुछ साफ़ tables में होता है, मुश्किल लग रहा था। मुझे data का ढाँचा फिर से बनाना पड़ता और पक्का करना पड़ता कि कुछ ज़रूरी न खो जाए। पुराना NestJS-MongoDB combo अभी भी चल रहा था, लेकिन मुझे बुरा एहसास था कि यह जल्द ही फट जाएगा। यह चिंता मुझे तब तक परेशान करती रही जब तक मैंने आख़िरकार नहीं कहा, "ठीक है, चलो करते हैं।"
फ़ैसला करने के बाद, मुझे अच्छी तरह planning करनी पड़ी। मैं बैठा और सोचा कि अपनी MongoDB चीज़ों को Supabase tables में कैसे बदलूँ, जैसे एक नक्शा बनाना कि हर चीज़ कहाँ जाएगी। पहला कदम था अपना सारा data MongoDB से सुरक्षित निकालना, इसलिए मैंने उसे JSON files में dump करने के लिए एक छोटा script लिखा। यह रहा वह code, basic है लेकिन काम कर गया:
async function exportCollection(collectionName, outputFileName, query = {}) {
try {
await mongoose.connect(mongoURI);
const Model = mongoose.model(
collectionName,
genericSchema,
collectionName.toLowerCase()
);
const documents = await Model.find(query).lean();
const outputPath = path.join(__dirname, outputFileName);
fs.writeFileSync(outputPath, JSON.stringify(documents, null, 2));
await mongoose.disconnect();
return documents;
} catch (error) {
if (mongoose.connection.readyState !== 0) {
await mongoose.disconnect();
}
}
}
यह script MongoDB से जुड़ा, सारा data निकाला और उसे मेरे computer पर JSON files के रूप में save किया। यह एक backup जैसा था: अगर मुझसे गड़बड़ हो जाती, तो भी मैं सब कुछ खोए बिना प्रयोग कर सकता था। मैंने इसे हर collection के लिए चलाया, files check कीं कि सही दिख रही हैं, और यह जानकर थोड़ा बेहतर लगा कि मेरे पास copies हैं। फिर भी, असली काम शुरू करने में मैं टालमटोल करता रहा; screen को घूरता, coffee पीता और सोचता, "शायद कल।" आलस छोड़कर सच में काम में लगने के लिए बड़ा धक्का चाहिए था, लेकिन एक बार लग गया, तो पूरी तरह लग गया।
Users migrate करना: password की पहेली
यहीं से मामला सच में पेचीदा हुआ: 182k users को shift करना और उनके passwords को चालू रखना। Users उम्मीद करते हैं कि app update के बाद वे उसी email और password से login कर सकें, ऐसा ही होना चाहिए। लेकिन इसे करना एक बुरा सपना था। अच्छी बात? Supabase passwords के लिए bcrypt इस्तेमाल करता है, बिल्कुल MongoDB जैसा, इसलिए मुझे सबको फिर से बनाना या users से नए passwords चुनवाना नहीं पड़ा। वह बहुत बुरा experience होता: लोगों को password reset करना नफ़रत है, और मैं उनमें से बहुत से users खो देता।
Supabase की नखरेबाज़ auth table से निपटना
Supabase में logins के लिए auth.users table है, और यह बहुत सख़्त है। इसमें ढेरों fields हैं जिन्हें बिल्कुल सही भरना पड़ता है, वरना यह भड़क जाती है; जैसे अगर null की जगह खाली string डाल दी, तो यह टूट जाती है। मुझे इस GitHub thread में दूसरे लोगों की errors मिलीं, और उन्होंने मुझे सतर्क कर दिया। मुझे इसे एकदम सही match करना था। Supabase के लिए एक user ऐसा दिखता था (मैंने phone वाली चीज़ें इस्तेमाल नहीं कीं, इसलिए वे null हैं; अगर आप करते हैं तो बदल लें):
const supabaseUser = {
id: uid,
email: user.email,
encrypted_password: user.password || null,
instance_id: "00000000–0000–0000–0000–000000000000",
aud: "authenticated",
role: "authenticated",
email_confirmed_at: user.createdAt
? new Date(user.createdAt).toISOString()
: new Date().toISOString(),
created_at: user.createdAt
? new Date(user.createdAt).toISOString()
: new Date().toISOString(),
updated_at: user.updatedAt ? new Date(user.updatedAt).toISOString() : null,
last_sign_in_at: null,
phone: user.phone || null,
phone_confirmed_at: null,
confirmed_at: user.createdAt
? new Date(user.createdAt).toISOString()
: new Date().toISOString(),
invited_at: null,
confirmation_token: "",
confirmation_sent_at: null,
recovery_token: "",
recovery_sent_at: null,
email_change_token_new: "",
email_change: "",
email_change_sent_at: null,
email_change_token_current: "",
email_change_confirm_status: 0,
banned_until: null,
reauthentication_token: "",
reauthentication_sent_at: null,
is_super_admin: null,
phone_change: "",
phone_change_token: "",
phone_change_sent_at: null,
is_sso_user: false,
deleted_at: null,
is_anonymous: false,
raw_user_meta_data: JSON.stringify(rawUserMetaData),
raw_app_meta_data: JSON.stringify(rawAppMetaData),
};
मुझे हर एक field सही रखनी थी वरना Supabase उसे नहीं लेता। अपने सारे MongoDB users shift करने के लिए मैंने एक बड़ा script लिखा जो उन्हें लेता, इस format में बदलता और एक CSV file के रूप में save करता। यह रहा वह script, लंबा है लेकिन पूरा process दिखाता है:
const fs = require("fs");
const path = require("path");
const { parse } = require("json2csv");
const { v4: uuidv4 } = require("uuid");
const mongoose = require("mongoose");
const mongoURI = "your-connection-string";
// Define a generic schema for flexible document structure
const userSchema = new mongoose.Schema({}, { strict: false });
async function fetchUsersFromMongoDB() {
try {
console.log("Connecting to MongoDB…");
await mongoose.connect(mongoURI);
console.log("Connected to MongoDB");
const UserModel = mongoose.model("Users", userSchema, "users");
console.log("Fetching users from MongoDB…");
const users = await UserModel.find({}).lean();
console.log(`Found ${users.length} users in MongoDB`);
await mongoose.disconnect();
console.log("Disconnected from MongoDB");
return users;
} catch (error) {
console.error("Error fetching users from MongoDB:", error);
if (mongoose.connection.readyState !== 0) {
await mongoose.disconnect();
}
throw error;
}
}
async function processUsers() {
console.time("Total processing time");
const mongoUsersFilePath = path.join(__dirname, "mongo_users.json");
try {
// Step 1: Fetch users from MongoDB
const users = await fetchUsersFromMongoDB();
// Step 2: Write users to a fresh JSON file
fs.writeFileSync(mongoUsersFilePath, JSON.stringify(users, null, 2));
console.log(`Fresh users data written to: ${mongoUsersFilePath}`);
// Step 3: Read the fresh users file
const usersData = JSON.parse(fs.readFileSync(mongoUsersFilePath, "utf8"));
console.log(`Read ${usersData.length} users from fresh file`);
// Step 4: Filter users to include only those with an email or at least one provider
const filteredUsers = usersData.filter((user) => {
return user.email || user.googleId || user.appleId;
});
console.log(`Filtered users: ${filteredUsers.length}`);
// Create a Set to track used IDs and ensure uniqueness
const usedIds = new Set();
const usedEmails = new Set();
const duplicateEmails = [];
// Step 5: Transform the data to match Supabase Auth users structure
const transformedUsers = [];
filteredUsers.forEach((user) => {
if (!user.email) {
console.log("Skipping user without email:", user._id);
return;
}
// Check for duplicate email
const lowerEmail = user.email.toLowerCase();
if (usedEmails.has(lowerEmail)) {
duplicateEmails.push({
email: user.email,
id: user._id,
googleId: user.googleId,
appleId: user.appleId,
});
console.warn(`Duplicate email detected and skipped: ${user.email}`);
return;
}
// Mark this email as used
usedEmails.add(lowerEmail);
let uid = uuidv4();
while (usedIds.has(uid)) {
console.warn("Duplicate ID detected:", uid);
uid = uuidv4();
}
usedIds.add(uid);
const rawUserMetaData = {
sub: uid,
email: user.email,
email_verified: true,
phone_verified: false,
};
const providers = [];
// Check for Google ID
if (user.googleId) {
providers.push("google");
rawUserMetaData.iss = "https://accounts.google.com";
rawUserMetaData.sub = user.googleId;
rawUserMetaData.provider_id = user.googleId;
}
// Check for Apple ID
if (user.appleId) {
providers.push("apple"); // Only override iss and sub if Google wasn't already set
if (!user.googleId) {
rawUserMetaData.iss = "https://appleid.apple.com";
rawUserMetaData.sub = user.appleId;
rawUserMetaData.provider_id = user.appleId;
}
}
// If no external providers, use email
if (providers.length === 0) {
providers.push("email");
}
// Set the raw app metadata
const rawAppMetaData = {
provider: providers[0], // Primary provider is the first one
providers: providers, // All providers in an array
};
transformedUsers.push({
id: uid,
email: user.email || null,
encrypted_password: user.password,
instance_id: "00000000–0000–0000–0000–000000000000",
aud: "authenticated",
role: "authenticated",
invited_at: null,
confirmation_token: "",
confirmation_sent_at: null,
recovery_token: "",
recovery_sent_at: null,
email_change_token_new: "",
email_change: "",
email_change_sent_at: null,
last_sign_in_at: new Date().toISOString(),
is_super_admin: null,
phone: "",
phone_confirmed_at: null,
phone_change: "",
phone_change_token: "",
phone_change_sent_at: null,
confirmed_at: new Date().toISOString(),
email_change_token_current: "",
email_change_confirm_status: 0,
banned_until: null,
reauthentication_token: "",
reauthentication_sent_at: null,
is_sso_user: false,
deleted_at: null,
is_anonymous: false,
created_at: user.createdAt
? new Date(user.createdAt).toISOString()
: new Date().toISOString(),
updated_at: null,
email_confirmed_at: user.createdAt
? new Date(user.createdAt).toISOString()
: new Date().toISOString(),
phone: user.phone || null,
phone_confirmed_at: null,
raw_user_meta_data: rawUserMetaData,
raw_app_meta_data: rawAppMetaData,
});
});
// Step 6: Convert the transformed data to CSV
const csvFields = [
"id",
"email",
"encrypted_password",
"instance_id",
"aud",
"role",
"email_confirmed_at",
"invited_at",
"confirmation_token",
"confirmation_sent_at",
"recovery_token",
"recovery_sent_at",
"email_change_token_new",
"email_change",
"email_change_sent_at",
"last_sign_in_at",
"raw_app_meta_data",
"raw_user_meta_data",
"is_super_admin",
"created_at",
"updated_at",
"phone",
"phone_confirmed_at",
"phone_change",
"phone_change_token",
"phone_change_sent_at",
"confirmed_at",
"email_change_token_current",
"email_change_confirm_status",
"banned_until",
"reauthentication_token",
"reauthentication_sent_at",
"is_sso_user",
"deleted_at",
"is_anonymous",
];
// Write duplicate emails to a file for reference
if (duplicateEmails.length > 0) {
fs.writeFileSync(
path.join(__dirname, "duplicate_emails.json"),
JSON.stringify(duplicateEmails, null, 2)
);
console.log("Duplicate emails written to duplicate_emails.json");
}
const csv = parse(transformedUsers, { fields: csvFields });
// Step 7: Write the CSV to a file
const csvFilePath = path.join(__dirname, "supabase_users.csv");
fs.writeFileSync(csvFilePath, csv);
console.log("CSV file has been generated:", csvFilePath);
// Step 8: Delete the temporary fresh users file
if (fs.existsSync(mongoUsersFilePath)) {
fs.unlinkSync(mongoUsersFilePath);
console.log(`Temporary file deleted: ${mongoUsersFilePath}`);
}
console.timeEnd("Total processing time");
} catch (error) {
console.error("Error processing users:", error);
} finally {
if (fs.existsSync(mongoUsersFilePath)) {
try {
fs.unlinkSync(mongoUsersFilePath);
console.log(`Temporary file deleted: ${mongoUsersFilePath}`);
} catch (deleteError) {
console.error(`Error deleting temporary file: ${deleteError.message}`);
}
}
}
}
CSV मिलने के बाद, मैंने Supabase में auth.users जैसी दिखने वाली एक नकली table बनाई। असली table का setup यह लंबी SQL है, जिसे आप Supabase के editor में schema auth > users > Definition के नीचे देख सकते हैं:
create table auth.users (
instance_id uuid null,
id uuid not null,
aud character varying(255) null,
role character varying(255) null,
email character varying(255) null,
encrypted_password character varying(255) null,
email_confirmed_at timestamp with time zone null,
invited_at timestamp with time zone null,
confirmation_token character varying(255) null,
confirmation_sent_at timestamp with time zone null,
recovery_token character varying(255) null,
recovery_sent_at timestamp with time zone null,
email_change_token_new character varying(255) null,
email_change character varying(255) null,
email_change_sent_at timestamp with time zone null,
last_sign_in_at timestamp with time zone null,
raw_app_meta_data jsonb null,
raw_user_meta_data jsonb null,
is_super_admin boolean null,
created_at timestamp with time zone null,
updated_at timestamp with time zone null,
phone text null default null::character varying,
phone_confirmed_at timestamp with time zone null,
phone_change text null default ''::character varying,
phone_change_token character varying(255) null default ''::character varying,
phone_change_sent_at timestamp with time zone null,
confirmed_at timestamp with time zone GENERATED ALWAYS as (LEAST(email_confirmed_at, phone_confirmed_at)) STORED null,
email_change_token_current character varying(255) null default ''::character varying,
email_change_confirm_status smallint null default 0,
banned_until timestamp with time zone null,
reauthentication_token character varying(255) null default ''::character varying,
reauthentication_sent_at timestamp with time zone null,
is_sso_user boolean not null default false,
deleted_at timestamp with time zone null,
is_anonymous boolean not null default false,
constraint users_pkey primary key (id),
constraint unique_email unique (email),
constraint users_phone_key unique (phone),
constraint users_email_change_confirm_status_check check (
(
(email_change_confirm_status >= 0)
and (email_change_confirm_status <= 2)
)
)
) TABLESPACE pg_default;
create unique INDEX IF not exists confirmation_token_idx on auth.users using btree (confirmation_token) TABLESPACE pg_default
where
((confirmation_token)::text !~ '^[0-9 ]*$'::text);
create unique INDEX IF not exists email_change_token_current_idx on auth.users using btree (email_change_token_current) TABLESPACE pg_default
where
(
(email_change_token_current)::text !~ '^[0-9 ]*$'::text
);
create unique INDEX IF not exists email_change_token_new_idx on auth.users using btree (email_change_token_new) TABLESPACE pg_default
where
(
(email_change_token_new)::text !~ '^[0-9 ]*$'::text
);
create unique INDEX IF not exists reauthentication_token_idx on auth.users using btree (reauthentication_token) TABLESPACE pg_default
where
(
(reauthentication_token)::text !~ '^[0-9 ]*$'::text
);
create unique INDEX IF not exists recovery_token_idx on auth.users using btree (recovery_token) TABLESPACE pg_default
where
((recovery_token)::text !~ '^[0-9 ]*$'::text);
create unique INDEX IF not exists users_email_partial_key on auth.users using btree (email) TABLESPACE pg_default
where
(is_sso_user = false);
create index IF not exists users_instance_id_email_idx on auth.users using btree (instance_id, lower((email)::text)) TABLESPACE pg_default;
create index IF not exists users_instance_id_idx on auth.users using btree (instance_id) TABLESPACE pg_default;
create index IF not exists users_is_anonymous_idx on auth.users using btree (is_anonymous) TABLESPACE pg_default;
मैंने अपनी temp table के लिए वही setup copy किया, उसमें CSV upload की, और फिर इस script से users को एक बार में हज़ार करके असली auth.users table में shift किया:
DO $$
DECLARE
batch_size INT := 1000; -- Adjust batch size as needed
records_inserted INT;
BEGIN
LOOP
INSERT INTO auth.users (
id,
email,
encrypted_password,
instance_id,
aud,
role,
email_confirmed_at,
invited_at,
confirmation_token,
confirmation_sent_at,
recovery_token,
recovery_sent_at,
email_change_token_new,
email_change,
email_change_sent_at,
last_sign_in_at,
raw_app_meta_data,
raw_user_meta_data,
is_super_admin,
created_at,
updated_at,
phone,
phone_confirmed_at,
phone_change,
phone_change_token,
phone_change_sent_at,
email_change_token_current,
email_change_confirm_status,
banned_until,
reauthentication_token,
reauthentication_sent_at,
is_sso_user,
deleted_at,
is_anonymous
)
SELECT
id,
email,
encrypted_password,
COALESCE(instance_id, '00000000-0000-0000-0000-000000000000'),
COALESCE(aud, 'authenticated'),
COALESCE(NULLIF(role, ''), 'authenticated'),
COALESCE(email_confirmed_at, NOW()),
COALESCE(invited_at, NULL),
COALESCE(confirmation_token, ''),
COALESCE(confirmation_sent_at, NULL),
COALESCE(recovery_token, ''),
COALESCE(recovery_sent_at, NULL),
COALESCE(email_change_token_new, ''),
COALESCE(email_change, ''),
COALESCE(email_change_sent_at, NULL),
COALESCE(last_sign_in_at, NOW()),
raw_app_meta_data, -- Use value from test_users table
raw_user_meta_data, -- Use value from test_users table
COALESCE(is_super_admin, NULL),
COALESCE(created_at, NOW()),
COALESCE(updated_at, NOW()),
COALESCE(phone, NULL),
COALESCE(phone_confirmed_at, NULL),
COALESCE(phone_change, ''),
COALESCE(phone_change_token, ''),
COALESCE(phone_change_sent_at, NULL),
COALESCE(email_change_token_current, ''),
COALESCE(email_change_confirm_status, 0),
COALESCE(banned_until, NULL),
COALESCE(reauthentication_token, ''),
COALESCE(reauthentication_sent_at, NULL),
COALESCE(is_sso_user, false),
COALESCE(deleted_at, NULL),
COALESCE(is_anonymous, false)
FROM public.test_users
LIMIT batch_size
ON CONFLICT (email) DO NOTHING; -- Ignore duplicates
GET DIAGNOSTICS records_inserted = ROW_COUNT;
-- Exit the loop if no more records are inserted
IF records_inserted = 0 THEN
EXIT;
END IF;
END LOOP;
END $$;
इस तरह मैं हर step देख सकता था: export करना, बदलना, load करना। किसी fancy tool से शायद कम समय लगता, लेकिन मुझे ठीक ठीक पता रहना पसंद था कि क्या हो रहा है। CSV load करने के बाद मैं temp table check करता, कुछ rows को spot-check करता, और फिर batch script को चलते हुए देखता ताकि पक्का हो कि वह अटके नहीं।
Custom fields की कोई जगह नहीं
Supabase की auth.users table आपको अपनी fields जोड़ने नहीं देती, और इसने मुझे चौंका दिया। MongoDB में मैं बस एक username field डाल देता था, कोई बड़ी बात नहीं। लेकिन Supabase मना करता है: जो वह देता है, उसी से काम चलाना पड़ता है। मैंने सोचा कि extra चीज़ें raw_user_meta_data में ठूँस दूँ, जो एक JSON थैले जैसा है, लेकिन बाद में search करने के लिए वह गड़बड़ होता। इसके बजाय, मैंने एक नई profiles table बनाई जो user ID के ज़रिए auth.users से जुड़ती है। इसमें username और locale जैसी चीज़ें रहती हैं, और सब साफ़ सुथरा रहता है।
यह discussion भी देखें, शायद काम आए: https://github.com/orgs/supabase/discussions/3491
इसे set up करने का मतलब था ज़्यादा काम: मुझे वे extra fields MongoDB से निकालनी पड़ीं, सही users से match करनी पड़ीं और नई table में load करनी पड़ीं। मैंने वह data निकालने के लिए एक और script लिखा, उसे CSV में बदला, और auth का काम पूरा होने के बाद Supabase पर upload किया। अब setup बेहतर है क्योंकि मैं बाद में बिना कुछ तोड़े और fields जोड़ सकता हूँ, लेकिन इससे migration में ज़्यादा समय लगा क्योंकि मुझे एक के बजाय दो tables संभालनी पड़ीं।
Google/Apple Sign-Ins का migration

Google और Apple logins भी सिरदर्द थे। MongoDB में मेरे पास googleId और appleId fields थीं, और कुछ users के दोनों एक ही email से जुड़े थे। Supabase इन्हें raw_user_meta_data में iss, sub और provider_id जैसी ख़ास fields से संभालता है।
मैंने इसे ऐसे set up किया:
const uid = uuidv4();
const providers = [];
// Build raw user metadata
const rawUserMetaData = {
sub: uid,
email: user.email,
email_verified: true,
phone_verified: false,
};
// Check for Google ID
if (user.googleId) {
providers.push("google");
rawUserMetaData.iss = "https://accounts.google.com";
rawUserMetaData.sub = user.googleId;
rawUserMetaData.provider_id = user.googleId;
}
// Check for Apple ID
if (user.appleId) {
providers.push("apple");
if (!user.googleId) {
rawUserMetaData.iss = "https://appleid.apple.com";
rawUserMetaData.sub = user.appleId;
rawUserMetaData.provider_id = user.appleId;
}
}
// If no external providers, use email
if (providers.length === 0) {
providers.push("email");
}
App में, मैंने sign-in flows को Supabase की APIs इस्तेमाल करने के लिए update किया। Apple के लिए:
import * as AppleAuthentication from "expo-apple-authentication";
import { supabase } from "your-supabase-client";
const credential = await AppleAuthentication.signInAsync({
requestedScopes: [
AppleAuthentication.AppleAuthenticationScope.FULL_NAME,
AppleAuthentication.AppleAuthenticationScope.EMAIL,
],
});
if (credential.identityToken) {
const { data, error } = await supabase.auth.signInWithIdToken({
provider: "apple",
token: credential.identityToken,
});
}
और Google के लिए:
import { GoogleSignin } from "@react-native-google-signin/google-signin";
import { supabase } from "your-supabase-client";
const googleUser = await GoogleSignin.signIn();
if (googleUser.idToken) {
const { data, error } = await supabase.auth.signInWithIdToken({
provider: "google",
token: googleUser.idToken,
});
}
यह काम कर गया, लेकिन Google और Apple दोनों वाले users को समझना मुश्किल था: मुझे एक को main ID चुनना पड़ा और दूसरे को extra के रूप में रखना पड़ा। मैंने app में इसे कई बार test किया ताकि पक्का हो कि कोई भी बाहर lock न हो।
सावधानी: Supabase में CSV Export से जुड़ी संभावित समस्याएँ आज (29/03/2025) की तारीख़ में, table data को CSV के रूप में export करना भरोसेमंद नहीं हो सकता है, इसलिए Supabase और अपने original source के बीच data की तुलना करते समय इसका ध्यान रखें।
TypeScript Types generate करना
Supabase में एक शानदार trick है: यह आपके लिए TypeScript types बना देता है। आप एक command चलाते हैं, और यह ऐसी files दे देता है जो आपके code को ठीक ठीक बताती हैं कि आपका database कैसा दिखता है। मेरे React Native और Expo app के लिए यह बहुत बड़ी बात थी: मुझे अंदाज़ा नहीं लगाना पड़ा कि कौन सा data मिलेगा, न ही अपने types लिखने पड़े। इससे bugs बहुत कम हो गए क्योंकि मुझसे गलती होने पर TypeScript मुझ पर चिल्लाता था। पहले मैं बेवकूफ़ी भरी गलतियाँ ठीक करने में घंटों लगाता था; अब यह कहीं तेज़ है।
Tables set up करने के बाद मैंने command चलाई, types file मिली, और उसे अपने app में लगा दिया। अचानक user.email या profile.username जैसी चीज़ें बस काम करने लगीं, अब यह सोचने की ज़रूरत नहीं कि spelling सही है या नहीं। यह छोटी बात है, लेकिन इससे app की coding अंदाज़ों का खेल कम और जीत ज़्यादा लगने लगी।
Database Functions से काम करवाना
Supabase आपको छोटे SQL functions लिखने देता है जो database में चलते हैं, जो बहुत काम का है। चीज़ें बदलने के लिए app की तरफ़ ढेर सारा code लिखने के बजाय, मैंने database को यह संभालने दिया।
यह रहा एक function जो मैंने user की profile में credits जोड़ने के लिए बनाया:
declare
current_credits int;
new_credits int;
begin
-- Get current credits from user_profiles table
select assistant_credits
into current_credits
from public.user_profiles
where public.user_profiles.user_id = increase_assistant_credits.user_id;
if current_credits is null then
current_credits := 0;
end if;
new_credits := current_credits + credits_to_add;
-- Update assistant_credits in user_profiles table
update public.user_profiles
set assistant_credits = new_credits
where public.user_profiles.user_id = increase_assistant_credits.user_id;
return new_credits;
end;
यह user के credits लेता है, कुछ जोड़ता है और वापस save कर देता है, सब एक ही बार में। इसके बिना मुझे credits fetch करने पड़ते, app में जोड़ने पड़ते और वापस भेजने पड़ते, यानी ज़्यादा code और गड़बड़ी के ज़्यादा मौके। मैंने दूसरे कामों के लिए भी ऐसे कुछ functions लिखे, और इससे मेरा app ज़्यादा फुर्तीला लगने लगा क्योंकि भारी काम database ने किया।
Backend वाली शर्त
यहाँ एक मोड़ है: Supabase ने मेरा backend पूरी तरह ख़त्म नहीं किया। कुछ admin login चीज़ें इस्तेमाल करने के लिए आपको यह service_role key चाहिए, लेकिन आप इसे app में नहीं रख सकते, यह सुरक्षित नहीं है। इसलिए मैंने उन secret कामों के लिए एक छोटा NestJS server रखा। यह पहले जितना बड़ा नहीं है, लेकिन चीज़ों को सुरक्षित रखने के लिए मौजूद है। ज़्यादातर काम Supabase करता है, इसलिए यह अब भी मेरे पुराने setup से कहीं आसान है।
निष्कर्ष: संघर्ष के लायक

182,000 users को Supabase पर migrate करना सिर्फ़ technical challenge नहीं था, यह मानसिक challenge भी था। टालमटोल, देर रातों और बार बार ख़ुद पर शक के बीच, ऐसे पल आए जब मुझे सच में लगा कि मैं सब कुछ तोड़ दूँगा। यह धीमी, थकाने वाली मेहनत थी, stress और हिचकिचाहट से भरी हुई। लेकिन कमज़ोर NestJS + MongoDB setup को पीछे छोड़कर कुछ ज़्यादा साफ़ और maintainable पर जाना पूरी तरह इसके लायक था।
Supabase ने मुझे वह सरलता, flexibility और community support दिया जिसकी full-time job के साथ अकेले dev के रूप में मुझे ज़रूरत थी। अगर आप अपने app का backend migrate करने की सोच रहे हैं, तो बस इतना जान लीजिए कि यह पूरी तरह मुमकिन है। यह न जल्दी होगा न आसान, लेकिन थोड़ी planning (और शायद कुछ लंबी रातों) के साथ, आप दूसरी तरफ़ एक ऐसे setup के साथ पहुँचेंगे जिस पर आप सच में भरोसा कर सकते हैं।