-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity.lua
More file actions
74 lines (61 loc) · 2.14 KB
/
entity.lua
File metadata and controls
74 lines (61 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
-- Entity is a basic class, that implements logic to automatically update
---@class YoursPackage: Atomic.Package
local package = current()
local MeadowsORM = package:getDependency("team.meadows.orm")
---@cast MeadowsORM MeadowsORM
local NOT_NULL = MeadowsORM.NOT_NULL
local UNIQUE = MeadowsORM.UNIQUE
local RawSQL = MeadowsORM.RawSQL
local database = MeadowsORM:create("users")
:id()
:column("steam_id", "varchar(32)", NOT_NULL + UNIQUE)
:column("money", "int", NOT_NULL, RawSQL("0"))
:build()
-- creating entity
-- first argument is yours package
-- second is a class name
---@class YoursPackage.User: MeadowsORM.Entity
---@field private id integer
---@field private steam_id string
---@field private money integer
---@field getId fun(self: self): integer
---@field setId fun(self: self, id: integer)
---@field getSteamId fun(self: self): string
---@field setSteamId fun(self: self, steamId: string)
---@field getMoney fun(self: self): integer
---@field setMoney fun(self: self, money: integer)
local User = database:entity(package, "User")
-- the methods as `getId`, `setMoney` will be automatically created by MeadowsORM
-- so you only need to describe it via LuaLS annotations (only if you use LuaLS)
---@param raw table Raw row (table) directly from the database
function User:init(raw)
-- Required!
-- It will setup your fields
self:super(raw) -- Required!!!
end
function User:addMoney(money)
-- it will automatically update the value in database
self:setMoney(self:getMoney() + money)
-- if you don't wanna update it automatically
-- you can just change the field by yourself
self.money = self:getMoney() + money
end
-- also you able to get your entity through `package.getClass` method
---@type YoursPackage.User
local UserClass = package:getClass("User")
--- Example №1 - find query
local function getUser(player)
local user = database:findUnique({
where = {
steam_id = player:SteamID()
}
})
if (not user) then
user = database:create({
data = {
steam_id = player:SteamID()
}
})
end
print(isInstanceOf(user, UserClass)) -- true (user object will be created as User's class instance)
end