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.
If you need to persist a property, add @objc
.
import NAMDatabase
class Person: NAMObjectModel {
@objc var name: String!
}
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 careful.
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()
// 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 by Identifier:
Person.deleteObject(withId: "MY_ID")
Delete with SQLite where:
Person.deleteAllDataWhere("name like 'Nar%'")
In Swift classes, if you want to ignore properties, just not add @objc
or you can use:
override class func ignoredProperties() -> [Any]! {
return ["name"]
}
A default property identifier
is the primary key, you can change it using:
override class func primaryKeys() -> [Any]! {
return ["id"]
}
NAMDatabase is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'NAMDatabase'
To run the example project, clone the repo, and run pod install
from the Example directory first.
See the examples here
- Create template to create Models;
- Database migration Helper;
narlei, [email protected]
NAMDatabase is available under the MIT license. See the LICENSE file for more info.