Skip to content

ceifa/sqlier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 

Repository files navigation

Are you tired of these lots of heavy database libraries which do a lot of things which you don't need? You came at the right place! This project aims to offer a way of writing a very simple code only one time and be able to switch between multiple database drivers without problems. The most easier and lightweight database abstraction of the market!

Alert: This project will not fill all the edge database cases and does not aim to do so.

Documentation

You can find the full library documentaion here.

Usage

Setup a database

Firstly you will need to setup your databases, sqlier can be initialized by running the initializer function sqlier.Initialize(name, drive, conn) which accepts the following arguments:

  1. STRING name: Identification name for the database (can be anything)
  2. STRING driver: The desired database driver, currently supports:
  3. optional | STRING Connection info: Only needed when using MySqloo driver, passing the database authentication details.
 -- Sqlite
sqlier.Initialize("db1", "sqlite")

 -- MySQL
sqlier.Initialize("db2", "mysqloo", { address = "localhost", port = "3306", database = "GmodServer", user = "root", password = "" })

Setup a model

Now you will have to setup a model, it works like a class:

local User = sqlier.Model({
    Table = "user",
    Columns = {
        Id = {
            Type = sqlier.Type.Integer,
            AutoIncrement = true
        },
        Name = {
            Type = sqlier.Type.String
        },
        Rank = {
            Type = sqlier.Type.String,
            MaxLength = 15
        },
        SteamId64 = {
            Type = sqlier.Type.SteamId64
        },
        CreateTimestamp = {
            Type = sqlier.Type.Timestamp
        }
    },
    Identity = "Id"
})

The columns CreateTimestamp and UpdateTimestamp are hard-coded internally populated automatically.

Instantiate, update or delete a model

local new_user = User({
    Name = ply:Nick(),
    SteamId = ply:SteamId64(),
    Rank = "user"
})
new_user:save()

new_user.Rank = "donator"
new_user:save()

new_user:delete()

If you want to know when a save or delete is done, you can pass a callback or use their async api, if using util.Promise:

new_user:save(function()
    print("Save is done")
end)

util.PromiseAsync(function()
    new_user:saveAsync():Await()
    new_user:deleteAsync():Await()
end)

Querying

We have some simple methods to do querying:

-- The fastest way, get by identity
User:get(2, function(user)
end)

-- Find one by property filtering
User:find({ Name = "ceifa" }, function(user)
end)

-- Get many by property filtering
User:filter({ Rank = "donator" }, function(users)
end)

If you have support for util.Promise, you can use the async methods:

util.PromiseAsync(function()
    local user = User:getAsync(2):Await()
    local user = User:findAsync({ Name = "ceifa" }):Await()
    local users = User:filterAsync({ Rank = "donator" }):Await()
end)

But if you want more complex queries, you will have to do it yourself:

local user_db = User:database()
if user_db.Driver == "sqlite" or user_db.Driver == "mysqloo" then
    user_db:query("SELECT Name, COUNT(*) as Quantity FROM user GROUP BY Name", function(names)
    end)
else
    error("Database driver not supported")
end

About

Yet another gmod database abstraction

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages