Update to PG doc library

This commit is contained in:
Daniel J. Summers 2023-02-17 20:26:17 -06:00
parent 1963d226ae
commit 612f04accc
10 changed files with 43 additions and 62 deletions

View File

@ -2,7 +2,7 @@
<ItemGroup>
<ProjectReference Include="..\MyWebLog.Domain\MyWebLog.Domain.fsproj" />
<ProjectReference Include="..\Npgsql.FSharp.Documents\Npgsql.FSharp.Documents.fsproj" />
<ProjectReference Include="..\..\..\Npgsql.Documents\src\Npgsql.FSharp.Documents\Npgsql.FSharp.Documents.fsproj" />
</ItemGroup>
<ItemGroup>

View File

@ -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<Category> source Table.Category webLogId
Document.findByWebLog<Category> 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
]
()
}

View File

@ -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 =

View File

@ -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<Page> Table.Page {| webLogDoc webLogId with Permalink = Permalink.toString permalink |}
Find.byContains<Page> 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<Page> source Table.Page webLogId
let! pages = Document.findByWebLog<Page> 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
}

View File

@ -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<Post> source Table.Post webLogId
let! posts = Document.findByWebLog<Post> 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
}

View File

@ -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<TagMap>
/// 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)
]
()

View File

@ -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<Theme> Table.Theme (ThemeId.toString themeId)
Find.byId<Theme> 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 ()

View File

@ -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<WebLog> Table.WebLog
all<WebLog> 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<WebLog> Table.WebLog (WebLogId.toString webLogId)
Find.byId<WebLog> 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 -> ()
}

View File

@ -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

View File

@ -16,7 +16,7 @@ type PostgresData (source : NpgsqlDataSource, log : ILogger<PostgresData>, 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
}