-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdatatype.ts
91 lines (83 loc) · 2.19 KB
/
datatype.ts
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
import { ClickhouseOrm, DATA_TYPE } from "../lib/index";
import { clientConfig } from "../mock";
/**
* defined Model
*/
const tableSchema = {
tableName: "datatype_table",
schema: {
time: { type: DATA_TYPE.DateTime, default: Date },
int32: { type: DATA_TYPE.Int32 },
int64: { type: DATA_TYPE.Int64 },
string: { type: DATA_TYPE.String },
fixedString: { type: DATA_TYPE.FixedString(3) },
lowStr: { type: DATA_TYPE.LowCardinality(DATA_TYPE.String) },
lowfixed: { type: DATA_TYPE.LowCardinality(DATA_TYPE.FixedString(3)), default: '987' },
uuid: { type: DATA_TYPE.UUID },
enum8: { type: DATA_TYPE.Enum8(`'enum1'=1,'enum2'=2,'enum4'=4`) },
enum16: { type: DATA_TYPE.Enum16(`'enum30000'=30000,'enum30100'=30100,'enum30200'=30200`) },
arr: { type: DATA_TYPE.Other('Array(String)') },
},
options: `ENGINE = MergeTree
PARTITION BY toYYYYMM(time)
ORDER BY time`,
autoCreate: true,
autoSync: true,
};
/**
* new instance
*/
const db = {
name: "orm_test",
engine: "Atomic", // default: Atomic
};
const chOrm = ClickhouseOrm({
client: clientConfig,
db,
debug: true,
});
const doDemo = async () => {
// create database 'orm_test'
await chOrm.createDatabase();
await chOrm.client
.query(`DROP TABLE IF EXISTS ${db.name}.${tableSchema.tableName}`)
.toPromise();
// register schema and create [if] table
const tableModel = await chOrm.model(tableSchema);
const uuidRes: any = await chOrm.client
.query(`SELECT generateUUIDv4() as uuid`)
.toPromise();
await tableModel.create({
time: new Date(),
int32: 666,
string: "90.0.1.21",
fixedString: "12",
lowStr: "low string",
lowfixed: '123',
uuid: uuidRes[0].uuid,
enum8: "enum1",
enum16: "enum30000",
arr: ['str1','str2'],
});
await tableModel.create({
time: new Date(),
int32: 666,
string: "90.0.1.21",
fixedString: "12",
lowStr: "low string 2",
uuid: uuidRes[0].uuid,
enum8: "enum4",
enum16: "enum30100",
arr: ['arr1'],
});
// do find
tableModel
.find({
select: "*,CAST(enum8, 'Int8'),CAST(enum16, 'Int16')",
limit: 10,
})
.then((res) => {
console.log("find:", res);
});
};
doDemo();