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)
}
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)
}
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('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
sql.descTable('demo')
This function will be converted to a 'DESC TABLE' statement
sql.getTimeIndex('demo')
This function return the time index of the current table
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
sql.select('*').from('demo').count()
Obtain the number of query results
sql.select('*').from('demo').today().count()
const timeIndex = await sql.getTimeIndex('demo')
sql.select('*').from('demo').duration(timeIndex, '5m').count()
Obtain the query result in the last five minutes
let res = await promQL.query('apm').duration('1m').run()
Note: You cannot use the Int type as a query condition
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