Skip to content

Commit

Permalink
Add Postgres support for nested subpath (JSON) expressions (#246)
Browse files Browse the repository at this point in the history
Implement the new SQLDialect.nestedSubpathExpression(in:for:) method for Postgres syntax.
  • Loading branch information
gwynne committed Jul 11, 2023
1 parent a88d025 commit b69f427
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/vapor/postgres-nio.git", from: "1.14.2"),
.package(url: "https://github.com/vapor/sql-kit.git", from: "3.26.0"),
.package(url: "https://github.com/vapor/sql-kit.git", from: "3.28.0"),
.package(url: "https://github.com/vapor/async-kit.git", from: "1.14.0"),
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.1.0")
],
Expand Down
15 changes: 15 additions & 0 deletions Sources/PostgresKit/PostgresDialect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,19 @@ public struct PostgresDialect: SQLDialect {
public var sharedSelectLockExpression: (any SQLExpression)? { SQLRaw("FOR SHARE") }

public var exclusiveSelectLockExpression: (any SQLExpression)? { SQLRaw("FOR UPDATE") }

public func nestedSubpathExpression(in column: any SQLExpression, for path: [String]) -> (any SQLExpression)? {
guard !path.isEmpty else { return nil }

let descender = SQLList(
[column] + path.dropLast().map(SQLLiteral.string(_:)),
separator: SQLRaw("->")
)
let accessor = SQLList(
[descender, SQLLiteral.string(path.last!)],
separator: SQLRaw("->>")
)

return SQLGroupExpression(accessor)
}
}

0 comments on commit b69f427

Please sign in to comment.