Socket
Socket
Sign inDemoInstall

@vercel/postgres

Package Overview
Dependencies
Maintainers
8
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vercel/postgres - npm Package Compare versions

Comparing version 0.1.0-canary.5 to 0.1.0-canary.6

2

dist/index.js

@@ -1,1 +0,1 @@

import{Pool as r,Client as t}from"@neondatabase/serverless";var i={},n=null;function a(e){return new t({...e})}function l(e){return new r({...e})}var u=new Proxy({},{get(e,o){if(!n){if(!process.env.POSTGRES_URL)throw new Error("@vercel/postgres: Missing required environment variable POSTGRES_URL");n=l({...i,connectionString:process.env.POSTGRES_URL})}return Reflect.get(n,o)}}),f=u;export{a as createClient,l as createPool,f as default,u as postgres};
import{Client as l}from"@neondatabase/serverless";var p={pool:"POSTGRES_URL",direct:"POSTGRES_URL_NON_POOLING"};function e(n="pool"){let o=process.env[p[n]];return o==="undefined"&&(o=void 0),o}function r(n){return n.includes("-pooler.")}function i(n){return!r(n)}var t=class extends Error{constructor(s,c){super(`VercelPostgresError - '${s}': ${c}`);this.code=s;this.name="VercelPostgresError"}};function _(n){let o=(n==null?void 0:n.connectionString)??e("direct");if(!o)throw new t("missing_connection_string","You did not supply a 'connectionString' and no 'POSTGRES_URL_NON_POOLING' env var was found.");if(!i(o))throw new t("invalid_connection_string","This connection string is meant to be used with a pooled connection. Try `createPool()` instead.");return new l({...n,connectionString:o})}import{Pool as g}from"@neondatabase/serverless";function w(n){let o=(n==null?void 0:n.connectionString)??e("pool");if(!o)throw new t("missing_connection_string","You did not supply a 'connectionString' and no 'POSTGRES_URL' env var was found.");if(!r(o))throw new t("invalid_connection_string","This connection string is meant to be used with a direct connection. Make sure to use a pooled connection string or try `createClient()` instead.");return new g({...n,connectionString:o})}export{_ as createClient,w as createPool,e as postgresConnectionString};
{
"name": "@vercel/postgres",
"version": "0.1.0-canary.5",
"version": "0.1.0-canary.6",
"description": "Connect to Vercel Postgres databases on the Edge",

@@ -24,2 +24,12 @@ "homepage": "https://vercel.com",

}
},
"./kysely": {
"import": {
"node": "./dist/kysely.js",
"default": "./dist/kysely.js"
},
"require": {
"node": "./dist/kysely.cjs",
"default": "./dist/kysely.cjs"
}
}

@@ -31,3 +41,5 @@ },

"files": [
"dist"
"dist/*.js",
"dist/*.cjs",
"!dist/**/*.test.ts"
],

