NAMDatabase
About
NAMDatabase is a very simple way to use SQLite. Forget parsers, selects, updates! Just .saveData()
or .delete()
for example.
The Core of solution is the NAMObjectModel class. It abstract all sql commands.
When you initialize the library for the first time, NAMDatabase creates a .sqlite database and all the tables based in classes that implement NAMObjectModel.
How it works
First, create a model:
If you need to persist a property, add @objc
.
import NAMDatabase
class Person: NAMObjectModel {
@objc var name: String!
}
Initialize
This code will create the Database and all Tables.
In your AppDelegate add:
NAMDatabase.shared()?.initialize(true)
Send true to log the Database Path.
If you need recreate the tables run:
NAMDatabase.shared()?.createAllTables()
This code will drop and recreate the database, be carrefully.
Saving Data:
let person = Person()
person.name = "Narlei"
person.identifier = "MY_ID"
person.saveData()
If a register with id == "MY_ID" already exists, will be updated.
To get a unique ID you can use:
let person = Person()
let id = person.getUniqueKey()
person.identifier = id
person.saveData()
Retrieving Data:
// Get by Identifier:
if let personResult = Person.getObjectWithId("MY_ID") as? Person {
print(personResult.name)
}
// Get with SQLite where:
if let array = Person.getAllDataWhere("name like 'Nar%'") as? [Person] {
print(array)
}
Delete Data
Delete by Identifier:
Person.deleteObject(withId: "MY_ID")
Delete with SQLite where:
Person.deleteAllDataWhere("name like 'Nar%'")
Ignored Properties
In Swift classes, if you want to ignore properties, just not add @objc
or you can use:
override class func ignoredProperties() -> [Any]! {
return ["name"]
}
Primary Key
A default property identifier
is the primary key, you can change it using:
override class func primaryKeys() -> [Any]! {
return ["id"]
}
Installation
NAMDatabase is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'NAMDatabase'
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
See the examples here
TODO
- Create template to create Models;
- Database migration Helper;
Author
narlei, [email protected]
License
NAMDatabase is available under the MIT license. See the LICENSE file for more info.