-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.lua
76 lines (63 loc) · 1.53 KB
/
example.lua
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
local Utils = require("utils")
local Typedef = require("typedef")
local Schema = [[
.Prop {
id : integer
name : string
exp : double
birthday : date
newbie : boolean
}
.Bag {
name : string
items : <integer, integer> # 道具ID和数量字典
}
.Player {
prop : Prop
sign : binary
storage : *Bag # 背包列表
# 私有field
.device {
anme : string
net : string
# other
}
}
]]
-- 导出schema
local typeList = Typedef.parse_text(Schema)
Utils.dump(typeList)
local ORM = require("orm")
-- 初始化ORM 全局
ORM.init(typeList)
local rawData = {
prop = {id = 1001, name = "Anni", exp = 100.10, newbie = false},
sign = nil,
storage = {
{name = "bag1", items = {[1001] = 10}},
{name = "bag2", items = {[1002] = 30}}
},
device = {},
}
-- 创建数据管理对象
local player = ORM.create("Player", rawData)
-- 启动脏数据管理
ORM.enable_dirty(player)
player.prop.id = 123
player.prop.exp = 11.221
player.storage[1] = ORM.clone_mongodata(player.storage[2])
player.storage[2].name = "bag2new" -- 非循环引用
Utils.dump(player)
-- 获取脏数据结构
local dirtyInfo = ORM.get_dirty_info(player)
Utils.dump(dirtyInfo)
-- 序列化脏数据成mongo语句
local dirtyMongo = ORM.get_mongo_dirty_data(player)
Utils.dump(dirtyMongo)
-- 完成一次存盘后清理dirty标记
ORM.clear_dirty_info(player)
-- 关闭存盘数据管理
ORM.disable_dirty(player)
-- 获取原始数据
local data = ORM.clone_mongodata(player)
Utils.dump(data)