Skip to content
This repository has been archived by the owner on Jul 20, 2019. It is now read-only.

Using a database other than mongoose

Aaron Pettengill edited this page Jan 6, 2018 · 3 revisions

If you're not using mongoose there's no problem. express-reuse makes it easy to drop in a custom database access module. If you already have a database module to use, you simply:

expressReuse.useDatabase( /* your database module */ )

Note: useDatabase checks for compatibility with pre-made strategies. If you're not using any pre-made strategies and want to completely change the structure of the database module, then you need to use:

expressReuse.useModule({
  database: /* your database module */
})

Creating a custom database module

Chances are, nobody has written a database module for you. You can, of course, pass any old object into useModule but your app may crash horribly. In order to make your module compatible with pre-made strategies you need 3 primary functions (and 1 optional one).

{
  //Required
  async getUser     (selection, projection = []) {},
  async newUser     (userData)                   {},
  async updateUser  (user)                       {},

  //Optional
  init(options) {}
}

And on top of that you can add whatever you want. For example: the default database module has a userModel property that it initializes with a mongoose model from the options object.

Here is a more detailed explanation of each function:

async getUser ( selection, projection = [] )

selection is an object that contains the fields we want to find a matching user for.

projection is an array of strings that contains the fields that are being requested.

returns a javascript object containing the user information. Must contain at least an id and the fields specified in projection

In this example code, we want to query the database for a user who has the email "example@email.com" and we want to know the emailConfirmed and password fields of that user.

var selection = {email: "example@email.com"}
var projection = ["emailConfirmed", "password"]
var user = await database.getUser(selection, projection)

The user object should now look like this:

{
  id: /*user id*/,
  emailConfirmed: /*true or false */,
  password: /* hashed password */
}

async newUser (userData)

userData is an object containing the user information to save new user with.

returns the user. Needs to at least contain an id field.

var userData = {
  email: "bob@email.com",
  password: /* password hash */
}
var user = database.newUser(userData)

user should at least look like

{
  id: 'user id'
}

async updateUser (user)

user is an object that was returned from getUser, but with changes made that need to be saved.

returns the newly updated user info

var user = database.getUser(selection, projection)
user.email = "bob@email.com"
var updatedUser = database.updateUser(user)

init ( options )

Having an init function is optional. options is the options object that gets passed to express-reuse.

app.use("/auth", expressReuse({
  //This object is the options object that get's passed to init
})