Skip to content

databaseHandler

Zephyrrus edited this page Feb 6, 2016 · 3 revisions

What is it?

It's a object used for easier access to the database by the modules. Every module get's its own table (no possibility to conflicts yet, so two modules can get a table with the same name)

#Constructor

function databaseHandler(moduleName, structure)

moduleName - this will be used as a table name. Soon to be replaced by tableName

structure - an object containing the structure of the table

#Structure Example:

[
  {name: "id", type: "autonumber", primaryKey: true},
  {name: "uid", type: "number", required: true},
  {name: "reason", type: "string"},
  {name: "addedDate", type: "datetime", required: true},
  {name: "addedBy", type: "number", required: true}
];

Obligatory keys:

  • name: the name of the row

  • type: the type of the row, can be one of the following autonumber, number, int, integer (will be converted to integer type), string, datetime, text (will be converted to TEXT type).

Optional keys:

  • required: set this to true if the row is required when doing inserts.

  • primaryKey: set this to true if you want that row to be your primary key (if the content when inserting is null, it will be autonumber)

#Inserting into the database It is done by the add function. Before inserting, every key is checked if it has a correct row in the table (based on the structure provided previously by the module), then the number of required keys is verified, if one or several is missing it will result in an error.

add([params], callback(err, res))

params is the values to insert, it must be an array of structures like the following one [{"rowName": "value"}]

Example: [{"uid": clientID}, {"name": clientName}]

Errors returned by this function are always an object like the following one

{type: "HANDLER_ERROR_INSERT",error: "One or more required parameters are missing"}

Errors returned by the SQLITE3 module are in the following format:

{type: "SQLITEERROR",error: err}

Example:

database.add([{"uid": user}, {"reason": reason}, {"addedDate": Date()}, {"addedBy": e.userID}], function(err, res){
    if(err) return (e.printError(e.channelID, err));
    //do stuffs
});

Clone this wiki locally