Home

Awesome

Ldbc

LiteDB Cmdlets, the document store in PowerShell


Ldbc is the PowerShell module with LiteDB, a small, fast, zero configuration NoSQL embedded database.

Some LiteDB features:

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.

Quick start

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 methods and module commands

LiteDBModuleOutput
Database
LiteDatabaseNew-LiteDatabasedatabase (needs Dispose)
LiteDatabaseUse-LiteDatabase {..}$Database (auto Dispose)
GetCollectionGet-LiteCollectioncollection instance
ExecuteInvoke-LiteCommandvalues, documents
BeginTransUse-LiteTransaction {..}..
+ Commit(success)
+ Rollback(failure)
Collection
CountGet-LiteData -Countcount
ExistsTest-LiteDatatrue or false
Find*Get-LiteDatadocuments
InsertAdd-LiteDatanone, ids
UpdateSet-LiteDatanone, count
UpsertSet-LiteData -Addnone, count
UpdateManyUpdate-LiteDatanone, count
DeleteManyRemove-LiteDatanone, count
Misc
RegisterTypeRegister-LiteTypenone

Work in progress

Work on module commands and features is in progress, they may change before v1.0.0

See also