Skip to content

Latest commit

 

History

History
233 lines (183 loc) · 7.27 KB

api.md

File metadata and controls

233 lines (183 loc) · 7.27 KB

Classes

TinyJsDb

Main tiny-js-db database controller

Table

the controller that handles the database tables

TinyJsDb

Main tiny-js-db database controller

Kind: global class

new TinyJsDb([opts])

Creates a new database instance and provide functions to manage the database.

Param Type Default Description
[opts] object Database configurations
[opts.autoGenerateIds] boolean true Set if ids will be generated automatically

Example

const TinyJsDb = require('tiny-js-db')
const db = new TinyJsDb()

db.tables ⇒ Array.<string>

Return all tables name from the database

Kind: instance property of TinyJsDb
Returns: Array.<string> - array of strings containing the tables name
Example

const tables = db.tables
console.log(tables)
// ['cars','countries']

db.relationships ⇒ Array.<Array>

Return the relationships from the database

Kind: instance property of TinyJsDb
Returns: Array.<Array> - array of tables instance pairs for each relationship on database
Example

const relationships = db.relationships
console.log(relationships)
// [
//   [ table1, table2 ],
//   [ table1, table3 ]
// ]

db.createTable(name) ⇒ object

Creates a new table on database

Kind: instance method of TinyJsDb
Returns: object - table instance

Param Type Description
name string name of the table

Example

db.createTable('cars')
// if you want the new table instance
const cars = db.createTable('cars')

db.getTable(table) ⇒ object

Return a table instance

Kind: instance method of TinyJsDb
Returns: object - table instance

Param Type Description
table string | object An string containing the table name or an instance of a Table

Example

const cars db.getTable('cars')

db.tableExists(name) ⇒ boolean

Check if a table exists in the database

Kind: instance method of TinyJsDb
Returns: boolean - true if the table exists and false if not

Param Type Description
name string An string containing the table name

Example

if(db.tableExists('cars')){}

db.createRelationship(tableA, tableB) ⇒ object

Create a relationship between two tables

  • The order of the tables are not important
  • If you try to create a relationship that already exists, an error will throw

Kind: instance method of TinyJsDb
Returns: object - The created relationship

Param Type Description
tableA string | object An string containing the table name or an instance of a Table
tableB string | object An string containing the table name or an instance of a Table

Example

db.createRelationship('cars', 'countries')
// or
db.createRelationship(cars, 'countries')
// or
db.createRelationship(cars, countries)
// or
const newRelation = db.createRelationship(cars, countries)

db.getRelationship(tableA, tableB) ⇒ object

Get a relationship

  • The order of the tables are not important

Kind: instance method of TinyJsDb
Returns: object - The relationship

Param Type Description
tableA string | object An string containing the table name or an instance of a Table
tableB string | object An string containing the table name or an instance of a Table

Example

const relation = db.getRelationship('cars', 'countries')
// or
const relation = db.getRelationship('countries', 'cars')
// or
const relation = db.getRelationship(cars, 'countries')
// or
const relation = db.getRelationship(cars, countries)

db.relationshipExists(tableA, tableB) ⇒ boolean

Check if a relationship between two tables exist

  • The order of the tables are not important

Kind: instance method of TinyJsDb
Returns: boolean - true if the relationship exists and false if not

Param Type Description
tableA string | object An string containing the table name or an instance of a Table
tableB string | object An string containing the table name or an instance of a Table

Example

if(db.relationshipExists('cars','countries')){}
// or
if(db.relationshipExists('countries', 'cars')){}
// or
if(db.relationshipExists(cars,'countries')){}
// or
if(db.relationshipExists(cars,countries)){}

Table

the controller that handles the database tables

Kind: global class

new Table(db, tableName)

Contains the logic for maintaining the session. An instance of this class is placed on the app object where it can be accessed

Param Type Description
db object The TinyJsDb instance
tableName string the table name

table.getById(id, [relations]) ⇒ object

Get a record by its id

Kind: instance method of Table
Returns: object - - The record

Param Type Default Description
id number The desired record id
[relations] array [] an array containing the tables name of desired relations