npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

firestore-seed

v0.0.11

Published

Insert seed data to your Cloud Firestore database. Also supports uploading files linked from the Firestore documents.

Downloads

50

Readme

firestore-seed

firestore-seed insert initial seed data to Cloud Firestore.

Not tested at all. Use this at your own lisk.

Getting started

Add firestore-seed to your project.

$ yarn add firestore-seed

Create a simple seed program.

const admin = require("firebase-admin");
// serviceAccountKey.json can be generated in Firebase Console.
const serviceAccountKey = require("./serviceAccountKey.json");
const seed = require('firestore-seed');

// Initialize firebase-admin.
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://xxxxxx.firebaseio.com",
    storageBucket: "xxxxxx.appspot.com",
});

// Import seeds.
let messagesCollection = seed.collection("messages", [
    seed.doc("message1", {
        content: "Hello firestore-seed.",
        created: new Date(),
    }),
    seed.doc("message2", {
        content: "Good bye firestore-seed.",
        created: new Date(),
    })
]);

messagesCollection.importDocuments(admin).then(() => {
    console.log("Successfully imported documents.");
}).catch(e => {
    console.log("Failed to import documents: " + e);
});

Uploading files

You can also upload files to the Firebase Cloud Storage and add the download URL to the Firestore documents.

const admin = require("firebase-admin");
// serviceAccountKey.json can be generated in Firebase Console.
const serviceAccountKey = require("./serviceAccountKey.json");
const seed = require('firestore-seed');

// Initialize firebase-admin
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://xxxxxx.firebaseio.com",
    storageBucket: "xxxxxx.appspot.com",
});

// Import seeds.
let imageOptions = seed.imageOptions("images/{id}", "public/profiles/{id}")
let profilesCollection = seed.collection("profiles", [
    seed.doc("uphy", {
        name: "foo",
        introduction: "Hello, I'm foo.",
        created: new Date(),
        // Upload 'images/uphy/image.png' to 'public/profiles/uphy/image.png' and set its' download URL to this field.
        icon: seed.image("image.png", "image.png", imageOptions)
    }),
    seed.doc("yhpu", {
        name: "bar",
        introduction: "Hello, I'm bar.",
        content: "Good bye firestore-seed.",
        created: new Date(),
        // Upload 'images/yhpu/image.png' to 'public/profiles/yhpu/image.png' and set its' download URL to this field.
        icon: seed.image("image.png", "image.png", imageOptions)
    })
]);

profilesCollection.importDocuments(admin).then(() => {
    console.log("Successfully imported documents.");
}).catch(e => {
    console.log("Failed to import documents: " + e);
});

Subcollections

For inserting subcollections, use seed.subcollection(docs).

const admin = require("firebase-admin");
// serviceAccountKey.json can be generated in Firebase Console.
const serviceAccountKey = require("./serviceAccountKey.json");
const seed = require('firestore-seed');

// Initialize firebase-admin
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://xxxxxx.firebaseio.com",
    storageBucket: "xxxxxx.appspot.com",
});

// Import seeds.
seed.importCollections(admin,
    [
        seed.collection("users", [
            seed.doc("uphy", {
                name: 'Yuhi Ishikura',
                rooms: seed.subcollection([
                    seed.doc("room1", {
                        "ref": seed.docRef("rooms", "room1")
                    }),
                    seed.doc("room2", {
                        "ref": seed.docRef("rooms", "room2")
                    })
                ])
            }),
            seed.doc("suzuki", {
                name: 'Taro Suzuki',
                rooms: seed.subcollection([
                    seed.doc("room1", {
                        "ref": seed.docRef("rooms", "room1")
                    }),
                    seed.doc("room2", {
                        "ref": seed.docRef("rooms", "room2")
                    })
                ]),
            }),
            seed.doc("tanaka", {
                name: 'Tanaka Jiro',
                rooms: seed.subcollection([
                    seed.doc("room2", {
                        "ref": seed.docRef("rooms", "room2")
                    })
                ]),
            })
        ]),
        seed.collection("rooms", [
            seed.doc("room1", {
                name: "Room 1",
                messages: seed.subcollection([
                    seed.doc("message1", {
                        time: Date.now(),
                        content: "Message 1 of room1",
                        user: seed.docRef("users", "uphy")
                    }),
                    seed.doc("message2", {
                        time: Date.now(),
                        content: "Message 2 of room1",
                        user: seed.docRef("users", "suzuki")
                    })
                ])
            }),
            seed.doc("room2", {
                name: "Room 2",
                messages: seed.subcollection([
                    seed.doc("message1", {
                        time: Date.now(),
                        content: "Message 1 of room2",
                        user: seed.docRef("users", "uphy")
                    }),
                    seed.doc("message2", {
                        time: Date.now(),
                        content: "Message 2 of room2",
                        user: seed.docRef("users", "suzuki")
                    }),
                    seed.doc("message3", {
                        time: Date.now(),
                        content: "Message 3 of room2",
                        user: seed.docRef("users", "tanaka")
                    })
                ])
            })
        ])
    ]
).then(() => {
    console.log("Successfully imported documents.");
}).catch(e => {
    console.log("Failed to import documents: " + e);
});

Predefined script

You can use firestore-seed more simply with npm scripts.

  1. yarn add -D firestore-seed
  2. Create seed file
    const seed = require('firestore-seed');
    
    // Export collection or collection array.
    module.exports = seed.collection("messages", [
        seed.doc("message1", {
            content: "Hello firestore-seed.",
            created: new Date(),
        }),
        seed.doc("message2", {
            content: "Good bye firestore-seed.",
            created: new Date(),
        })
    ]);
  3. Add following properties to your package.json
    "scripts": {
        "seed": "firestore-seed",
        /* ... */
    },
    /* ... */
    "firestore-seed": {
        /* Firestore database URL(required) */
        "databaseURL": "https://xxxxxxx.firebaseio.com",
        /* Firebase service account key file. */
        "credentialPath": "./firebase-credential.json",
        /* Firestore seed file created in #2 */
        "seedDataPath": "./firestore-seed.js"
    }
  4. Run yarn seed