Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,34 @@ let (data, response) = try await client.collection(name: "companies").documents(
)
```

### Delete all documents in a collection

`truncate` deletes every document and keeps the collection and its schema. The server ignores `filterBy` when `truncate` is set.

```swift
let (data, response) = try await client.collection(name: "companies").documents().delete(
options: DeleteDocumentsParameters(filterBy: "", truncate: true)
)
```

### Update a collection

```swift
let schema = CollectionUpdateSchema(fields: [
Field(name: "country", type: "string", drop: true),
Field(name: "company_category", type: "string")
])
let (updatedSchema, response) = try await client.collection(name: "companies").update(schema: schema)
```

To change only `metadata` or `synonym_sets`, leave out `fields`:

```swift
let (updatedSchema, response) = try await client.collection(name: "companies").update(
schema: CollectionUpdateSchema(metadata: ["owner": "search"])
)
```

### Export documents

```swift
Expand Down
10 changes: 10 additions & 0 deletions Sources/Typesense/Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public struct Collection {
return (nil, response)
}

public func update(schema: CollectionUpdateSchema) async throws -> (CollectionUpdateSchema?, URLResponse?) {
let schemaData = try encoder.encode(schema)
let (data, response) = try await apiCall.patch(endPoint: endpointPath(), body: schemaData)
if let result = data {
let updatedSchema = try decoder.decode(CollectionUpdateSchema.self, from: result)
return (updatedSchema, response)
}
return (nil, response)
}

private func endpointPath() throws -> String {
return "\(Collections.RESOURCEPATH)/\(try collectionName.encodeURL())"
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Typesense/Models/CollectionUpdateSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import AnyCodable
public struct CollectionUpdateSchema: Codable {

/** A list of fields for querying, filtering and faceting */
public var fields: [Field]
public var fields: [Field]?
/** List of synonym set names to associate with this collection */
public var synonymSets: [String]?
/** Optional details about the collection, e.g., when it was created, who created it etc. */
public var metadata: AnyCodable?

public init(fields: [Field], synonymSets: [String]? = nil, metadata: AnyCodable? = nil) {
public init(fields: [Field]? = nil, synonymSets: [String]? = nil, metadata: AnyCodable? = nil) {
self.fields = fields
self.synonymSets = synonymSets
self.metadata = metadata
Expand All @@ -35,7 +35,7 @@ public struct CollectionUpdateSchema: Codable {

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(fields, forKey: .fields)
try container.encodeIfPresent(fields, forKey: .fields)
try container.encodeIfPresent(synonymSets, forKey: .synonymSets)
try container.encodeIfPresent(metadata, forKey: .metadata)
}
Expand Down
26 changes: 26 additions & 0 deletions Tests/TypesenseTests/CollectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,30 @@ final class CollectionTests: XCTestCase {
}
}

func testCollectionUpdate() async throws {
try await createCollection()
let schema = CollectionUpdateSchema(fields: [
Field(name: "country", type: "string", drop: true),
Field(name: "company_category", type: "string")
])
let (updatedSchema, _) = try await client.collection(name: "companies").update(schema: schema)
XCTAssertEqual(updatedSchema?.fields?.map(\.name), ["country", "company_category"])

let (collection, _) = try await client.collection(name: "companies").retrieve()
let fieldNames = try XCTUnwrap(collection).fields.map(\.name)
XCTAssertTrue(fieldNames.contains("company_category"))
XCTAssertFalse(fieldNames.contains("country"))
}

func testCollectionUpdateMetadataOnly() async throws {
try await createCollection()
let (updatedSchema, _) = try await client.collection(name: "companies").update(schema: CollectionUpdateSchema(metadata: ["owner": "search"]))
XCTAssertNil(updatedSchema?.fields)
XCTAssertEqual(updatedSchema?.metadata, ["owner": "search"])

let (collection, _) = try await client.collection(name: "companies").retrieve()
XCTAssertEqual(collection?.metadata, ["owner": "search"])
XCTAssertEqual(collection?.fields.count, 4)
}

}
11 changes: 11 additions & 0 deletions Tests/TypesenseTests/DocumentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -398,5 +398,16 @@ final class DocumentTests: XCTestCase {
}
}

func testDocumentsTruncate() async throws {
try await createDocument()
let (data, _) = try await client.collection(name: "companies").documents().delete(
options: DeleteDocumentsParameters(filterBy: "", truncate: true)
)
XCTAssertEqual(data?.numDeleted, 1)

let (collection, _) = try await client.collection(name: "companies").retrieve()
XCTAssertEqual(collection?.numDocuments, 0)
}


}
Loading