Class: Database

database.Database

Database

Constructor

new Database(url, usernameopt, passwordopt, loggeropt) → {Database}

  • Create a new Database from specify
  • It is wrap of MongoClient
  • All of operations with database must pass though this object to ensure that every thing is fine
  • Do not use mongodb.MongoClient directly
Parameters:
Name Type Attributes Default Description
url string

Specify address of mongodb server

username string <optional>
null

Account to access to mongodb server

password string <optional>
null

Credential to access to mongodb server

logger module:flatlog.FlatLog <optional>
DEFAULT_LOGGER

Use to make write message uniform

Source:
Returns:
  • The new database object
Type
Database
Example
// default database
var db1 = new Database('mongodb://localhost/gwisp')

// access database by credential
var db2 = new Database('mongodb://localhost/gwisp', 'kevin', 'secret')

// custom logger
const flatlog = require('./lib/flatlog')

var logger = flatlog({caller 'kevin'})
var db3 = new Database('mongodb://localhost/gwisp', null, null, logger)

Members

_dbclient :module:mongodb.MongoClient

Instance of connection to mongoose database server

Type:
Source:

Methods

_createCollection(name, schema, indexing, callback)

  • Create collection in database from specification
  • Create indexing of collection in database from specification
Parameters:
Name Type Description
name string

Name of collection

schema module:database.CollectionSchema

Specify field of collection

indexing module:database.CollectionIndex

Specify indexing of collection

callback StdCallback
  • Function will be call after done
  • Result contains collection object
Source:
Example
// see constructor for detail
var db = new Database(url)

var schema = {
  autoIndexId: false,
  validator: {
    $and: [
      {id: {$type: 'string', $regex: /^[a-z]{2}$/}},
      {name: {$type: 'string', $regex: /^[a-zA-Z0-9 ]{1,16}$/}}
    ]
  }
}
var index = {
  keys: {id: 1},
  options: {unique: true}
}

db.createCollection('region', schema, index, function(err, collection) {
  // handle for result of operation here
})

_createCollections(collections, callback)

  • Create multi collection from array of specification
Parameters:
Name Type Description
collections Array.<module:database.CollectionSpec>

List of specifications

callback StdCallback

Function will be call after done

Source:
Example
// see constructor for detail
var db = new Database(url)

var collections = [
  {
    name: 'region',
    schema: {
      autoIndexId: false,
      validator: {
        $and: [
          {id: {$type: 'string', $regex: /^[a-z]{2}$/}},
          {name: {$type: 'string', $regex: /^[a-zA-Z0-9 ]{1,16}$/}}
        ]
      }
    },
    index: {
      keys: {id: 1},
      options: {unique: true}
    }
  },
  {
    name: 'book',
    schema: {
      validator: {
        $and: [
          {name: {$type: 'string', $regex: /^[a-zA-Z0-9 ]{1,16}$/}},
          {price: {$type: 'number'}}
        ]
      }
    },
    index: {
      keys: {name: 1},
      options: {unique: true}
    }
  }
]

db._createCollections(collections, function(err) {
  // handle for result here
})

close(callback)

  • Close connection to database
Parameters:
Name Type Description
callback StdCallback

Function will be call after done

Source:
Example
// see constructor for detail
var db = new Database(url)

db.connect(function(err, dbclient) {
  if (err) return

  db.close(function(err) {
    if (err) return

    // connection to database server was close
  })
})

connect(callback)

  • Async connect to database
Parameters:
Name Type Description
callback StdCallback

Function will be call after done

Source:
Example
// see constructor for detail
var db = new Database(url)

db.connect(function(err, dbclient) {
  if (err) return

  // do some thing with database here
})

drop(callback)

  • Drop database on server then close connection
Parameters:
Name Type Description
callback StdCallback

Function will be call after done

Source:
Example
// see constructor for detail
var db = new Database(url)

db.drop(function(err) {
  // handle for result of drop here
})

renew(callback)

Create new database

  • If database is not exists, create new database
  • If database is early exists, drop database then create new database
Parameters:
Name Type Description
callback StdCallback

Function will be call after done

Source: