-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtestTable.js
More file actions
298 lines (255 loc) · 10.7 KB
/
testTable.js
File metadata and controls
298 lines (255 loc) · 10.7 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
'use strict'
const ChainsqlAPI = require('../src/index');
const c = new ChainsqlAPI();
var root = {
secret: "xnoPBzXtMeMyMHUVTgbuqAfg1SUTb",
address: "zHb9CJAWyB4zj91VRWn96DkukG4bwdtyTh"
}
var owner = {
address: "zpMZ2H58HFPB5QTycMGWSXUeF47eA8jyd4",
secret: "xnnUqirFepEKzVdsoBKkMf577upwT"
}
var user = {
address: "zKQwdkkzpUQC9haHFEe2EwUsKHvvwwPHsv",
secret: "xnJn5J5uYz3qnYX72jXkAPVB3ZsER"
}
var userOperation = owner;
var user2 = {
address: "zhd8rfb9dyoq7b8vMBqSm3dbzJpUNFNtRt",
secret: "xnoHuFw7CcgXD29fv2yi8uGkiqSqm"
}
var smRoot = {
secret: "p97evg5Rht7ZB7DbEpVqmV3yiSBMxR3pRBKJyLcRWt7SL5gEeBb",
address: "zN7TwUjJ899xcvNXZkNJ8eFFv2VLKdESsj",
publicKey: 'pYvWhW4azFwanovo5MhL71j5PyTWSJi2NVurPYUrE9U…txxQB7xeGvFmdjbtKRzBQ4g9bCW5hjBQSeb7LePMwFM'
}
const smUser7 ={
secret:"pwRdHmA4cSUKKtFyo4m2vhiiz5g6ym58Noo9dTsUU97mARNjevj",
address: "zMXMtS2C36J1p3uhTxRFWV8pEhHa8AMMSL",
publicKey: "pYvXDbsUUr5dpumrojYApjG8nLfFMXhu3aDvxq5oxEa4ZSeyjrMzisdPsYjfxyg9eN3ZJsNjtNENbzXPL89st39oiSp5yucU"
}
var grantAddr = "0xzzzzzzzzzzzzzzzzzzzzBZbvji";
var sTableName = "hjkz9";
var sTableNameNew = "table_new"
var tableRaw = [
{ 'field': 'id', 'type': 'int' },
{ 'field': 'time', 'type': 'datetime' },
{ 'field': 'txHash', 'type': 'varchar', 'length': 100 },
{ 'field': 'name', 'type': 'varchar', 'length': 100 },
{ 'field': 'email', 'type': 'varchar', 'length': 100 },
{ 'field': 'account', 'type': 'varchar', 'length': 40 }
];
// var insertRaw = [
// { 'id': 1, 'name': 'zhangsan', 'email': '123', 'account': root.address, 'time': '2018-10-18 14:31:00', 'txHash': "txHash" },
// { 'id': 2, 'name': 'lisi', 'email': '124', 'account': root.address, 'time': '2018-10-18 14:31:00', 'txHash': "txHash" },
// { 'id': 3, 'name': 'wangwu', 'email': '125', 'account': root.address, 'time': '2018-10-18 14:31:00', 'txHash': "txHash" },
// { 'id': 4, 'name': 'zhaoliu', 'email': '126', 'account': root.address, 'time': '2018-10-18 14:31:00', 'txHash': "txHash" }
// ];
var insertRaw = [
{ 'id': 1, 'name': 'zhangsan', 'email': '123', 'account': root.address, 'time': '2018-10-18 14:31:00' },
{ 'id': 2, 'name': 'lisi', 'email': '124', 'account': root.address, 'time': '2018-10-18 14:31:00' },
{ 'id': 3, 'name': 'wangwu', 'email': '125', 'account': root.address, 'time': '2018-10-18 14:31:00' },
{ 'id': 4, 'name': 'zhaoliu', 'email': '126', 'account': root.address, 'time': '2018-10-18 14:31:00' }
];
var tagStep = {
active: 1, table_create: 2, table_create_operationRule: 3,
table_rename: 4, table_grant: 5, table_drop: 6,
table_insert: 7, table_insert_operationRule: 8, table_delete: 9,
table_update: 10, table_get: 11, table_transaction: 12
}
var wsAddr = 'ws://192.168.29.69:8006'
main();
async function main() {
let res = await c.connect(wsAddr);
console.log("connect successfully.")
c.setRestrict(true);
//test
c.as(smRoot)
try{
var tableRaw = [
{ 'field': 'id', 'type': 'int', 'length': 100, 'PK': 1, 'NN': 1 },
{ 'field': 'name', 'type': 'varchar', 'length': 100 },
{ 'field': 'age', 'type': 'int' },
{ 'field': 'account', 'type': 'varchar', 'length': 40 }
];
var rule = {
'Insert': {
'Condition': { 'account': '$account' }, //Condition:指定插入操作可设置的默认值
'Count': { 'AccountField': 'account', 'CountLimit': 5 }
},
'Delete': {
'Condition': { 'account': '$account' } //只能删除自己插入的数据
},
'Get': {
'Condition': { 'id': { '$ge': 5 } } //只能查询id大于5的数据
},
'Update': {
'Fields': ['name'] //只能更新name字段
}
};
var option = {
confidential: false,
operationRule: rule
};
var lll;
// lll = await c.createTable(sTableName, tableRaw, option).submit({ expect: 'db_success' });
// console.log(" createTable_operationRule", lll);
// var insertRaw2 = [
// { 'id': 1, 'name': 'zhangsan', 'age': 123 }
// ];
// lll = await c.table(sTableName).insert(insertRaw2).submit({ expect: 'validate_success' });
// console.log(" insert", lll);
var rs = await c.table(sTableName).get({ 'id': 1 }).delete().submit({ expect: 'db_success' })
console.log("testDelete", rs)
}catch(e){
console.error(e)
}
return ;
//t
/**************************************/
let nStep = tagStep.table_insert_operationRule;
// userOperation = user;
switch (nStep) {
case tagStep.active: active(); break;
case tagStep.table_create: table_create(); break;
case tagStep.table_create_operationRule: table_create_operationRule(); break;
case tagStep.table_rename: table_rename(); break;
case tagStep.table_grant: table_grant(); break;
case tagStep.table_drop: table_drop(); break;
case tagStep.table_insert: table_insert(); break;
case tagStep.table_insert_operationRule: table_insert_operationRule(); break;
case tagStep.table_delete: table_delete(); break;
case tagStep.table_update: table_update(); break;
case tagStep.table_get: table_get(); break;
case tagStep.table_transaction: table_transaction(); break;
default: break;
}
/**************************************/
}
var active = async function () {
c.as(root);
var amount = 20000
console.log("----------- active >>>>>>>>>>>>>");
let res = await c.pay(owner.address, amount).submit({ expect: 'validate_success' })
console.log("\n owner", owner.address, ":", res)
res = await c.pay(user.address, amount).submit({ expect: 'validate_success' })
console.log("\n user", user.address, ":", res)
res = await c.pay(user2.address, amount).submit({ expect: 'validate_success' })
console.log("\n user2", user2.address, ":", res)
console.log("\n----------- active <<<<<<<<<<<<<");
}
var table_create = async function () {
c.as(smRoot)
try {
var option = {
confidential: true
};
let lll = await c.createTable(sTableName, tableRaw,option).submit({expect:"db_success"});
console.log(" createTable", sTableName, lll);
} catch (error) {
console.log(" createTable ", sTableName, error);
}
};
var table_create_operationRule = async function () {
try{
c.as(smRoot)
var rule = {
'Insert': {
'Condition': { 'txHash': '$tx_hash' } //Condition:指定插入操作可设置的默认值
}
};
var option = {
confidential: false,
operationRule: rule
};
let lll = await c.createTable(sTableName, tableRaw, option).submit({ expect: 'db_success' });
console.log(" createTable_operationRule", lll);
}catch(e){
console.error(e);
}
}
var table_rename = async function () {
c.as(owner)
let lll = await c.renameTable(sTableName, sTableNameNew).submit({ expect: 'db_success' });
console.log(" table_rename :", lll);
}
var table_grant = async function () {
c.as(owner)
var flag = { insert: true, update: true }
let lll = await c.grant(sTableName, grantAddr, flag).submit({ expect: 'db_success' });
console.log(" table_grant :", lll);
}
var table_drop = async function () {
c.as(owner)
let lll = await c.dropTable(sTableName).submit({ expect: 'db_success' });
console.log(" dropTable", sTableName, lll);
}
var table_insert = async function () {
c.as(smRoot)
// c.as(userOperation)
// c.use(owner.address)
try {
let lll = await c.table(sTableName).insert(insertRaw, "txHash").submit({ expect: 'db_success' });
console.log(" insert", lll);
} catch (error) {
console.log(" insert error: ", error);
}
};
var table_insert_operationRule = async function () {
c.as(smRoot)
// c.use(owner.address)
let lll = await c.table(sTableName).insert(insertRaw).submit({ expect: 'db_success' });
console.log(" insert", lll);
}
var table_delete = async function () {
c.as(userOperation)
c.use(owner.address)
var rs = await c.table(sTableName).get({ 'id': 3 }).delete().submit({ expect: 'db_success' });
console.log("testDelete", rs)
}
var table_update = async function () {
c.as(userOperation)
c.use(owner.address)
let lll = await c.table(sTableName).get({ name: { $regex: '/s/' } }).update({ account: "update regex 's'" }).submit({ expect: 'db_success' });
console.log(" update (regix)1:", lll);
//
lll = await c.table(sTableName).get({ email: 125 }).update({ account: "email==125" }).submit({ expect: 'db_success' });
console.log(" update (==)2:", lll);
//
lll = await c.table(sTableName).get({ $or: [{ email: "126" }, { name: "zhangsan" }] }).update({ account: "update email == 123 || name == zhangsan" }).submit({ expect: 'db_success' });
console.log(" update (or)3", lll);
}
var table_get = async function () {
c.as(userOperation)
c.use(owner.address)
let lll = await c.table(sTableName).get().submit();
console.log(" all record:", lll);
lll = await c.table(sTableName).get().withFields(["SUM(id) as sum"]).submit();
console.log(" all record sum(id):", lll);
if (lll.lines.length > 0) {
console.log(" sum:", lll.lines[0].sum)
}
lll = await c.table(sTableName).get({ $or: [{ email: "123" }, { name: "zhangsan" }] }).submit();
console.log(" record (or)", lll);
lll = await c.table(sTableName).get({ name: { $regex: '/s/' } }).submit();
console.log(" regex record:", lll);
lll = await c.table(sTableName).get({ name: { $regex: '/s/' } }).withFields(["COUNT(*) as count"]).submit();
console.log(" record count:", lll);
lll = await c.table(sTableName).get({ name: { $regex: '/s/' } }).withFields([]).submit();
console.log(" record count:", lll);
lll = await c.table(sTableName).get({ name: { $regex: '/s/' } }).limit({ index: 0, total: 1 }).withFields([]).submit();
console.log(" record count(limit):", lll);
lll = await c.table(sTableName).get({ name: { $regex: '/s/' } }).withFields(["account"]).submit();
console.log(" record with fields:", lll);
}
var table_transaction = async function () {
c.as(userOperation)
c.beginTran();
c.createTable(sTableName, tableRaw);
c.grant(sTableName, user.address, { insert: true, update: true }, user.publicKey)
c.table(sTableName).insert(insertRaw);
c.table(sTableName).get().update({ account: "updateTxs" });
c.table(sTableName).get({ 'id': 3 }).delete()
var rs = await c.commit({ expect: 'db_success' });
console.log(" table_transaction", rs);
}