From c14897d6edfa857e39700cd13b2efa629b29708e Mon Sep 17 00:00:00 2001 From: rightfold Date: Mon, 30 Jan 2017 11:12:55 +0100 Subject: [PATCH] Support Instant serialization --- bower.json | 3 ++- src/Database/PostgreSQL.js | 4 ++++ src/Database/PostgreSQL.purs | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/bower.json b/bower.json index 043a51e..e27ae5f 100644 --- a/bower.json +++ b/bower.json @@ -14,7 +14,8 @@ "purescript-foldable-traversable": "^2.0.0", "purescript-newtype": "^1.1.0", "purescript-bytestrings": "^0.0.3", - "purescript-arrays": "^3.1.0" + "purescript-arrays": "^3.1.0", + "purescript-datetime": "^2.0.0" }, "repository": { "type": "git", diff --git a/src/Database/PostgreSQL.js b/src/Database/PostgreSQL.js index 40c0008..f18a544 100644 --- a/src/Database/PostgreSQL.js +++ b/src/Database/PostgreSQL.js @@ -52,6 +52,10 @@ exports._query = function(client) { }; }; +exports.instantToString = function(i) { + return new Date(i).toUTCString(); +}; + exports.unsafeIsBuffer = function(x) { return x instanceof Buffer; }; diff --git a/src/Database/PostgreSQL.purs b/src/Database/PostgreSQL.purs index 7a17c01..0a080ed 100644 --- a/src/Database/PostgreSQL.purs +++ b/src/Database/PostgreSQL.purs @@ -25,6 +25,7 @@ import Control.Monad.Error.Class (catchError, throwError) import Control.Monad.Except (runExcept) import Data.Array (head) import Data.ByteString (ByteString) +import Data.DateTime.Instant (Instant) import Data.Either (Either(..)) import Data.Foreign (Foreign, isNull, readArray, readChar, readInt, readString, toForeign, unsafeFromForeign) import Data.Foreign.Null (writeNull) @@ -143,6 +144,9 @@ instance fromSQLValueByteString :: FromSQLValue ByteString where | unsafeIsBuffer x = Just $ unsafeFromForeign x | otherwise = Nothing +instance toSQLValueInstant :: ToSQLValue Instant where + toSQLValue = instantToString + instance toSQLValueMaybe :: (ToSQLValue a) => ToSQLValue (Maybe a) where toSQLValue Nothing = writeNull toSQLValue (Just x) = toSQLValue x @@ -151,6 +155,7 @@ instance fromSQLValueMaybe :: (FromSQLValue a) => FromSQLValue (Maybe a) where fromSQLValue x | isNull x = Just Nothing | otherwise = Just <$> fromSQLValue x +foreign import instantToString :: Instant -> Foreign foreign import unsafeIsBuffer :: ∀ a. a -> Boolean -- | Create a new connection pool.