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()
}
Squirrel-Connector
Squirrel ORM connector for mongodb
Requirements
Mongodb 3.4
Usage
Set connector
Model interface
Model
We want to store post with comments. Our post have to conform to
Model
protocol and all structures used inPost
have to conform toCodable
Presentable
Presentable struct can be used as expected projection as result of
find
methodModel examples
Cache
Squirrel Connector uses caches to store
Projection
ofPresentable
objectDocumentation
You can learn more in documentation
See Also
Installation
swift 4