售 价:¥
温馨提示:数字商品不支持退换货,不提供源文件,不支持导出打印
为你推荐
Mongoose for Application Development
Table of Contents
Mongoose for Application Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Introducing Mongoose to the Technology Stack
The technology stack – Node.js, npm, MongoDB, and Express
The language and the server – JavaScript and Node
Single-threaded versus multithreaded
Blocking versus non-blocking code
JavaScript callbacks
Running the callback
A Node.js example
The database – MongoDB
The framework – Express
What Mongoose is all about
What is Mongoose good for?
What Mongoose is not ideally suited for
The cornerstones of Mongoose
Mongoose schemas
Mongoose models
Installing the full stack
Installing the prerequisites
Installing Node.js
Installing npm
Installing MongoDB
Installing Express.js
Installing Mongoose
Direct installation into project
Using project dependencies – package.json
Creating a project
Summary
2. Establishing a Database Connection
Mongoose default connection
Using multiple connections
About the connection string
Setting the port
Specifying a database user
Connection options
Closing the connection
Calling the close command
Closing when the Node process ends
Connection events
Connecting our project
Creating the connection
Catching the events
Opening the connection at application start
Creating the database
Summary
3. Schemas and Models
Introducing schemas
Field sizes
Data types allowed in schemas
String
Number
Date
Boolean
Buffer
ObjectId
Mixed
Tracking changes to Mixed type
Array
Warning – array defined as mixed type
Custom SchemaTypes
Where to write the schemas
Writing a schema
Modifying an existing schema
Setting a default value
Only allowing unique entries
Our final User schema
What's that "__v" thing?
Why is this needed?
Defining the Project schema
Improving the Project schema
Building models
Instances
Interacting with instances
Finding a single instance
Finding many instances
Considerations when choosing your model name
Setting the collection name
Overriding the collection name in the schema
Overriding the collection name in the model
Building models from our schemas
Our complete code
Summary
4. Interacting with Data – an Introduction
Model methods and instance methods
Setting up the project
Code structure
Adding the routes files
Tying the routes to Mongoose models
URLs and routes
Routes for user management
Routes for project management
Summary
5. Interacting with Data – Creation
Creating an instance
Adding data to the instance
Saving an instance
Using the saved data
Creating and saving database entry in one step
Chaining methods
The Model.create() method
CRUD – create data
Adding a new user form
Adding the Jade template
Linking the view to the URL
Adding the create user function
Error trapping
Creating a user session
Displaying the confirmation page
Try it out!
Adding create project functionality
Routes
New files and functions
Summary
6. Interacting with Data – Reading, Querying, and Finding
Approaches to find and read data
Using the QueryBuilder
Single query operation
Static helper methods – finding data
CRUD – reading user and project data
findOne() – finding a single user
Login form
Login action
Housekeeping – adding homepage links
Try it out!
find() – finding a list of projects and returning JSON to AJAX
Creating a new static find method
Setting up the route
Updating the view
Building the AJAX call
Try it out!
findByld() – finding a single project
Route setup
Creating the view
Summary
7. Interacting with Data – Updating
Model helper methods
Building the commands
Which method to choose
The catch
The three-step find-edit-save approach
CRUD – editing users and projects
Tracking user login
Editing the current user
Routes in use
Setting up the form
Setting up the controllers
Committing the edit
Editing projects
Routes
New files and functions
Summary
8. Interacting with Data – Deleting
Deleting data
CRUD – deleting user and projects
The "Are you sure" page
Deleting the user
Improving on this
Deleting individual projects
Routes
New files and functions
Summary
9. Validating Data
Mongoose validation – the basics
Default validators
All SchemaTypes
Number SchemaType
String SchemaType
Understanding validation errors
Doing it your way – create custom validation
Single function – no custom error message
Returning a custom error message
Validating a regular expression
Taking the messages out of the schema
Using multiple re-usable validators
Non-blocking, asynchronous validation
Extending Mongoose validation
Adding validation to our project
Summary
10. Complex Schemas
Population – references to other collections
Defining data for population
Saving population references
Retrieving populated data
Querying to return a subset of results
Populating into multiple parent items
Subdocuments
Creating subdocuments
Saving and validation
Retrieving subdocuments
Accessing a specific subdocument
Deleting subdocuments
Data management when modifying existing schemas
Summary
11. Plugins – Re-using Code
Reusable schema plugins
Creating a schema plugin
Applying the plugin to an existing schema
Using an external file
Using schema middleware
Not just for plugins
Sharing with the community
Summary
Index
买过这本书的人还买过
读了这本书的人还在读
同类图书排行榜