FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
orm/example.lua at develop · TeamMeadows/orm · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
TeamMeadows
/
orm
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
orm
/
example.lua
Copy path
More file actions
More file actions
Latest commit
History
History
History
88 lines (71 loc) · 2.81 KB
Breadcrumbs
orm
/
example.lua
Copy path
File metadata and controls
88 lines (71 loc) · 2.81 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
---
@class
Example
:
Atomic.Package
local
package
=
current
()
local
MeadowsORM
=
package
:
getDependency
(
"
team.meadows.orm
"
)
---
@cast
MeadowsORM
MeadowsORM
---
this is bitflags
---
you can summary them to combine
local
UNIQUE
=
MeadowsORM
.
UNIQUE
local
NOT_NULL
=
MeadowsORM
.
NOT_NULL
---
1. Describing table structure
local
group
=
MeadowsORM
:
create
(
"
groups
"
)
:
id
()
--
add `id` column with auto increment
:
column
(
"
name
"
,
"
text
"
,
UNIQUE
+
NOT_NULL
)
:
column
(
"
title
"
,
"
text
"
,
UNIQUE
+
NOT_NULL
)
:
createdAt
()
--
add `created_at` column, that will be automatically insert current timestamp when you create new row in database
:
updatedAt
()
--
add `updated_at` column, that will be automatically updated when you update something in a database row
:
build
()
--
needed to create TableInterface (class that would be provide methods to interact with this table in database)
--
also, `build` method is creating an `CREATE TABLE` SQL query and automatically sends it to the database
--
so its basically creates an table in database in runtime
local
users
=
MeadowsORM
:
create
(
"
users
"
)
:
id
()
:
column
(
"
name
"
,
"
text
"
,
UNIQUE
+
NOT_NULL
)
:
column
(
"
group
"
,
"
text
"
,
UNIQUE
+
NOT_NULL
,
"
0
"
)
:
relation
(
group
,
"
id
"
,
"
group
"
)
--
relation one-to-one by Id column by default
:
build
()
---
2. Creating Entity (class)
--
it is optional
--
--
sets the deserialisation class for the table
--
this means that when raw data from the database is received by MeadowsORM, it will convert it into this class (deserialise it)
---
@class
Example.Group
:
MeadowsORM.Entity
---
@field
private
id integer
---
@field
private
name string
---
@field
private
title string
---
@field
private
created_at Atomic.Time.NaiveDateTime
---
@field
private
updated_at Atomic.Time.NaiveDateTime
local
Group
=
group
:
entity
(
package
,
"
Group
"
)
---
@param
raw
table Raw row from a database
function
Group
:
init
(
raw
)
--
REQUIRED! + should be on EXACTLY first line of your constructor method!
super
(
self
,
raw
)
--
this line call `MeadowsORM.Entity.init` method, providing `raw` to it as an argument
--
why? `MeadowsORM.Entity.init` method setting fields up
end
--
you can create new methods here
--
to use it later
---
3. Intecation with database
--
your query should be in coroutine!
--
(you can put it in coroutine using `async` function that defined by Atomic Framework)
--
as in this example
async
(
function
()
--
Object of a class Group (that we define at line 39)
local
userWithId2
=
users
:
findUnique
({
where
=
{
--
selecting user with id == 2
id
=
2
}
})
if
(
userWithId2
)
then
assert
(
userWithId2
.
group
==
"
user
"
)
else
print
(
"
user with id = 2 not found
"
)
end
--
selecting users with id that LESS THAN (lt) 4
--
Array of objects of a class Group
local
users
=
users
:
findMany
({
where
=
{
id
=
{
lt
=
"
4
"
}
}
})
assert
(
#
users
<
4
)
end
)
Back
|
FazBrowse Home
|
New Git URL