Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StORM Optionals Support, Subclassing Support & more! #6

Open
wants to merge 54 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
7fbe972
Working on subclassing the postgres-storm object
ryancoyne Nov 23, 2017
6b468a6
Making the mirroring support optionals & if the value is nil, it does…
ryancoyne Nov 23, 2017
11d32ea
Updating last thing for setting up tables to Postgres-StORM
ryancoyne Nov 23, 2017
8616a81
Fixing an issue with the mirroring
ryancoyne Nov 23, 2017
65550d3
Adding in some log prints
ryancoyne Nov 23, 2017
6cb4d45
Fixing a dumb issue
ryancoyne Nov 23, 2017
4c73b20
Fixing last issues with mirroring & getting the correct superclasses
ryancoyne Nov 23, 2017
ab82626
Last few changes
ryancoyne Nov 23, 2017
1028767
Cleaning up the storm module for what I need in the postgresStorm module
ryancoyne Nov 24, 2017
4fda089
Updating the cols function
ryancoyne Nov 24, 2017
ad1f579
Making it so StORM has the backwards capability for this update
ryancoyne Nov 24, 2017
2c24931
Working on backwards compatability for when we mirror
ryancoyne Nov 24, 2017
42dd9f0
Last changes for backwards compatability
ryancoyne Nov 24, 2017
deda7d1
Last changes for backwards compatability
ryancoyne Nov 24, 2017
1d40673
Working on last update to storm for specifying in an override functio…
ryancoyne Nov 24, 2017
49e74f0
Renaming the open function for overriding the primaryKey to primaryKe…
ryancoyne Nov 24, 2017
90f85f3
Oopsie - fixing the upate to the primaryKeyLabel
ryancoyne Nov 24, 2017
05eb48e
Fixing the issue with multiple primary keys in the setup statement
ryancoyne Nov 24, 2017
62ec4d7
Fixing last issue for saving/updating
ryancoyne Nov 24, 2017
fa8eda5
Updating StORM last time after making few minor changes after testing…
ryancoyne Nov 25, 2017
ed3f71b
Making it so created & modified dates for integers is automatically s…
ryancoyne Nov 26, 2017
93d6e57
Making it so StORM fills the integer value for the date for created &…
ryancoyne Nov 26, 2017
7d8994b
Last change for defaulting values for created/modified integer date
ryancoyne Nov 27, 2017
32b3fa9
Last change to cols function to return columns even when they are nil
ryancoyne Nov 27, 2017
7c3313a
Reworking using foundation to get the current date interval since 197…
ryancoyne Nov 30, 2017
d81a777
Updating extension
ryancoyne Nov 30, 2017
715bdbd
Adding comments to some of the CCX extensions & mirroring protocol ob…
ryancoyne Nov 30, 2017
ef598db
Update README.md
ryancoyne Nov 30, 2017
b3a1bf8
Renaming the StORM extensions/mirroring protocols
ryancoyne Dec 1, 2017
d3b5286
Last comment change
ryancoyne Dec 1, 2017
cb25dd7
Merge branch 'master' of https://github.com/ryancoyne/StORM
ryancoyne Dec 1, 2017
7f42d55
Updating naming conventions of the extensions/mirroring protocol
ryancoyne Dec 1, 2017
13b5b78
Update README.md
ryancoyne Dec 1, 2017
8aa8911
Update README.md
ryancoyne Dec 1, 2017
e2ff7e0
Update README.md
ryancoyne Dec 1, 2017
bca0e2d
Getting rid of the primaryKeyLabel variable that was static to storm …
ryancoyne Dec 1, 2017
aaf2e00
Updating the changes to my storm module
buckettech Dec 14, 2018
e89694e
Updating a statement to return the string value for conforming to Cus…
buckettech Dec 16, 2018
b1624e3
Working on fixing this issue
buckettech Dec 16, 2018
5aa05cf
CHanging up something in storm
buckettech Dec 16, 2018
ec64bbc
Taking out the print statement when running through all the children
buckettech Dec 21, 2018
afabb1c
Updating the package.swift to swift 4 SPM
buckettech Dec 23, 2018
6172f4b
Updating the StORM Package.swift file to SPM version 4
buckettech Dec 23, 2018
3e46381
Updating the package file for storm to specify the file path
buckettech Dec 23, 2018
f748a72
Updating package file
buckettech Dec 23, 2018
6fd469d
Updating package file
buckettech Dec 23, 2018
f92222e
Update Package.swift
ryancoyne Dec 23, 2018
3b67e22
Updatingg the package file
buckettech Dec 23, 2018
9ca347a
Adding in the path for StORM
buckettech Dec 23, 2018
9bfdc12
Adding in the path for StORM and adding in the comma
buckettech Dec 23, 2018
42d6efe
Updating the StORM package to use my packages that use the updated pa…
buckettech Dec 23, 2018
ab848b0
Updating the package file to make it line up with everything
buckettech Dec 23, 2018
5c41d23
Updating the package to swift 4.2 and removed out SwiftString
buckettech Dec 30, 2018
d85e125
Updating the StORM package so we dont have SPM conflicts
buckettech Jan 5, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
// Generated automatically by Perfect Assistant Application
// Date: 2018-03-02 16:12:45 +0000
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "StORM",
targets: [],
dependencies: [
.Package(url: "https://github.com/PerfectlySoft/PerfectLib.git", majorVersion: 3),
.Package(url: "https://github.com/iamjono/SwiftMoment.git", majorVersion: 1),
.Package(url: "https://github.com/iamjono/SwiftString.git", majorVersion: 2),
]
name: "StORM",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "StORM",
targets: ["StORM"]),
],
dependencies: [
.package(url: "https://github.com/ryancoyne/PerfectLib.git", from: "4.0.0"),
.package(url: "https://github.com/ryancoyne/SwiftMoment.git", from: "1.0.0"),
// .package(url: "https://github.com/ryancoyne/SwiftString.git", from: "2.0.0"),
],
targets: [
.target(
name: "StORM",
dependencies: ["PerfectLib", "SwiftMoment"],
path: "Sources/StORM"
),
]
)
113 changes: 113 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,116 @@ StORM is a modular ORM for Swift, layered on top of Perfect.
It aims to be easy to use, but flexible, and maintain consistency between datasource implementations for the user: you, the developer. It tries to allow you to write great code without worrying about the details of how to interact with the database.

