What is LIBK
What is LIBK
LIBK is GMOD Lua Library for fast and straightforward development of database-backed addons. Support MySQL and SQLite with the same code.
Database
LibK gives you a few options for working with databases:
No more manual query writing or SQL injection through abstraction models with support for SQLite and MySQL.
The ability to establish a database connection very quickly.
Every query is guaranteed to be executed, and there will be automatic reconnection if the database connection is interrupted.
Working with Network
Clients can initiate server transactions that are wrapped as a promise
Objects (i.e. class instances) can be sent from server to client
Addon Loader
Loading files by filename is accessible.
Supports file ordering
No need to manually include files anymore
Load an addon after game mode initialisation
Wait until another addon has finished loading
Basic example
This example shows a simple addon that will save player joins to the database. In any shared file:
-- Initialize the Addon
LibK.InitializeAddon{
addonName = "MyAddon", --Name of the addon
author = "Kamshak", --Name of the author
luaroot = "myaddon", --Folder that contains the client/shared/server structure
}
MyAddon = {}
LibK.SetupDatabase( "MyAddon", MyAddon )
-- Create a Database Model
MyAddon.PlayerJoins = class( "PlayerJoins" )
MyAddon.PlayerJoins.static.DB = "MyAddon"
MyAddon.PlayerJoins.static.model = {
tableName = "ps2_plyjoinstreak",
fields = {
playerId = "int",
joinedTime = "createdTime" --automatically set to time this entry was created
}
}
MyAddon.PlayerJoins:include( DatabaseModel )
-- Use It
hook.Add( "LibK_PlayerInitialSpawn", "Save Player Join", function( ply )
local join = Pointshop2.PlayerJoins:new( )
join.playerId = ply.kPlayerId
join:save()
end )
Addon Structure
LibK suggests a simple addon structure that separates code into shared, server and client folders.
Some features can only be used with this structure; however, it is optional.
Setting up an addon
You can read how to set up the addon on this page: https://github.com/Kamshak/LibK
References
Official plugin page: https://github.com/Kamshak/LibK
Middleclass: https://github.com/kikito/middleclass