Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DurkoMatko/1097d1c18a00d5fd4195253205c53ff2 to your computer and use it in GitHub Desktop.
Save DurkoMatko/1097d1c18a00d5fd4195253205c53ff2 to your computer and use it in GitHub Desktop.
Import data to Firestore with Firebase Admin SDK (NodeJS) doesn't work for all collections
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: `https://${projectId}.firebaseio.com`
});
/* IMPORT DATA */
const testOrgName = "importedBrowserUser18";
const testUserName = "importedBrowserUser18";
const testUserEmail = "importedBrowserUser18@example.com";
const testUserPwd = "importedBrowserUser18";
const categories = ["Man", "Woman", "Adult", "Boy", "Girl", "Child", "Baby", "Food", "Hygiene", "Other"]
admin
.auth()
.listUsers()
.then(listUsersResult => {
const selectedUser = listUsersResult.users.filter(
usr => usr.email.toLocaleLowerCase() === testUserEmail.toLocaleLowerCase()
);
if (selectedUser.length === 0) {
const testOrg = {
name: testOrgName,
createdAt: Date()
};
admin
.firestore()
.collection("organizations")
.add(testOrg)
.then(createdTestOrg => {
// eslint-disable-next-line no-console
console.log("Successfully created new org:", createdTestOrg.id);
admin
.auth()
.createUser({
email: testUserEmail,
password: testUserPwd,
displayName: testUserName,
emailVerified: false
})
.then(userRecord => {
// eslint-disable-next-line no-console
console.log("Successfully created new user:", userRecord.uid);
const testUser = {
name: userRecord.displayName,
organization: admin
.firestore()
.doc(`organizations/${createdTestOrg.id}`)
};
admin
.firestore()
.collection("profiles")
.doc(userRecord.uid)
.set(testUser)
.then(() => {
// eslint-disable-next-line no-console
console.log("Successfully created new user profile");
importProductsForTestUser(createdTestOrg.id)
})
.catch(error => {
// eslint-disable-next-line no-console
console.log("Error creating test user profile:", error);
process.exit(1);
});
})
.catch(error => {
// eslint-disable-next-line no-console
console.log("Error creating test user:", error);
process.exit(1);
});
})
.catch(error => {
// eslint-disable-next-line no-console
console.log("Error creating test organization:", error);
process.exit(1);
});
}
});
function importProductsForTestUser(testOrgUid){
const orgUid = testOrgUid
admin
.auth()
.listUsers()
.then(listUsersResult => {
const selectedUser = listUsersResult.users.filter(
usr => usr.email.toLocaleLowerCase() === testUserEmail.toLocaleLowerCase()
);
if (selectedUser.length === 1) {
for (var i = 0; i < 5; i++) {
let productItem = {
isDeleted: false,
//organization: admin.firestore().doc('organizations/' + orgUid),
createdBy: admin.firestore().doc('profiles/' + selectedUser[0].uid),
createdAt: Date()
};
productItem.category = categories[Math.floor(Math.random() * categories.length)];
productItem.name = makeRandomName(10)
admin
.firestore()
.collection("products")
.add(productItem)
//.doc(productItem.name)
//.set(productItem)
.then((newProd)=>{
console.log("Successfully created test product");
})
.catch(error => {
// eslint-disable-next-line no-console
console.log("Error creating test product:", error);
process.exit(1);
});
}
process.exit(0);
}
});
}
function makeRandomName(length) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < length; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment