UNPKG

@kevin_men/websocket

Version:

TDengine Connector for nodejs and browser using WebSocket.

59 lines (58 loc) 1.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const config_1 = require("../src/common/config"); const src_1 = require("../src"); let db = 'power'; let stable = 'meters'; let tags = ['California.SanFrancisco', 3]; let multi = [ [1706786044994, 1706786044995, 1706786044996], [10.2, 10.3, 10.4], [292, 293, 294], [0.32, 0.33, 0.34], ]; let dsn = 'ws://root:taosdata@localhost:6041'; async function Prepare() { let conf = new config_1.WSConfig(dsn); let wsSql = await (0, src_1.sqlConnect)(conf); await wsSql.exec(`create database if not exists ${db} KEEP 3650 DURATION 10 BUFFER 16 WAL_LEVEL 1;`); await wsSql.exec(`CREATE STABLE if not exists ${db}.${stable} (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int);`); wsSql.close(); } (async () => { let stmt = null; let connector = null; try { await Prepare(); let wsConf = new config_1.WSConfig(dsn); wsConf.setDb(db); connector = await (0, src_1.sqlConnect)(wsConf); stmt = await connector.stmtInit(); await stmt.prepare(`INSERT INTO ? USING ${db}.${stable} (location, groupId) TAGS (?, ?) VALUES (?, ?, ?, ?)`); await stmt.setTableName('d1001'); let tagParams = stmt.newStmtParam(); tagParams.setVarchar([tags[0]]); tagParams.setInt([tags[1]]); await stmt.setTags(tagParams); let bindParams = stmt.newStmtParam(); bindParams.setTimestamp(multi[0]); bindParams.setFloat(multi[1]); bindParams.setInt(multi[2]); bindParams.setFloat(multi[3]); await stmt.bind(bindParams); await stmt.batch(); await stmt.exec(); } catch (e) { console.error(e); } finally { if (stmt) { await stmt.close(); } if (connector) { await connector.close(); } (0, src_1.destroy)(); } })();