airtable-api 0.3.2.3 → 0.3.2.4
raw patch · 3 files changed
+41/−6 lines, 3 filesPVP: minor bump suggested
API additions: PVP suggests at least a minor version bump
API changes (from Hackage documentation)
+ Airtable.Table: instance Data.Aeson.Types.ToJSON.ToJSON a => Data.Aeson.Types.ToJSON.ToJSON (Airtable.Table.Table a)
+ Airtable.Table: vDeleteWhere :: Table a -> (a -> Bool) -> Table a
+ Airtable.Table: vSelect :: (HasCallStack, HasRecordId r, Show a) => Table a -> r -> a
+ Airtable.Table: vSelectAll :: Table a -> [a]
+ Airtable.Table: vSelectKeyWhere :: Table a -> (a -> Bool) -> [RecordID]
+ Airtable.Table: vSelectMaybe :: (HasRecordId r) => Table a -> r -> Maybe a
+ Airtable.Table: vSelectWhere :: Table a -> (a -> Bool) -> [a]
Files
- airtable-api.cabal +1/−1
- src/Airtable/Query.hs +3/−3
- src/Airtable/Table.hs +37/−2
airtable-api.cabal view
@@ -1,5 +1,5 @@ name: airtable-api-version: 0.3.2.3+version: 0.3.2.4 synopsis: Requesting and introspecting Tables within an Airtable project. -- description: homepage: https://github.com/ooblahman/airtable-api
src/Airtable/Query.hs view
@@ -26,7 +26,7 @@ import Control.Lens ((^.), (.~), (&)) import Control.Monad (void) -import Data.Aeson (FromJSON, ToJSON, eitherDecode, toJSON)+import Data.Aeson import Data.Monoid import Data.ByteString.Lazy (ByteString) import qualified Data.ByteString.Char8 as BC@@ -100,7 +100,7 @@ where url = tableNameToUrl opts tname netOpts = mkWreqOptions opts - payload = toJSON a+ payload = object ["fields" .= a] -- | Update a record on a given table, using the supplied fields ('a'). updateRecord :: (ToJSON a) => AirtableOptions -> TableName -> RecordID -> a -> IO ()@@ -110,7 +110,7 @@ where url = tableNameToUrl opts tname <> "/" <> rec2str recId netOpts = mkWreqOptions opts- payload = toJSON a+ payload = object ["fields" .= a] -- | Delete a record on a table. deleteRecord :: AirtableOptions -> TableName -> RecordID -> IO ()
src/Airtable/Table.hs view
@@ -23,12 +23,18 @@ , toList , exists , select+ , vSelect , selectMaybe+ , vSelectMaybe , selectAll+ , vSelectAll , selectAllKeys , selectWhere+ , vSelectWhere , selectKeyWhere+ , vSelectKeyWhere , deleteWhere+ , vDeleteWhere ) where @@ -128,6 +134,11 @@ offset <- v .:? "offset" return $ (fromRecords recs) { tableOffset = offset } +instance (ToJSON a) => ToJSON (Table a) where+ toJSON tbl = object [ "offset" .= tableOffset tbl+ , "records" .= selectAll tbl+ ]+ instance Monoid (Table a) where mempty = Table mempty Nothing mappend (Table t1 o) (Table t2 _) = Table (mappend t1 t2) o@@ -162,14 +173,26 @@ Just v -> v Nothing -> error $ "lookup failed in map: " <> show k +-- | Same as 'select', but returns the record object.+vSelect :: (HasCallStack, HasRecordId r, Show a) => Table a -> r -> a+vSelect tbl rec = recordObj (select tbl rec)+ -- | Safely lookup a record using its RecordID. selectMaybe :: (HasRecordId r) => Table a -> r -> Maybe (Record a) selectMaybe tbl rec = toRecId rec `Map.lookup` tableRecords tbl +-- | Same as 'selectMaybe', but returns the record object.+vSelectMaybe :: (HasRecordId r) => Table a -> r -> Maybe a+vSelectMaybe tbl rec = recordObj <$> selectMaybe tbl rec+ -- | Read all records. selectAll :: Table a -> [Record a] selectAll = map snd . toList +-- | Same as 'selectAll', but returns the record object.+vSelectAll :: Table a -> [a]+vSelectAll = map recordObj . selectAll+ -- | Read all RecordID's. selectAllKeys :: Table a -> [RecordID] selectAllKeys = map fst . toList@@ -178,10 +201,22 @@ selectWhere :: Table a -> (Record a -> Bool) -> [Record a] selectWhere tbl f = filter f (selectAll tbl) --- | Select all RecordID's satisfying a condition.+-- | Same as 'selectWhere', but returns the record object.+vSelectWhere :: Table a -> (a -> Bool) -> [a]+vSelectWhere tbl f = filter f (vSelectAll tbl)++-- | Select all records satisfying a condition. selectKeyWhere :: Table a -> (Record a -> Bool) -> [RecordID]-selectKeyWhere tbl f = map recordId $ filter f (selectAll tbl)+selectKeyWhere tbl f = map recordId (selectWhere tbl f) +-- | Same as 'selectKeyWhere', but returns the record object.+vSelectKeyWhere :: Table a -> (a -> Bool) -> [RecordID]+vSelectKeyWhere tbl f = selectKeyWhere tbl (f . recordObj) + -- | Delete all Records satisfying a condition. deleteWhere :: Table a -> (Record a -> Bool) -> Table a deleteWhere (Table recs off) f = Table (Map.filter (\v -> not $ f v) recs) off++-- | Same as 'deleteWhere', but returns the record object.+vDeleteWhere :: Table a -> (a -> Bool) -> Table a+vDeleteWhere tbl f = deleteWhere tbl (f . recordObj)