@prisma/extension-accelerate
Advanced tools
Comparing version 0.0.0-experimental-9ab6d60 to 0.0.0-experimental-a2eb2bb
@@ -0,36 +1,146 @@ | ||
/// <reference lib="dom" /> | ||
import PrismaDefault, { type Prisma } from "@prisma/client/scripts/default-index.js"; | ||
import type { Types } from "@prisma/client/runtime"; | ||
type QueryOptionsCb = Parameters<Types.Extensions.UserArgs["query"]["someModel"]["someAction"]>[0]; | ||
export interface PrismaCacheStrategy { | ||
/** TODO */ | ||
/** | ||
* Specifies the caching parameters for Accelerate. | ||
* | ||
* `cacheStrategy` only applies when used with an Accelerate connection string. | ||
*/ | ||
readonly cacheStrategy?: { | ||
/** TODO */ | ||
/** | ||
* `swr` is short for Stale-While-Revalidate. | ||
* | ||
* `swr` defines the number of seconds that Accelerate may serve _stale_ cache data. | ||
* _Stale_ data is a cache hit, but the cache will be refreshed in the background by Accelerate. | ||
* The Prisma operation will not be blocked while data is refreshed. | ||
* | ||
* Use `swr` to reduce the latency of accessing the data while still maintaining | ||
* a more up-to-date value in the Accelerate cache. | ||
* `swr` without `ttl` will not reduce database load since Accelerate will | ||
* run the query in the background. | ||
* | ||
* `swr` can be combined with `ttl`. | ||
* `swr` applies **after** `ttl` has expired. | ||
* The total number of seconds data will be cached is `ttl + swr`. | ||
* | ||
* `swr` only applies when used with an Accelerate connection string. | ||
*/ | ||
readonly swr?: number; | ||
/** | ||
* `ttl` is short for Time-to-Live. | ||
* | ||
* `ttl` defines the number of seconds that Accelerate may serve _fresh_ cache data. | ||
* _Fresh_ data is a cache hit and will not execute the query against the database. | ||
* | ||
* Use `ttl` to reduce database load and latency for data that does not | ||
* require frequent updates. | ||
* `ttl` without `swr` will incur a blocking database query for the first | ||
* request after `ttl` expires. | ||
* | ||
* It's recommended to combine `ttl` and `swr` to maintain low latency while | ||
* Accelerate refreshes cached data in the background. | ||
* `swr` applies **after** `ttl` has expired. | ||
* The total number of seconds data will be cached is `ttl + swr`. | ||
* | ||
* `ttl` only applies when used with an Accelerate connection string. | ||
*/ | ||
readonly ttl?: number; | ||
}; | ||
} | ||
declare function withCacheHeaders(params: QueryOptionsCb): Promise<any>; | ||
interface AccelerateInfo { | ||
/** | ||
* The cache status of the response. | ||
* * `ttl` indicates a cache hit within the `ttl` duration and no database query was executed | ||
* * `swr` indicates a cache hit within the `swr` duration and the data is being refreshed by Accelerate in the background | ||
* * `miss` indicates that both `ttl` and `swr` have expired and the database query was executed by the request | ||
* * `none` indicates that no cache strategy was specified and the database query was executed by the request | ||
*/ | ||
cacheStatus: "ttl" | "swr" | "miss" | "none"; | ||
/** | ||
* The date the response was last refreshed. | ||
*/ | ||
lastModified: Date; | ||
/** | ||
* The datacenter region that received the request. | ||
*/ | ||
region: string; | ||
/** | ||
* Unique identifier of the request. Useful for troubleshooting. | ||
*/ | ||
requestId: string; | ||
/** | ||
* The unique signature of the Prisma operation. | ||
*/ | ||
signature: string; | ||
} | ||
interface AcceleratePromise<T> extends Prisma.PrismaPromise<T> { | ||
withAccelerateInfo(): Prisma.PrismaPromise<{ | ||
data: T; | ||
info: AccelerateInfo | null; | ||
}>; | ||
} | ||
declare const _default: (client: any) => PrismaDefault.PrismaClientExtends<Types.Extensions.InternalArgs<{}, { | ||
$allModels: { | ||
aggregate<T, A>(this: T, args: Prisma.Exact<A, Types.Public.Args<T, "aggregate"> & PrismaCacheStrategy>): Promise<Prisma.Result<T, A, "aggregate">>; | ||
count<T_1, A_1>(this: T_1, args?: Prisma.Exact<A_1, Types.Public.Args<T_1, "count"> & PrismaCacheStrategy> | undefined): Promise<Prisma.Result<T_1, A_1, "count">>; | ||
findFirst<T_2, A_2>(this: T_2, args?: Prisma.Exact<A_2, Types.Public.Args<T_2, "findFirst"> & PrismaCacheStrategy> | undefined): Promise<Prisma.Result<T_2, A_2, "findFirst">>; | ||
findFirstOrThrow<T_3, A_3>(this: T_3, args?: Prisma.Exact<A_3, Types.Public.Args<T_3, "findFirstOrThrow"> & PrismaCacheStrategy> | undefined): Promise<Prisma.Result<T_3, A_3, "findFirstOrThrow">>; | ||
findMany<T_4, A_4>(this: T_4, args?: Prisma.Exact<A_4, Types.Public.Args<T_4, "findMany"> & PrismaCacheStrategy> | undefined): Promise<Prisma.Result<T_4, A_4, "findMany">>; | ||
findUnique<T_5, A_5>(this: T_5, args: Prisma.Exact<A_5, Types.Public.Args<T_5, "findUnique"> & PrismaCacheStrategy>): Promise<Prisma.Result<T_5, A_5, "findUnique">>; | ||
findUniqueOrThrow<T_6, A_6>(this: T_6, args: Prisma.Exact<A_6, Types.Public.Args<T_6, "findUniqueOrThrow"> & PrismaCacheStrategy>): Promise<Prisma.Result<T_6, A_6, "findUniqueOrThrow">>; | ||
groupBy<T_7, A_7>(this: T_7, args: Prisma.Exact<A_7, Types.Public.Args<T_7, "groupBy"> & PrismaCacheStrategy>): Promise<Prisma.Result<T_7, A_7, "groupBy">>; | ||
aggregate<T, A>(this: T, args: Prisma.Exact<A, Types.Public.Args<T, "aggregate"> & PrismaCacheStrategy>): AcceleratePromise<Prisma.Result<T, A, "aggregate">>; | ||
count<T_1, A_1>(this: T_1, args?: Prisma.Exact<A_1, Types.Public.Args<T_1, "count"> & PrismaCacheStrategy> | undefined): AcceleratePromise<Prisma.Result<T_1, A_1, "count">>; | ||
findFirst<T_2, A_2>(this: T_2, args?: Prisma.Exact<A_2, Types.Public.Args<T_2, "findFirst"> & PrismaCacheStrategy> | undefined): AcceleratePromise<Prisma.Result<T_2, A_2, "findFirst"> | null>; | ||
findFirstOrThrow<T_3, A_3>(this: T_3, args?: Prisma.Exact<A_3, Types.Public.Args<T_3, "findFirstOrThrow"> & PrismaCacheStrategy> | undefined): AcceleratePromise<Prisma.Result<T_3, A_3, "findFirstOrThrow">>; | ||
findMany<T_4, A_4>(this: T_4, args?: Prisma.Exact<A_4, Types.Public.Args<T_4, "findMany"> & PrismaCacheStrategy> | undefined): AcceleratePromise<Prisma.Result<T_4, A_4, "findMany">>; | ||
findUnique<T_5, A_5>(this: T_5, args: Prisma.Exact<A_5, Types.Public.Args<T_5, "findUnique"> & PrismaCacheStrategy>): AcceleratePromise<Prisma.Result<T_5, A_5, "findUnique"> | null>; | ||
findUniqueOrThrow<T_6, A_6>(this: T_6, args: Prisma.Exact<A_6, Types.Public.Args<T_6, "findUniqueOrThrow"> & PrismaCacheStrategy>): AcceleratePromise<Prisma.Result<T_6, A_6, "findUniqueOrThrow">>; | ||
groupBy<T_7, A_7>(this: T_7, args: Prisma.Exact<A_7, Types.Public.Args<T_7, "groupBy"> & PrismaCacheStrategy>): AcceleratePromise<Prisma.Result<T_7, A_7, "groupBy">>; | ||
}; | ||
}, {}, {}> & Types.Extensions.InternalArgs<{}, {}, { | ||
$allModels: { | ||
aggregate: typeof withCacheHeaders; | ||
count: typeof withCacheHeaders; | ||
findFirst: typeof withCacheHeaders; | ||
findFirstOrThrow: typeof withCacheHeaders; | ||
findMany: typeof withCacheHeaders; | ||
findUnique: typeof withCacheHeaders; | ||
findUniqueOrThrow: typeof withCacheHeaders; | ||
groupBy: typeof withCacheHeaders; | ||
aggregate: (params: { | ||
model?: string | undefined; | ||
operation: string; | ||
args: object; | ||
query: (args: object) => Promise<unknown>; | ||
}) => Promise<any>; | ||
count: (params: { | ||
model?: string | undefined; | ||
operation: string; | ||
args: object; | ||
query: (args: object) => Promise<unknown>; | ||
}) => Promise<any>; | ||
findFirst: (params: { | ||
model?: string | undefined; | ||
operation: string; | ||
args: object; | ||
query: (args: object) => Promise<unknown>; | ||
}) => Promise<any>; | ||
findFirstOrThrow: (params: { | ||
model?: string | undefined; | ||
operation: string; | ||
args: object; | ||
query: (args: object) => Promise<unknown>; | ||
}) => Promise<any>; | ||
findMany: (params: { | ||
model?: string | undefined; | ||
operation: string; | ||
args: object; | ||
query: (args: object) => Promise<unknown>; | ||
}) => Promise<any>; | ||
findUnique: (params: { | ||
model?: string | undefined; | ||
operation: string; | ||
args: object; | ||
query: (args: object) => Promise<unknown>; | ||
}) => Promise<any>; | ||
findUniqueOrThrow: (params: { | ||
model?: string | undefined; | ||
operation: string; | ||
args: object; | ||
query: (args: object) => Promise<unknown>; | ||
}) => Promise<any>; | ||
groupBy: (params: { | ||
model?: string | undefined; | ||
operation: string; | ||
args: object; | ||
query: (args: object) => Promise<unknown>; | ||
}) => Promise<any>; | ||
}; | ||
}, {}> & Types.Extensions.DefaultArgs>; | ||
export default _default; |
@@ -6,2 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/// <reference lib="dom" /> | ||
// importing default is needed for ESM compatibility | ||
@@ -12,15 +13,64 @@ // default-index is a CJS file, so named exports are not resolved | ||
const EXTENSION_NAME = "@prisma/extension-accelerate"; | ||
function withCacheHeaders(params) { | ||
const { args } = params; | ||
const { cacheStrategy, ...rest } = args; | ||
const { __internalParams, query } = params; | ||
__internalParams.headers = { | ||
...__internalParams.headers, | ||
"cache-control": cacheStrategy | ||
? `max-age=${cacheStrategy.ttl};stale-while-revalidate=${cacheStrategy.swr}` | ||
: `no-cache`, | ||
function makeWithCacheHeaders() { | ||
let machineHint = undefined; | ||
return async (params) => { | ||
const { args } = params; | ||
const { cacheStrategy, __accelerateInfo = false, ...rest } = args; | ||
let info = null; | ||
const { __internalParams, query } = params; | ||
__internalParams.customDataProxyFetch = (fetch) => { | ||
return async (url, options) => { | ||
const cacheControl = new Array(); | ||
if (typeof cacheStrategy?.ttl === "number") { | ||
cacheControl.push(`max-age=${cacheStrategy.ttl}`); | ||
} | ||
if (typeof cacheStrategy?.swr === "number") { | ||
cacheControl.push(`stale-while-revalidate=${cacheStrategy.swr}`); | ||
} | ||
options.headers = { | ||
...options.headers, | ||
"cache-control": cacheControl.length > 0 ? cacheControl.join(",") : `no-cache`, | ||
}; | ||
if (machineHint) { | ||
options.headers["accelerate-query-engine-jwt"] = machineHint; | ||
} | ||
const response = await fetch(url, options); | ||
// Response is not available in Node 16 and lower, so we need to detect it | ||
if ("Response" in globalThis && response instanceof Response) { | ||
// on non-Node runtimes, response will be a Response instance | ||
info = { | ||
cacheStatus: response.headers.get("accelerate-cache-status"), | ||
lastModified: new Date(response.headers.get("last-modified") ?? ""), | ||
region: response.headers.get("cf-ray")?.split("-")[1] ?? "unspecified", | ||
requestId: response.headers.get("cf-ray") ?? "unspecified", | ||
signature: response.headers.get("accelerate-signature") ?? "unspecified", | ||
}; | ||
machineHint = | ||
response.headers.get("accelerate-query-engine-jwt") ?? undefined; | ||
} | ||
else if (typeof response === "object") { | ||
// in Node, response will be an object | ||
info = { | ||
cacheStatus: response.headers["accelerate-cache-status"], | ||
lastModified: new Date(response.headers["last-modified"]), | ||
region: response.headers["cf-ray"]?.split("-")[1] ?? "unspecified", | ||
requestId: response.headers["cf-ray"] ?? "unspecified", | ||
signature: response.headers["accelerate-signature"] ?? "unspecified", | ||
}; | ||
machineHint = response.headers["accelerate-query-engine-jwt"]; | ||
} | ||
return response; | ||
}; | ||
}; | ||
if (__accelerateInfo) { | ||
const data = await query(rest, __internalParams); | ||
return { data, info }; | ||
} | ||
else { | ||
return query(rest, __internalParams); | ||
} | ||
}; | ||
return query(rest, __internalParams); | ||
} | ||
exports.default = default_index_js_1.default.Prisma.defineExtension((client) => { | ||
const withCacheHeaders = makeWithCacheHeaders(); | ||
const xclient = client.$extends({ | ||
@@ -48,32 +98,88 @@ name: EXTENSION_NAME, | ||
aggregate(args) { | ||
const ctx = this; | ||
return xclient[ctx.name].aggregate(args); | ||
const ctx = default_index_js_1.default.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].aggregate(args), { | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].aggregate({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
count(args) { | ||
const ctx = this; | ||
return xclient[ctx.name].count(args); | ||
const ctx = default_index_js_1.default.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].count(args), { | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].count({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
findFirst(args) { | ||
const ctx = this; | ||
return xclient[ctx.name].findFirst(args); | ||
const ctx = default_index_js_1.default.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].findFirst(args), { | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].findFirst({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
findFirstOrThrow(args) { | ||
const ctx = this; | ||
return xclient[ctx.name].findFirstOrThrow(args); | ||
const ctx = default_index_js_1.default.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].findFirstOrThrow(args), { | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].findFirstOrThrow({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
findMany(args) { | ||
const ctx = this; | ||
return xclient[ctx.name].findMany(args); | ||
const ctx = default_index_js_1.default.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].findMany(args), { | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].findMany({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
findUnique(args) { | ||
const ctx = this; | ||
return xclient[ctx.name].findUnique(args); | ||
const ctx = default_index_js_1.default.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].findUnique(args), { | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].findUnique({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
findUniqueOrThrow(args) { | ||
const ctx = this; | ||
return xclient[ctx.name].findUniqueOrThrow(args); | ||
const ctx = default_index_js_1.default.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].findUniqueOrThrow(args), { | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].findUniqueOrThrow({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
groupBy(args) { | ||
const ctx = this; | ||
return xclient[ctx.name].groupBy(args); | ||
const ctx = default_index_js_1.default.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].groupBy(args), { | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].groupBy({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
@@ -80,0 +186,0 @@ }, |
@@ -1,11 +0,146 @@ | ||
import PrismaDefault from "@prisma/client/scripts/default-index.js"; | ||
/// <reference lib="dom" /> | ||
import PrismaDefault, { type Prisma } from "@prisma/client/scripts/default-index.js"; | ||
import type { Types } from "@prisma/client/runtime"; | ||
export interface PrismaCacheStrategy { | ||
/** TODO */ | ||
/** | ||
* Specifies the caching parameters for Accelerate. | ||
* | ||
* `cacheStrategy` only applies when used with an Accelerate connection string. | ||
*/ | ||
readonly cacheStrategy?: { | ||
/** TODO */ | ||
/** | ||
* `swr` is short for Stale-While-Revalidate. | ||
* | ||
* `swr` defines the number of seconds that Accelerate may serve _stale_ cache data. | ||
* _Stale_ data is a cache hit, but the cache will be refreshed in the background by Accelerate. | ||
* The Prisma operation will not be blocked while data is refreshed. | ||
* | ||
* Use `swr` to reduce the latency of accessing the data while still maintaining | ||
* a more up-to-date value in the Accelerate cache. | ||
* `swr` without `ttl` will not reduce database load since Accelerate will | ||
* run the query in the background. | ||
* | ||
* `swr` can be combined with `ttl`. | ||
* `swr` applies **after** `ttl` has expired. | ||
* The total number of seconds data will be cached is `ttl + swr`. | ||
* | ||
* `swr` only applies when used with an Accelerate connection string. | ||
*/ | ||
readonly swr?: number; | ||
/** | ||
* `ttl` is short for Time-to-Live. | ||
* | ||
* `ttl` defines the number of seconds that Accelerate may serve _fresh_ cache data. | ||
* _Fresh_ data is a cache hit and will not execute the query against the database. | ||
* | ||
* Use `ttl` to reduce database load and latency for data that does not | ||
* require frequent updates. | ||
* `ttl` without `swr` will incur a blocking database query for the first | ||
* request after `ttl` expires. | ||
* | ||
* It's recommended to combine `ttl` and `swr` to maintain low latency while | ||
* Accelerate refreshes cached data in the background. | ||
* `swr` applies **after** `ttl` has expired. | ||
* The total number of seconds data will be cached is `ttl + swr`. | ||
* | ||
* `ttl` only applies when used with an Accelerate connection string. | ||
*/ | ||
readonly ttl?: number; | ||
}; | ||
} | ||
declare const _default: (client: any) => PrismaDefault.PrismaClientExtends<any>; | ||
interface AccelerateInfo { | ||
/** | ||
* The cache status of the response. | ||
* * `ttl` indicates a cache hit within the `ttl` duration and no database query was executed | ||
* * `swr` indicates a cache hit within the `swr` duration and the data is being refreshed by Accelerate in the background | ||
* * `miss` indicates that both `ttl` and `swr` have expired and the database query was executed by the request | ||
* * `none` indicates that no cache strategy was specified and the database query was executed by the request | ||
*/ | ||
cacheStatus: "ttl" | "swr" | "miss" | "none"; | ||
/** | ||
* The date the response was last refreshed. | ||
*/ | ||
lastModified: Date; | ||
/** | ||
* The datacenter region that received the request. | ||
*/ | ||
region: string; | ||
/** | ||
* Unique identifier of the request. Useful for troubleshooting. | ||
*/ | ||
requestId: string; | ||
/** | ||
* The unique signature of the Prisma operation. | ||
*/ | ||
signature: string; | ||
} | ||
interface AcceleratePromise<T> extends Prisma.PrismaPromise<T> { | ||
withAccelerateInfo(): Prisma.PrismaPromise<{ | ||
data: T; | ||
info: AccelerateInfo | null; | ||
}>; | ||
} | ||
declare const _default: (client: any) => PrismaDefault.PrismaClientExtends<Types.Extensions.InternalArgs<{}, { | ||
$allModels: { | ||
aggregate<T, A>(this: T, args: Prisma.Exact<A, Types.Public.Args<T, "aggregate"> & PrismaCacheStrategy>): AcceleratePromise<Prisma.Result<T, A, "aggregate">>; | ||
count<T_1, A_1>(this: T_1, args?: Prisma.Exact<A_1, Types.Public.Args<T_1, "count"> & PrismaCacheStrategy>): AcceleratePromise<Prisma.Result<T_1, A_1, "count">>; | ||
findFirst<T_2, A_2>(this: T_2, args?: Prisma.Exact<A_2, Types.Public.Args<T_2, "findFirst"> & PrismaCacheStrategy>): AcceleratePromise<Prisma.Result<T_2, A_2, "findFirst">>; | ||
findFirstOrThrow<T_3, A_3>(this: T_3, args?: Prisma.Exact<A_3, Types.Public.Args<T_3, "findFirstOrThrow"> & PrismaCacheStrategy>): AcceleratePromise<Prisma.Result<T_3, A_3, "findFirstOrThrow">>; | ||
findMany<T_4, A_4>(this: T_4, args?: Prisma.Exact<A_4, Types.Public.Args<T_4, "findMany"> & PrismaCacheStrategy>): AcceleratePromise<Prisma.Result<T_4, A_4, "findMany">>; | ||
findUnique<T_5, A_5>(this: T_5, args: Prisma.Exact<A_5, Types.Public.Args<T_5, "findUnique"> & PrismaCacheStrategy>): AcceleratePromise<Prisma.Result<T_5, A_5, "findUnique">>; | ||
findUniqueOrThrow<T_6, A_6>(this: T_6, args: Prisma.Exact<A_6, Types.Public.Args<T_6, "findUniqueOrThrow"> & PrismaCacheStrategy>): AcceleratePromise<Prisma.Result<T_6, A_6, "findUniqueOrThrow">>; | ||
groupBy<T_7, A_7>(this: T_7, args: Prisma.Exact<A_7, Types.Public.Args<T_7, "groupBy"> & PrismaCacheStrategy>): AcceleratePromise<Prisma.Result<T_7, A_7, "groupBy">>; | ||
}; | ||
}, {}, {}> & Types.Extensions.InternalArgs<{}, {}, { | ||
$allModels: { | ||
aggregate: (params: { | ||
model?: string; | ||
operation: string; | ||
args: object; | ||
query: (args: object) => Promise<unknown>; | ||
}) => Promise<any>; | ||
count: (params: { | ||
model?: string; | ||
operation: string; | ||
args: object; | ||
query: (args: object) => Promise<unknown>; | ||
}) => Promise<any>; | ||
findFirst: (params: { | ||
model?: string; | ||
operation: string; | ||
args: object; | ||
query: (args: object) => Promise<unknown>; | ||
}) => Promise<any>; | ||
findFirstOrThrow: (params: { | ||
model?: string; | ||
operation: string; | ||
args: object; | ||
query: (args: object) => Promise<unknown>; | ||
}) => Promise<any>; | ||
findMany: (params: { | ||
model?: string; | ||
operation: string; | ||
args: object; | ||
query: (args: object) => Promise<unknown>; | ||
}) => Promise<any>; | ||
findUnique: (params: { | ||
model?: string; | ||
operation: string; | ||
args: object; | ||
query: (args: object) => Promise<unknown>; | ||
}) => Promise<any>; | ||
findUniqueOrThrow: (params: { | ||
model?: string; | ||
operation: string; | ||
args: object; | ||
query: (args: object) => Promise<unknown>; | ||
}) => Promise<any>; | ||
groupBy: (params: { | ||
model?: string; | ||
operation: string; | ||
args: object; | ||
query: (args: object) => Promise<unknown>; | ||
}) => Promise<any>; | ||
}; | ||
}, {}> & Types.Extensions.DefaultArgs>; | ||
export default _default; |
@@ -1,23 +0,2 @@ | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
/// <reference lib="dom" /> | ||
// importing default is needed for ESM compatibility | ||
@@ -27,14 +6,66 @@ // default-index is a CJS file, so named exports are not resolved | ||
import PrismaDefault from "@prisma/client/scripts/default-index.js"; | ||
var EXTENSION_NAME = "@prisma/extension-accelerate"; | ||
function withCacheHeaders(params) { | ||
var args = params.args; | ||
var _a = args, cacheStrategy = _a.cacheStrategy, rest = __rest(_a, ["cacheStrategy"]); | ||
var _b = params, __internalParams = _b.__internalParams, query = _b.query; | ||
__internalParams.headers = __assign(__assign({}, __internalParams.headers), { "cache-control": cacheStrategy | ||
? "max-age=".concat(cacheStrategy.ttl, ";stale-while-revalidate=").concat(cacheStrategy.swr) | ||
: "no-cache" }); | ||
return query(rest, __internalParams); | ||
const EXTENSION_NAME = "@prisma/extension-accelerate"; | ||
function makeWithCacheHeaders() { | ||
let machineHint = undefined; | ||
return async (params) => { | ||
const { args } = params; | ||
const { cacheStrategy, __accelerateInfo = false, ...rest } = args; | ||
let info = null; | ||
const { __internalParams, query } = params; | ||
__internalParams.customDataProxyFetch = (fetch) => { | ||
return async (url, options) => { | ||
const cacheControl = new Array(); | ||
if (typeof cacheStrategy?.ttl === "number") { | ||
cacheControl.push(`max-age=${cacheStrategy.ttl}`); | ||
} | ||
if (typeof cacheStrategy?.swr === "number") { | ||
cacheControl.push(`stale-while-revalidate=${cacheStrategy.swr}`); | ||
} | ||
options.headers = { | ||
...options.headers, | ||
"cache-control": cacheControl.length > 0 ? cacheControl.join(",") : `no-cache`, | ||
}; | ||
if (machineHint) { | ||
options.headers["accelerate-query-engine-jwt"] = machineHint; | ||
} | ||
const response = await fetch(url, options); | ||
// Response is not available in Node 16 and lower, so we need to detect it | ||
if ("Response" in globalThis && response instanceof Response) { | ||
// on non-Node runtimes, response will be a Response instance | ||
info = { | ||
cacheStatus: response.headers.get("accelerate-cache-status"), | ||
lastModified: new Date(response.headers.get("last-modified") ?? ""), | ||
region: response.headers.get("cf-ray")?.split("-")[1] ?? "unspecified", | ||
requestId: response.headers.get("cf-ray") ?? "unspecified", | ||
signature: response.headers.get("accelerate-signature") ?? "unspecified", | ||
}; | ||
machineHint = | ||
response.headers.get("accelerate-query-engine-jwt") ?? undefined; | ||
} | ||
else if (typeof response === "object") { | ||
// in Node, response will be an object | ||
info = { | ||
cacheStatus: response.headers["accelerate-cache-status"], | ||
lastModified: new Date(response.headers["last-modified"]), | ||
region: response.headers["cf-ray"]?.split("-")[1] ?? "unspecified", | ||
requestId: response.headers["cf-ray"] ?? "unspecified", | ||
signature: response.headers["accelerate-signature"] ?? "unspecified", | ||
}; | ||
machineHint = response.headers["accelerate-query-engine-jwt"]; | ||
} | ||
return response; | ||
}; | ||
}; | ||
if (__accelerateInfo) { | ||
const data = await query(rest, __internalParams); | ||
return { data, info }; | ||
} | ||
else { | ||
return query(rest, __internalParams); | ||
} | ||
}; | ||
} | ||
export default PrismaDefault.Prisma.defineExtension(function (client) { | ||
var xclient = client.$extends({ | ||
export default PrismaDefault.Prisma.defineExtension((client) => { | ||
const withCacheHeaders = makeWithCacheHeaders(); | ||
const xclient = client.$extends({ | ||
name: EXTENSION_NAME, | ||
@@ -50,5 +81,5 @@ query: { | ||
findUniqueOrThrow: withCacheHeaders, | ||
groupBy: withCacheHeaders | ||
} | ||
} | ||
groupBy: withCacheHeaders, | ||
}, | ||
}, | ||
}); | ||
@@ -61,37 +92,93 @@ return xclient.$extends({ | ||
// TODO: can we define these in a map that ensures query and model overrides stay in sync/ | ||
aggregate: function (args) { | ||
var ctx = this; | ||
return xclient[ctx.name].aggregate(args); | ||
aggregate(args) { | ||
const ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].aggregate(args), { | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].aggregate({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
count: function (args) { | ||
var ctx = this; | ||
return xclient[ctx.name].count(args); | ||
count(args) { | ||
const ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].count(args), { | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].count({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
findFirst: function (args) { | ||
var ctx = this; | ||
return xclient[ctx.name].findFirst(args); | ||
findFirst(args) { | ||
const ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].findFirst(args), { | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].findFirst({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
findFirstOrThrow: function (args) { | ||
var ctx = this; | ||
return xclient[ctx.name].findFirstOrThrow(args); | ||
findFirstOrThrow(args) { | ||
const ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].findFirstOrThrow(args), { | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].findFirstOrThrow({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
findMany: function (args) { | ||
var ctx = this; | ||
return xclient[ctx.name].findMany(args); | ||
findMany(args) { | ||
const ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].findMany(args), { | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].findMany({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
findUnique: function (args) { | ||
var ctx = this; | ||
return xclient[ctx.name].findUnique(args); | ||
findUnique(args) { | ||
const ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].findUnique(args), { | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].findUnique({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
findUniqueOrThrow: function (args) { | ||
var ctx = this; | ||
return xclient[ctx.name].findUniqueOrThrow(args); | ||
findUniqueOrThrow(args) { | ||
const ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].findUniqueOrThrow(args), { | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].findUniqueOrThrow({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
groupBy: function (args) { | ||
var ctx = this; | ||
return xclient[ctx.name].groupBy(args); | ||
} | ||
} | ||
} | ||
groupBy(args) { | ||
const ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].groupBy(args), { | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].groupBy({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); |
{ | ||
"name": "@prisma/extension-accelerate", | ||
"type": "module", | ||
"version": "0.0.0-experimental-9ab6d60", | ||
"version": "0.0.0-experimental-a2eb2bb", | ||
"description": "Prisma Client extension for Accelerate", | ||
@@ -19,2 +19,3 @@ "sideEffects": false, | ||
"lint:check": "eslint src/** --max-warnings 0", | ||
"test": "node --test --loader dotenv/config --loader ts-node/esm test/*.test.ts", | ||
"types:check": "tsc --noEmit" | ||
@@ -26,3 +27,4 @@ }, | ||
"devDependencies": { | ||
"@prisma/client": "^4.9.0", | ||
"prisma": "^4.11.0", | ||
"@prisma/client": "^4.11.0", | ||
"@tsconfig/esm": "^1.0.2", | ||
@@ -33,2 +35,3 @@ "@tsconfig/node14": "^1.0.3", | ||
"@typescript-eslint/parser": "^5.47.1", | ||
"dotenv": "^16.0.3", | ||
"eslint": "^8.30.0", | ||
@@ -40,4 +43,4 @@ "prettier": "2.8.1", | ||
"peerDependencies": { | ||
"@prisma/client": ">=4.9.0" | ||
"@prisma/client": ">=4.10.0" | ||
} | ||
} |
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
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
32202
659
12
3