1
0
mirror of https://github.com/lxsang/antd-web-apps synced 2024-12-28 18:28:21 +01:00
antd-web-apps/silk/BaseModel.lua

52 lines
1.1 KiB
Lua
Raw Normal View History

2018-08-22 19:38:36 +02:00
2018-08-21 15:16:36 +02:00
-- create class
2018-08-27 20:10:53 +02:00
BaseObject:subclass("BaseModel", {registry = {}})
2018-08-21 15:16:36 +02:00
function BaseModel:initialize()
2018-08-22 19:38:36 +02:00
self.db = self.registry.db
if self.db and self.name and self.name ~= "" and self.fields and not self.db:available(self.name) then
self.db:createTable(self.name, self.fields)
end
2018-08-21 15:16:36 +02:00
end
2018-08-22 19:38:36 +02:00
function BaseModel:create(m)
if self.db and m then
return self.db:insert(self.name,m)
end
return false
2018-08-21 15:16:36 +02:00
end
2018-08-22 19:38:36 +02:00
function BaseModel:update(m)
if self.db and m then
return self.db:update(self.name,m)
end
return false
2018-08-21 15:16:36 +02:00
end
2018-08-22 19:38:36 +02:00
function BaseModel:delete(cond)
if self.db and cond then
2018-08-22 19:38:36 +02:00
return self.db:delete(self.name,cond)
end
return false
2018-08-21 15:16:36 +02:00
end
2018-08-22 19:38:36 +02:00
function BaseModel:find(cond)
if self.db and cond then
2018-08-22 19:38:36 +02:00
return self.db:find(self.name, cond)
end
return false
end
2018-09-05 16:56:04 +02:00
function BaseModel:get(id)
local data, order = self:find({exp = {["="] = { id = id}} })
if not data or #order == 0 then return false end
return data[1]
end
2018-08-22 19:38:36 +02:00
function BaseModel:findAll()
if self.db then
2018-08-22 19:38:36 +02:00
return self.db:getAll(self.name)
end
return false
2018-08-21 15:16:36 +02:00
end