From 612f04accc605bebd91ea397e66d37b5714a1da2 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Fri, 17 Feb 2023 20:26:17 -0600 Subject: [PATCH] Update to PG doc library --- src/MyWebLog.Data/MyWebLog.Data.fsproj | 2 +- .../Postgres/PostgresCategoryData.fs | 22 ++++++++----------- src/MyWebLog.Data/Postgres/PostgresHelpers.fs | 7 +++--- .../Postgres/PostgresPageData.fs | 20 +++++++---------- .../Postgres/PostgresPostData.fs | 9 ++++---- .../Postgres/PostgresTagMapData.fs | 9 ++++---- .../Postgres/PostgresThemeData.fs | 10 ++++----- .../Postgres/PostgresWebLogData.fs | 14 +++++------- .../Postgres/PostgresWebLogUserData.fs | 10 ++++----- src/MyWebLog.Data/PostgresData.fs | 2 +- 10 files changed, 43 insertions(+), 62 deletions(-) diff --git a/src/MyWebLog.Data/MyWebLog.Data.fsproj b/src/MyWebLog.Data/MyWebLog.Data.fsproj index 9e43e6e..38a5455 100644 --- a/src/MyWebLog.Data/MyWebLog.Data.fsproj +++ b/src/MyWebLog.Data/MyWebLog.Data.fsproj @@ -2,7 +2,7 @@ - + diff --git a/src/MyWebLog.Data/Postgres/PostgresCategoryData.fs b/src/MyWebLog.Data/Postgres/PostgresCategoryData.fs index ce0bda7..28d491a 100644 --- a/src/MyWebLog.Data/Postgres/PostgresCategoryData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresCategoryData.fs @@ -13,14 +13,12 @@ type PostgresCategoryData (source : NpgsqlDataSource, log : ILogger) = /// Count all categories for the given web log let countAll webLogId = log.LogTrace "Category.countAll" - Sql.fromDataSource source - |> Query.countByContains Table.Category (webLogDoc webLogId) + Count.byContains Table.Category (webLogDoc webLogId) /// Count all top-level categories for the given web log let countTopLevel webLogId = log.LogTrace "Category.countTopLevel" - Sql.fromDataSource source - |> Query.countByContains Table.Category {| webLogDoc webLogId with ParentId = None |} + Count.byContains Table.Category {| webLogDoc webLogId with ParentId = None |} /// Retrieve all categories for the given web log in a DotLiquid-friendly format let findAllForView webLogId = backgroundTask { @@ -78,7 +76,7 @@ type PostgresCategoryData (source : NpgsqlDataSource, log : ILogger) = /// Find all categories for the given web log let findByWebLog webLogId = log.LogTrace "Category.findByWebLog" - Document.findByWebLog source Table.Category webLogId + Document.findByWebLog Table.Category webLogId /// Create parameters for a category insert / update let catParameters (cat : Category) = @@ -90,15 +88,13 @@ type PostgresCategoryData (source : NpgsqlDataSource, log : ILogger) = match! findById catId webLogId with | Some cat -> // Reassign any children to the category's parent category - let! children = - Sql.fromDataSource source - |> Query.findByContains Table.Category {| ParentId = CategoryId.toString catId |} + let! children = Find.byContains Table.Category {| ParentId = CategoryId.toString catId |} let hasChildren = not (List.isEmpty children) if hasChildren then let! _ = Sql.fromDataSource source |> Sql.executeTransactionAsync [ - Query.updateQuery Table.Category, + Query.update Table.Category, children |> List.map (fun child -> catParameters { child with ParentId = cat.ParentId }) ] () @@ -112,7 +108,7 @@ type PostgresCategoryData (source : NpgsqlDataSource, log : ILogger) = let! _ = Sql.fromDataSource source |> Sql.executeTransactionAsync [ - Query.updateQuery Table.Post, + Query.update Table.Post, posts |> List.map (fun post -> [ "@id", Sql.string (PostId.toString post.Id) "@data", Query.jsonbDocParam @@ -123,7 +119,7 @@ type PostgresCategoryData (source : NpgsqlDataSource, log : ILogger) = ] () // Delete the category itself - do! Sql.fromDataSource source |> Query.deleteById Table.Category (CategoryId.toString catId) + do! Delete.byId Table.Category (CategoryId.toString catId) return if hasChildren then ReassignedChildCategories else CategoryDeleted | None -> return CategoryNotFound } @@ -131,7 +127,7 @@ type PostgresCategoryData (source : NpgsqlDataSource, log : ILogger) = /// Save a category let save (cat : Category) = backgroundTask { log.LogTrace "Category.save" - do! Sql.fromDataSource source |> Query.save Table.Category (CategoryId.toString cat.Id) cat + do! save Table.Category (CategoryId.toString cat.Id) cat } /// Restore categories from a backup @@ -140,7 +136,7 @@ type PostgresCategoryData (source : NpgsqlDataSource, log : ILogger) = let! _ = Sql.fromDataSource source |> Sql.executeTransactionAsync [ - Query.insertQuery Table.Category, cats |> List.map catParameters + Query.insert Table.Category, cats |> List.map catParameters ] () } diff --git a/src/MyWebLog.Data/Postgres/PostgresHelpers.fs b/src/MyWebLog.Data/Postgres/PostgresHelpers.fs index 573236c..c413fd3 100644 --- a/src/MyWebLog.Data/Postgres/PostgresHelpers.fs +++ b/src/MyWebLog.Data/Postgres/PostgresHelpers.fs @@ -185,10 +185,9 @@ module Document = |> tryHead /// Find a document by its ID for the given web log - let findByWebLog<'TDoc> source table webLogId : Task<'TDoc list> = - Sql.fromDataSource source - |> Query.findByContains table (webLogDoc webLogId) - + let findByWebLog<'TDoc> table webLogId : Task<'TDoc list> = + Find.byContains table (webLogDoc webLogId) + /// Functions to support revisions module Revisions = diff --git a/src/MyWebLog.Data/Postgres/PostgresPageData.fs b/src/MyWebLog.Data/Postgres/PostgresPageData.fs index c1b1679..729381a 100644 --- a/src/MyWebLog.Data/Postgres/PostgresPageData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresPageData.fs @@ -46,14 +46,12 @@ type PostgresPageData (source : NpgsqlDataSource, log : ILogger) = /// Count all pages for the given web log let countAll webLogId = log.LogTrace "Page.countAll" - Sql.fromDataSource source - |> Query.countByContains Table.Page (webLogDoc webLogId) + Count.byContains Table.Page (webLogDoc webLogId) /// Count all pages shown in the page list for the given web log let countListed webLogId = log.LogTrace "Page.countListed" - Sql.fromDataSource source - |> Query.countByContains Table.Page {| webLogDoc webLogId with IsInPageList = true |} + Count.byContains Table.Page {| webLogDoc webLogId with IsInPageList = true |} /// Find a page by its ID (without revisions) let findById pageId webLogId = @@ -75,7 +73,7 @@ type PostgresPageData (source : NpgsqlDataSource, log : ILogger) = log.LogTrace "Page.delete" match! pageExists pageId webLogId with | true -> - do! Sql.fromDataSource source |> Query.deleteById Table.Page (PageId.toString pageId) + do! Delete.byId Table.Page (PageId.toString pageId) return true | false -> return false } @@ -83,8 +81,7 @@ type PostgresPageData (source : NpgsqlDataSource, log : ILogger) = /// Find a page by its permalink for the given web log let findByPermalink permalink webLogId = log.LogTrace "Page.findByPermalink" - Sql.fromDataSource source - |> Query.findByContains Table.Page {| webLogDoc webLogId with Permalink = Permalink.toString permalink |} + Find.byContains Table.Page {| webLogDoc webLogId with Permalink = Permalink.toString permalink |} |> tryHead /// Find the current permalink within a set of potential prior permalinks for the given web log @@ -109,7 +106,7 @@ type PostgresPageData (source : NpgsqlDataSource, log : ILogger) = /// Get all complete pages for the given web log let findFullByWebLog webLogId = backgroundTask { log.LogTrace "Page.findFullByWebLog" - let! pages = Document.findByWebLog source Table.Page webLogId + let! pages = Document.findByWebLog Table.Page webLogId let! revisions = Revisions.findByWebLog source Table.PageRevision Table.Page PageId webLogId return pages @@ -143,7 +140,7 @@ type PostgresPageData (source : NpgsqlDataSource, log : ILogger) = let! _ = Sql.fromDataSource source |> Sql.executeTransactionAsync [ - Query.insertQuery Table.Page, + Query.insert Table.Page, pages |> List.map (fun page -> Query.docParameters (PageId.toString page.Id) { page with Revisions = [] }) Revisions.insertSql Table.PageRevision, @@ -156,7 +153,7 @@ type PostgresPageData (source : NpgsqlDataSource, log : ILogger) = let save (page : Page) = backgroundTask { log.LogTrace "Page.save" let! oldPage = findFullById page.Id page.WebLogId - do! Sql.fromDataSource source |> Query.save Table.Page (PageId.toString page.Id) { page with Revisions = [] } + do! save Table.Page (PageId.toString page.Id) { page with Revisions = [] } do! updatePageRevisions page.Id (match oldPage with Some p -> p.Revisions | None -> []) page.Revisions () } @@ -166,8 +163,7 @@ type PostgresPageData (source : NpgsqlDataSource, log : ILogger) = log.LogTrace "Page.updatePriorPermalinks" match! findById pageId webLogId with | Some page -> - do! Sql.fromDataSource source - |> Query.update Table.Page (PageId.toString page.Id) { page with PriorPermalinks = permalinks } + do! update Table.Page (PageId.toString page.Id) { page with PriorPermalinks = permalinks } return true | None -> return false } diff --git a/src/MyWebLog.Data/Postgres/PostgresPostData.fs b/src/MyWebLog.Data/Postgres/PostgresPostData.fs index 01537b2..4f5cafd 100644 --- a/src/MyWebLog.Data/Postgres/PostgresPostData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresPostData.fs @@ -111,7 +111,7 @@ type PostgresPostData (source : NpgsqlDataSource, log : ILogger) = /// Get all complete posts for the given web log let findFullByWebLog webLogId = backgroundTask { log.LogTrace "Post.findFullByWebLog" - let! posts = Document.findByWebLog source Table.Post webLogId + let! posts = Document.findByWebLog Table.Post webLogId let! revisions = Revisions.findByWebLog source Table.PostRevision Table.Post PostId webLogId return posts @@ -207,7 +207,7 @@ type PostgresPostData (source : NpgsqlDataSource, log : ILogger) = let save (post : Post) = backgroundTask { log.LogTrace "Post.save" let! oldPost = findFullById post.Id post.WebLogId - do! Sql.fromDataSource source |> Query.save Table.Post (PostId.toString post.Id) { post with Revisions = [] } + do! save Table.Post (PostId.toString post.Id) { post with Revisions = [] } do! updatePostRevisions post.Id (match oldPost with Some p -> p.Revisions | None -> []) post.Revisions } @@ -218,7 +218,7 @@ type PostgresPostData (source : NpgsqlDataSource, log : ILogger) = let! _ = Sql.fromDataSource source |> Sql.executeTransactionAsync [ - Query.insertQuery Table.Post, + Query.insert Table.Post, posts |> List.map (fun post -> Query.docParameters (PostId.toString post.Id) { post with Revisions = [] }) Revisions.insertSql Table.PostRevision, @@ -232,8 +232,7 @@ type PostgresPostData (source : NpgsqlDataSource, log : ILogger) = log.LogTrace "Post.updatePriorPermalinks" match! findById postId webLogId with | Some post -> - do! Sql.fromDataSource source - |> Query.update Table.Post (PostId.toString post.Id) { post with PriorPermalinks = permalinks } + do! update Table.Post (PostId.toString post.Id) { post with PriorPermalinks = permalinks } return true | None -> return false } diff --git a/src/MyWebLog.Data/Postgres/PostgresTagMapData.fs b/src/MyWebLog.Data/Postgres/PostgresTagMapData.fs index 3993685..c4a5a4e 100644 --- a/src/MyWebLog.Data/Postgres/PostgresTagMapData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresTagMapData.fs @@ -20,7 +20,7 @@ type PostgresTagMapData (source : NpgsqlDataSource, log : ILogger) = log.LogTrace "TagMap.delete" let! exists = Document.existsByWebLog source Table.TagMap tagMapId TagMapId.toString webLogId if exists then - do! Sql.fromDataSource source |> Query.deleteById Table.TagMap (TagMapId.toString tagMapId) + do! Delete.byId Table.TagMap (TagMapId.toString tagMapId) return true else return false } @@ -52,16 +52,15 @@ type PostgresTagMapData (source : NpgsqlDataSource, log : ILogger) = |> Sql.executeAsync fromData /// Save a tag mapping - let save (tagMap : TagMap) = backgroundTask { - do! Sql.fromDataSource source |> Query.save Table.TagMap (TagMapId.toString tagMap.Id) tagMap - } + let save (tagMap : TagMap) = + save Table.TagMap (TagMapId.toString tagMap.Id) tagMap /// Restore tag mappings from a backup let restore (tagMaps : TagMap list) = backgroundTask { let! _ = Sql.fromDataSource source |> Sql.executeTransactionAsync [ - Query.insertQuery Table.TagMap, + Query.insert Table.TagMap, tagMaps |> List.map (fun tagMap -> Query.docParameters (TagMapId.toString tagMap.Id) tagMap) ] () diff --git a/src/MyWebLog.Data/Postgres/PostgresThemeData.fs b/src/MyWebLog.Data/Postgres/PostgresThemeData.fs index 757b8d4..54166fc 100644 --- a/src/MyWebLog.Data/Postgres/PostgresThemeData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresThemeData.fs @@ -25,14 +25,12 @@ type PostgresThemeData (source : NpgsqlDataSource, log : ILogger) = /// Does a given theme exist? let exists themeId = log.LogTrace "Theme.exists" - Sql.fromDataSource source - |> Query.existsById Table.Theme (ThemeId.toString themeId) + Exists.byId Table.Theme (ThemeId.toString themeId) /// Find a theme by its ID let findById themeId = log.LogTrace "Theme.findById" - Sql.fromDataSource source - |> Query.tryById Table.Theme (ThemeId.toString themeId) + Find.byId Table.Theme (ThemeId.toString themeId) /// Find a theme by its ID (excludes the text of templates) let findByIdWithoutText themeId = @@ -48,7 +46,7 @@ type PostgresThemeData (source : NpgsqlDataSource, log : ILogger) = log.LogTrace "Theme.delete" match! exists themeId with | true -> - do! Sql.fromDataSource source |> Query.deleteById Table.Theme (ThemeId.toString themeId) + do! Delete.byId Table.Theme (ThemeId.toString themeId) return true | false -> return false } @@ -56,7 +54,7 @@ type PostgresThemeData (source : NpgsqlDataSource, log : ILogger) = /// Save a theme let save (theme : Theme) = log.LogTrace "Theme.save" - Sql.fromDataSource source |> Query.save Table.Theme (ThemeId.toString theme.Id) theme + save Table.Theme (ThemeId.toString theme.Id) theme interface IThemeData with member _.All () = all () diff --git a/src/MyWebLog.Data/Postgres/PostgresWebLogData.fs b/src/MyWebLog.Data/Postgres/PostgresWebLogData.fs index 5af36e3..f3d2d2f 100644 --- a/src/MyWebLog.Data/Postgres/PostgresWebLogData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresWebLogData.fs @@ -13,13 +13,12 @@ type PostgresWebLogData (source : NpgsqlDataSource, log : ILogger) = /// Add a web log let add (webLog : WebLog) = log.LogTrace "WebLog.add" - Sql.fromDataSource source |> Query.insert Table.WebLog (WebLogId.toString webLog.Id) webLog + insert Table.WebLog (WebLogId.toString webLog.Id) webLog /// Retrieve all web logs let all () = log.LogTrace "WebLog.all" - Sql.fromDataSource source - |> Query.all Table.WebLog + all Table.WebLog /// Delete a web log by its ID let delete webLogId = backgroundTask { @@ -54,21 +53,18 @@ type PostgresWebLogData (source : NpgsqlDataSource, log : ILogger) = /// Find a web log by its ID let findById webLogId = log.LogTrace "WebLog.findById" - Sql.fromDataSource source - |> Query.tryById Table.WebLog (WebLogId.toString webLogId) + Find.byId Table.WebLog (WebLogId.toString webLogId) /// Update settings for a web log let updateSettings (webLog : WebLog) = log.LogTrace "WebLog.updateSettings" - Sql.fromDataSource source |> Query.update Table.WebLog (WebLogId.toString webLog.Id) webLog + update Table.WebLog (WebLogId.toString webLog.Id) webLog /// Update RSS options for a web log let updateRssOptions (webLog : WebLog) = backgroundTask { log.LogTrace "WebLog.updateRssOptions" match! findById webLog.Id with - | Some blog -> - do! Sql.fromDataSource source - |> Query.update Table.WebLog (WebLogId.toString webLog.Id) { blog with Rss = webLog.Rss } + | Some blog -> do! update Table.WebLog (WebLogId.toString webLog.Id) { blog with Rss = webLog.Rss } | None -> () } diff --git a/src/MyWebLog.Data/Postgres/PostgresWebLogUserData.fs b/src/MyWebLog.Data/Postgres/PostgresWebLogUserData.fs index ebc03c5..33bd3cc 100644 --- a/src/MyWebLog.Data/Postgres/PostgresWebLogUserData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresWebLogUserData.fs @@ -34,7 +34,7 @@ type PostgresWebLogUserData (source : NpgsqlDataSource, log : ILogger) = if isAuthor then return Error "User has pages or posts; cannot delete" else - do! Sql.fromDataSource source |> Query.deleteById Table.WebLogUser usrId + do! Delete.byId Table.WebLogUser usrId return Ok true | None -> return Error "User does not exist" } @@ -77,7 +77,7 @@ type PostgresWebLogUserData (source : NpgsqlDataSource, log : ILogger) = let! _ = Sql.fromDataSource source |> Sql.executeTransactionAsync [ - Query.insertQuery Table.WebLogUser, + Query.insert Table.WebLogUser, users |> List.map (fun user -> Query.docParameters (WebLogUserId.toString user.Id) user) ] () @@ -88,16 +88,14 @@ type PostgresWebLogUserData (source : NpgsqlDataSource, log : ILogger) = log.LogTrace "WebLogUser.setLastSeen" match! findById userId webLogId with | Some user -> - do! Sql.fromDataSource source - |> Query.update Table.WebLogUser (WebLogUserId.toString userId) - { user with LastSeenOn = Some (Noda.now ()) } + do! update Table.WebLogUser (WebLogUserId.toString userId) { user with LastSeenOn = Some (Noda.now ()) } | None -> () } /// Save a user let save (user : WebLogUser) = log.LogTrace "WebLogUser.save" - Sql.fromDataSource source |> Query.save Table.WebLogUser (WebLogUserId.toString user.Id) user + save Table.WebLogUser (WebLogUserId.toString user.Id) user interface IWebLogUserData with member _.Add user = save user diff --git a/src/MyWebLog.Data/PostgresData.fs b/src/MyWebLog.Data/PostgresData.fs index 4af3459..81218c6 100644 --- a/src/MyWebLog.Data/PostgresData.fs +++ b/src/MyWebLog.Data/PostgresData.fs @@ -16,7 +16,7 @@ type PostgresData (source : NpgsqlDataSource, log : ILogger, ser : // Set up the PostgreSQL document store Configuration.useDataSource source Configuration.useSerializer - { new IDocumentSerializer with + { new Documents.IDocumentSerializer with member _.Serialize<'T> (it : 'T) : string = Utils.serialize ser it member _.Deserialize<'T> (it : string) : 'T = Utils.deserialize ser it }