@prisma/extension-accelerate
Advanced tools
Comparing version 0.0.0-experimental-570549d to 0.0.0-experimental-5877c0d
/// <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]; | ||
import type { Types } from "@prisma/client/runtime/library.js"; | ||
export interface PrismaCacheStrategy { | ||
@@ -84,26 +83,14 @@ /** | ||
} | ||
declare function withCacheHeaders(params: QueryOptionsCb): Promise<any>; | ||
declare const _default: (client: any) => PrismaDefault.PrismaClientExtends<Types.Extensions.InternalArgs<{}, { | ||
export declare function withAccelerate(): (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> | 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">>; | ||
aggregate<T, A>(this: T, args: Prisma.Exact<A, Prisma.Args<T, "aggregate"> & PrismaCacheStrategy>): AcceleratePromise<Prisma.Result<T, A, "aggregate">>; | ||
count<T_1, A_1>(this: T_1, args?: Prisma.Exact<A_1, Prisma.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, Prisma.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, Prisma.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, Prisma.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, Prisma.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, Prisma.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, Prisma.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; | ||
}; | ||
}, {}> & Types.Extensions.DefaultArgs>; | ||
export default _default; | ||
}, {}, {}> & Types.Extensions.InternalArgs<{}, {}, {}, {}> & Types.Extensions.DefaultArgs>; | ||
export {}; |
"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 __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -6,2 +29,4 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.withAccelerate = void 0; | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
/// <reference lib="dom" /> | ||
@@ -13,24 +38,36 @@ // importing default is needed for ESM compatibility | ||
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`, | ||
}; | ||
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 | ||
function makeWithCacheHeaders() { | ||
let machineHint = undefined; | ||
async function getFetcher() { | ||
if (typeof globalThis?.fetch === "function") { | ||
return globalThis.fetch; | ||
} | ||
else { | ||
const { fetch } = await Promise.resolve().then(() => __importStar(require("./node-fetch.js"))); | ||
return fetch; | ||
} | ||
} | ||
return async (params) => { | ||
const { args } = params; | ||
const { cacheStrategy, __accelerateInfo = false, ...rest } = args; | ||
let info = null; | ||
const { __internalParams, query } = params; | ||
__internalParams.customDataProxyFetch = () => { | ||
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 fetch = await getFetcher(); | ||
const response = await fetch(url, options); | ||
info = { | ||
@@ -43,137 +80,133 @@ cacheStatus: response.headers.get("accelerate-cache-status"), | ||
}; | ||
} | ||
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; | ||
machineHint = | ||
response.headers.get("accelerate-query-engine-jwt") ?? undefined; | ||
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 xclient = client.$extends({ | ||
name: EXTENSION_NAME, | ||
query: { | ||
$allModels: { | ||
aggregate: withCacheHeaders, | ||
count: withCacheHeaders, | ||
findFirst: withCacheHeaders, | ||
findFirstOrThrow: withCacheHeaders, | ||
findMany: withCacheHeaders, | ||
findUnique: withCacheHeaders, | ||
findUniqueOrThrow: withCacheHeaders, | ||
groupBy: withCacheHeaders, | ||
function withAccelerate() { | ||
return default_index_js_1.default.Prisma.defineExtension((client) => { | ||
const withCacheHeaders = makeWithCacheHeaders(); | ||
const xclient = client.$extends({ | ||
name: EXTENSION_NAME, | ||
query: { | ||
$allModels: { | ||
aggregate: withCacheHeaders, | ||
count: withCacheHeaders, | ||
findFirst: withCacheHeaders, | ||
findFirstOrThrow: withCacheHeaders, | ||
findMany: withCacheHeaders, | ||
findUnique: withCacheHeaders, | ||
findUniqueOrThrow: withCacheHeaders, | ||
groupBy: withCacheHeaders, | ||
}, | ||
}, | ||
}, | ||
}); | ||
return xclient.$extends({ | ||
name: EXTENSION_NAME, | ||
model: { | ||
$allModels: { | ||
// TODO: these functions are repetitive. Is there a type we can use to generic this? | ||
// TODO: can we define these in a map that ensures query and model overrides stay in sync/ | ||
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, | ||
}); | ||
}, | ||
}); | ||
}); | ||
return xclient.$extends({ | ||
name: EXTENSION_NAME, | ||
model: { | ||
$allModels: { | ||
// TODO: these functions are repetitive. Is there a type we can use to generic this? | ||
// TODO: can we define these in a map that ensures query and model overrides stay in sync/ | ||
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 = 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 = 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 = 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 = 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 = 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 = 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 = 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, | ||
}); | ||
}, | ||
}); | ||
}, | ||
}, | ||
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 = 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 = 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 = 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 = 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 = 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 = 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, | ||
}); | ||
}, | ||
}); | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); | ||
} | ||
exports.withAccelerate = withAccelerate; |
/// <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]; | ||
import type { Types } from "@prisma/client/runtime/library.js"; | ||
export interface PrismaCacheStrategy { | ||
@@ -84,26 +83,14 @@ /** | ||
} | ||
declare function withCacheHeaders(params: QueryOptionsCb): Promise<any>; | ||
declare const _default: (client: any) => PrismaDefault.PrismaClientExtends<Types.Extensions.InternalArgs<{}, { | ||
export declare function withAccelerate(): (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">>; | ||
aggregate<T, A>(this: T, args: Prisma.Exact<A, Prisma.Args<T, "aggregate"> & PrismaCacheStrategy>): AcceleratePromise<Prisma.Result<T, A, "aggregate">>; | ||
count<T_1, A_1>(this: T_1, args?: Prisma.Exact<A_1, Prisma.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, Prisma.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, Prisma.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, Prisma.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, Prisma.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, Prisma.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, Prisma.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; | ||
}; | ||
}, {}> & Types.Extensions.DefaultArgs>; | ||
export default _default; | ||
}, {}, {}> & Types.Extensions.InternalArgs<{}, {}, {}, {}> & Types.Extensions.DefaultArgs>; | ||
export {}; |
@@ -0,1 +1,2 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
/// <reference lib="dom" /> | ||
@@ -5,26 +6,38 @@ // importing default is needed for ESM compatibility | ||
// the types create a separate resolution issue, so they are still imported by name | ||
import PrismaDefault from "@prisma/client/scripts/default-index.js"; | ||
import PrismaDefault, {} from "@prisma/client/scripts/default-index.js"; | ||
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`, | ||
}; | ||
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 | ||
function makeWithCacheHeaders() { | ||
let machineHint = undefined; | ||
async function getFetcher() { | ||
if (typeof globalThis?.fetch === "function") { | ||
return globalThis.fetch; | ||
} | ||
else { | ||
const { fetch } = await import("./node-fetch.js"); | ||
return fetch; | ||
} | ||
} | ||
return async (params) => { | ||
const { args } = params; | ||
const { cacheStrategy, __accelerateInfo = false, ...rest } = args; | ||
let info = null; | ||
const { __internalParams, query } = params; | ||
__internalParams.customDataProxyFetch = () => { | ||
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 fetch = await getFetcher(); | ||
const response = await fetch(url, options); | ||
info = { | ||
@@ -37,137 +50,132 @@ cacheStatus: response.headers.get("accelerate-cache-status"), | ||
}; | ||
} | ||
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; | ||
machineHint = | ||
response.headers.get("accelerate-query-engine-jwt") ?? undefined; | ||
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 xclient = client.$extends({ | ||
name: EXTENSION_NAME, | ||
query: { | ||
$allModels: { | ||
aggregate: withCacheHeaders, | ||
count: withCacheHeaders, | ||
findFirst: withCacheHeaders, | ||
findFirstOrThrow: withCacheHeaders, | ||
findMany: withCacheHeaders, | ||
findUnique: withCacheHeaders, | ||
findUniqueOrThrow: withCacheHeaders, | ||
groupBy: withCacheHeaders, | ||
export function withAccelerate() { | ||
return PrismaDefault.Prisma.defineExtension((client) => { | ||
const withCacheHeaders = makeWithCacheHeaders(); | ||
const xclient = client.$extends({ | ||
name: EXTENSION_NAME, | ||
query: { | ||
$allModels: { | ||
aggregate: withCacheHeaders, | ||
count: withCacheHeaders, | ||
findFirst: withCacheHeaders, | ||
findFirstOrThrow: withCacheHeaders, | ||
findMany: withCacheHeaders, | ||
findUnique: withCacheHeaders, | ||
findUniqueOrThrow: withCacheHeaders, | ||
groupBy: withCacheHeaders, | ||
}, | ||
}, | ||
}, | ||
}); | ||
return xclient.$extends({ | ||
name: EXTENSION_NAME, | ||
model: { | ||
$allModels: { | ||
// TODO: these functions are repetitive. Is there a type we can use to generic this? | ||
// TODO: can we define these in a map that ensures query and model overrides stay in sync/ | ||
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, | ||
}); | ||
}, | ||
}); | ||
}); | ||
return xclient.$extends({ | ||
name: EXTENSION_NAME, | ||
model: { | ||
$allModels: { | ||
// TODO: these functions are repetitive. Is there a type we can use to generic this? | ||
// TODO: can we define these in a map that ensures query and model overrides stay in sync/ | ||
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(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(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(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(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(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(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(args) { | ||
const ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].groupBy(args), { | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].groupBy({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
}, | ||
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(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(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(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(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(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(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-570549d", | ||
"version": "0.0.0-experimental-5877c0d", | ||
"description": "Prisma Client extension for Accelerate", | ||
@@ -26,18 +26,21 @@ "sideEffects": false, | ||
"devDependencies": { | ||
"prisma": "^4.11.0", | ||
"@prisma/client": "^4.11.0", | ||
"@tsconfig/esm": "^1.0.2", | ||
"@prisma/client": "^4.16.2", | ||
"@tsconfig/esm": "^1.0.4", | ||
"@tsconfig/node14": "^1.0.3", | ||
"@tsconfig/node18-strictest-esm": "^1.0.1", | ||
"@typescript-eslint/eslint-plugin": "^5.47.1", | ||
"@typescript-eslint/parser": "^5.47.1", | ||
"dotenv": "^16.0.3", | ||
"eslint": "^8.30.0", | ||
"prettier": "2.8.1", | ||
"@typescript-eslint/eslint-plugin": "^5.61.0", | ||
"@typescript-eslint/parser": "^5.61.0", | ||
"dotenv": "^16.3.1", | ||
"eslint": "^8.44.0", | ||
"prettier": "2.8.8", | ||
"prisma": "^4.16.2", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.9.4" | ||
"typescript": "^5.1.6" | ||
}, | ||
"peerDependencies": { | ||
"@prisma/client": ">=4.10.0" | ||
"@prisma/client": ">=4.16.1" | ||
}, | ||
"engines": { | ||
"node": ">=16" | ||
} | ||
} |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
39900
12
876
11