54 lines
1.3 KiB
Markdown
54 lines
1.3 KiB
Markdown
|
# LiteDB: An IndexedDB/MongoDb-inspired wrapper around SQLite
|
||
|
|
||
|
|
||
|
|
||
|
## Usage
|
||
|
|
||
|
To use LiteDb, pass the path to the database file, the current schema version
|
||
|
and an upgrade handler. This makes it easy to upgrade the database as new
|
||
|
stores and indexes are needed.
|
||
|
|
||
|
### Create (or upgrade) a store
|
||
|
|
||
|
Use the `createStore()` method to create or upgrade a store. If the store exists,
|
||
|
the instance will be returned so the same fluid code style can be used.
|
||
|
|
||
|
$db->createStore("foo")
|
||
|
->addIndex("bar");
|
||
|
|
||
|
### Create (insert) data
|
||
|
|
||
|
Create records using the `add()` or `addAll()` methods on the store.
|
||
|
|
||
|
### Update data
|
||
|
|
||
|
Update records using the `put()` method on the store.
|
||
|
|
||
|
### Delete data
|
||
|
|
||
|
Delete records using the `delete()` method on the store.
|
||
|
|
||
|
### Retrieving (selecting) data
|
||
|
|
||
|
Find records with the `get()` method, passing the key value to match against
|
||
|
and optionally the key to match.
|
||
|
|
||
|
### Example
|
||
|
|
||
|
$db = new LiteDb("data.db", 1, function ($db, $oldVersion) {
|
||
|
switch ($oldVersion) {
|
||
|
case null:
|
||
|
$db->createStore('users', 'username')
|
||
|
->addIndex('username', [ 'unique' => true ])
|
||
|
->addIndex('id', [ 'autoIncrement' => true ]);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
|
||
|
$db->users->add([
|
||
|
'username' => 'bob',
|
||
|
'password' => 'supersecret'
|
||
|
]);
|
||
|
|
||
|
$user = $db->users->get('bob');
|