-
-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve the dbseed.js script #263
Changes from all commits
0f344d9
f48e295
a60b3ce
9264f14
e58f237
e85a3f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,35 @@ | ||
/* eslint-env node */ | ||
|
||
// Seeding data is only for non-production environments. | ||
if (process.env.NODE_ENV === 'production') { | ||
throw new Error( | ||
'Data is not allowed to be seeded in a production environment.' | ||
) | ||
} | ||
|
||
const path = require('path') | ||
|
||
// Load the local development configuration file. | ||
require('dotenv').config({ path: path.join(__dirname, '.env.local') }) | ||
|
||
const admin = require('firebase-admin') | ||
const faker = require('faker') | ||
const yargs = require('yargs/yargs') | ||
const { hideBin } = require('yargs/helpers') | ||
|
||
// The argument --seedSize=n can be passed in. | ||
// If it is not passed in the default seed size is 10. | ||
const { seedSize = 10 } = yargs(hideBin(process.argv)).argv | ||
const { PROJECT_ID: projectId } = process.env | ||
|
||
const projectId = 'protege-dev-env' | ||
process.env.FIRESTORE_EMULATOR_HOST = 'localhost:8080' | ||
admin.initializeApp({ projectId }) | ||
|
||
const db = admin.firestore() | ||
|
||
function getSeedData() { | ||
async function seedData() { | ||
try { | ||
;[...Array(10).keys()].map(() => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need to map the array as we're not mapping to anything. A loop is good for the job. |
||
db.collection('jobs').add({ | ||
for (let i = 0; i < seedSize; i++) { | ||
const job = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found it difficult to grok the job when it was directly in the add method. Happy to put it back if you prefer that. |
||
approved: true, | ||
companyDescription: faker.lorem.paragraph(), | ||
companyEmail: faker.internet.email(), | ||
|
@@ -43,8 +62,11 @@ function getSeedData() { | |
]), | ||
status: faker.random.arrayElement(['viewed', 'sent']), | ||
// dateApplied: faker.date.recent(), | ||
}) | ||
) | ||
} | ||
|
||
await db.collection('jobs').add(job) | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
console.log('database seed was successful') | ||
} catch (error) { | ||
|
@@ -53,4 +75,4 @@ function getSeedData() { | |
} | ||
} | ||
|
||
getSeedData() | ||
seedData() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Throw an error if we are in the production environment.