-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.lua
More file actions
72 lines (58 loc) · 1.93 KB
/
cache.lua
File metadata and controls
72 lines (58 loc) · 1.93 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
-- 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"))
:index("steam_id", false, true) -- index also being used for fast search in cache (O(1))
:cache() -- applying cache (TableCache class)
:build()
--- Example №1 - find query
local function loadUser(player)
local user = database:findUnique({
where = {
steam_id = player:SteamID()
},
cache = true -- inserting found row in cache = true
})
if (not user) then
user = database:create({
data = {
steam_id = player:SteamID()
},
cache = true -- insering new created row in cache = true
})
end
end
---@param player Player
---@return YoursPackage.User?
local function getUser(player)
local cache = database:getCache()
-- O(1) user get
return cache:findIndexed("steam_id", player:SteamID())
end
---@param money integer
---@param operation? MeadowsORM.WhereBuilder.WhereCompareIndexes
local function getUsersWithMoney(money, operation)
local cache = database:getCache()
-- O(n) users get
return cache:find("money", money, operation or "eq")
end
local user = getUser(Player(1))
print(user and user:getMoney())
-- find users with money=1000
getUsersWithMoney(1000)
-- find users with money>1000
getUsersWithMoney(1000, "gt") -- gt = greater than
-- find users with money>=1000
getUsersWithMoney(1000, "gte") -- gte = greater than and equals
-- find users with money<1000
getUsersWithMoney(1000, "lt") -- lt = less than
-- find users with money<=1000
getUsersWithMoney(1000, "lte") -- lte = less than and equals