Database
Nano framework has inbuilt database
module that uses realm.
Storing and reading data from default table:
All the methods like onPress
, onLongPress
, onStart
, onResume
, onPause
and onEnd
in framework has access to moduleParams parameter that has all the available modules, you can use database
module from it like below.
By default Nano framework creates a table called key-value
in realm database. It helps you store key-value data quickly right away.
For example, below is the code to store/update key-value data using setValue
method from database
module.
const buttonPress = {
component: NANO.BUTTON,
value: 'CLICK TO SAVE DATA TO SESSION',
onPress: ({ moduleParams }) => {
moduleParams["database"].setValue("name", "value")
}
};
const screen = {
name: 'HomeScreen',
screen: {
h1: [buttonPress],
}
};
You can read and delete data from database in any screen using getValue
, deleteValue
and deleteAllValues
like below.
const text2 = {
component: NANO.TEXT,
value: "I AM IN ANOTHER SCREEN",
};
const screen = {
name: 'AnotherScreen',
screen: {
h1: [text],
},
onStart: ({moduleParams}) => {
// read single value
console.log(moduleParams["database"].getValue("name"))
// delete single value
moduleParams["database"].deleteValue("name")
// delete All values
moduleParams["database"].deleteAllValues()
}
};
Creating new table, storing, reading data from it:
For creating a new table, you can create it's schema in nano.config.js
file like below and increasing its version to higher number other than 1.
below is the simple table named books
.
const books = {
name: "books",
properties: {
_id: 'string',
name: 'string',
author: 'string',
},
primaryKey: '_id',
}
export const DataBaseConfig = {
schema: [books]
schemaVersion: 2,
encryptionKey: new Int8Array(64) // You can encrypt the realm database file on disk with AES-256 + SHA-2 by supplying a 64-byte encryption key when opening a realm.
};
Here is how you can store/update and data from books table using setData
, updateData
, deleteData
, deleteAllData
and deleteAll
.
const buttonPress = {
component: NANO.BUTTON,
value: 'CLICK TO SAVE DATA TO DATABASE',
onPress: ({ moduleParams }) => {
// adding new data into books table
moduleParams["database"].setData("books", {
"_id" : "1",
"name" : "Nice book by Nano",
"author" : "Mr. Author"
})
// You can update with passing primary key as second parameter
moduleParams["database"].updateData("books","_id","1", {
"_id" : "2",
"name" : "Updated Nice book by Nano",
"author" : "Updated Mr. Author"
})
// You can delete with passing primary key/coloumn name and its value
moduleParams["database"].deleteData("books","_id", "2")
// You can delete all data of a table
moduleParams["database"].deleteAllData("books")
// You can delete all data from all tables
moduleParams["database"].deleteAllTables()
}
};
const screen = {
name: 'HomeScreen',
screen: {
h1: [buttonPress],
}
};
Here is how you can read data from books table using getDataByPrimaryKey
and getData
.
const text2 = {
component: NANO.TEXT,
value: "I AM IN ANOTHER SCREEN",
};
const screen = {
name: 'AnotherScreen',
screen: {
h1: [text],
},
onStart: ({moduleParams}) => {
// read single row
console.log(moduleParams["database"].getDataByPrimaryKey("books", "2"))
// read all data
console.log(moduleParams["database"].getData("books"))
}
};
If you want more access to created realm database, you can always use getRealmInstance
method to get the relam instance and use according to your needs.
const buttonPress = {
component: NANO.BUTTON,
value: 'CLICK TO GET INSTANCE',
onPress: ({ moduleParams }) => {
// get entire realm Instance
const realmInstance = moduleParams["database"].getRealmInstance()
}
};
const screen = {
name: 'HomeScreen',
screen: {
h1: [buttonPress],
}
};