querykit-cli

0.2.0

QueryKit command line tool to produce type-safe accessors
QueryKit/querykit-cli

What's New

2015-09-16T22:25:08Z

QueryKit Logo

QueryKit CLI

A command line tool to generate extensions for your Core Data models including QueryKit attributes for type-safe Core Data querying. Allowing you to use Xcode generated managed object classes with QueryKit.

Installation

Installation can be done via Homebrew as follows:

$ brew install querykit/formulae/querykit-cli

Alternatively, you can build it yourself with Xcode 7.

$ git clone https://github.com/QueryKit/querykit-cli
$ cd querykit-cli
$ make
$ make install

Usage

You can run QueryKit against your model providing the directory to save the extensions.

$ querykit <modelfile> <directory-to-output>

Example

We've provided an example application using QueryKit's CLI tool.

$ querykit Todo/Model.xcdatamodeld Todo/Model
-> Generated 'Task' 'Todo/Model/Task+QueryKit.swift'
-> Generated 'User' 'Todo/Model/User+QueryKit.swift'

QueryKit CLI will generate a QueryKit extension for each of your managed object subclasses that you can add to Xcode, in this case Task and User.

These extensions provide you with properties on your model for type-safe filtering and ordering.

Task+QueryKit.swift

import QueryKit

extension Task {
  static var createdAt:Attribute<NSDate> { return Attribute("createdAt") }
  static var creator:Attribute<User> { return Attribute("creator") }
  static var name:Attribute<String> { return Attribute("name") }
  static var complete:Attribute<Bool> { return Attribute("complete") }
}

extension Attribute where AttributeType: Task {
  var creator:Attribute<User> { return attribute(AttributeType.creator) }
  var createdAt:Attribute<NSDate> { return attribute(AttributeType.createdAt) }
  var name:Attribute<String> { return attribute(AttributeType.name) }
  var complete:Attribute<Bool> { return attribute(AttributeType.complete) }
}

User+QueryKit.swift

import QueryKit

extension User {
  static var name:Attribute<String> { return Attribute("name") }
}

extension Attribute where AttributeType: User {
  var name:Attribute<String> { return attribute(AttributeType.name) }
}

We can use these properties in conjunction with QueryKit to build type-safe queries. For example, here we are querying for all the Tasks which are created by a user with the name Kyle which are completed, ordered by their created date.

Task.queryset(context)
  .filter { $0.user.name == "Kyle" }
  .exclude { $0.completed == true }
  .orderBy { $0.createdAt.ascending }

Description

  • Swift Tools
View More Packages from this Author

Dependencies

  • None
Last updated: Sun Oct 20 2024 08:15:34 GMT-0900 (Hawaii-Aleutian Daylight Time)