Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/neondatabase-labs/neon-vercel-knex
Example use of Neon serverless driver on Vercel Edge Functions with Knex.js
https://github.com/neondatabase-labs/neon-vercel-knex
knex knexjs neon postgis postgresql serverless vercel vercel-edge-functions
Last synced: about 1 month ago
JSON representation
Example use of Neon serverless driver on Vercel Edge Functions with Knex.js
- Host: GitHub
- URL: https://github.com/neondatabase-labs/neon-vercel-knex
- Owner: neondatabase-labs
- Created: 2023-07-18T13:38:35.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-05-13T20:53:16.000Z (9 months ago)
- Last Synced: 2024-12-19T01:44:51.110Z (about 1 month ago)
- Topics: knex, knexjs, neon, postgis, postgresql, serverless, vercel, vercel-edge-functions
- Language: JavaScript
- Homepage: https://neon.tech
- Size: 198 KB
- Stars: 1
- Watchers: 12
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# neon-vercel-knex
This repo demonstrates using [Knex.js](https://knexjs.org/) with [Neon's serverless driver](https://www.npmjs.com/package/@neondatabase/serverless) on [Vercel](https://vercel.com/) Edge Functions.
We implement a simple app that generates a JSON listing of the user's nearest 10 UNESCO World Heritage sites via IP geolocation (data copyright © 1992 – 2022 [UNESCO/World Heritage Centre](https://whc.unesco.org/en/syndication/)).
The Vercel Edge Function API endpoint source is found in `./api-src`.
### How it works
Knex.js tries to import some Node.js modules that aren't available on Edge Functions. We provide shims for these modules in `./shims`, and these are listed as `file:` packages in `package.json`. The `assert`, `events` and `util.promisify` packages are also installed to support Knex.js.
To make the shims work with Edge Functions requires a build step. We use `esbuild` to bundle the source files in `./api-src` with all dependencies, depositing the output files in `./api`. The build command is found in `package.json` > `"scripts"` > `"build"`.
Note that the line `export const config = { runtime: 'edge' }`, which is required to create an Edge function, is not present in the source files. Because it must take this specific form, it is instead appended at the end of the build process. It can be edited (e.g. to add a `regions` key) in the `--footer:js=` argument of the `esbuild` build command.
## Deploy
* Ensure the `psql` client is installed.
* Create a Neon database and make a note of the connection string from the [Neon console](https://console.neon.tech/).
* Clone this repo, then:
```bash
# get dependencies
npm install
npm install -g vercel@latest# set up Vercel
npx vercel login
npx vercel link# create DATABASE_URL environment variable, remote and local
npx vercel env add DATABASE_URL # paste in the connection string: postgres://...
npx vercel env pull .env.local # now bring it down into ./.env.local for local use# create the schema and copy data to DB
(source .env.local \
&& curl -s https://gist.githubusercontent.com/jawj/a8d53ff339707c65128af83b4783f4fe/raw/45dbcc819b00ecb72f80b0cf91e01b3d055662b5/whc-sites-2021.psql \
| psql $DATABASE_URL)# test ...
npx vercel dev# ... and deploy
npx vercel deploy
```## Feedback and support
Please visit [Neon Community](https://community.neon.tech/) or [Support](https://neon.tech/docs/introduction/support).