User List Object Model - pcimino/nodejs-restify-mongodb GitHub Wiki

User List Object Model

##UserList userList.js Schema This is a simple object and isn't (currently) persisted in the database.

Explicit APIs The purpose is to search and return a collection of User objects. I often see REST APIs defined like this:

  • /api/users Returns all users
  • /api/users/:id Returns a single user based on ObjectId

I prefer to be explicit with regard to enforcing what the API is returning:

  • /api/user/:id - 0 or 1 user object, based on unique id
  • /api/userlist - Returns a UserList object which contains an array of 0 to many User objects. Also supports pagination.

Input Fields The UserList object has a number of input fields:

  • name: { type: String, default: '' }, // search name
  • email: { type: String, default: '' }, // search email
  • username: { type: String, default: '' }, // search email
  • itemsPerPage: { type: Number, min: -1, default: -1}, // number of records to return, -1 is unlimited
  • pageNumber: { type: Number, min: -1, default: -1}, // page number 1-N
  • ascendingSortFlag: { type: Boolean, },
  • sortField: { type: String, default: '' }

The search in user-routes.js is not the most efficient. The three String search fields are inclusive and case insensitive and always used even if the fields are empty (name: 'a' returns 'a', 'A', 'baas', etc).

Output Fields The (available) pageCount returns the number of available pages. For an unlimited search this will be 1, if the query asks for 2 records per page, and there are 11 records, the pageCount will be 6.

The users[] contains User objects. I wanted to specify the type {users:[User]} unfortunately I was having trouble with the find() method; couldn't seem to cast the JSON into user objects.

  • pageCount: Number
  • users: []

Note: In the code you'll see I'm having some issues using the /api/user/:id URI format. When it's working the client simply gets a 404 response, which was not really a problem. But then I started getting a 405 on all requests. So until I figure that out, I'm manually checking for request parameters and calling the appropriate GET.

Setting up Routes in routes.js

Return Home