0.2.1 • Published 10 years ago

lsdb.js v0.2.1

Weekly downloads
4
License
MIT
Repository
github
Last release
10 years ago

Usage

Creating a database and table

// 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

Listing, describing, and dropping tables

// 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"]

Inserting into tables

// 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']] );

Querying the database

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
    }
  ]
*/

Methods