在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):itdxer/4DaysORM开源软件地址(OpenSource Url):https://github.com/itdxer/4DaysORM开源编程语言(OpenSource Language):Lua 100.0%开源软件介绍(OpenSource Introduction):Lua 4Days ORM 10 minutes tutorialDatabase configurationBefore the beginning you should add some simple settings to your database configuration. You must create some global variable DB = {} Development configurations:
Database configurations
After setting configurations you can add 2 modules import to your file local Table = require("orm.model")
local fields = require("orm.tools.fields") Create tablelocal User = Table({
__tablename__ = "user",
username = fields.CharField({max_length = 100, unique = true}),
password = fields.CharField({max_length = 50, unique = true}),
age = fields.IntegerField({max_length = 2, null = true}),
job = fields.CharField({max_length = 50, null = true}),
time_create = fields.DateTimeField({null = true})
}) For every table is created a column
Also you can add different settings to your table fields
Types of table fieldsSupported types of table fields
Also you can create your types of table fields. But about it later. Create dataTry to create a new user: local user = User({
username = "Bob Smith",
password = "SuperSecretPassword",
time_create = os.time()
}) Now you created new user, but it was not added to database. You can add him. user:save() Now this user with all the information is in database. We can get his print("User " .. user.username .. " has id " .. user.id)
-- User Bob Smith has id 1 Update dataYou can change your data: user.username = "John Smith" This value was changed in model, but it has not been changed in database table. user:save() Now try to get new username for user: print("New user name is " .. user.username) -- New user name is John Smith You have updated in database only the column that you changed. You can also edit columns for the value by another terms: User.get:where({time_create__null = true})
:update({time_create = os.time()}) The conditions will be described in the next chapter Remove dataAnd also you can remove your data from table. user:delete() You can also delete columns for the value by another terms: -- add test user
user = User({username = "SomebodyNew", password = "NotSecret"})
user:save()
User.get:where({username = "SomebodyNew"}):delete() The conditions will be described in the next chapter Get dataAlso we can get data from table. But before this let's create 5 test users. user = User({username = "First user", password = "secret1", age = 22})
user:save()
user = User({username = "Second user", password = "secret_test", job = "Lua developer"})
user:save()
user = User({username = "Another user", password = "old_test", age = 44})
user:save()
user = User({username = "New user", password = "some_passwd", age = 23, job = "Manager"})
user:save()
user = User({username = "Old user", password = "secret_passwd", age = 44})
user:save() And now try get one of them: local first_user = User.get:first()
print("First user name is: " .. first_user.username)
-- First user name is: First user But also we can get all users from table: local users = User.get:all()
print("We get " .. users:count() .. " users")
-- We get 5 users Method Limit and OffsetSometime we need to get not one but not all users. For the first, try to get first 2 users from the table. users = User.get:limit(2):all()
print("We get " .. users:count() .. " users")
-- We get 2 users
print("Second user name is: " .. users[2].username)
-- Second user name is: Second user Great! But if we want to get next two users? We can do this by using following example: users = User.get:limit(2):offset(2):all()
print("Second user name is: " .. users[2].username)
-- Second user name is: New user Order resultAlso you can sort your result by order. We want to sort users from the oldest to the youngest. users = User.get:order_by({desc('age')}):all()
print("First user id: " .. users[1].id)
-- First user id: 3 But we have 2 users with age 44. We can order them by name. users = User.get:order_by({desc('age'), asc('username')}):all() You can order your table query by other parameters too. Group resultAnd now try to group your results: users = User.get:group_by({'age'}):all()
print('Find ' .. users:count() ..' users')
-- Find 4 users Where and HavingThese two methods have the same syntax. But user = User.get:where({username = "First user"}):first()
print("User id is: " .. user.id) -- User id is: 1 And the same for users = User.get:group_by({'id'}):having({age = 44}):all()
print("We get " .. users:count() .. " users with age 44")
-- We get 2 users with age 44 Great! But what if we need to do more operations than just a differentiation of table fields. We can do that! This is the list with some rules: For example we use for default
Super SELECTBut if we do ... user = User.get:where({age__lt = 30,
age__lte = 30,
age__gt = 10,
age__gte = 10
})
:order_by({asc('id')})
:group_by({'age', 'password'})
:having({id__in = {1, 3, 5},
id__notin = {2, 4, 6},
username__null = false
})
:limit(2)
:offset(1)
:all() This example doesn't make sense. But it works! JOIN TABLESNow we can create a join of tables. But before that we create some table with local News = Table({
__tablename__ = "group",
title = fields.CharField({max_length = 100, unique = false, null = false}),
text = fields.TextField({null = true}),
create_user_id = fields.ForeignKey({to = User})
}) And add two test news: local user = User.get:first()
local news = News({title = "Some news", create_user_id = user.id})
news:save()
news = News({title = "Other title", create_user_id = user.id})
news:save() Now try to get all the news from the owner. local news = News.get:join(User):all()
print("First news user id is: " .. news[1].user.id) -- First news user id is: 1 But if we want to get all users and also to get three news for each user . We can do this by following example: local user = User.get:join(News):first()
print("User " .. user.id .. " has " .. user.news_all:count() .. " news")
-- User 1 has 2 news
for _, user_news in pairs(user.news_all) do
print(user_news.title)
end
-- Some news
-- Other title If you want to get all the values from tables you can combine table's names and prefix "_all". Like in previous example user.news_all
Create column typesWe can create a field type for every table. Try to create EmailField type: fields.EmailField = fields:register({
__type__ = "varchar",
settings = {
max_length = 100
},
validator = function (value)
return value:match("[A-Za-z0-9%.%%%+%-]+@[A-Za-z0-9%.%%%+%-]+%.%w%w%w?%w?")
end,
to_type = function (value)
return value
end,
as = function (value)
return "'" .. value .. "'"
end
}) Let's make it step by step:
local UserEmails = Table({
__tablename__ = "user_emails",
email = fields.EmailField(),
user_id = fields.ForeignKey({ to = User })
})
local user_email = UserEmails({
email = "mailexample.com",
user_id = user.id
})
user_email:save() -- This email wasn't added!
-- And try again
local user_email = UserEmails({
email = "[email protected]",
user_id = user.id
})
user_email:save() -- This email was added!
user_email.email = "not email"
user_email:save() -- This email wasn't updated
user_email.email = "[email protected]"
user_email:save() -- This email was updated FinalAll code you can see in example.lua file. Feel free to use it! Good luck! |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论