Model

From Onix OS Wiki
Jump to: navigation, search

The models automatically load connection-dependent functions according to the connected database. They then prepare the model for use in the model. The model must be returned as a definition. Otherwise, the operation will be incorrect.

Make connection[edit | edit source]

First we need to define database connection;

def db = (database("dbtype","dbstring"))

Database type and Database strings are connection types. For example you need sqlite3 connection;

Example[edit | edit source]

def db = (database("sqlite3","sqlite_database_example.db"))


Define the connection model[edit | edit source]

Now ready to connect models and databases;

def modeldef = (model(db, "modelname", {....table definitions}))

Db is your database connection. Model name and table definitions make table structure. Modeldef is simple model definition.

Example[edit | edit source]

For example we need to users and payments tables;

def user = (model(db, "users", {
	"id": "int"
	"name": "text",
	"surname": "text",
	"username": "text",
	"email": "text",
	"password": "text",
}))
def payment = (model(db, "payments", {
	"user_id": "int",
	"money": "int",
	"currency": "text",
	"datetime": "DATETIME",
	"duedate": "DATETIME"
}))

Drop and Migrate automatically[edit | edit source]

All models ready for use. If you need migration with database;

drop(modeldef) 
#=> if your table exist in database use drop function first before migration.
migrate(modeldef)


Example[edit | edit source]

Modeldef is your definition. We need two models for migrate;

drop(user)
drop(payment)
migrate(user)
migrate(payment)

All the examples mentioned above depend on one. Please perform your tests in order. Otherwise, you may have involuntary consequences.