Skip to content
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

SeedDB script is working now #93

Merged
merged 8 commits into from
Jan 22, 2018
Merged

SeedDB script is working now #93

merged 8 commits into from
Jan 22, 2018

Conversation

phuchle
Copy link
Contributor

@phuchle phuchle commented Jan 19, 2018

What it does:
Connects to mongoDB and clears out any existing documents, then throws down new data. Currently only 3 mock users are implemented.

Usage:

  1. Turn on MongoDB on your local.
    Ubuntu: sudo service mongod start
    Mac: mongod

  2. cd src/server
    node seedDB.js
    See success messages logged to console and script exiting.

  3. Start mongo shell with mongo

  4. In mongo shell:
    show dbs => expect aaf to be present
    use aaf to switch to the db
    show collections => expect users, donors, families, names, organizers to be present.
    db.names.find() to return names.
    db.names.find().length() to be 3
    db.users.find().length() to be 3
    db.donors.find().length() to be 1
    etc for other models.

  5. Poke at it as much as you want.

Methods:
There is a liberal use of async/await in order to get this script to work in a 'synchronous' manner. The await keyword can be used on anything that returns a promise and must be used inside an async function. await allows you to pause the execution of your code until the promise comes back with data or errors out, essentially telling the program to wait for the promise to be done. This allows the script to run asynchronously but look synchronous.

The mongoose methods I'm using in the script are all asynchronous so that's why there are so many cases of async/await.

I had to set the ecmaVersion to 2017 in order to use async/await. Make sure you're on Node 8+ for it to work properly.

Slops:
I also just realized I left a comment in on seedDB.js line 216 that is no longer relevant.
Prettier + autosave wrecked me and I have some noise in the model files and eslintrc.json. We should probably standardize prettier settings as well.
Wishlist not created yet, as I think this is best done when simulating when families adding items to the db.

"plugins": [
"react"
],
"plugins": ["react"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious but why did you change this to inline?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only one item, can fit in one line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a prettier preset. I think 80 characters is the limit for making line breaks. All the formatting changes were auto-saved by prettier so there is a lot of noise :(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotcha, thanks

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this prettier preset remove use of arrow functions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it doesn't, but it defaults to a style of param => { ... } instead of (param) => { ... }. The methods on the models use a traditional function because I believe those methods will need this to point to the model in order to do stuff.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For multiple params, it will always be in (param1, param2) => {} because javascript requires that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, i saw some arrow functions on the other files. i saw one change from () => {} to function() {}.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In most cases, we can use arrow functions but there are a few edge cases where we need a function declaration in order to access this correctly or accessing arguments within the function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still figuring those edge cases out. I remember some guys running into issues at work with 'this' binding in es5.

server/seedDB.js Outdated
console.error(`Error creating Name. Error: ${err}`);
return;
}
console.log('Successfully created name');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're passing err and name into this await, should you be still using name?

Should this line be

console.log(`Successfully created ${name}`);

?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you're using ${name} , then aren't you using name?

Copy link
Contributor Author

@phuchle phuchle Jan 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be something like console.log(`Successfully created name: ${name}`); Similarly with other .save() method calls (will update).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I can agree with

console.log(`Successfully created name: ${name}`);

Would make more sense than Successfully created Josh.

Copy link
Contributor

@jwu910 jwu910 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think just changing the log syntax, everything else seems to make sense thus far.

@phuchle
Copy link
Contributor Author

phuchle commented Jan 21, 2018

Made the requested changes!

});

