Skip to content

Database API

Tim Stableford edited this page Feb 17, 2017 · 1 revision

Database API

P-Brain has a generic key-value storage system for storing data globally, specific to a skill, or specific to a user and skill.

Objects

skill

This is just a string representation of the skill.

user

This object is expected when calling user specific database functions and is passed into queries. It contains the following fields:

  • user_id (integer): The unique user ID.
  • username (text): The unique username.
  • password (text): The hashed and salted password.
  • is_admin (boolean): If the user is an admin this is true.

key

This is just a string representation of the key.

value

This can be any Javascript object compatible with JSON.stringify().

API

All function calls to the database are generators. The database object is globally accessible in global.db. This list will contain functions which are most likely to be used when developing a skill. For the full list see sqlite_db/index.js.

User and skill settings

Used for things like the users name. If all 3 values are not specified for getValue then it will return an array in the format: [{skill: '<skill>', username: '<username>', key: '<key>', value: '<value>'}]. If all are specified then it will simply return the value.

  • db.setValue(skill, user, key, value)
  • db.getValue(skill[optional], [optional], [optional])

Example

This example will get the users name if it is set. yield global.db.getValue('name', user, 'name')

Skill settings

These are used for config values such as API keys for a skill. If all both values are not specified for getSkillValue then it will return an array in the format: [{skill: '<skill>', key: '<key>', value: '<value>'}]. If all are specified then it will simply return the value.

  • db.setSkillValue(skill, key, value)
  • db.getSkillValue(skill[optional], key[optional])

Global settings

These ones are used for global settings, such as port numbers. If 'key' is not specified for getGlobalValue then it will return an array in the format: [{key: '<key>', value: '<value>'}]. If all are specified then it will simply return the value.

  • db.setGlobalValue(key, value)
  • db.getGlobalValue(key)
Clone this wiki locally