Skip to content

Commit 14257fe

Browse files
authored
Kg non standard environment examples (#43)
added cloudflare & nextjs examples (will add more in future) in `examples/` subdir to showcase usage of `astra-db-ts` on non-standard environments
1 parent b1f71cc commit 14257fe

File tree

18 files changed

+2825
-0
lines changed

18 files changed

+2825
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,5 @@ so you'll need to set it explicitly if you want to use the native fetch implemen
356356
On some environments, such as Cloudflare Workers, you may additionally need to use the events
357357
polyfill for the client to work properly (i.e. `npm i events`). Cloudflare's node-compat won't
358358
work here.
359+
360+
Check out the `examples/` subdirectory for some non-standard runtime examples with more info.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Your Astra DB token (AstraCS:...)
2+
ASTRA_DB_TOKEN=
3+
4+
# Your Astra DB ID (https://<id>-<region>.apps.astra.datastax.com)
5+
ASTRA_DB_ENDPOINT=
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# Logs
2+
3+
logs
4+
_.log
5+
npm-debug.log_
6+
yarn-debug.log*
7+
yarn-error.log*
8+
lerna-debug.log*
9+
.pnpm-debug.log*
10+
11+
# Diagnostic reports (https://nodejs.org/api/report.html)
12+
13+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
14+
15+
# Runtime data
16+
17+
pids
18+
_.pid
19+
_.seed
20+
\*.pid.lock
21+
22+
# Directory for instrumented libs generated by jscoverage/JSCover
23+
24+
lib-cov
25+
26+
# Coverage directory used by tools like istanbul
27+
28+
coverage
29+
\*.lcov
30+
31+
# nyc test coverage
32+
33+
.nyc_output
34+
35+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
36+
37+
.grunt
38+
39+
# Bower dependency directory (https://bower.io/)
40+
41+
bower_components
42+
43+
# node-waf configuration
44+
45+
.lock-wscript
46+
47+
# Compiled binary addons (https://nodejs.org/api/addons.html)
48+
49+
build/Release
50+
51+
# Dependency directories
52+
53+
node_modules/
54+
jspm_packages/
55+
56+
# Snowpack dependency directory (https://snowpack.dev/)
57+
58+
web_modules/
59+
60+
# TypeScript cache
61+
62+
\*.tsbuildinfo
63+
64+
# Optional npm cache directory
65+
66+
.npm
67+
68+
# Optional eslint cache
69+
70+
.eslintcache
71+
72+
# Optional stylelint cache
73+
74+
.stylelintcache
75+
76+
# Microbundle cache
77+
78+
.rpt2_cache/
79+
.rts2_cache_cjs/
80+
.rts2_cache_es/
81+
.rts2_cache_umd/
82+
83+
# Optional REPL history
84+
85+
.node_repl_history
86+
87+
# Output of 'npm pack'
88+
89+
\*.tgz
90+
91+
# Yarn Integrity file
92+
93+
.yarn-integrity
94+
95+
# dotenv environment variable files
96+
97+
.dev.vars
98+
.env.development.local
99+
.env.test.local
100+
.env.production.local
101+
.env.local
102+
103+
# parcel-bundler cache (https://parceljs.org/)
104+
105+
.cache
106+
.parcel-cache
107+
108+
# Next.js build output
109+
110+
.next
111+
out
112+
113+
# Nuxt.js build / generate output
114+
115+
.nuxt
116+
dist
117+
118+
# Gatsby files
119+
120+
.cache/
121+
122+
# Comment in the public line in if your project uses Gatsby and not Next.js
123+
124+
# https://nextjs.org/blog/next-9-1#public-directory-support
125+
126+
# public
127+
128+
# vuepress build output
129+
130+
.vuepress/dist
131+
132+
# vuepress v2.x temp and cache directory
133+
134+
.temp
135+
.cache
136+
137+
# Docusaurus cache and generated files
138+
139+
.docusaurus
140+
141+
# Serverless directories
142+
143+
.serverless/
144+
145+
# FuseBox cache
146+
147+
.fusebox/
148+
149+
# DynamoDB Local files
150+
151+
.dynamodb/
152+
153+
# TernJS port file
154+
155+
.tern-port
156+
157+
# Stores VSCode versions used for testing VSCode extensions
158+
159+
.vscode-test
160+
161+
# yarn v2
162+
163+
.yarn/cache
164+
.yarn/unplugged
165+
.yarn/build-state.yml
166+
.yarn/install-state.gz
167+
.pnp.\*
168+
169+
# wrangler project
170+
171+
.dev.vars
172+
.wrangler/

examples/cloudflare-workers/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# astra-db-ts w/ Cloudflare Workers
2+
3+
## Overview
4+
5+
`astra-db-ts` works nearly natively with the Cloudflare Workers runtime. The only detour required
6+
to make this work is to install the `events` polyfill, which is not included natively in the
7+
Workers runtime. Luckily, this is as simple as doing `npm i events` in your project.
8+
9+
This is a simple example of how it can be used to interact with an Astra database; it'll simply
10+
list out all the collections in a given database.
11+
12+
Check out the [Non-standard runtime support](../../README.md#non-standard-runtime-support) section
13+
in the main `README.md` for more information common between non-standard runtimes.
14+
15+
## Getting started
16+
17+
### Prerequisites:
18+
19+
- Make sure you have an existing Astra Database running @ [astra.datastax.com](https://astra.datastax.com/).
20+
- You'll need an API key and a database endpoint URL to get started.
21+
22+
### How to use this example:
23+
24+
1. Clone this repository to your local machine.
25+
26+
2. Run `npm install` to install the required dependencies.
27+
28+
3. Copy the `.dev.vars.example` file to `.dev.vars` and fill in the required values.
29+
30+
4. Run `npm run dev` to start the local development server.
31+
32+
5. Visit `http://localhost:8787` in your browser to see the example in action (or just press `b`).
33+
34+
### Steps to start your own project:
35+
36+
1. Use the typical `npm create cloudflare@latest` to create a new Cloudflare Workers project.
37+
38+
2. Install `@datastax/astra-db-ts` and `events` by running `npm i @datastax/astra-db-ts events`.
39+
40+
3. You should be able to use `@datastax/astra-db-ts` in your project as normal now.
41+
42+
## Full code sample
43+
44+
```ts
45+
import { DataAPIClient } from '@datastax/astra-db-ts';
46+
47+
export interface Env {
48+
ASTRA_DB_TOKEN: string,
49+
ASTRA_DB_ENDPOINT: string,
50+
}
51+
52+
export default {
53+
async fetch(_req: Request, env: Env, _ctx: ExecutionContext): Promise<Response> {
54+
// Creates the client. `astra-db-ts` can automatically infer that it should be using
55+
// the native `fetch` client under the hood for you.
56+
const client = new DataAPIClient(env.ASTRA_DB_TOKEN);
57+
const db = client.db(env.ASTRA_DB_ENDPOINT);
58+
59+
// Simple example which (attempts to) list all the collections in the database
60+
try {
61+
const collections = await db.listCollections();
62+
63+
return new Response(JSON.stringify(collections), {
64+
headers: { 'Content-Type': 'application/json' },
65+
});
66+
} catch (error: any) {
67+
return new Response(JSON.stringify({ error: error.message }), {
68+
headers: { 'Content-Type': 'application/json' },
69+
status: 500,
70+
});
71+
}
72+
},
73+
};
74+
```

0 commit comments

Comments
 (0)