Skip to content

Commit 11d826a

Browse files
committed
guard against empty writes
1 parent b21a215 commit 11d826a

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

Sources/_MongoDB/Collections/Mongo.CollectionModel.swift

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -311,34 +311,35 @@ extension Mongo.CollectionModel
311311
func insert(some elements:some Sequence<some Insertable>) async throws
312312
{
313313
var count:Int = 0
314-
let response:Mongo.InsertResponse = try await session.run(
315-
command: Mongo.Insert.init(Self.name,
316-
writeConcern: .majority)
314+
let insert:Mongo.Insert = .init(Self.name, writeConcern: .majority)
315+
{
316+
$0[.ordered] = false
317+
}
318+
documents:
319+
{
320+
for element:some Insertable in elements
317321
{
318-
$0[.ordered] = false
322+
$0.append(element)
323+
count += 1
319324
}
320-
documents:
321-
{
322-
for element:some Insertable in elements
323-
{
324-
$0.append(element)
325-
count += 1
326-
}
327-
},
328-
against: self.database)
325+
}
329326

330327
if count == 0
331328
{
332329
return
333330
}
334331

332+
let response:Mongo.InsertResponse = try await self.session.run(
333+
command: insert,
334+
against: self.database)
335+
335336
let _:Mongo.Insertions = try response.insertions()
336337
}
337338

338339
@inlinable
339340
func insert(some element:some Insertable) async throws
340341
{
341-
let response:Mongo.InsertResponse = try await session.run(
342+
let response:Mongo.InsertResponse = try await self.session.run(
342343
command: Mongo.Insert.init(Self.name, writeConcern: .majority)
343344
{
344345
$0.append(element)
@@ -354,14 +355,23 @@ extension Mongo.CollectionModel
354355
func upsert(
355356
some elements:some Sequence<some Insertable>) async throws -> Mongo.Updates<Element.ID>
356357
{
357-
let response:Mongo.UpdateResponse<Element.ID> = try await session.run(
358-
command: Mongo.Update<Mongo.One, Element.ID>.init(Self.name)
358+
var count:Int = 0
359+
let update:Mongo.Update<Mongo.One, Element.ID> = .init(Self.name)
360+
{
361+
for element:some BSONDocumentEncodable & Identifiable<Element.ID> in elements
359362
{
360-
for element:some BSONDocumentEncodable & Identifiable<Element.ID> in elements
361-
{
362-
$0.upsert(element)
363-
}
364-
},
363+
$0.upsert(element)
364+
count += 1
365+
}
366+
}
367+
368+
if count == 0
369+
{
370+
return .init(selected: 0, modified: 0, upserted: [])
371+
}
372+
373+
let response:Mongo.UpdateResponse<Element.ID> = try await self.session.run(
374+
command: update,
365375
against: self.database)
366376

367377
return try response.updates()
@@ -370,7 +380,7 @@ extension Mongo.CollectionModel
370380
@inlinable
371381
func upsert(some element:some Insertable) async throws -> Element.ID?
372382
{
373-
let response:Mongo.UpdateResponse<Element.ID> = try await session.run(
383+
let response:Mongo.UpdateResponse<Element.ID> = try await self.session.run(
374384
command: Mongo.Update<Mongo.One, Element.ID>.init(Self.name)
375385
{
376386
$0.upsert(element)

0 commit comments

Comments
 (0)