Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(examples): change examples to ES modules #518

Merged
merged 14 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
parameters:
image:
type: string
default: &default-image 'cimg/node:14.17.6'
default: &default-image 'cimg/node:14.19'
docker:
- image: << parameters.image >>
steps:
Expand Down
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
1. [#511](https://github.com/influxdata/influxdb-client-js/pull/511): Replace rollup with tsup.
1. [#514](https://github.com/influxdata/influxdb-client-js/pull/514): Repair links in generated API documentation.
1. [#516](https://github.com/influxdata/influxdb-client-js/pull/516): Replace ts-node with esbuild-runner.
1. [#518](https://github.com/influxdata/influxdb-client-js/pull/518): Change examples to ES modules.

## 1.28.0 [2022-07-29]

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ $ pnpm add @influxdata/influxdb-client-apis
Use the following examples to get started with the JavaScript client for InfluxDB:

- @influxdata/influxdb-client
- [write points](./examples/write.js)
- [write points](./examples/write.mjs)
- [query data](./examples/query.ts)
- @influxdata/influxdb-client-apis
- [setup / onboarding](./examples/onboarding.js)
- [create bucket](./examples/createBucket.js)
- [setup / onboarding](./examples/onboarding.mjs)
- [create bucket](./examples/createBucket.mjs)

See [examples](./examples/README.md) for more advanced use case like the following:

Expand Down
2 changes: 1 addition & 1 deletion examples/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"overrides": [
{
"files": ["*.js"],
"files": ["*.js", "*.mjs"],
"rules": {
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/naming-convention": ["off"]
Expand Down
14 changes: 7 additions & 7 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ This directory contains javascript and typescript examples for node.js, browser,
- Prerequisites
- [node](https://nodejs.org/en/) installed
- Run `npm install` in this directory
- Change variables in [./env.js](env.js) to configure connection to your InfluxDB instance. The file can be used as-is against a new [docker InfluxDB v2.1 OSS GA installation](https://docs.influxdata.com/influxdb/v2.1/get-started/)
- Change variables in [./env.mjs](env.mjs) to configure connection to your InfluxDB instance. The file can be used as-is against a new [docker InfluxDB v2.3 OSS GA installation](https://docs.influxdata.com/influxdb/v2.3/get-started/)
- Examples are executable. If it does not work for you, run `npm run esr EXAMPLE.ts`.
- [write.js](./write.js)
- [write.mjs](./write.mjs)
Write data points to InfluxDB.
- [query.ts](./query.ts)
Query InfluxDB with [Flux](https://docs.influxdata.com/influxdb/v2.1/get-started/).
- [queryWithParams.ts](./queryWithParams.ts)
Supply parameters to a [Flux](https://docs.influxdata.com/influxdb/v2.1/get-started/) query.
- [ping.js](./ping.js)
- [ping.mjs](./ping.mjs)
Check status of InfluxDB server.
- [createBucket.js](./createBucket.js)
- [createBucket.mjs](./createBucket.mjs)
Creates an example bucket.
- [onboarding.js](./onboarding.js)
- [onboarding.mjs](./onboarding.mjs)
Performs onboarding of a new influxDB database. It creates a new organization, bucket and user that is then used in all examples.
- [influxdb-1.8.ts](./influxdb-1.8.ts)
How to use forward compatibility APIs from InfluxDB 1.8.
- [rxjs-query.ts](./rxjs-query.ts)
Use [RxJS](https://rxjs.dev/) to query InfluxDB with [Flux](https://docs.influxdata.com/influxdb/v2.1/get-started/).
- [writeAdvanced.js](./writeAdvanced.js)
- [writeAdvanced.mjs](./writeAdvanced.mjs)
Shows how to control the way of how data points are written to InfluxDB.
- [follow-redirects.js](./follow-redirects.js)
- [follow-redirects.mjs](./follow-redirects.mjs)
Shows how to configure the client to follow HTTP redirects.
- [delete.ts](./delete.ts)
Shows how to delete data from a bucket.
Expand Down
19 changes: 10 additions & 9 deletions examples/createBucket.js → examples/createBucket.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ This example creates a new bucket. If a bucket of the same name already exists,
it is deleted and then created again.
*/

const {InfluxDB, HttpError} = require('@influxdata/influxdb-client')
const {OrgsAPI, BucketsAPI} = require('@influxdata/influxdb-client-apis')
const {url, org, token} = require('./env')
import {InfluxDB, HttpError} from '@influxdata/influxdb-client'
import {OrgsAPI, BucketsAPI} from '@influxdata/influxdb-client-apis'
import {url, org, token} from './env.mjs'
const influxDB = new InfluxDB({url, token})

async function recreateBucket(name) {
Expand Down Expand Up @@ -49,9 +49,10 @@ async function recreateBucket(name) {
)
}

recreateBucket('example-bucket')
.then(() => console.log('\nFinished SUCCESS'))
.catch((error) => {
console.error(error)
console.log('\nFinished ERROR')
})
try {
await recreateBucket('example-bucket')
console.log('\nFinished SUCCESS')
} catch (e) {
console.error(e)
console.log('\nFinished ERROR')
}
4 changes: 2 additions & 2 deletions examples/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import {InfluxDB} from '@influxdata/influxdb-client'
import {DeleteAPI} from '@influxdata/influxdb-client-apis'
import {url, token, org, bucket} from './env'
import {url, token, org, bucket} from './env.mjs'
const influxDB = new InfluxDB({url, token})

/*
The functionality of the DeleteAPI is fully demonstrated with
the following sequence of examples:
- write.js
- write.mjs
- query.ts
- delete.ts
- query.ts
Expand Down
9 changes: 1 addition & 8 deletions examples/env.js → examples/env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,4 @@ const username = 'my-user'
/**InfluxDB password */
const password = 'my-password'

module.exports = {
url,
token,
org,
bucket,
username,
password,
}
export {url, token, org, bucket, username, password}
46 changes: 25 additions & 21 deletions examples/follow-redirects.js → examples/follow-redirects.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
// Shows how to configure InfluxDB node.js client to follow redirects. //
/////////////////////////////////////////////////////////////////////////

const {url: targetUrl, token, org} = require('./env')
// const {InfluxDB} = require('@influxdata/influxdb-client')
const {InfluxDB} = require('../packages/core')
import {url as targetUrl, token, org} from './env.mjs'
import {InfluxDB} from '@influxdata/influxdb-client'
import {createServer} from 'node:http'
import followRedirects from 'follow-redirects'

// start a simple HTTP server that always redirects to a configured InfluxDB
const http = require('http')
const server = http.createServer((req, res) => {
const server = createServer((req, res) => {
const reqUrl = new URL(req.url, `http://${req.headers.host}`)
console.info(`Redirecting ${req.method} ${reqUrl} to ${targetUrl + req.url}`)
res.writeHead(307, {location: targetUrl + req.url})
res.end()
})
server.listen(0, 'localhost', () => {
server.listen(0, 'localhost', async () => {
const addressInfo = server.address()
console.info('Redirection HTTP server started:', addressInfo)

Expand All @@ -27,21 +27,25 @@ server.listen(0, 'localhost', () => {
transportOptions: {
// The following transport option is required in order to follow HTTP redirects in node.js.
// Browsers and deno follow redirects OOTB.
'follow-redirects': require('follow-redirects'),
'follow-redirects': followRedirects,
beforeRedirect: (options, _response, request) => {
// setup Authorization header for a redirected message,
// authorization and cookie headers are removed by follow-redirects
if (request.headers.authorization) {
options.headers.authorization = request.headers.authorization
}
},
},
}).getQueryApi(org)
queryApi
.collectRows('buckets()')
.then((data) => {
console.info('Available buckets:')
data.forEach((x) => console.info('', x.name))
console.log('\nQuery SUCCESS')
})
.catch((error) => {
console.error(error)
console.log('\nQuery ERROR')
})
.finally(() => {
server.close()
})
try {
const data = await queryApi.collectRows('buckets()')
console.info('Available buckets:')
data.forEach((x) => console.info('', x.name))
console.log('\nQuery SUCCESS')
} catch (e) {
console.error(e)
console.log('\nQuery ERROR')
} finally {
server.close()
}
})
38 changes: 18 additions & 20 deletions examples/invokableScripts.js → examples/invokableScripts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ This example shows how to use API-invokable scripts. See also
https://docs.influxdata.com/influxdb/cloud/api-guide/api-invokable-scripts/ .
*/

const {InfluxDB, HttpError} = require('@influxdata/influxdb-client')
const {
import {InfluxDB, HttpError} from '@influxdata/influxdb-client'
import {
ScriptsAPI,
FluxScriptInvocationAPI,
} = require('@influxdata/influxdb-client-apis')
const {url, token} = require('./env')
} from '@influxdata/influxdb-client-apis'
import {url, token} from './env.mjs'
import {argv} from 'node:process'

const influxDB = new InfluxDB({url, token})
const scriptsAPI = new ScriptsAPI(influxDB)
Expand Down Expand Up @@ -60,7 +61,7 @@ async function invokeScript(scriptID) {
console.log('*** Invoke example script ***')

// parse count as a first script argument or use 10
const count = Number.parseInt(require('process').argv[2] || '10')
const count = Number.parseInt(argv[2] || '10')

// execute script with count parameter
const params = {count: count}
Expand Down Expand Up @@ -96,23 +97,20 @@ async function invokeScript(scriptID) {
// console.log(response)
}

async function example() {
try {
await listScripts()
await deleteScript()
const {id} = await createScript()
await invokeScript(id)
console.log('\nFinished SUCCESS')
} catch (e) {
if (e instanceof HttpError && e.statusCode === 404) {
console.error(
`API invokable scripts are not supported by InfluxDB at ${url} .`
)
console.error('Modify env.mjs with InfluxDB Cloud URL and token.')
} else {
console.error(e)
}
console.log('\nFinished ERROR')
}

example()
.then(() => console.log('\nFinished SUCCESS'))
.catch((error) => {
if (error instanceof HttpError && error.statusCode === 404) {
console.error(
`API invokable scripts are not supported by InfluxDB at ${url} .`
)
console.error('Modify env.js with InfluxDB Cloud URL and token.')
} else {
console.error(error)
}
console.log('\nFinished ERROR')
})
37 changes: 0 additions & 37 deletions examples/onboarding.js

This file was deleted.

34 changes: 34 additions & 0 deletions examples/onboarding.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env node
/*
This example setups a new INFLUXDB database with user,organization
and bucket that can be then used in examples. All values that used
for onboarding are defined in ./env.ts .
*/

import {InfluxDB} from '@influxdata/influxdb-client'
import {SetupAPI} from '@influxdata/influxdb-client-apis'
import {url, username, password, org, bucket, token} from './env.mjs'

console.log('*** ONBOARDING ***')
const setupApi = new SetupAPI(new InfluxDB({url}))
try {
const {allowed} = await setupApi.getSetup()
if (allowed) {
await setupApi.postSetup({
body: {
org,
bucket,
username,
password,
token,
},
})
console.log(`InfluxDB '${url}' is now onboarded.`)
} else {
console.log(`InfluxDB '${url}' has been already onboarded.`)
}
console.log('\nFinished SUCCESS')
} catch (e) {
console.error(e)
console.log('\nFinished ERROR')
}
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": true,
"license": "MIT",
"scripts": {
"browser": "node scripts/server.js",
"browser": "node scripts/server.mjs",
"clean": "echo 'nothing to clean'",
"build": "echo 'nothing to build'",
"test": "echo 'run examples to test'",
Expand Down
9 changes: 4 additions & 5 deletions examples/ping.js → examples/ping.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#!/usr/bin/node
#!/usr/bin/env node
/*
This example shows how to check state InfluxDB instance.
InfluxDB OSS APIs are available through '@influxdata/influxdb-client-apis' package.

See https://docs.influxdata.com/influxdb/v2.1/api/
*/
import {InfluxDB} from '@influxdata/influxdb-client'
import {PingAPI} from '@influxdata/influxdb-client-apis'
import {url} from './env.mjs'

const {InfluxDB} = require('@influxdata/influxdb-client')
// const {PingAPI} = require('@influxdata/influxdb-client-apis')
const {PingAPI} = require('../packages/apis')
const {url} = require('./env')
const timeout = 10 * 1000 // timeout for ping

console.log('*** PING STATUS ***')
Expand Down
Loading