Skip to content

Commit 5805d86

Browse files
authored
Merge Initialise Swift Client #1
2 parents 05d7dd8 + ae8011d commit 5805d86

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+3116
-0
lines changed

.circleci/config.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Use the latest 2.1 version of CircleCI pipeline process engine. See: https://circleci.com/docs/2.0/configuration-reference
2+
version: 2.1
3+
# Use a package of configuration called an orb.
4+
orbs:
5+
# Declare a dependency on the welcome-orb
6+
welcome: circleci/welcome-orb@0.4.1
7+
# Orchestrate or schedule a set of jobs
8+
workflows:
9+
# Name the workflow "welcome"
10+
welcome:
11+
# Run the welcome/run job in its own container
12+
jobs:
13+
- welcome/run

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
/*.xcodeproj
5+
xcuserdata/
6+
DerivedData/
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1300"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "Typesense"
18+
BuildableName = "Typesense"
19+
BlueprintName = "Typesense"
20+
ReferencedContainer = "container:">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
<BuildActionEntry
24+
buildForTesting = "YES"
25+
buildForRunning = "YES"
26+
buildForProfiling = "NO"
27+
buildForArchiving = "NO"
28+
buildForAnalyzing = "YES">
29+
<BuildableReference
30+
BuildableIdentifier = "primary"
31+
BlueprintIdentifier = "ApiTests"
32+
BuildableName = "ApiTests"
33+
BlueprintName = "ApiTests"
34+
ReferencedContainer = "container:">
35+
</BuildableReference>
36+
</BuildActionEntry>
37+
<BuildActionEntry
38+
buildForTesting = "YES"
39+
buildForRunning = "YES"
40+
buildForProfiling = "NO"
41+
buildForArchiving = "NO"
42+
buildForAnalyzing = "YES">
43+
<BuildableReference
44+
BuildableIdentifier = "primary"
45+
BlueprintIdentifier = "TypesenseTests"
46+
BuildableName = "TypesenseTests"
47+
BlueprintName = "TypesenseTests"
48+
ReferencedContainer = "container:">
49+
</BuildableReference>
50+
</BuildActionEntry>
51+
</BuildActionEntries>
52+
</BuildAction>
53+
<TestAction
54+
buildConfiguration = "Debug"
55+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
56+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
57+
shouldUseLaunchSchemeArgsEnv = "YES">
58+
<Testables>
59+
<TestableReference
60+
skipped = "NO">
61+
<BuildableReference
62+
BuildableIdentifier = "primary"
63+
BlueprintIdentifier = "ApiTests"
64+
BuildableName = "ApiTests"
65+
BlueprintName = "ApiTests"
66+
ReferencedContainer = "container:">
67+
</BuildableReference>
68+
</TestableReference>
69+
<TestableReference
70+
skipped = "NO">
71+
<BuildableReference
72+
BuildableIdentifier = "primary"
73+
BlueprintIdentifier = "TypesenseTests"
74+
BuildableName = "TypesenseTests"
75+
BlueprintName = "TypesenseTests"
76+
ReferencedContainer = "container:">
77+
</BuildableReference>
78+
</TestableReference>
79+
</Testables>
80+
</TestAction>
81+
<LaunchAction
82+
buildConfiguration = "Debug"
83+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
84+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
85+
launchStyle = "0"
86+
useCustomWorkingDirectory = "NO"
87+
ignoresPersistentStateOnLaunch = "NO"
88+
debugDocumentVersioning = "YES"
89+
debugServiceExtension = "internal"
90+
allowLocationSimulation = "YES">
91+
</LaunchAction>
92+
<ProfileAction
93+
buildConfiguration = "Release"
94+
shouldUseLaunchSchemeArgsEnv = "YES"
95+
savedToolIdentifier = ""
96+
useCustomWorkingDirectory = "NO"
97+
debugDocumentVersioning = "YES">
98+
<MacroExpansion>
99+
<BuildableReference
100+
BuildableIdentifier = "primary"
101+
BlueprintIdentifier = "Typesense"
102+
BuildableName = "Typesense"
103+
BlueprintName = "Typesense"
104+
ReferencedContainer = "container:">
105+
</BuildableReference>
106+
</MacroExpansion>
107+
</ProfileAction>
108+
<AnalyzeAction
109+
buildConfiguration = "Debug">
110+
</AnalyzeAction>
111+
<ArchiveAction
112+
buildConfiguration = "Release"
113+
revealArchiveInOrganizer = "YES">
114+
</ArchiveAction>
115+
</Scheme>

Package.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// swift-tools-version:5.5.0
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "Typesense",
8+
platforms: [
9+
.iOS(.v15), .macOS(.v12)
10+
],
11+
products: [
12+
// Products define the executables and libraries a package produces, and make them visible to other packages.
13+
.library(
14+
name: "Typesense",
15+
targets: ["Typesense"]),
16+
],
17+
dependencies: [
18+
// Dependencies declare other packages that this package depends on.
19+
// .package(url: /* package url */, from: "1.0.0"),
20+
],
21+
targets: [
22+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
23+
// Targets can depend on other targets in this package, and on products in packages this package depends on.
24+
.target(
25+
name: "Typesense",
26+
dependencies: []),
27+
.testTarget(
28+
name: "TypesenseTests",
29+
dependencies: ["Typesense"]),
30+
]
31+
)

