Skip to content

Latest commit

 

History

History
142 lines (101 loc) · 2.61 KB

api.md

File metadata and controls

142 lines (101 loc) · 2.61 KB

Document

Getting Started

query by sql

import Greptime from greptime
let { sql } = Greptime({})
try {
  let res = await sql.select('*').from('script').query()
  console.log(
    '====================\n',
    res.sql,
    '\n-------------------\n',
    res.schema,
    '\n-------------------\n',
    res.rows.length,
    '\n====================\n'
  )
} catch (error) {
  console.log(`show error:`, error)
}

query by promQL

import Greptime from greptime
let { promQL } = Greptime({})
try {
  let res = await promQL.query('demo').duration('1m').run()
  console.log(`res:`, res.data)
} catch (error) {
  console.log(`error:`, error)
}

API

common

init

import Greptime from greptime
let { sql, promQL } = Greptime({
  host = 'http://127.0.0.1:4000',
  dbname = 'public',
  username = '',
  password = '',
})

The default parameter is to connect to the local client. The default database name is public and there is no user name or password. If you want to connect to the cloud service, the corresponding parameter initialization needs to be replicated in the cloud background.

sql

createTable

sql.createTable('demo', {
  timeIndex: 'ts',
  tags: ['test'],
  // The default type of the data is double, you can use other types like this   {startTime: ''}
  fields: ['data', { startTime: 'double' }],
})

This function will be converted to a 'CREATE TABLE IF NOT EXISTS' statement

descTable

sql.descTable('demo')

This function will be converted to a 'DESC TABLE' statement

getTimeIndex

sql.getTimeIndex('demo')

This function return the time index of the current table

query(sql:? string)

sql.select('*').from('demo').limit(100).orderBy('ts', 'ASC').query()

sql.query('select * from demo limit 100')
  • Call by chain
  • Finally, you need to call the query method to get the return value
  • You can also immediately run SQL queries

count

sql.select('*').from('demo').count()

Obtain the number of query results

sql syntactic sugar

today

sql.select('*').from('demo').today().count()

duration

const timeIndex = await sql.getTimeIndex('demo')
sql.select('*').from('demo').duration(timeIndex, '5m').count()

Obtain the query result in the last five minutes

promQL

query by duration

let res = await promQL.query('apm').duration('1m').run()

Note: You cannot use the Int type as a query condition

query by time range

let res = await promQL.query('apm').start('2023-08-06 00:00').end('2023-08-07 00:00').run()

You can use any time format that dayjs can parse