Skip to content

Commit

Permalink
Don't mention the Swift Snippet's file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Joannis committed Dec 23, 2024
1 parent f68c8b2 commit 9a8a001
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ let package = Package(

Let's create our first connection:

@Snippet(path: "site/Snippets/mongokitten-basics.swift", slice: "connection")
@Snippet(path: "site/Snippets/mongokitten-basics", slice: "connection")

### Defining Our Models

For our social network, we'll create a Post model using the ``Codable`` protocol:

@Snippet(path: "site/Snippets/mongokitten-models.swift", slice: "models")
@Snippet(path: "site/Snippets/mongokitten-models", slice: "models")

The ``ObjectId`` type is MongoDB's native unique identifier, similar to a ``UUID``. Every document in MongoDB has an `_id` field, which must be unique within a collection.

### Creating Posts

Let's create a function to save posts:

@Snippet(path: "site/Snippets/mongokitten-basics.swift", slice: "insert")
@Snippet(path: "site/Snippets/mongokitten-basics", slice: "insert")

1. Create the post as a regular Swift struct
2. Access the posts ``MongoCollection``, this is similar to a table in a relational database
Expand All @@ -66,7 +66,7 @@ In BSON, all entities are stored as '**documents**'.

To read posts, we can use MongoKitten's query API:

@Snippet(path: "site/Snippets/mongokitten-basics.swift", slice: "find-all")
@Snippet(path: "site/Snippets/mongokitten-basics", slice: "find-all")

All the methods are chainable, and modify the query. MongoKitten will only execute the query when you call ``MongoCursor.drain``, or iterate over the results of any query.

Expand All @@ -80,13 +80,13 @@ The drain function will execute the query and return the results as an array of

MongoKitten supports filtering and sorting on most queries.

@Snippet(path: "site/Snippets/mongokitten-basics.swift", slice: "find-by-author")
@Snippet(path: "site/Snippets/mongokitten-basics", slice: "find-by-author")

The ``MongoCollection.find(_:) [7HX5V]`` method returns a ``FindQueryBuilder``, a type of ``MongoCursor`` that allows you to chain more methods.

### Sorting and Limiting

@Snippet(path: "site/Snippets/mongokitten-basics.swift", slice: "find-recent")
@Snippet(path: "site/Snippets/mongokitten-basics", slice: "find-recent")

The **find** method accepts one argument, a filter. By providing the find filter, MongoDB will only return documents that match the filter.

Expand All @@ -99,7 +99,7 @@ Then, chain the following methods:

MongoDB, and by extension MongoKitten, uses ``BSON`` (Binary JSON) as its native data format. While MongoKitten handles most BSON conversions automatically through Codable, you can also work with BSON directly:

@Snippet(path: "site/Snippets/mongokitten-basics.swift", slice: "bson")
@Snippet(path: "site/Snippets/mongokitten-basics", slice: "bson")

## Next Steps

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Make sure you have MongoDB running locally before starting.

The ``ConnectionManager`` handles WebSocket connections and MongoDB change notifications:

@Snippet(path: "site/Snippets/realtime-mongodb-app.swift", slice: "connection-manager")
@Snippet(path: "site/Snippets/realtime-mongodb-app", slice: "connection-manager")

1. The manager is an actor to ensure thread-safe access to connections
2. It maintains a dictionary of active WebSocket connections
Expand All @@ -38,7 +38,7 @@ The use of `withRegisteredClient` ensures that the WebSocket connection is prope

Now that the ``ConnectionManager`` is implemented, we can watch for changes in the MongoDB database. For this, we'll tie the ``ConnectionManager`` to the application lifecycle using the ``Service`` protocol.

@Snippet(path: "site/Snippets/realtime-mongodb-app.swift", slice: "watch-changes")
@Snippet(path: "site/Snippets/realtime-mongodb-app", slice: "watch-changes")

1. Get a reference to the posts collection
2. Create a change stream watching for post changes
Expand All @@ -53,7 +53,7 @@ This flow is very scalable, as only one ChangeStream is created and maintained p

Let's create the main application entry point:

@Snippet(path: "site/Snippets/realtime-mongodb-app.swift", slice: "main")
@Snippet(path: "site/Snippets/realtime-mongodb-app", slice: "main")

1. Connect to MongoDB
2. Create the connection manager
Expand All @@ -64,7 +64,7 @@ Let's create the main application entry point:

### Adding Routes

@Snippet(path: "site/Snippets/realtime-mongodb-app.swift", slice: "routes")
@Snippet(path: "site/Snippets/realtime-mongodb-app", slice: "routes")

This snippet adds a POST route to the application that creates a new post in the database. That process then triggers the change streams, which broadcast to all connected clients.

Expand Down

0 comments on commit 9a8a001

Please sign in to comment.