LiteDB Cmdlets, the document store in PowerShell
Ldbc is the PowerShell module with LiteDB, a small, fast, zero configuration NoSQL embedded database.
- Ldbc works with Windows PowerShell and PowerShell Core.
- LiteDB is included and ready to use without installing anything.
Some LiteDB features:
- Single datafile
- ACID transactions
- Store files and stream data
- Cross collections references
- Recovery after writing failures
- Mapping classes to documents
Ldbc makes LiteDB operations and data PowerShell friendly. You can store and retrieve hashtables and dictionaries, PowerShell classes and custom objects, .NET complex types. Operate on data using specialized cmdlets or LiteDB SQL.
Step 1: Get and install
Ldbc is published as the PSGallery module.
You can install the module by this command:
Install-Module Ldbc
Step 2: In a PowerShell command prompt import the module:
Import-Module Ldbc
Step 3: Take a look at help and available commands:
help about_Ldbc
Get-Command -Module Ldbc
help Use-LiteDatabase -Full
Step 4: Try add, get, remove operations with a memory database
(a) using Add-LiteData
, Get-LiteData
, Remove-LiteData
:
Use-LiteDatabase :memory: {
# get the collection, specify auto id
$test = Get-LiteCollection Test Int32
# add two documents
@{Name = 'John'}, @{Name = 'Mary'} | Add-LiteData $test
# find using filter with an argument
$r = Get-LiteData $test -Where 'Name = @0', John
"$r" # {"_id":1,"Name":"John"}
# remove one by _id
Remove-LiteData $test -ById 1
# get all documents
$r = Get-LiteData $test
"$r" # {"_id":2,"Name":"Mary"}
}
(b) ditto using just Invoke-LiteCommand
and LiteDB SQL:
Use-LiteDatabase :memory: {
# add two documents
Invoke-LiteCommand 'INSERT INTO Test : INT VALUES {Name: "John"}, {Name: "Mary"}' -Quiet
# find using WHERE with parameters
$r = Invoke-LiteCommand 'SELECT $ FROM Test WHERE Name = @param1' @{param1 = 'John'}
"$r" # {"_id":1,"Name":"John"}
# remove using WHERE with parameters
Invoke-LiteCommand 'DELETE Test WHERE _id = @_id' @{_id = 1} -Quiet
# get all documents
$r = Invoke-LiteCommand 'SELECT $ FROM Test'
"$r" # {"_id":2,"Name":"Mary"}
}
(c) store and retrieve PowerShell custom objects
Use-LiteDatabase :memory: {
# get the collection
$test = Get-LiteCollection Test
# get PS objects, select some properties, insert
Get-ChildItem | Select-Object Name, Mode, Length | Add-LiteData $test
# get back PS custom objects
Get-LiteData $test -As PS
}
Next steps
Read cmdlets help with basic examples. Take a look at tests in the repository for more technical examples.
Read LiteDB docs. Some API may be needed and used directly in addition to provided by the module.
LiteDB | Module | Output |
---|---|---|
Database | ||
LiteDatabase | New-LiteDatabase | database (needs Dispose) |
LiteDatabase | Use-LiteDatabase {..} | $Database (auto Dispose) |
GetCollection | Get-LiteCollection | collection instance |
Execute | Invoke-LiteCommand | values, documents |
BeginTrans | Use-LiteTransaction {..} | .. |
+ Commit | (success) | |
+ Rollback | (failure) | |
Collection | ||
Count | Get-LiteData -Count | count |
Exists | Test-LiteData | true or false |
Find* | Get-LiteData | documents |
Insert | Add-LiteData | none, ids |
Update | Set-LiteData | none, count |
Upsert | Set-LiteData -Add | none, count |
UpdateMany | Update-LiteData | none, count |
DeleteMany | Remove-LiteData | none, count |
Misc | ||
RegisterType | Register-LiteType | none |
Work on module commands and features is in progress, they may change before v1.0.0