Skip to content

Commit

Permalink
Merge pull request #288 from orlandos-nl/jw-upsert-success-check-fix-7.0
Browse files Browse the repository at this point in the history
upsert-success check fails (also adds typesafe index building to Meow)
  • Loading branch information
JaapWijnen authored Aug 8, 2022
2 parents fbf80d2 + 3048cc2 commit 5641e17
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 3 deletions.
32 changes: 32 additions & 0 deletions Sources/Meow/Indexes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import MongoKitten

public func SortedIndex<M, T>(
named name: String,
field: QuerySubject<M, T>,
order: Sorting.Order = .ascending
) -> _MongoIndex {
SortedIndex(named: name, field: field.path.string, order: order)
}

public func UniqueIndex<M, T>(
named name: String,
field: QuerySubject<M, T>,
order: Sorting.Order = .ascending
) -> _MongoIndex {
UniqueIndex(named: name, field: field.path.string, order: order)
}

public func TTLIndex<M, T>(
named name: String,
field: QuerySubject<M, T>,
expireAfterSeconds seconds: Int
) -> _MongoIndex {
TTLIndex(named: name, field: field.path.string, expireAfterSeconds: seconds)
}

public func TextScoreIndex<M, T>(
named name: String,
field: QuerySubject<M, T>
) -> _MongoIndex {
TextScoreIndex(named: name, field: field.path.string)
}
7 changes: 7 additions & 0 deletions Sources/Meow/MeowCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,10 @@ extension MeowCollection where M: MutableModel & ReadableModel {
return try await self.deleteAll(where: filter.makeDocument(), writeConcern: writeConcern)
}
}

extension MeowCollection where M: KeyPathQueryable {
public func buildIndexes(@MongoIndexBuilder build: (QueryMatcher<M>) -> _MongoIndexes) async throws {
let matcher = QueryMatcher<M>()
try await self.raw.createIndexes(build(matcher).indexes)
}
}
2 changes: 1 addition & 1 deletion Sources/Meow/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ extension MutableModel {
public func save(in database: MeowDatabase) async throws -> MeowOperationResult {
let reply = try await database.collection(for: Self.self).upsert(self)
return MeowOperationResult(
success: reply.updatedCount == 1,
success: reply.updatedCount == 1 || reply.upserted != nil,
n: reply.updatedCount,
writeErrors: reply.writeErrors
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ extension MongoCollection {
transaction: nil
).decode(MongoIndex.self)
}

public func buildIndexes(@MongoIndexBuilder build: () -> _MongoIndexes) async throws {
return try await createIndexes(build().indexes)
}
}

public struct MongoIndex: Decodable {
Expand Down
2 changes: 1 addition & 1 deletion Sources/MongoKitten/IndexBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public struct MongoIndexBuilder {
}

public struct _MongoIndexes {
internal var indexes: [CreateIndexes.Index]
public private(set) var indexes: [CreateIndexes.Index]
}

public struct _MongoIndex {
Expand Down
26 changes: 26 additions & 0 deletions Tests/MeowTests/MeowTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,31 @@ class MeowTests: XCTestCase {
XCTAssertEqual(User.resolveFieldPath(\User.$password), ["password"])
XCTAssertEqual(User.resolveFieldPath(\User.$profile.$firstName), ["profile", "firstName"])
}

func testMigration() async throws {
try await meow.migrate("Unique Email", on: User.self) { migrator in
migrator.add { users in
try await users.buildIndexes { user in
UniqueIndex(named: "unique-email", field: user.$email)
}
}
}

let myEmail = "joannis@orlandos.nl"

let user1 = User(email: myEmail, password: "test1")
let user2 = User(email: myEmail, password: "test2")

let saveResult1 = try await user1.save(in: meow)
XCTAssertTrue(saveResult1.success)
let saveResult2 = try await user2.save(in: meow)
XCTAssertFalse(saveResult2.success)

let count = try await meow[User.self].count { user in
user.$email == myEmail
}

XCTAssertEqual(count, 1)
}
}
#endif
23 changes: 22 additions & 1 deletion Tests/MongoKittenTests/CRUDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ class CrudTests : XCTestCase {
XCTAssertEqual(updatedAccount.name, "updated")
}

func testIndexe () async throws {
func testIndexes () async throws {
try await testBulkCreateDummyAccounts()
let schema = mongo[DummyAccount.collectionName]

Expand All @@ -488,4 +488,25 @@ class CrudTests : XCTestCase {
XCTAssertEqual(result[0].name, "_id_")
XCTAssertEqual(result[1].name, "nameIndex")
}

func testIndexBuilder() async throws {
let schema = mongo[DummyAccount.collectionName]

try await schema.buildIndexes {
UniqueIndex(named: "unique-name", field: "name")
}

let dummyAccount1 = DummyAccount(name: "Dum", password: "test1", age: 1337)
let dummyAccount2 = DummyAccount(name: "Dum", password: "test2", age: 1338)

let result1 = try await schema.insertEncoded(dummyAccount1)
XCTAssertEqual(result1.insertCount, 1)

let result2 = try await schema.insertEncoded(dummyAccount2)
XCTAssertEqual(result2.insertCount, 0)

let count = try await schema.count()

XCTAssertEqual(count, 1)
}
}

0 comments on commit 5641e17

Please sign in to comment.