@vercel/postgres
Advanced tools
Comparing version 0.1.0-canary.22 to 0.1.0-canary.25
{ | ||
"name": "@vercel/postgres", | ||
"version": "0.1.0-canary.22", | ||
"version": "0.1.0-canary.25", | ||
"description": "Connect to Vercel Postgres databases on the Edge", | ||
@@ -8,3 +8,3 @@ "homepage": "https://vercel.com", | ||
"type": "git", | ||
"url": "https://github.com/vercel/vercel.git", | ||
"url": "https://github.com/vercel/postgres.git", | ||
"directory": "packages/postgres" | ||
@@ -16,45 +16,23 @@ }, | ||
".": { | ||
"types": "./index.d.ts", | ||
"types": "./dist/index.d.ts", | ||
"import": { | ||
"edge-light": "./index.js", | ||
"node": "./index-node.js", | ||
"default": "./index-node.js" | ||
"edge-light": "./dist/index.js", | ||
"node": "./dist/index-node.js", | ||
"default": "./dist/index-node.js" | ||
}, | ||
"require": { | ||
"edge-light": "./index.cjs", | ||
"node": "./index-node.cjs", | ||
"default": "./index-node.cjs" | ||
"edge-light": "./dist/index.cjs", | ||
"node": "./dist/index-node.cjs", | ||
"default": "./dist/index-node.cjs" | ||
} | ||
}, | ||
"./kysely": { | ||
"types": "./kysely.d.ts", | ||
"import": { | ||
"edge-light": "./kysely.js", | ||
"node": "./kysely-node.js", | ||
"default": "./kysely-node.js" | ||
}, | ||
"require": { | ||
"edge-light": "./kysely.cjs", | ||
"node": "./kysely-node.cjs", | ||
"default": "./kysely-node.cjs" | ||
} | ||
} | ||
}, | ||
"main": "./index.cjs", | ||
"module": "./index.js", | ||
"types": "./index.d.ts", | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"*.js", | ||
"*.cjs", | ||
"*.d.ts" | ||
"dist/*.js", | ||
"dist/*.cjs", | ||
"dist/*.d.ts" | ||
], | ||
"lint-staged": { | ||
"*": [ | ||
"prettier --ignore-unknown --write" | ||
], | ||
"*.{js,jsx,ts,tsx}": [ | ||
"eslint --max-warnings=0 --fix" | ||
] | ||
}, | ||
"prettier": "@vercel/style-guide/prettier", | ||
"jest": { | ||
@@ -78,25 +56,11 @@ "preset": "ts-jest", | ||
"@types/ws": "8.5.4", | ||
"@vercel/style-guide": "4.0.2", | ||
"eslint": "8.25.0", | ||
"husky": "8.0.1", | ||
"jest": "29.2.1", | ||
"kysely": "0.24.2", | ||
"lint-staged": "13.0.3", | ||
"prettier": "2.7.1", | ||
"prettier": "2.8.8", | ||
"ts-jest": "29.0.3", | ||
"tsup": "6.3.0", | ||
"typescript": "4.8.4" | ||
"typescript": "4.8.4", | ||
"eslint-config-custom": "0.0.0", | ||
"tsconfig": "0.0.0" | ||
}, | ||
"peerDependencies": { | ||
"kysely": "0.24.2", | ||
"ws": "8.13.0" | ||
}, | ||
"peerDependenciesMeta": { | ||
"kysely": { | ||
"optional": true | ||
}, | ||
"ws": { | ||
"optional": true | ||
} | ||
}, | ||
"engines": { | ||
@@ -106,12 +70,9 @@ "node": ">=14.6" | ||
"scripts": { | ||
"build": "tsup && cp ./dist/* .", | ||
"eslint-check": "eslint --max-warnings=0 .", | ||
"precommit": "lint-staged", | ||
"build": "tsup", | ||
"lint": "eslint \"**/*.ts\"", | ||
"prettier-check": "prettier --check .", | ||
"publint": "npx publint --yes", | ||
"release": "pnpm build && changeset publish", | ||
"test": "jest --env @edge-runtime/jest-environment .test.ts && jest --env node .test.ts", | ||
"type-check": "tsc --noEmit", | ||
"version-packages": "changeset version" | ||
"type-check": "tsc --noEmit" | ||
} | ||
} |
123
README.md
@@ -7,6 +7,6 @@ # @vercel/postgres 🚧 | ||
**Note:** If you want to use an ORM instead of writing your own queries, see [@vercel/postgres-kysely](https://npmjs.org/package/@vercel/postgres-kysely). | ||
### Install | ||
**Note:** If your project is using TypeScript >=5.0.0 and your project can support it, we recommend setting `moduleResolution: "bundler"` in your `tsconfig.json`. | ||
```bash | ||
@@ -16,93 +16,6 @@ pnpm install @vercel/postgres | ||
### [RECOMMENDED] Using an ORM (Kysely) | ||
### Create a Pool | ||
[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: | ||
If you need a `pg` `Pool` object, you can use the `createPool` function. | ||
```bash | ||
pnpm i kysely | ||
``` | ||
Specify a schema: | ||
```typescript | ||
import { Generated, ColumnType } from 'kysely'; | ||
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; | ||
} | ||
``` | ||
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 { createKyselyPool } from '@vercel/postgres/kysely'; | ||
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(); | ||
``` | ||
**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`: | ||
@@ -134,5 +47,5 @@ | ||
### Create a Raw Client | ||
### Create a Client | ||
If you need a raw `pg` `Client` object, you can use the `createPool` function. | ||
If you need a `pg` `Client` object, you can use the `createClient` function. | ||
@@ -144,7 +57,19 @@ Automatically uses `process.env.POSTGRES_URL_NON_POOLING`: | ||
const client = createClient(); | ||
/** | ||
* Clients cannot be reused, so you have to create them, connect them, and disconnect them | ||
* per query. This is why you should use a pool unless you explicitly need a single client. | ||
*/ | ||
async function queryPosts() { | ||
const client = createClient(); | ||
const { rows, fields } = await client.query( | ||
'SELECT * from POSTS WHERE likes > 100;', | ||
); | ||
await client.connect(); | ||
try { | ||
const { rows, fields } = await client.query( | ||
'SELECT * from POSTS WHERE likes > 100;', | ||
); | ||
} finally { | ||
await client.end(); | ||
} | ||
} | ||
``` | ||
@@ -160,6 +85,2 @@ | ||
}); | ||
const { rows, fields } = await client.query( | ||
'SELECT * from POSTS WHERE likes > 100;', | ||
); | ||
``` | ||
@@ -166,0 +87,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 3 instances in 1 package
4
15
3
11304
12
36
102
1