Skip to content

thisdp/MORM_DB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MORM_DB - MTASA Lua ORM for DataBase

MORM_DB is a Lua ORM (Object Relational Mapping) library developed for MTASA (Multi Theft Auto: San Andreas) to simplify database operations.

Features

  • Object-oriented database operation interface -Support automatic migration of database structure
  • Support transaction processing
  • Support chain calls
  • Supports synchronous and asynchronous queries
  • Rich data type support
  • Complete error handling and data validation

Installation

Copy the classlib.lua file to your MTASA server resource directory and add the corresponding reference in meta.xml.

How to use

1. Define data model

class "account" { 
uid = "uint32", 
username = "char[32]", 
password = "char[256]", 
constructor = function(self, data) 
if type(data) ~= "table" then return end 
for k, v in pairs(data) do 
self[k] = v 
end 
end
}

2. Connect to database

db = morm:Open("sqlite", "test.db")

3. Create table

db:Create(account):Query()

4. Insert data

local newAccount = account{ 
uid = 1, 
username = "testuser", 
password = "testpass"
}
db:Create(newAccount):Query()

5. Query data

-- Query all data
db:Select("*"):From("account"):Query()

--Conditional query
db:Select("*"):From("account"):Where("uid", 1):Query()

-- Sort and limit the number of results
db:Select("*"):From("account"):OrderBy("uid", "DESC"):Limit(10):Query()

6. Update data

local acc = account{ 
uid = 1
}
acc.username = "newusername"
db:Update(acc):Query()

7. Delete data

-- Delete table
db:Delete("account"):Query()

8. Transaction processing

db:BeginTransaction()
-- Perform multiple operations
db:Create(account1):Query()
db:Create(account2):Query()
-- Submit transaction
db:Commit()

API Reference

DataBase class

Constructor

  • Open(dbType, ...): Open database connection

Table operations

  • Create(...): Create a table or insert data
  • AutoMigrate(...): Automatically migrate database structure
  • Delete(tableName): delete table
  • Drop(tableName, columnName): delete table or column
  • Change(tableName, columnName, newDefinition): Modify column name and definition
  • Add(tableName, columnName, columnDefinition): Add column
  • Modify(tableName, columnName, newDefinition): Modify column definition
  • Alert(tableName, columnName, newDefinition): Modify column definition (alias)

Query construction

  • Select(selectColumn): Add SELECT clause
  • From(fromTable): Add FROM clause
  • Where(columnName, value): Add WHERE clause
  • OrderBy(columnName, order): Add ORDER BY clause
  • Limit(limit, offset): add LIMIT clause
  • Table(tableNameOrTemplate): Specify the table name
  • Raw(raw, ...): Add raw SQL statement

Data operations

  • Find(...): Find data
  • Update(...): update data

Execute query

  • Query(timedout, callback): execute query

Transaction processing

  • BeginTransaction(): Start transaction
  • Commit(): commit transaction
  • Rollback(): rollback transaction

Data type support

  • Integer types: int8, int16, int24, int32, int64, uint8, uint16, uint24, uint32, uint64
  • String types: char, varchar, text, mediumtext, longtext
  • Floating point types: float, double, decimal
  • Date and time types: date, time, datetime, timestamp
  • Boolean type: boolean, bool
  • Binary types: blob, mediumblob, longblob

License

MIT License

About

MTASA Lua ORM for DataBase

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages