This commit is contained in:
Justin Mazzocchi 2020-09-29 16:44:31 -07:00
parent 1d18e0d5e2
commit c616b13dcb
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
13 changed files with 43 additions and 67 deletions

View file

@ -12,16 +12,13 @@ public struct AccountList: Codable, FetchableRecord, PersistableRecord {
}
extension AccountList {
static let joins = hasMany(
AccountListJoin.self,
using: ForeignKey([AccountListJoin.Columns.listId]))
.order(AccountListJoin.Columns.index)
static let joins = hasMany(AccountListJoin.self).order(AccountListJoin.Columns.index)
static let accounts = hasMany(
AccountRecord.self,
through: joins,
using: AccountListJoin.account)
var accounts: QueryInterfaceRequest<AccountResult> {
request(for: Self.accounts).accountResultRequest
AccountResult.request(request(for: Self.accounts))
}
}

View file

@ -8,7 +8,7 @@ struct AccountListJoin: Codable, FetchableRecord, PersistableRecord {
let listId: UUID
let index: Int
static let account = belongsTo(AccountRecord.self, using: ForeignKey([Columns.accountId]))
static let account = belongsTo(AccountRecord.self)
}
extension AccountListJoin {

View file

@ -8,7 +8,7 @@ struct AccountPinnedStatusJoin: Codable, FetchableRecord, PersistableRecord {
let statusId: String
let index: Int
static let status = belongsTo(StatusRecord.self, using: ForeignKey([Columns.statusId]))
static let status = belongsTo(StatusRecord.self)
}
extension AccountPinnedStatusJoin {

View file

@ -63,10 +63,8 @@ extension AccountRecord: FetchableRecord, PersistableRecord {
}
extension AccountRecord {
static let moved = belongsTo(AccountRecord.self, key: "moved")
static let pinnedStatusJoins = hasMany(
AccountPinnedStatusJoin.self,
using: ForeignKey([AccountPinnedStatusJoin.Columns.accountId]))
static let moved = belongsTo(AccountRecord.self)
static let pinnedStatusJoins = hasMany(AccountPinnedStatusJoin.self)
.order(AccountPinnedStatusJoin.Columns.index)
static let pinnedStatuses = hasMany(
StatusRecord.self,
@ -74,7 +72,7 @@ extension AccountRecord {
using: AccountPinnedStatusJoin.status)
var pinnedStatuses: QueryInterfaceRequest<StatusResult> {
request(for: Self.pinnedStatuses).statusResultRequest
StatusResult.request(request(for: Self.pinnedStatuses))
}
init(account: Account) {

View file

@ -8,9 +8,8 @@ struct AccountResult: Codable, Hashable, FetchableRecord {
let moved: AccountRecord?
}
extension QueryInterfaceRequest where RowDecoder == AccountRecord {
var accountResultRequest: QueryInterfaceRequest<AccountResult> {
including(optional: AccountRecord.moved)
.asRequest(of: AccountResult.self)
extension AccountResult {
static func request(_ request: QueryInterfaceRequest<AccountRecord>) -> QueryInterfaceRequest<Self> {
request.including(optional: AccountRecord.moved.forKey(CodingKeys.moved)).asRequest(of: self)
}
}

View file

@ -8,7 +8,7 @@ struct AccountStatusJoin: Codable, FetchableRecord, PersistableRecord {
let statusId: String
let collection: ProfileCollection
static let status = belongsTo(StatusRecord.self, using: ForeignKey([Columns.statusId]))
static let status = belongsTo(StatusRecord.self)
}
extension AccountStatusJoin {

View file

@ -190,8 +190,7 @@ public extension ContentDatabase {
func contextObservation(parentID: String) -> AnyPublisher<[[Status]], Error> {
ValueObservation.tracking { db -> [[StatusResult]] in
guard let parent = try StatusRecord.filter(StatusRecord.Columns.id == parentID)
.statusResultRequest
guard let parent = try StatusResult.request(StatusRecord.filter(StatusRecord.Columns.id == parentID))
.fetchOne(db) else {
return [[]]
}
@ -211,13 +210,12 @@ public extension ContentDatabase {
accountID: String,
collection: ProfileCollection) -> AnyPublisher<[[Status]], Error> {
ValueObservation.tracking { db -> [[StatusResult]] in
let statuses = try StatusRecord.filter(
let statuses = try StatusResult.request(StatusRecord.filter(
AccountStatusJoin
.select(AccountStatusJoin.Columns.statusId, as: String.self)
.filter(sql: "accountId = ? AND collection = ?", arguments: [accountID, collection.rawValue])
.contains(StatusRecord.Columns.id))
.order(StatusRecord.Columns.createdAt.desc)
.statusResultRequest
.order(StatusRecord.Columns.createdAt.desc))
.fetchAll(db)
if
@ -266,7 +264,7 @@ public extension ContentDatabase {
}
func accountObservation(id: String) -> AnyPublisher<Account?, Error> {
ValueObservation.tracking(AccountRecord.filter(AccountRecord.Columns.id == id).accountResultRequest.fetchOne)
ValueObservation.tracking(AccountResult.request(AccountRecord.filter(AccountRecord.Columns.id == id)).fetchOne)
.removeDuplicates()
.publisher(in: databaseWriter)
.map {

View file

@ -81,21 +81,17 @@ extension StatusRecord: FetchableRecord, PersistableRecord {
}
extension StatusRecord {
static let account = belongsTo(AccountRecord.self, key: "account",
using: ForeignKey([StatusRecord.Columns.accountId]))
static let account = belongsTo(AccountRecord.self)
static let accountMoved = hasOne(AccountRecord.self,
through: Self.account,
using: AccountRecord.moved,
key: "accountMoved")
using: AccountRecord.moved)
static let reblogAccount = hasOne(AccountRecord.self,
through: Self.reblog,
using: Self.account,
key: "reblogAccount")
using: Self.account)
static let reblogAccountMoved = hasOne(AccountRecord.self,
through: Self.reblogAccount,
using: AccountRecord.moved,
key: "reblogAccountMoved")
static let reblog = belongsTo(StatusRecord.self, key: "reblog")
using: AccountRecord.moved)
static let reblog = belongsTo(StatusRecord.self)
static let ancestorJoins = hasMany(
StatusContextJoin.self,
using: ForeignKey([StatusContextJoin.Columns.parentId]))
@ -114,11 +110,11 @@ extension StatusRecord {
using: StatusContextJoin.status)
var ancestors: QueryInterfaceRequest<StatusResult> {
request(for: Self.ancestors).statusResultRequest
StatusResult.request(request(for: Self.ancestors))
}
var descendants: QueryInterfaceRequest<StatusResult> {
request(for: Self.descendants).statusResultRequest
StatusResult.request(request(for: Self.descendants))
}
init(status: Status) {

View file

@ -13,6 +13,15 @@ struct StatusResult: Codable, Hashable, FetchableRecord {
}
extension StatusResult {
static func request(_ request: QueryInterfaceRequest<StatusRecord>) -> QueryInterfaceRequest<Self> {
request.including(required: StatusRecord.account.forKey(CodingKeys.account))
.including(optional: StatusRecord.accountMoved.forKey(CodingKeys.accountMoved))
.including(optional: StatusRecord.reblogAccount.forKey(CodingKeys.reblogAccount))
.including(optional: StatusRecord.reblogAccountMoved.forKey(CodingKeys.reblogAccountMoved))
.including(optional: StatusRecord.reblog.forKey(CodingKeys.reblog))
.asRequest(of: self)
}
var accountResult: AccountResult {
AccountResult(account: account, moved: accountMoved)
}
@ -23,14 +32,3 @@ extension StatusResult {
return AccountResult(account: reblogAccount, moved: reblogAccountMoved)
}
}
extension QueryInterfaceRequest where RowDecoder == StatusRecord {
var statusResultRequest: QueryInterfaceRequest<StatusResult> {
including(required: StatusRecord.account)
.including(optional: StatusRecord.accountMoved)
.including(optional: StatusRecord.reblogAccount)
.including(optional: StatusRecord.reblogAccountMoved)
.including(optional: StatusRecord.reblog)
.asRequest(of: StatusResult.self)
}
}

View file

@ -46,6 +46,6 @@ extension Timeline {
.order(StatusRecord.Columns.createdAt.desc)
var statuses: QueryInterfaceRequest<StatusResult> {
request(for: Self.statuses).statusResultRequest
StatusResult.request(request(for: Self.statuses))
}
}

View file

@ -156,10 +156,7 @@ public extension IdentityDatabase {
func identityObservation(id: UUID, immediate: Bool) -> AnyPublisher<Identity, Error> {
ValueObservation.tracking(
IdentityRecord
.filter(IdentityRecord.Columns.id == id)
.identityResultRequest
.fetchOne)
IdentityResult.request(IdentityRecord.filter(IdentityRecord.Columns.id == id)).fetchOne)
.removeDuplicates()
.publisher(in: databaseWriter, scheduling: immediate ? .immediate : .async(onQueue: .main))
.tryMap {
@ -172,10 +169,7 @@ public extension IdentityDatabase {
func identitiesObservation() -> AnyPublisher<[Identity], Error> {
ValueObservation.tracking(
IdentityRecord
.order(IdentityRecord.Columns.lastUsedAt.desc)
.identityResultRequest
.fetchAll)
IdentityResult.request(IdentityRecord.order(IdentityRecord.Columns.lastUsedAt.desc)).fetchAll)
.removeDuplicates()
.publisher(in: databaseWriter)
.map { $0.map(Identity.init(result:)) }
@ -184,9 +178,7 @@ public extension IdentityDatabase {
func recentIdentitiesObservation(excluding: UUID) -> AnyPublisher<[Identity], Error> {
ValueObservation.tracking(
IdentityRecord
.order(IdentityRecord.Columns.lastUsedAt.desc)
.identityResultRequest
IdentityResult.request(IdentityRecord.order(IdentityRecord.Columns.lastUsedAt.desc))
.filter(IdentityRecord.Columns.id != excluding)
.limit(9)
.fetchAll)
@ -207,9 +199,7 @@ public extension IdentityDatabase {
func identitiesWithOutdatedDeviceTokens(deviceToken: Data) -> AnyPublisher<[Identity], Error> {
databaseWriter.readPublisher(
value: IdentityRecord
.order(IdentityRecord.Columns.lastUsedAt.desc)
.identityResultRequest
value: IdentityResult.request(IdentityRecord.order(IdentityRecord.Columns.lastUsedAt.desc))
.filter(IdentityRecord.Columns.lastRegisteredDeviceToken != deviceToken)
.fetchAll)
.map { $0.map(Identity.init(result:)) }

View file

@ -29,8 +29,8 @@ extension IdentityRecord {
static let pushSubscriptionAlerts = Column(IdentityRecord.CodingKeys.pushSubscriptionAlerts)
}
static let instance = belongsTo(Identity.Instance.self, key: "instance")
static let account = hasOne(Identity.Account.self, key: "account")
static let instance = belongsTo(Identity.Instance.self)
static let account = hasOne(Identity.Account.self)
var instance: QueryInterfaceRequest<Identity.Instance> {
request(for: Self.instance)

View file

@ -10,10 +10,10 @@ struct IdentityResult: Codable, Hashable, FetchableRecord {
let account: Identity.Account?
}
extension QueryInterfaceRequest where RowDecoder == IdentityRecord {
var identityResultRequest: QueryInterfaceRequest<IdentityResult> {
including(optional: IdentityRecord.instance)
.including(optional: IdentityRecord.account)
.asRequest(of: IdentityResult.self)
extension IdentityResult {
static func request(_ request: QueryInterfaceRequest<IdentityRecord>) -> QueryInterfaceRequest<Self> {
request.including(optional: IdentityRecord.instance.forKey(CodingKeys.instance))
.including(optional: IdentityRecord.account.forKey(CodingKeys.account))
.asRequest(of: self)
}
}