Objective DB
JSON based Database.
Hecho en 🇵🇷 por Radamés J. Valentín Reyes
Status
Experimental/Under Development
Importing
import 'package:objective_db/objective_db.dart';
About the project
- The program will throw errors every time an error is encountered or a forbidden operation is attempted(such as editing the uuid).
- uuid's in this project are composed of 40 alphanumeric characters.
- All data is stored as .json and returned as Map<String,dynamic>
- When insertin Map<String,dynamic> a new file will be created and a reference to it will be stored in the parent .json object.
Functions, classes and methods
Entry
Entry class is the root of the file structure.
Entry entry = Entry(dbPath: "./database");
Select method
returns a DbObject which contains many more useful methods like insert and delete.
Entry entry = Entry(dbPath: "./database");
DbObject dbObject = entry.select();
Insert method
If the value is a double, int or String the insert method will add the key to the selected Map, if the key already exists the value will be replaced. If you you try to insert a Map a new file will be created and a reference to the file will be generated. If the value you try to replace has a Map assigned to it you will get an error. If you try to insert a List<Map<String,dynamic>> new files and references will be generated.
Entry entry = Entry(dbPath: "./database");
entry.select().insert(
key: "Stores",
value: [
{
"Name": "Frappé inc",
"balance": 18.59,
},
{
"Name": "Rica Pizza",
"balance": 19000,
},
],
);
View method
Returns a Map<String,dynamic> with parsed .json contents. You will see that the only property that stored Map<String,dynamic> contains is "uuid". The reason is that this are references not the actual Object.
Entry entry = Entry(dbPath: "./database");
List<DbObject> objects = entry.select().selectMultiple(
key: "Stores",
);
for(DbObject object in objects){
print(object.view());
}
Delete method
If your delete target is an object reference(Map<String,dynamic> with uuid) deletes the object and all linked/referenced objects(children).
Entry entry = Entry(dbPath: "./database");
List<DbObject> objects = entry.select().selectMultiple(
key: "Stores",
);
DbObject pizzaPlace = objects.firstWhere((element)=> (element.view()["Name"] as String).toLowerCase().contains("pizza"));
entry.select().delete(
key: "Stores",
uuid: pizzaPlace.uuid,
);
Delete key method
If your target is a String, double or int, the property gets removed from the target object.
Entry entry = Entry(dbPath: "./database");
entry.select().deleteKey(key: "string");
Pop method
If the key value is of type List it will remove the element at the specified index.
Entry entry = Entry(dbPath: "./database");
entry.select().pop(
index: 1,
key: "numbers",
);
Insert at method
If the key value is a List the specified value will be inserted at the specified index.
Entry entry = Entry(dbPath: "./database");
entry.select().insertAt(
index: 1,
key: "numbers",
value: [
78,
],
);
Move item method
Moves item on the list
Entry entry = Entry(dbPath: "./database");
entry.select().move(
key: "numbers",
from: 3,
to: 2,
);
Swap item method
Swaps the position of list items
Entry entry = Entry(dbPath: "./database");
entry.select().swap(
key: "numbers",
from: 0,
to: 1,
);
Encryption
You can now Encrypt, Decrypt and change the password of the databases. Note: By assigning encryption keys to the Objects you will automatically encrypt files. Encryption is off by default. The longer the password the more secure but the more time it takes to compute. It is possible to generate extremely lange numbers that the computer cannot compute. RSA encryption is beign used in the background.
Additional import
import 'package:einfache_krypto/einfache_krypto.dart';
Encrypt database
Entry entry = Entry(
dbPath: "./database",
cipherKeys: CipherGen(seed: 7863),
);
entry.encryptEntireDatabase();
Decrypt database
Entry entry = Entry(
dbPath: "./database",
cipherKeys: CipherGen(seed: 7863),
);
entry.decryptEntireDatabase();
Change database password
CipherGen newPassword = CipherGen(seed: 1034);
Entry entry = Entry(
dbPath: "./database",
cipherKeys: newPassword,
);
entry.changeDatabasePassword(
oldPassword: CipherGen(
seed: 7863,
),
);