Swiftpack.co - Package - TofPlay/TStorage

TStorage

Xcode Swift Version SwiftPM Carthage Plaform

WARNING: Since Swift 4.1 Apple changed how they managed metadata. Since Swift 4.1 TProperties generate a crash at runtime

Features

  • [x] SQL support
  • [x] easy declaration
  • [x] generate tables automatically
  • [x] support optional
  • [x] command line compatible (with SwiftPM)
  • [x] migration available

Requirements

  • macOS 10.12+
  • iOS 10.3+
  • watchOS 10.3+
  • tvOS 10.2+
  • Swift 4.0
  • Xcode 9.0+

Installation

SwiftPM

TStorage is available for installation using the Swift Package Manager. Add the following to your Package.swift:

// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
  name: "YourApp",
  dependencies: [
    // Dependencies declare other packages that this package depends on.
    .package(url: "https://github.com/TofPlay/TStorage.git", .branch("master")),
    ],
  targets: [
    // Targets are the basic building blocks of a package. A target can define a module or a test suite.
    // Targets can depend on other targets in this package, and on products in packages which this package depends on.
    .target(
      name: "YourApp",
      dependencies: ["TStorage"]),
    ]
)

Carthage

Create a Cartfile

github "TofPlay/TStorage"

Run carthage update

Follow the instructions to add

  • For iOS
    • $(SRCROOT)/Carthage/Build/iOS/TProperties.framework
    • $(SRCROOT)/Carthage/Build/iOS/TStorage.framework
  • For macOS
    • $(SRCROOT)/Carthage/Build/Mac/TProperties.framework
    • $(SRCROOT)/Carthage/Build/Mac/TStorage.framework
  • For tvOS
    • $(SRCROOT)/Carthage/Build/tvOS/TProperties.framework
    • $(SRCROOT)/Carthage/Build/tvOS/TStorage.framework
  • For watchOS
    • $(SRCROOT)/Carthage/Build/watchOS/TProperties.framework
    • $(SRCROOT)/Carthage/Build/watchOS/TStorage.framework

Usage example

Default folder

Default path of the sqlite database for

  • macOS (Cocoa App): /Users/<User>/Library/Containers/<Bundle identifier>/Data/Library/Application Support/<App name>/<App name>.db
  • macOS (Command Line): /Users/<User>/Library/Application Support/<App name>/<App name>.db
  • iOS: /Application/<UUID of your app on the device>/Library/Application Support/<App name>/<App name>.db
  • tvOS: /Application/<UUID of your app on the device>/Library/Application Support/<App name>/<App name>.db
  • tvOS: /PluginKitPlugin/<UUID of your plugin on the device>/Library/Application Support/<Plugin name>/<Plugin name>.db

Declaration

import TStorage

public class Data : TStorage {
  public var varBool:Bool = false
  public var varInt:Int = 0
  public var varInt64:Int64 = 0
  public var varFloat:Float = 0.0
  public var varDouble:Double = 0.0
  public var varDate:Date = Date()
  public var varTimeInterval:TimeInterval = 0
  public var varString:String = ""

  public var varOptBool:Bool? = nil
  public var varOptInt:Int? = nil
  public var varOptInt64:Int64? = nil
  public var varOptFloat:Float? = nil
  public var varOptDouble:Double? = nil
  public var varOptDate:Date? =  nil
  public var varOptTimeInterval:TimeInterval? = nil
  public var varOptString:String? =  nil
}

Generate data

func genData() -> [Data] {
  var lRet:[Data] = []
  var lValBool = false
  
  for lI in 1...10 {
    let lData = Data()
    lData.varBool = lValBool
    lData.varInt = Int(lI)
    lData.varInt64 = Int64(1_234_567 * lI)
    lData.varFloat = Float.pi * Float(lI)
    lData.varDouble = Double.pi * Double(lI)
    lData.varDate = Date(timeIntervalSinceNow: TimeInterval(lI * 10))
    lData.varTimeInterval = Date().timeIntervalSinceNow + TimeInterval(lI * 2)
    lData.varString = "String \(lI)"
    
    lValBool = !lValBool
    lRet.append(lData)
  }
  
  return lRet
}

Data exist

Data.exist()

Count data

Data.count()

Count with condition

Data.count(where: "varInt > 2 AND varInt < 6")

Insert datas

Data.insert(genData())

Remove all datas

Data.remove()

Remove with condition

Data.remove(where: "varInt > 2 AND varInt < 6")

Select all datas

let lRows:[Data] = Data.select()

Select with condition

let lRows:[Data] = Data.select(where: "varInt > 3 AND varInt < 8")

Select with offset and limit

let lRows:[Data] = Data.select(offset: 3, limit: 4)

Migration


  // New Data
  class Data : TStorage {
    public var valueBool:Bool = false
    public var valueInt:Int = 0
    public var valueInt64:Int64 = 0
    public var valueFloat:Float = 0.0
    public var valueDouble:Double = 0.0
    public var valueDate:Date = Date()
    public var valueTimeInterval:TimeInterval = 0
    public var valueString:String = ""
  }

  let lDate = Date()
  
  // Migrate previous Data to the new Data
  Data.migration {
    pRow -> Data? in
    
    let lObj = Data()
    
    lObj.valueBool = pRow.cast(name: "varBool") ?? false
    lObj.valueInt = pRow.cast(name: "varInt") ?? 0
    lObj.valueInt64 = pRow.cast(name: "varInt64") ?? 0
    lObj.valueFloat = pRow.cast(name: "varFloat") ?? 0.0
    lObj.valueDouble = pRow.cast(name: "varDouble") ?? 0.0
    lObj.valueDate = pRow.cast(name: "varDate") ?? lDate
    lObj.valueTimeInterval = pRow.cast(name: "varTimeInterval") ?? 0
    lObj.valueString = pRow.cast(name: "varString") ?? ""

    return lObj
  }

Created with XcodeTool

Component generated with XcodeTool and the template ComponentCrossPlatform

Base on Swift Cross Platform Framework tutorial

Github

link
Stars: 2
Help us keep the lights on

Used By

Total: