SquirrelConnector

master

Squirrel ORM connector
Swift-Squirrel/Squirrel-Connector

Squirrel-Connector

CircleCI platform SPM swift

Squirrel ORM connector for mongodb

Requirements

Mongodb 3.4

Usage

import SquirrelConnector

Set connector

Connector.setConnector(url: String)
//or
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
    static func distinct<T: Primitive>(on key: String,
                                       filtering query: Query? = nil,
                                       readConcern: ReadConcern? = nil,
                                       collation: Collation? = nil,
                                       resultType: T.Type = T.self) throws -> [T]
}

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

Model

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

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

public protocol Presentable {
    init()
}

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 post.save() // 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

Cache

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()
}

Documentation

You can learn more in documentation

See Also

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

Installation

swift 4

.package(url: "https://github.com/Swift-Squirrel/Squirrel-Connector.git", from: "0.1.9"),

Description

  • Swift Tools 4.1.0
View More Packages from this Author

Dependencies

Last updated: Mon Oct 21 2024 14:12:00 GMT-0900 (Hawaii-Aleutian Daylight Time)