// Instantiate the database.
var db = new LSDb( 'music' );
// Create a new table, setting default keys. Returns true on success.
db.create( 'publishers', ['name', 'location'] );
// return: true
// Create another table, setting default keys and value types.
db.create( 'albums', {'artist':'string', 'title':'string', 'year':'number', 'publishers':'array'} );
// return: true
// Get a list of all table names.
db.showTables();
// return: ["albums", "publishers"]
// Get a list of all keys in a table.
db.describe('albums');
// return: Object {artist: "string", title: "string", year: "number", publishers: "array"}
// Drop a table. Returns true on success.
db.drop('publishers');
// return: true
db.showTables();
// return: ["albums"]
// Insertions must follow the defined table data types.
db.insert( 'albums', ['s / s / s', 'beak & claw', 2013, ['anticon']] );
// return: true
// Inserting the wrong type will return false. If debug is enabled, the console will log an error.
db.insert( 'albums', ['s / s / s', 'beak & claw', '2013', 'anticon'] );
// return: false
/* console:
LSDb: failed to insert, 'year' accepts number format only
LSDb: failed to insert, 'publishers' accepts array format only
*/
// Inserting additional entries will incrementally build the table.
db.insert( 'albums', ['volcano choir', 'repave', 2013, ['jagjaguwar']] );
db.insert( 'albums', ['pedro the lion', 'winners never quit', 2000, ['jade tree']] );
db.insert( 'albums', ['pedro the lion', 'control', 2002, ['jade tree']] );
LSDb provides 2 methods for querying the database.
query( ) - Returns all rows and data from a specific table in an array.
// Select all data from a table.
db.query( 'albums' );
/* return:
[
{
ID: 0,
artist: 's / s / s',
publishers: [
'anticon'
],
title: 'beak & claw',
year: 2013
},
{
ID: 1,
artist: 'volcano choir',
publishers: [
'jagjaguwar'
],
title: 'repave',
year: 2013
},
{
ID: 2,
artist: 'pedro the lion',
publishers: [
'jade tree'
],
title: 'winners never quit',
year: 2000
},
{
ID: 3,
artist: 'pedro the lion',
publishers: [
'jade tree'
],
title: 'control',
year: 2002
}
]
*/
select( ) - Starts a chainable complex query against a specific table. Use in conjunction with results( ) to return an array of results.
// Select all data from a table.
db.select( 'albums' ).results();
// returns same results as db.query( 'albums' );
db.select( 'albums', ['artist','title'] )
.limit( 2 )
.results();
/* return:
[
{
ID: 0,
artist: 's / s / s',
title: 'beak & claw'
},
{
ID: 1,
artist: 'volcano choir',
title: 'repave'
}
]
*/
db.select( 'albums', ['artist','title','year'] )
.where({ column: 'year', operator: '>', pattern: 2000 })
.results();
/* return:
[
{
ID: 0,
artist: 's / s / s',
title: 'beak & claw',
year: 2013
},
{
ID: 1,
artist: 'volcano choir',
title: 'repave',
year: 2013
},
{
ID: 3,
artist: 'pedro the lion',
title: 'control',
year: 2002
}
]
*/
db.select( 'albums', ['title','year'] )
.where({ column: 'artist', operator: '===', pattern: 'pedro the lion' })
.limit( 1 )
.results();
/* return:
[
{
ID: 2,
title: 'winners never quit',
year: 2000
}
]
*/
Method | Arguments | Description | Return |
---|---|---|---|
LSDb() | database_name (String) | Constructor | |
create() | table_name (String), keys (Array or Object) |
Creates and defines a table. Passing an array creates a table with all columns defined as string. Passing an object allows you to set the definition per column to string, number, array, object, or JSON. | Boolean |
describe() | table_name | Returns an object of table keys and their definitions. | Object |
drop() | table_name | Deletes a table. | Boolean |
dropAll() | Deletes all tables. | Boolean | |
insert() | table_name (String), values (Array) |
Inserts data into the table. | Boolean |
limit() | limit (Number) | Limits a result set built from a chained select() function. Accepts a number and limits the results() Array to this number. | Object |
query() | table_name (String) | Returns an array of all data objects from the table. | Array |
results() | Closes a select() chain and returns the filtered results. | Array | |
select() | table_name (String), column_name (String, Array) |
Returns an chainable instance of the LSDb object for complex querying. | Object |
showTables() | Returns an array of all tables. | Array | |
where() | (Object) | Limits a result set built from a chained select() function. Accepts an object in the following format:{ column: String, operator: String, pattern: String or Number } Limits the results() Array to items that match the pattern object. |
Object |