|
1 | | -import { WSConfig } from '../src/common/config'; |
2 | | -import { sqlConnect, destroy, setLogLevel } from '../src' |
| 1 | +import { WSConfig } from "../src/common/config"; |
| 2 | +import { sqlConnect, destroy, setLogLevel } from "../src"; |
3 | 3 |
|
4 | | -let dsn = 'ws://127.0.0.1:6041'; |
| 4 | +let dsn = "ws://127.0.0.1:6041"; |
5 | 5 | async function json_tag_example() { |
6 | 6 | let wsSql = null; |
7 | 7 | try { |
8 | | - |
9 | 8 | let conf = new WSConfig(dsn); |
10 | | - conf.setUser('root'); |
11 | | - conf.setPwd('taosdata'); |
| 9 | + conf.setUser("root"); |
| 10 | + conf.setPwd("taosdata"); |
12 | 11 | wsSql = await sqlConnect(conf); |
13 | 12 | console.log("Connected to " + dsn + " successfully."); |
14 | | - |
| 13 | + |
15 | 14 | // create database |
16 | | - await wsSql.exec('CREATE DATABASE IF NOT EXISTS example_json_tag'); |
| 15 | + await wsSql.exec("CREATE DATABASE IF NOT EXISTS example_json_tag"); |
17 | 16 | console.log("Create database example_json_tag successfully."); |
18 | | - |
| 17 | + |
19 | 18 | // create table |
20 | | - await wsSql.exec('create table if not exists example_json_tag.stb (ts timestamp, v int) tags(jt json)'); |
| 19 | + await wsSql.exec( |
| 20 | + "create table if not exists example_json_tag.stb (ts timestamp, v int) tags(jt json)" |
| 21 | + ); |
21 | 22 |
|
22 | 23 | console.log("Create stable example_json_tag.stb successfully"); |
23 | 24 |
|
24 | | - let insertQuery = 'INSERT INTO ' + |
| 25 | + let insertQuery = |
| 26 | + "INSERT INTO " + |
25 | 27 | 'example_json_tag.tb1 USING example_json_tag.stb TAGS(\'{"name":"value"}\') ' + |
26 | 28 | "values(now, 1) "; |
27 | 29 | let taosResult = await wsSql.exec(insertQuery); |
28 | | - console.log("Successfully inserted " + taosResult.getAffectRows() + " rows to example_json_tag.stb."); |
| 30 | + console.log( |
| 31 | + "Successfully inserted " + |
| 32 | + taosResult.getAffectRows() + |
| 33 | + " rows to example_json_tag.stb." |
| 34 | + ); |
29 | 35 |
|
30 | | - let sql = 'SELECT ts, v, jt FROM example_json_tag.stb limit 100'; |
| 36 | + let sql = "SELECT ts, v, jt FROM example_json_tag.stb limit 100"; |
31 | 37 | let wsRows = await wsSql.query(sql); |
32 | 38 | while (await wsRows.next()) { |
33 | 39 | let row = wsRows.getData(); |
34 | 40 | if (row) { |
35 | | - console.log('ts: ' + row[0] + ', v: ' + row[1] + ', jt: ' + row[2]); |
| 41 | + console.log( |
| 42 | + "ts: " + row[0] + ", v: " + row[1] + ", jt: " + row[2] |
| 43 | + ); |
36 | 44 | } |
37 | 45 | } |
38 | | - |
39 | 46 | } catch (err: any) { |
40 | | - console.error(`Failed to create database example_json_tag or stable stb, ErrCode: ${err.code}, ErrMessage: ${err.message}`); |
| 47 | + console.error( |
| 48 | + `Failed to create database example_json_tag or stable stb, ErrCode: ${err.code}, ErrMessage: ${err.message}` |
| 49 | + ); |
41 | 50 | } finally { |
42 | 51 | if (wsSql) { |
43 | 52 | await wsSql.close(); |
44 | 53 | } |
45 | 54 | } |
46 | | - |
47 | 55 | } |
48 | 56 |
|
49 | 57 | async function all_type_example() { |
50 | 58 | let wsSql = null; |
51 | 59 | try { |
52 | 60 | let conf = new WSConfig(dsn); |
53 | | - conf.setUser('root'); |
54 | | - conf.setPwd('taosdata'); |
| 61 | + conf.setUser("root"); |
| 62 | + conf.setPwd("taosdata"); |
55 | 63 | wsSql = await sqlConnect(conf); |
56 | 64 | console.log("Connected to " + dsn + " successfully."); |
57 | | - |
| 65 | + |
58 | 66 | // create database |
59 | | - await wsSql.exec('CREATE DATABASE IF NOT EXISTS all_type_example'); |
| 67 | + await wsSql.exec("CREATE DATABASE IF NOT EXISTS all_type_example"); |
60 | 68 | console.log("Create database all_type_example successfully."); |
61 | | - |
| 69 | + |
62 | 70 | // create table |
63 | | - await wsSql.exec('create table if not exists all_type_example.stb (ts timestamp, ' + |
64 | | - 'int_col INT, double_col DOUBLE, bool_col BOOL, binary_col BINARY(100),' + |
65 | | - 'nchar_col NCHAR(100), varbinary_col VARBINARY(100), geometry_col GEOMETRY(100)) ' + |
66 | | - 'tags(int_tag INT, double_tag DOUBLE, bool_tag BOOL, binary_tag BINARY(100),' + |
67 | | - 'nchar_tag NCHAR(100), varbinary_tag VARBINARY(100), geometry_tag GEOMETRY(100));'); |
| 71 | + await wsSql.exec( |
| 72 | + "create table if not exists all_type_example.stb (ts timestamp, " + |
| 73 | + "int_col INT, double_col DOUBLE, bool_col BOOL, binary_col BINARY(100)," + |
| 74 | + "nchar_col NCHAR(100), varbinary_col VARBINARY(100), geometry_col GEOMETRY(100)) " + |
| 75 | + "tags(int_tag INT, double_tag DOUBLE, bool_tag BOOL, binary_tag BINARY(100)," + |
| 76 | + "nchar_tag NCHAR(100), varbinary_tag VARBINARY(100), geometry_tag GEOMETRY(100));" |
| 77 | + ); |
68 | 78 |
|
69 | 79 | console.log("Create stable all_type_example.stb successfully"); |
70 | 80 |
|
71 | | - let insertQuery = "INSERT INTO all_type_example.tb1 using all_type_example.stb " |
72 | | - + "tags(1, 1.1, true, 'binary_value', 'nchar_value', '\\x98f46e', 'POINT(100 100)') " |
73 | | - + "values(now, 1, 1.1, true, 'binary_value', 'nchar_value', '\\x98f46e', 'POINT(100 100)')"; |
| 81 | + let insertQuery = |
| 82 | + "INSERT INTO all_type_example.tb1 using all_type_example.stb " + |
| 83 | + "tags(1, 1.1, true, 'binary_value', 'nchar_value', '\\x98f46e', 'POINT(100 100)') " + |
| 84 | + "values(now, 1, 1.1, true, 'binary_value', 'nchar_value', '\\x98f46e', 'POINT(100 100)')"; |
74 | 85 | let taosResult = await wsSql.exec(insertQuery); |
75 | | - console.log("Successfully inserted " + taosResult.getAffectRows() + " rows to all_type_example.stb."); |
| 86 | + console.log( |
| 87 | + "Successfully inserted " + |
| 88 | + taosResult.getAffectRows() + |
| 89 | + " rows to all_type_example.stb." |
| 90 | + ); |
76 | 91 |
|
77 | | - let sql = 'SELECT * FROM all_type_example.stb limit 100'; |
| 92 | + let sql = "SELECT * FROM all_type_example.stb limit 100"; |
78 | 93 | let wsRows = await wsSql.query(sql); |
79 | 94 | let meta = wsRows.getMeta(); |
80 | 95 | console.log("wsRow:meta:=>", meta); |
81 | 96 | while (await wsRows.next()) { |
82 | 97 | let row = wsRows.getData(); |
83 | 98 | console.log(row); |
84 | 99 | } |
85 | | - |
86 | 100 | } catch (err: any) { |
87 | | - console.error(`Failed to create database all_type_example or stable stb, ErrCode: ${err.code}, ErrMessage: ${err.message}`); |
| 101 | + console.error( |
| 102 | + `Failed to create database all_type_example or stable stb, ErrCode: ${err.code}, ErrMessage: ${err.message}` |
| 103 | + ); |
88 | 104 | } finally { |
89 | 105 | if (wsSql) { |
90 | 106 | await wsSql.close(); |
91 | 107 | } |
92 | 108 | } |
93 | | - |
94 | 109 | } |
95 | 110 |
|
96 | 111 | async function test() { |
97 | | - await json_tag_example() |
98 | | - await all_type_example() |
| 112 | + await json_tag_example(); |
| 113 | + await all_type_example(); |
99 | 114 | destroy(); |
100 | 115 | } |
101 | 116 |
|
102 | | -test() |
| 117 | +test(); |
0 commit comments