v2kiran / PSLiteDB

PowerShell wrapper for LiteDB

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ConvertTo-LiteDbBson - how to use long, datatime

chucksullivancvs opened this issue · comments

I use LiteDB in C# and I am able to create classes with Long (Int64) and DateTime date types and insert them into my db collection.

Now I need to do the same in PowerShell so I am trying PSLiteDB.

However I get errors on ConvertTo-LiteDbBson when I do.

Here's an example. Works unless I use the long or int64 and datetime type values.

I'm looking for guidance in getting this example to accept a long (specifically a datetime.tick) and a datetime).

Pls advise.


function Main()
{

Import-Module C:\PSScripts\PSLiteDB-master\PSLiteDB

$dbPath = "C:\temp\test1.db"
New-LiteDBDatabase -Path $dbPath -Verbose
Open-LiteDBConnection -Database $dbPath
New-LiteDBCollection -Collection Event

[Int]$myint = 1
[Long]$mylong = (Get-Date).Ticks
[String]$mystring = "foo"
[DateTime]$mydate = (Get-Date)
[Bool]$mybool = $true

$myOBJ = [PSCustomObject]@{
    MyInt = $myint
    MyLong = $mylong
    MyString = $mystring
    MyDate = $mydate
    MyVool = $mybool
}


$myBSON = $myOBJ | ConvertTo-LiteDbBson

Add-LiteDBDocument -Collection Event -Document $myBSON

Find-LiteDBDocument -Collection Event

}

Main


These are the errors I get when I do.


Exception calling "Deserialize" with "1" argument(s): "Value was either too large or too small for an Int32."
At C:\PSScripts\PSLiteDB-master\PSLiteDB.psm1:37 char:17

  •             $obj = [LiteDB.JsonSerializer]::Deserialize(
    
  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    • FullyQualifiedErrorId : OverflowException

Add-LiteDBDocument : Cannot validate argument on parameter 'Document'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\PSScripts\EnableLiteDBTest.ps1:28 char:52

  • Add-LiteDBDocument -Collection Event -Document $myBSON
    
  •                                                ~~~~~~~
    
    • CategoryInfo : InvalidData: (:) [Add-LiteDBDocument], ParameterBindingValidationException
    • FullyQualifiedErrorId : ParameterArgumentValidationError,PSLiteDB.AddLiteDBDocument

Unable to find type [PSLiteDB.Helpers.MSJsonDateConverter1].
At C:\PSScripts\PSLiteDB-master\PSLiteDB.psm1:51 char:47

  • ... $hash[$kvp.key] = [PSLiteDB.Helpers.MSJsonDateConverter1]::Conver ...
  •                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (PSLiteDB.Helpers.MSJsonDateConverter1:TypeName) [], RuntimeException
    • FullyQualifiedErrorId : TypeNotFound

the first issue of using [long] type has to do with pslitedb using json and then converting that to bsondocument for litedb and I guess the deserialize method needs extended json and not just json so anyway long story short I dont have a solution for that except to to store the $mylong as a [string] and then maybe convert it back to long on retrieval or usage.
Take a look at this issue here for more details:
mbdavid/LiteDB#834

The datetime type should work in pslitedb.

Regarding this error Unable to find type [PSLiteDB.Helpers.MSJsonDateConverter1].this is typo in the module . Please change the lines 51 & 83 from [PSLiteDB.Helpers.MSJsonDateConverter1].to[PSLiteDB.Helpers.MSJsonDateConverter].`

I think litedb v5 will be released soon so I will revisit these issues and maybe implement convertto-litedbbson in c#.

Ok. Thx for the reply.

I've already gone ahead and begun using string instead of int or datetime and then converting the values once I retrieve them.

whats the issue with datetime...do you get the same errors?

This code:

Import-Module C:\PSScripts\PSLiteDB-master\PSLiteDB

$dbPath = "C:\temp\test1000.db"
New-LiteDBDatabase -Path $dbPath -Verbose
Open-LiteDBConnection -Database $dbPath
New-LiteDBCollection -Collection Event

[Int]$myint = 1
[String]$mystring = "foo"
[DateTime]$mydate = Get-Date
[Bool]$mybool = $true

$myOBJ = [PSCustomObject]@{
    MyInt = $myint
    MyString = $mystring
    MyDate = $mydate
    MyBool = $mybool
}
$myBSON = $myOBJ | ConvertTo-LiteDbBson

generates this output and error

VERBOSE: Sucessfully created a LiteDB Database at Path: ['C:\temp\test1000.db']

Database : C:\temp\test1000.db
Log : LiteDB.Logger
Mapper : LiteDB.BsonMapper
Engine : LiteDB.LiteEngine
FileStorage : LiteDB.LiteStorage

Unable to find type [PSLiteDB.Helpers.MSJsonDateConverter1].
At C:\PSScripts\PSLiteDB-master\PSLiteDB.psm1:51 char:47

  • ... $hash[$kvp.key] = [PSLiteDB.Helpers.MSJsonDateConverter1]::Conver ...
  •                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (PSLiteDB.Helpers.MSJsonDateConverter1:TypeName) [], RuntimeException
    • FullyQualifiedErrorId : TypeNotFound

please check my earlier reply. you need to change the references on lines 51 & 83

Thx. Sorry. Missed that. Didn't realize you meant to for me modify the psm1. I tried it and that worked. Was able to add a DateTime typed value.

Fyi ... Back to the Long issue. The reason I was using Long was because I was converting from DateTime.Ticks values. I found that by storing dates as Long I am able to use the tick value in queries to select a range of dates like this:

var results = requests.Find(Query.Between("CreateTick", fromTicks, toTicks));

Query.Between does/did not support date ranges at the time ... not sure if it does now or not. I'll have to retest.

See: mbdavid/LiteDB#1134