Main tiny-js-db database controller
Kind: global class
- TinyJsDb
- new TinyJsDb([opts])
- .tables ⇒
Array.<string>
- .relationships ⇒
Array.<Array>
- .createTable(name) ⇒
object
- .getTable(table) ⇒
object
- .tableExists(name) ⇒
boolean
- .createRelationship(tableA, tableB) ⇒
object
- .getRelationship(tableA, tableB) ⇒
object
- .relationshipExists(tableA, tableB) ⇒
boolean
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()
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']
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 ]
// ]
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')
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')
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')){}
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)
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)
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)){}
the controller that handles the database tables
Kind: global class
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 |
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 |