Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
157 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ test/files/main.js | |
|
||
package-lock.json | ||
|
||
.config* | ||
.config.js | ||
|
||
# Compiled docs | ||
docs/*.html | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict'; | ||
|
||
module.exports = Object.freeze({ | ||
mongodbUri: 'mongodb://localhost:27017/ecommerce', | ||
stripeSecretKey: 'YOUR STRIPE KEY HERE' | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
if (process.env.NODE_ENV) { | ||
try { | ||
module.exports = require('./' + process.env.NODE_ENV); | ||
console.log('Using ' + process.env.NODE_ENV); | ||
} catch (err) { | ||
module.exports = require('./development'); | ||
} | ||
} else { | ||
console.log('using production'); | ||
module.exports = require('./production'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict'; | ||
|
||
module.exports = Object.freeze({ | ||
mongodbUri: 'mongodb://localhost:27017/ecommerce_test', | ||
stripeSecretKey: 'test' | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# ecommerce-netlify-functions | ||
|
||
This sample demonstrates using Mongoose to build an eCommerce shopping cart using [Netlify Functions](https://www.netlify.com/products/functions/), which runs on [AWS Lambda](https://mongoosejs.com/docs/lambda.html). | ||
|
||
Other tools include: | ||
|
||
1. Stripe for payment processing | ||
2. [Mocha](https://masteringjs.io/mocha) and [Sinon](https://masteringjs.io/sinon) for testing | ||
|
||
## Running This Example | ||
|
||
1. Make sure you have a MongoDB instance running on `localhost:27017`, or update `mongodbUri` in `.config/development.js` to your MongoDB server's address. | ||
2. Run `npm install` | ||
3. Run `npm run seed` | ||
4. Run `npm start` | ||
5. Visit `http://localhost:8888/.netlify/functions/getProducts` to list all available products | ||
6. Run other endpoints using curl or postman | ||
|
||
## Testing | ||
|
||
Make sure you have a MongoDB instance running on `localhost:27017`, or update `mongodbUri` in `.config/test.js` to your MongoDB server's address. | ||
Then run `npm test`. | ||
|
||
``` | ||
$ npm test | ||
> test | ||
> env NODE_ENV=test mocha ./test/*.test.js | ||
Using test | ||
Add to Cart | ||
✔ Should create a cart and add a product to the cart | ||
✔ Should find the cart and add to the cart | ||
✔ Should find the cart and increase the quantity of the item(s) in the cart | ||
Checkout | ||
✔ Should do a successful checkout run | ||
Get the cart given an id | ||
✔ Should create a cart and then find the cart. | ||
Products | ||
✔ Should get all products. | ||
Remove From Cart | ||
✔ Should create a cart and then it should remove the entire item from it. | ||
✔ Should create a cart and then it should reduce the quantity of an item from it. | ||
8 passing (112ms) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
const config = require('./.config'); | ||
const mongoose = require('mongoose'); | ||
|
||
let conn = null; | ||
|
||
module.exports = async function connect() { | ||
if (conn != null) { | ||
return conn; | ||
} | ||
conn = mongoose.connection; | ||
await mongoose.connect(config.mongodbUri); | ||
return conn; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
examples/ecommerce-netlify-functions/netlify/functions/getCart.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
examples/ecommerce-netlify-functions/netlify/functions/getProducts.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
examples/ecommerce-netlify-functions/netlify/functions/removeFromCart.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
{ | ||
"dependencies": { | ||
"mongoose": "^6.3.5", | ||
"sinon": "^14.0.0", | ||
"stripe": "^9.6.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "10.0.0", | ||
"netlify-cli": "^10.7.1" | ||
}, | ||
"scripts": { | ||
"test": "mocha --exit ./test/*.test.js" | ||
} | ||
"dependencies": { | ||
"mongoose": "6.3.5", | ||
"sinon": "14.0.0", | ||
"stripe": "9.6.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "10.0.0", | ||
"netlify-cli": "10.7.1" | ||
}, | ||
"scripts": { | ||
"seed": "env NODE_ENV=development node ./seed", | ||
"start": "env NODE_ENV=development netlify dev", | ||
"test": "env NODE_ENV=test mocha ./test/*.test.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
const { Product } = require('./models'); | ||
const config = require('./.config'); | ||
const mongoose = require('mongoose'); | ||
|
||
async function createProducts() { | ||
await mongoose.connect(config.mongodbUri); | ||
|
||
await Product.create({ | ||
productName: 'Dummy Product', | ||
productPrice: 500 | ||
}); | ||
|
||
await Product.create({ | ||
productName: 'Another Dummy Product', | ||
productPrice: 600 | ||
}); | ||
|
||
console.log('done'); | ||
process.exit(0); | ||
} | ||
|
||
createProducts(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
|
||
const { after } = require('mocha'); | ||
const config = require('../.config'); | ||
const mongoose = require('mongoose'); | ||
|
||
before(async() => { | ||
await mongoose.connect(config.mongodbUri); | ||
await mongoose.connection.dropDatabase(); | ||
}); | ||
|
||
after(async function() { | ||
await mongoose.disconnect(); | ||
}); |