// Hash password before saving
userSchema.pre('save', next => {
userSchema.pre('save', function(next) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we losing the arrow functions? shouldn't this be (next) =>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the official mongoose docs:

Do not declare methods using ES6 arrow functions (=>). Arrow functions explicitly prevent binding this, so your method will not have access to the document and the above examples will not work.
mongoose docs: methods

@phuchle phuchle merged commit 7494b26 into AAF-63-models Jan 22, 2018
@phuchle phuchle deleted the AAF-63-models-phuc branch January 22, 2018 18:09
rainrivas pushed a commit that referenced this pull request Jul 10, 2018
* Closing #30 by connecting to Heroku (#53)

* Uncommenting logic to use build folder. Also using double quotes to check travis behavior

* Adding `build` to source control

* Adding Procfile to have heroku run a specific server command

* Mofidying the path for express public folder

* Modyfing homepage field on package.json and pushing up new build that use the homepage url

* Adding build folder to eslint ignore file so we do not fail CI. Also removing our build folder from .gitignore so we can easily push up the build.

* Install mongoose and bcrypt for writing models.

* Add base user model and placeholder files for donor, family, and organizer models.

* Add more fields and refine methods on userSchema.

* Run user.js through prettier.

* Add wishlist schema.

* Refine existing models for consistency and clarity. Add initial version of other base models.

* Add a few more methods before realizing I have no idea what I'm doing.

* Update models.  Add donor and family controllers. Add body-parser package for dealing with form data.

* Setting up a very basic set of api routes

* Adding basic logic for /pairing route. Modifying Donor and Family schema
to make references to each otehr for matching and adding default values
to it. /pairing should find all donors and filter them based on budget then sort budgets in descending order.

* Adding organizer to the donor and family models

* Adding pseudo logic for /paring/balance route

* Adding notes to /pairing/balance to remind us to use the readWishlist() method

* Correcting issues with api.js

Switching the min/max budgets in the LTE/GTE query and removing the p from "res"

* SeedDB script is working now (#93)

* Fix capitalization error in server/models.

* Update ecmaVersion to latest in order to use async/await.

* Implement working seedDB script by making liberal use of async/await.

* Add consistent error/sucess output on saving each model.

* Aaf 97 budget pairing (#101)

* Fixing #100 - Removing extra key, field, from Donor.sort

* Pulling the data from the request correctly so we can find budgets accordingly

* Fixing #97. The max budget cap is not implemented so we sort at a +/- 10% in ascending order for now. Also did some work for setting the database route to 'aaf' in our api file. Also setting matchedFamily to null in our models and correcting the name mapping in the db seed file.

* Correcting the sort order to be as #97 is asking.

* Add wishlist seeding to seedDB script (#102)

Add wishlist seeding to seed DB and refine error handling for wishlists and families.

* AAF-104 Change body parser declaration from app to routes

* Api test setup (#106)

* WIP :: Setting up API tests with base skeleton

* Modifying app.js to export the app.listen so that the tests can run correctly. Setting up a single happy path test for /api

* Enhancing api tests to make proper assertions.

* Adding logic to /api/pairing to handle missing budget params. Making tests more relevant to pairing route.

* Adding tests for making sure messages are only should when the appropriate number of donors are returned

* Removing duplicated bodyparser line in api.js

* Cleaning up the Pairing test to split the tests into sub-sets. Also adding some basic wishlist test templates for the wishlist routes.

* Removing the babel dependencies that were added since they aren't actually used in testing.

* Fixing the issue where only one test would run. Also, since multiple tests can no run you can watch the api tests using $(npm bin)/jest api --watch
This assumes you're naming api tests as <ROUTE>Api.test.js

* Removing useless comment on app.js

* Fixing the linter issue for wishlist route so npm test will execute

* Fixing the unused imports issue

* AAF-111 Add usePushEach as deprecation workaround for mongo that affects array push methods (#112)

* AAF-111 Add usePushEach as deprecation workaround for mongo that affects array push methods

* AAF-111 Npm update and package lock

* AAF-111 Add jest-cli as dev dependency

* AAF-94 wishlist api read (#108)

* add first route for families to read their wishlist

* Add more error handling.

Add a family with an empty wishlist.

* AAF94 Add a description for the regex
Fix the logic for the if statement
Change the response status to 400 and made the output messages more discriptive

* AAF 94 Change the HTTP code for no family found to 404.

* AAF-94 Fix uppercase for const variable

* AAF-94 Revert the change to the familyId variable and fix the route

* AAF-94 Remove the delete route

* AAF-113 npm test api -- Fix #113 (#114)

*  Trying to fix #113 by using concurrently to run all the tests but I doubt that this will work correctly. I might have to make the changes at the travis.yml file

* (#113)Telling travis to cache node_modules so that it doesn't have to constantly install if things don't change

* 113 - Skipping the wishlist tests since they aren't actually written. However, when travis runs concurrently, it might be failing to make a second connection to mongo.. will need to test this

* AAF#113 - Adding mongodb service and seed script to travis.yml to solve the issue with scripts not running.

* AAF-#113 - Skipping the POST routes tests in the wishlist API

* Removing node 4 and 6 from Travis since we won't be supporting that in prod. Also modifying our README to show system requirements that are recommending a minium node version of 7 and a mongodb version of 3.6 (#113)

* Modifying the test:api command to now run on a node nevironment since we're testing API (node services) and not some dom elements. Also setting API test to trigger OS Notification and to force quit once the execution is done ( no watch mode ) (#113)

* moved the totalListCost outside of the list array (#117)

* Aaf 109 families filter (#122)

AAF-109 Add families filter route.

This route is expected to be used like so:

- GET /api/families with no queries returns list of all families

- GET /api/families?filter=_id&value=123
Passing filter and value will look up families with the provided filter and value and return an object with a property families, an array of the families.

If you pass less than 2 or more than 2 query fields, the route throws 400.

If you pass exactly 2 query fields but they are not filter or value, it will throw 400.

eg: filter: foo and valu: bar or foo: bar and bar: foo

* Aaf 110 donors filter (#126)

* AAF-109 Write show and filter route for donor.  Experimenting with controller abstraction. 

* AAF-110 Rebuild bcrypt and node-sass due to errors that caused npm start to break.

* Removing bcrypt from our package.json since we do not need this package... i dont think

* readding bcrypt as version 2.0.1 since we are using it in seed file.

* Setting up Bcrypt v3.0

* AAF-63 Update node-sass-chokidar. 1.3.0 released. node-sass dependencies should build with node-gyp

* AAF-63 Autogenerate packagelock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants