@prisma/extension-accelerate
Advanced tools
Comparing version 0.0.0-experimental-7e4409c to 0.0.0-experimental-830bbc0
/// <reference lib="dom" /> | ||
import PrismaDefault, { type Prisma } from "@prisma/client/scripts/default-index.js"; | ||
import type { Types } from "@prisma/client/runtime"; | ||
type QueryOptionsCb = Parameters<Exclude<Types.Extensions.UserArgs["query"]["someModel"], (...args: any) => any>["someAction"]>[0]; | ||
export interface PrismaCacheStrategy { | ||
@@ -84,3 +83,2 @@ /** | ||
} | ||
declare function withCacheHeaders(params: QueryOptionsCb): Promise<any>; | ||
declare const _default: (client: any) => PrismaDefault.PrismaClientExtends<Types.Extensions.InternalArgs<{}, { | ||
@@ -99,12 +97,52 @@ $allModels: { | ||
$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; |
@@ -12,54 +12,64 @@ "use strict"; | ||
const EXTENSION_NAME = "@prisma/extension-accelerate"; | ||
async function withCacheHeaders(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`, | ||
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; | ||
}; | ||
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", | ||
}; | ||
} | ||
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", | ||
}; | ||
} | ||
return response; | ||
}; | ||
if (__accelerateInfo) { | ||
const data = await query(rest, __internalParams); | ||
return { data, info }; | ||
} | ||
else { | ||
return query(rest, __internalParams); | ||
} | ||
}; | ||
if (__accelerateInfo) { | ||
const data = await query(rest, __internalParams); | ||
return { data, info }; | ||
} | ||
else { | ||
return query(rest, __internalParams); | ||
} | ||
} | ||
exports.default = default_index_js_1.default.Prisma.defineExtension((client) => { | ||
const withCacheHeaders = makeWithCacheHeaders(); | ||
const xclient = client.$extends({ | ||
@@ -66,0 +76,0 @@ name: EXTENSION_NAME, |
/// <reference lib="dom" /> | ||
import PrismaDefault, { type Prisma } from "@prisma/client/scripts/default-index.js"; | ||
import type { Types } from "@prisma/client/runtime"; | ||
type QueryOptionsCb = Parameters<Exclude<Types.Extensions.UserArgs["query"]["someModel"], (...args: any) => any>["someAction"]>[0]; | ||
export interface PrismaCacheStrategy { | ||
@@ -84,3 +83,2 @@ /** | ||
} | ||
declare function withCacheHeaders(params: QueryOptionsCb): Promise<any>; | ||
declare const _default: (client: any) => PrismaDefault.PrismaClientExtends<Types.Extensions.InternalArgs<{}, { | ||
@@ -99,12 +97,52 @@ $allModels: { | ||
$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; | ||
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; |
@@ -7,54 +7,64 @@ /// <reference lib="dom" /> | ||
const EXTENSION_NAME = "@prisma/extension-accelerate"; | ||
async function withCacheHeaders(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`, | ||
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; | ||
}; | ||
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", | ||
}; | ||
} | ||
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", | ||
}; | ||
} | ||
return response; | ||
}; | ||
if (__accelerateInfo) { | ||
const data = await query(rest, __internalParams); | ||
return { data, info }; | ||
} | ||
else { | ||
return query(rest, __internalParams); | ||
} | ||
}; | ||
if (__accelerateInfo) { | ||
const data = await query(rest, __internalParams); | ||
return { data, info }; | ||
} | ||
else { | ||
return query(rest, __internalParams); | ||
} | ||
} | ||
export default PrismaDefault.Prisma.defineExtension((client) => { | ||
const withCacheHeaders = makeWithCacheHeaders(); | ||
const xclient = client.$extends({ | ||
@@ -61,0 +71,0 @@ name: EXTENSION_NAME, |
{ | ||
"name": "@prisma/extension-accelerate", | ||
"type": "module", | ||
"version": "0.0.0-experimental-7e4409c", | ||
"version": "0.0.0-experimental-830bbc0", | ||
"description": "Prisma Client extension for Accelerate", | ||
@@ -26,4 +26,4 @@ "sideEffects": false, | ||
"devDependencies": { | ||
"prisma": "^4.10.0", | ||
"@prisma/client": "^4.10.0", | ||
"prisma": "^4.11.0", | ||
"@prisma/client": "^4.11.0", | ||
"@tsconfig/esm": "^1.0.2", | ||
@@ -30,0 +30,0 @@ "@tsconfig/node14": "^1.0.3", |
32202
659