README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Typesense Swift
2+
3+
A great new way to implement your searches on iOS using [Typesense](https://github.com/typesense/typesense) ⚡️🔍✨ Typesense Swift is a high level wrapper that helps you easily implement searching using Typesense.
4+
5+
## Installation
6+
7+
Add `Typesense Swift` Swift Package to your project. You can refer [Apple's Documentation](https://developer.apple.com/documentation/swift_packages/adding_package_dependencies_to_your_app) to add Typesense Swift as a dependency to your iOS Project. You can also import Typesense into your own Swift Package by adding this line to dependencies array of `Package.swift`:
8+
9+
```swift
10+
...
11+
dependencies: [
12+
.package(url: "https://github.com/typesense/typesense-swift", .upToNextMajor(from: "0.1.0"),
13+
],
14+
...
15+
```
16+
17+
## Usage
18+
19+
### Setting up the client
20+
21+
Import Typesense onto your Swift Project:
22+
23+
```swift
24+
import Typesense
25+
```
26+
27+
Declare the Typesense nodes that are available as `Node` members:
28+
29+
```swift
30+
let node1 = Node(host: "localhost", port: "8108", nodeProtocol: "http")
31+
let node2 = Node(host: "super-awesome.search", port: "8080", nodeProtocol: "https") //and so on
32+
```
33+
34+
Create a configuration and hence a client with the Nodes mentioned:
35+
36+
```swift
37+
let myConfig = Configuration(nodes: [node1, node2], apiKey: "coolstuff")
38+
39+
let client = Client(config: myConfig)
40+
```
41+
You can use Typesense parameters like `nearestNode` and `connectionTimeoutSeconds` while creating the configuration. You can also pass in a `logger` parameter to debug the code like this:
42+
43+
```swift
44+
let myConfig = Configuration(nodes: [node1, node2], apiKey: "coolstuff", logger: Logger(debugMode: true))
45+
```
46+
47+
### Indexing documents
48+
49+
You can create a collection by first defining a collection schema:
50+
51+
```swift
52+
let myCoolSchema = CollectionSchema(name: "schools", fields: [Field(name: "school_name", type: "string"), Field(name: "num_students", type: "int32"), Field(name: "country", type: "string", facet: true)], defaultSortingField: "num_students")
53+
54+
let (data, response) = try await client.collections.create(schema: myCoolSchema)
55+
```
56+
57+
Define the structure of your document as per your collection, and index it by inserting/upserting it to the collection:
58+
59+
```swift
60+
struct School: Codable {
61+
var id: String
62+
var school_name: String
63+
var num_students: Int
64+
var country: String
65+
}
66+
67+
let document = School(id: "7", school_name: "Hogwarts", num_students: 600, country: "United Kingdom")
68+
let documentData = try JSONEncoder().encode(document)
69+
let (data, response) = try await client.collection(name: "schools").documents().create(document: documentData)
70+
//or
71+
let (data, response) = try await client.collection(name: "schools").documents().upsert(document: documentData)
72+
```
73+
You can perform CRUD actions to `Collections` and `Documents` that belong to a certain collection. You can also use `.importBatch()` on the `documents()` method to import and index a batch of documents (in .jsonl format).
74+
75+
### Searching
76+
77+
Define your [search parameters](https://typesense.org/docs/0.22.1/api/documents.html#arguments) clearly and then perform the search operation by mentioning your Document Type:
78+
79+
```swift
80+
let searchParameters = SearchParameters(q: "hog", queryBy: "school_name", filterBy: "num_students:>500", sortBy: "num_students:desc")
81+
82+
let (data, response) = try await client.collection(name: "schools").documents().search(searchParameters, for: School.self)
83+
```
84+
This returns a `SearchResult` object as the data, which can be further parsed as desired.
85+
86+
## Contributing
87+
88+
Issues and pull requests are welcome on GitHub at [Typesense Swift](https://github.com/typesense/typesense-swift). Do note that the Models used in the Swift client are generated by [Swagger-Codegen](https://github.com/swagger-api/swagger-codegen) and are automated to be modified in order to prevent major errors. So please do use the shell script that is provided in the repo to generate the models:
89+
90+
```shell
91+
sh get-models.sh
92+
```
93+
94+
The generated Models (inside the Models directory) are to be used inside the Models directory of the source code as well. Models need to be generated as and when the [Typesense-Api-Spec](https://github.com/typesense/typesense-api-spec) is updated.

0 commit comments

Comments
 (0)