@prisma/extension-accelerate
Advanced tools
Comparing version 0.0.0-experimental-35892f1 to 0.0.0-experimental-3ba6a4e
/// <reference lib="dom" /> | ||
export type RequestOptions = { | ||
export type FetchOptions = { | ||
method?: string; | ||
@@ -8,2 +8,5 @@ headers?: Record<string, string>; | ||
export type RequestResponse = { | ||
body?: { | ||
cancel(): Promise<void>; | ||
} | null; | ||
ok: boolean; | ||
@@ -25,2 +28,2 @@ url: string; | ||
*/ | ||
export declare function fetch(url: string, options?: RequestOptions): Promise<RequestResponse>; | ||
export declare function fetch(url: string, options?: FetchOptions): Promise<RequestResponse>; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { | ||
@@ -30,6 +7,9 @@ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); | ||
}; | ||
var _FakeHeaders_headers; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
var _NodeHeaders_headers; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.fetch = void 0; | ||
const https = __importStar(require("https")); | ||
const node_https_1 = __importDefault(require("node:https")); | ||
/** | ||
@@ -49,3 +29,3 @@ * Imitates `fetch` via `https` to only suit our needs, it does nothing more. | ||
// we execute the https request and build a fetch response out of it | ||
const request = https.request(url, httpsOptions, (response) => { | ||
const request = node_https_1.default.request(url, httpsOptions, (response) => { | ||
const { statusCode = 200, headers: { location }, } = response; | ||
@@ -100,2 +80,7 @@ if (statusCode >= 301 && statusCode <= 399 && location) { | ||
return { | ||
body: { | ||
cancel() { | ||
return Promise.resolve(); | ||
}, | ||
}, | ||
text: () => Promise.resolve(Buffer.concat(incomingData).toString()), | ||
@@ -107,15 +92,15 @@ json: () => Promise.resolve(JSON.parse(Buffer.concat(incomingData).toString())), | ||
url: url, | ||
headers: new FakeHeaders(headers), | ||
headers: new NodeHeaders(headers), | ||
}; | ||
} | ||
class FakeHeaders { | ||
class NodeHeaders { | ||
constructor(init = {}) { | ||
_FakeHeaders_headers.set(this, new Map()); | ||
_NodeHeaders_headers.set(this, new Map()); | ||
for (const [key, value] of Object.entries(init)) { | ||
if (typeof value === "string") { | ||
__classPrivateFieldGet(this, _FakeHeaders_headers, "f").set(key, value); | ||
__classPrivateFieldGet(this, _NodeHeaders_headers, "f").set(key, value); | ||
} | ||
else if (Array.isArray(value)) { | ||
for (const val of value) { | ||
__classPrivateFieldGet(this, _FakeHeaders_headers, "f").set(key, val); | ||
__classPrivateFieldGet(this, _NodeHeaders_headers, "f").set(key, val); | ||
} | ||
@@ -126,18 +111,21 @@ } | ||
append(name, value) { | ||
__classPrivateFieldGet(this, _FakeHeaders_headers, "f").set(name, value); | ||
__classPrivateFieldGet(this, _NodeHeaders_headers, "f").set(name, value); | ||
} | ||
delete(name) { | ||
__classPrivateFieldGet(this, _FakeHeaders_headers, "f").delete(name); | ||
__classPrivateFieldGet(this, _NodeHeaders_headers, "f").delete(name); | ||
} | ||
get(name) { | ||
return __classPrivateFieldGet(this, _FakeHeaders_headers, "f").get(name) ?? null; | ||
return __classPrivateFieldGet(this, _NodeHeaders_headers, "f").get(name) ?? null; | ||
} | ||
getSetCookie() { | ||
throw new Error("Method not implemented."); | ||
} | ||
has(name) { | ||
return __classPrivateFieldGet(this, _FakeHeaders_headers, "f").has(name); | ||
return __classPrivateFieldGet(this, _NodeHeaders_headers, "f").has(name); | ||
} | ||
set(name, value) { | ||
__classPrivateFieldGet(this, _FakeHeaders_headers, "f").set(name, value); | ||
__classPrivateFieldGet(this, _NodeHeaders_headers, "f").set(name, value); | ||
} | ||
forEach(callbackfn, thisArg) { | ||
for (const [key, value] of __classPrivateFieldGet(this, _FakeHeaders_headers, "f")) { | ||
for (const [key, value] of __classPrivateFieldGet(this, _NodeHeaders_headers, "f")) { | ||
callbackfn.call(thisArg, value, key, this); | ||
@@ -147,2 +135,2 @@ } | ||
} | ||
_FakeHeaders_headers = new WeakMap(); | ||
_NodeHeaders_headers = new WeakMap(); |
/// <reference lib="dom" /> | ||
export type RequestOptions = { | ||
export type FetchOptions = { | ||
method?: string; | ||
@@ -8,2 +8,5 @@ headers?: Record<string, string>; | ||
export type RequestResponse = { | ||
body?: { | ||
cancel(): Promise<void>; | ||
} | null; | ||
ok: boolean; | ||
@@ -25,2 +28,2 @@ url: string; | ||
*/ | ||
export declare function fetch(url: string, options?: RequestOptions): Promise<RequestResponse>; | ||
export declare function fetch(url: string, options?: FetchOptions): Promise<RequestResponse>; |
@@ -1,2 +0,2 @@ | ||
import * as https from "https"; | ||
import https from "node:https"; | ||
/** | ||
@@ -65,2 +65,7 @@ * Imitates `fetch` via `https` to only suit our needs, it does nothing more. | ||
return { | ||
body: { | ||
cancel() { | ||
return Promise.resolve(); | ||
}, | ||
}, | ||
text: () => Promise.resolve(Buffer.concat(incomingData).toString()), | ||
@@ -72,6 +77,6 @@ json: () => Promise.resolve(JSON.parse(Buffer.concat(incomingData).toString())), | ||
url: url, | ||
headers: new FakeHeaders(headers), | ||
headers: new NodeHeaders(headers), | ||
}; | ||
} | ||
class FakeHeaders { | ||
class NodeHeaders { | ||
#headers = new Map(); | ||
@@ -99,2 +104,5 @@ constructor(init = {}) { | ||
} | ||
getSetCookie() { | ||
throw new Error("Method not implemented."); | ||
} | ||
has(name) { | ||
@@ -101,0 +109,0 @@ return this.#headers.has(name); |
{ | ||
"name": "@prisma/extension-accelerate", | ||
"type": "module", | ||
"version": "0.0.0-experimental-35892f1", | ||
"version": "0.0.0-experimental-3ba6a4e", | ||
"description": "Prisma Client extension for Accelerate", | ||
"sideEffects": false, | ||
"exports": { | ||
".": { | ||
"import": "./dist/esm/index.js", | ||
"require": "./dist/cjs/index.js" | ||
} | ||
"node": { | ||
"import": "./dist/esm/entry.node.js", | ||
"require": "./dist/cjs/entry.node.js" | ||
}, | ||
"import": "./dist/esm/entry.fetch.js", | ||
"require": "./dist/cjs/entry.fetch.js", | ||
"default": "./dist/esm/entry.fetch.js" | ||
}, | ||
"types": "./dist/cjs/index.d.ts", | ||
"types": "./dist/cjs/entry.node.d.ts", | ||
"scripts": { | ||
@@ -26,12 +29,12 @@ "build": "./scripts/build.sh", | ||
"devDependencies": { | ||
"@prisma/client": "^5.0.0", | ||
"@tsconfig/esm": "^1.0.4", | ||
"@tsconfig/node14": "^1.0.3", | ||
"@tsconfig/node18-strictest-esm": "^1.0.1", | ||
"@typescript-eslint/eslint-plugin": "^5.61.0", | ||
"@typescript-eslint/parser": "^5.61.0", | ||
"@prisma/client": "^5.2.0", | ||
"@tsconfig/node-lts": "^18.12.4", | ||
"@tsconfig/node16": "^16.1.1", | ||
"@tsconfig/strictest": "^2.0.1", | ||
"@typescript-eslint/eslint-plugin": "^6.4.1", | ||
"@typescript-eslint/parser": "^6.4.1", | ||
"dotenv": "^16.3.1", | ||
"eslint": "^8.44.0", | ||
"prettier": "2.8.8", | ||
"prisma": "^5.0.0", | ||
"eslint": "^8.47.0", | ||
"prettier": "^3.0.2", | ||
"prisma": "^5.2.0", | ||
"ts-node": "^10.9.1", | ||
@@ -41,3 +44,3 @@ "typescript": "^5.1.6" | ||
"peerDependencies": { | ||
"@prisma/client": ">=5.0.0" | ||
"@prisma/client": ">=4.16.1" | ||
}, | ||
@@ -44,0 +47,0 @@ "engines": { |
# Accelerate Prisma Client extension | ||
Prisma Client extension to enable _Accelerate_. | ||
This is the package for the [Prisma Client extension](https://www.prisma.io/docs/concepts/components/prisma-client/client-extensions?utm_source=github&utm_medium=accelerate-readme) that enables usage of Prisma Accelerate. | ||
[Prisma Accelerate](https://www.prisma.io/data-platform/accelerate?utm_source=github&utm_medium=accelerate-readme) provides an external connection pool and global caching layer that you can use to speed up your database queries. | ||
It is part of the [Prisma](https://www.prisma.io?utm_source=github&utm_medium=accelerate-readme) ecosystem, alongside other tools such as: | ||
- [Prisma ORM](https://github.com/prisma/prisma): Next-generation Node.js and TypeScript ORM, supporting PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, CockroachDB, and MongoDB. | ||
- [Prisma Pulse](https://www.prisma.io/data-platform/pulse?utm_source=github&utm_medium=accelerate-readme): Real-time database events with type-safe subscriptions. | ||
Prisma is leading Data DX, a philosophy that promotes simplicity in data-driven application development. Learn more on the [Data DX manifesto](https://www.datadx.io/?utm_source=github&utm_medium=accelerate-readme). | ||
## Getting started with Accelerate | ||
### Resources | ||
You can explore Accelerate with the following resources: | ||
- [Get started](https://www.prisma.io/docs/data-platform/accelerate/getting-started?utm_source=github&utm_medium=accelerate-readme) | ||
- [Documentation](https://www.prisma.io/docs/data-platform/accelerate/what-is-accelerate?utm_source=github&utm_medium=accelerate-readme) | ||
- [Prisma Data Platform](https://console.prisma.io/login?utm_source=github&utm_medium=accelerate-readme) | ||
- [Accelerate Speed Test](https://accelerate-speed-test.prisma.io/?utm_source=github&utm_medium=accelerate-readme) | ||
### Using Accelerate | ||
#### 1. Enable Accelerate | ||
Log into [Prisma Data Platform](https://console.prisma.io/login?utm_source=github&utm_medium=accelerate-readme) and enable Accelerate for your project. | ||
#### 2. Add Accelerate to your application | ||
Replace the database connection string with the Accelerate connection string you generated in Prisma Data Platform (assuming you store your database connection string in the `DATABASE_URL` in the `.env` file): | ||
```bash | ||
DATABASE_URL="prisma://accelerate.prisma-data.net/?api_key=__API_KEY__" | ||
``` | ||
To be able to seamlessly continue to use Prisma Migrate, you can set the `directUrl` property in your `datasource`: | ||
```prisma | ||
datasource db { | ||
provider = "postgresql" | ||
url = env("DATABASE_URL") | ||
directUrl = env("DIRECT_DATABASE_URL") | ||
} | ||
``` | ||
And then add the URL that connects directly to your database as the value for `DIRECT_DATABASE_URL` : | ||
```prisma | ||
DATABASE_URL="prisma://accelerate.prisma-data.net/?api_key=__API_KEY__" | ||
DIRECT_DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE" | ||
``` | ||
Finally, extend your Prisma Client instance with the Accelerate extension to enable Accelerate’s connection pool: | ||
```ts | ||
import { PrismaClient } from "@prisma/client/edge"; | ||
import { withAccelerate } from "@prisma/extension-accelerate"; | ||
const prisma = new PrismaClient().$extends(withAccelerate()); | ||
``` | ||
#### 3. Add caching to your Accelerate queries | ||
You can optionally configure caching on a per-query level using the `ttl` (Time-To-Live) and `swl` (Stale-While-Revalidate) options: | ||
```ts | ||
await prisma.user.findMany({ | ||
cacheStrategy: { | ||
ttl: 3_600, | ||
swr: 500, | ||
}, | ||
}); | ||
``` |
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
73779
28
1358
77
8