Please see the full documentation at: [https://www.perfect.org/docs/StORM.html](https://www.perfect.org/docs/StORM.html)

# Latest Updates:

- Increased flexibility with determining the primary key, without having to have it your first declared variable in your model.
- Includes subclassing support.
- Includes optional variable support.

## Usage:
### Example -> Conforming to StORMirroring:
Say we have to models using PostgresStORM:

```
class AuditFields: PostgresStORM {

/// This is when the table row has been created.
var created : Int? = nil
/// This is the id of the user that has created the row.
var createdby : String? = nil
/// This is when the table row has been modified.
var modified : Int? = nil
/// This is the id of the user that has modified the row.
var modifiedby : String? = nil

// This is needed when created a subclass containing other fields to re-use for other models.
override init() {
super.init()
self.didInitializeSuperclass()
}
}

// The outer most class does not need to override init & call didInitializeSuperclass. This helps with identifying the id in the model.
class TestUser2: AuditFields {
// In this example we have id at the top but that is not mandatory now if you implement the primaryKeyLabel overrided function.
var id : Int? = nil
var firstname : String? = nil {
didSet {
if oldValue != nil && firstname == nil {
self.nullColumns.insert("firstname")
} else if firstname != nil {
self.nullColumns.remove("firstname")
}
}
}
var lastname : String? = nil {
didSet {
if oldValue != nil && lastname == nil {
self.nullColumns.insert("lastname")
} else if firstname != nil {
self.nullColumns.remove("lastname")
}
}
}
var phonenumber : String? = nil {
didSet {
if oldValue != nil && phonenumber == nil {
self.nullColumns.insert("phonenumber")
} else if firstname != nil {
self.nullColumns.remove("phonenumber")
}
}
}

override open func table() -> String {
return "testuser2"
}

override func to(_ this: StORMRow) {

// Audit fields:
id = this.data["id"] as? Int
created = this.data["created"] as? Int
createdby = this.data["createdby"] as? String
modified = this.data["modified"] as? Int
modifiedby = this.data["modifiedby"] as? String

firstname = this.data["firstname"] as? String
lastname = this.data["lastname"] as? String
phonenumber = this.data["phonenumber"] as? String

}

func rows() -> [TestUser2] {
var rows = [TestUser2]()
for i in 0..<self.results.rows.count {
let row = TestUser2()
row.to(self.results.rows[i])
rows.append(row)
}
return rows
}

}
```

Notice that you only need to call the didInitializeSuperclass() function in the superclass of TestUser2. You should NOT call this in your main model, in this case TestUser2.

### Example -> Getting all the children:
```
// Getting all the children (including nil values or NOT)
// Returns an array of children, making sure the overrided function primaryKeyLabel takes the child for that key and places it in the first index of the array:
self.allChildren(includingNilValues: true, primaryKey: self.primaryKeyLabel())
```

This provides the capability for PostgresStORM and other database supported StORM modules to be given the ability to subclass PostgresStORM objects deeper than just the single mirror of the class.

StORMMirroring also gives you the flexibility to place your primary key in a superclass model. All you would need to do is override the primaryKeyLabel function that StORMMirroring offers. This ensures that when we create the array of children from all the mirrors, it places the primary key as the first child in the array.

### Supporting Optionals in other database-StORM Modules:
- Make sure to keep track of going from an optional Non-nil value to nil, to update NULL/DEFAULT or NULL. See TestUser2 model.

### Other -> Automatic Auditing For other database-StORM Modules:
- StORMMirroring also supports automatic created & modified values to come back if they exist in the model. They are intended to be used as integers to store as epoch timestamps. Make sure to skip over modified or created depending on if you are creating a new record or modifying a record.

5 changes: 2 additions & 3 deletions Sources/StORM/Extract.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import Foundation
import SwiftMoment
import SwiftString

extension StORM {

Expand Down Expand Up @@ -78,14 +77,14 @@ extension StORM {
// Array Of Strings
// =======================================================================================
public static func arrayOfStrings(_ data: [String: Any], _ name: String, _ def: [String]? = [String]()) -> [String]? {
return (data[name] as? String ?? "").split(",").map{ $0.trimmed() } // note default ignored right now
return (data[name] as? String ?? "").split(separator: ",").map{ $0.trimmingCharacters(in: .whitespacesAndNewlines) } // note default ignored right now
}

// =======================================================================================
// Array Of Integers
// =======================================================================================
public static func arrayOfIntegers(_ data: [String: Any], _ name: String, _ def: [Int]? = [Int]()) -> [Int]? {
return (data[name] as? String ?? "").split(",").map{ Int($0.trimmed()) ?? 0 } // note default ignored right now
return (data[name] as? String ?? "").split(separator: ",").map{ Int($0.trimmingCharacters(in: .whitespacesAndNewlines)) ?? 0 } // note default ignored right now
}

// =======================================================================================
Expand Down
Loading