Package -


CircleCI platform SPM swift

Squirrel ORM connector for mongodb


Mongodb 3.4


import SquirrelConnector

Set connector

Connector.setConnector(url: String)
Connector.setConnector(host: String, port: Int = 27017, dbname: String = "squirrel")
// or
Connector.setConnector(username: String, password: String,
                       host: String, port: Int = 27017, dbname: String = "squirrel") 

Model interface

extension Model {
    static func find<T>(_ filter: Query? = nil, sortedBy sort: Sort? = nil, 
        collation: Collation? = nil, skipping skip: Int? = nil, limitedTo limit: Int? = nil)
        throws -> [T] where T: Presentable
    static func findOne<T>(_ filter: Query? = nil, sortedBy sort: Sort? = nil,
          collation: Collation? = nil, skipping skip: Int? = nil) 
          throws -> T? where T: Presentable
    static var collection: MongoKitten.Collection { get }
    static func create() throws
    static func drop() throws
    var json: String { get }
    mutating func save() throws
    mutating func remove() throws

extension Array where Element : Model {
    var json: String { get }
    mutating func saveAllDocuments() throws
    mutating func removeAllDocuments() throws


We want to store post with comments. Our post have to conform to Model protocol and all structures used in Post have to conform to Codable

struct Comment: Codable {
    let user: ObjectId
    let comment: String

struct Post: Model {
    init(title: String, body: String) {
        self.title = title
        self.body = body

    var id: ObjectId? = nil
    var title: String
    var body: String
    var comments: [Comment] = []


Presentable struct can be used as expected projection as result of find method

public protocol Presentable {

Model examples

// set connector
Connector.setConnector(host: "localhost", dbname: "exampledb")

var post = Post(title: "Dogs", body: "Dogs are not cats!")
post.comments.append(Comment(user: try! ObjectId("59984722610934e182846e7b"), comment: "blah"))

try Post.drop() // remove all posts from database
try // save Dogs post to database

let postsFromDatabase = try Post.find("title" == "Dogs") // returns [Post] where title is "Dogs"

struct Title: Presentable {
    var title: String

let titlesFromDatabase: [Title] = try Post.find() // returns only titles


Squirrel Connector uses caches to store Projection of Presentable object

public struct SquirrelConnectorCache {

    /// Default name for cache
    public static let defaultName = "ProjectionCache"

    /// Set projection cache manager
    /// - Parameters:
    ///   - name: name of cache folder
    ///   - config: Configuration
    public static func setProjectionCache(name: String = defaultName, config: Config) {

    /// Total disk size
    public static var totalDiskSize: UInt64 { get }

    /// Name of cache
    public static var name: String { get }

    /// Path of cache directory
    public static var path: String { get }

    /// Clears the front and back cache storages.
    /// - Parameter keepRoot: Pass `true` to keep the existing disk cache directory
    /// after removing its contents. The default value is `false`.
    public static func clear(keepingRootDirectory keepRoot: Bool = false)

    /// Clears all expired objects from front and back storages.
    public static func clearExpired()

See Also

  • MongoKitten Native MongoDB driver for Swift, written in Swift
  • Cache 📦 Nothing but Cache.


swift 4

.package(url: "", from: "0.1.9"),