@@ -61,2 +73,3 @@ "lint-staged": {

"jest": "29.2.1",
"kysely": "0.24.2",
"lint-staged": "13.0.3",

@@ -68,2 +81,5 @@ "prettier": "2.7.1",

},
"peerDependencies": {
"kysely": "0.24.2"
},
"engines": {

@@ -70,0 +86,0 @@ "node": ">=14.6"

@@ -10,36 +10,113 @@ # @vercel/postgres 🚧

```bash
npm install @vercel/postgres
pnpm install @vercel/postgres
```
### Basic Usage
### [RECOMMENDED] Using an ORM (Kysely)
#### Automatically Read Environment Variables
[Kysely](https://github.com/kysely-org/kysely) is supported out of the box. In order to use [Kysely](https://github.com/kysely-org/kysely), you need to import it and install `kysely` as a dependency for your project:
**Default Export**
```bash
pnpm i kysely
```
Specify a schema:
```typescript
import postgres from '@vercel/postgres';
import { Generated, ColumnType } from 'kysely';
await postgres.query('SELECT * from POSTS WHERE likes > 100;');
interface PersonTable {
// Columns that are generated by the database should be marked
// using the `Generated` type. This way they are automatically
// made optional in inserts and updates.
id: Generated<number>;
first_name: string;
gender: 'male' | 'female' | 'other';
// If the column is nullable in the database, make its type nullable.
// Don't use optional properties. Optionality is always determined
// automatically by Kysely.
last_name: string | null;
// You can specify a different type for each operation (select, insert and
// update) using the `ColumnType<SelectType, InsertType, UpdateType>`
// wrapper. Here we define a column `modified_at` that is selected as
// a `Date`, can optionally be provided as a `string` in inserts and
// can never be updated:
modified_at: ColumnType<Date, string | undefined, never>;
}
interface PetTable {
id: Generated<number>;
name: string;
owner_id: number;
species: 'dog' | 'cat';
}
interface MovieTable {
id: Generated<string>;
stars: number;
}
// Keys of this interface are table names.
interface Database {
person: PersonTable;
pet: PetTable;
movie: MovieTable;
}
```
**Named Export**
Now you can use this type by creating a new pooled [Kysely](https://github.com/kysely-org/kysely) connection. Note: your database connection
string will be automatically retrieved from your environment variables. This uses `createPool` from
`@vercel/postgres` under the hood.
```typescript
import { postgres } from '@vercel/postgres';
import { createKyselyPool } from '@vercel/postgres/kysely';
await postgres.query('SELECT * from POSTS WHERE likes > 100;');
interface Database {
person: PersonTable;
pet: PetTable;
movie: MovieTable;
}
const db = createKyselyPool<Database>();
await db
.insertInto('pet')
.values({ name: 'Catto', species: 'cat', owner_id: id })
.execute();
const person = await db
.selectFrom('person')
.innerJoin('pet', 'pet.owner_id', 'person.id')
.select(['first_name', 'pet.name as pet_name'])
.where('person.id', '=', id)
.executeTakeFirst();
```
#### Set Connection String Manually (Client or Pool)
**Note:** If you would like to use a `Client` with [Kysely](https://github.com/kysely-org/kysely) instead, call `createKyselyClient`. However, it is recommended to use pooling.
> For more information on using [Kysely](https://github.com/kysely-org/kysely), checkout the docs: https://github.com/kysely-org/kysely
### Create a Raw Pool
If you need a raw `pg` `Pool` object, you can use the `createPool` function.
Automatically uses `process.env.POSTGRES_URL`:
```typescript
import { createClient, createPool } from '@vercel/postgres';
import { createPool } from '@vercel/postgres';
const client = createClient({
connectionString: process.env.SOME_POSTGRES_CONNECTION_STRING,
});
const pool = createPool();
await client.query('SELECT * from POSTS WHERE likes > 100;');
const { rows, fields } = await pool.query(
'SELECT * from POSTS WHERE likes > 100;',
);
```
To specify a connection string:
```typescript
import { createPool } from '@vercel/postgres';
const pool = createPool({

@@ -49,48 +126,55 @@ connectionString: process.env.SOME_POSTGRES_CONNECTION_STRING,

await pool.query('SELECT * from POSTS WHERE likes > 100;');
const { rows, fields } = await pool.query(
'SELECT * from POSTS WHERE likes > 100;',
);
```
### Parameterized Query
### Create a Raw Client
If you need a raw `pg` `Client` object, you can use the `createPool` function.
Automatically uses `process.env.POSTGRES_URL_NON_POOLING`:
```typescript
import { postgres } from '@vercel/postgres';
import { createClient } from '@vercel/postgres';
await postgres.query('SELECT * from POSTS WHERE author=$1;', ['rauchg']);
const client = createClient();
const { rows, fields } = await client.query(
'SELECT * from POSTS WHERE likes > 100;',
);
```
### Connection Config
To specify a connection string:
When using the `createClient` or `createPool` functions, you can pass in additional options to configuration object alongside the connection string that conforms to `PostgresConfig` or `PostgresPoolConfig`, respectively:
```typescript
import { createClient } from '@vercel/postgres';
const client = createClient({
connectionString: process.env.SOME_POSTGRES_CONNECTION_STRING,
});
const { rows, fields } = await client.query(
'SELECT * from POSTS WHERE likes > 100;',
);
```
### Get the connection url
If you just want the connection URL, you can call `postgresConnectionString(type: 'pool' | 'direct'): string;`. This will read from your environment variables. For the `pool` type, it will look for the `POSTGRES_URL` environment variables. For the `direct` type, it will look for the `POSTGRES_URL_NON_POOLING` environment variables.
```typescript
export interface PostgresConfig {
connectionString: string;
keepAlive?: boolean | undefined;
statement_timeout?: false | number | undefined;
query_timeout?: number | undefined;
keepAliveInitialDelayMillis?: number | undefined;
idle_in_transaction_session_timeout?: number | undefined;
application_name?: string | undefined;
connectionTimeoutMillis?: number | undefined;
max?: number | undefined;
min?: number | undefined;
idleTimeoutMillis?: number | undefined;
log?: ((...messages: unknown[]) => void) | undefined;
allowExitOnIdle?: boolean | undefined;
maxUses?: number | undefined;
}
import { postgresConnectionString } from '@vercel/postgres';
export interface PostgresPoolConfig extends PostgresConfig {
max?: number | undefined;
min?: number | undefined;
idleTimeoutMillis?: number | undefined;
log?: ((...messages: unknown[]) => void) | undefined;
allowExitOnIdle?: boolean | undefined;
maxUses?: number | undefined;
}
const pooledConnectionString = postgresConnectionString('pool');
const directConnectionString = postgresConnectionString('direct');
```
### Connection Config
When using the `createClient` or `createPool` functions, you can pass in additional options alongside the connection string that conforms to `VercelPostgresClientConfig` or `VercelPostgresPoolConfig`.
### Documentation
The `@vercel/postgres` package uses the `pg` under the hood. For
The `@vercel/postgres` package uses the `pg` package. For
more detailed documentation, checkout [node-postgres](https://node-postgres.com/).

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc