@prisma/extension-accelerate
Advanced tools
Comparing version 0.0.0-experimental-4c5dffe to 0.0.0-experimental-570549d
@@ -0,4 +1,5 @@ | ||
/// <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]; | ||
type QueryOptionsCb = Parameters<Exclude<Types.Extensions.UserArgs["query"]["someModel"], (...args: any) => any>["someAction"]>[0]; | ||
export interface PrismaCacheStrategy { | ||
@@ -88,6 +89,6 @@ /** | ||
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">>; | ||
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">>; | ||
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">>; | ||
@@ -94,0 +95,0 @@ 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">>; |
@@ -6,2 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/// <reference lib="dom" /> | ||
// importing default is needed for ESM compatibility | ||
@@ -31,9 +32,23 @@ // default-index is a CJS file, so named exports are not resolved | ||
const response = await fetch(url, options); | ||
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"], | ||
}; | ||
// 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; | ||
@@ -40,0 +55,0 @@ }; |
@@ -0,4 +1,5 @@ | ||
/// <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]; | ||
type QueryOptionsCb = Parameters<Exclude<Types.Extensions.UserArgs["query"]["someModel"], (...args: any) => any>["someAction"]>[0]; | ||
export interface PrismaCacheStrategy { | ||
@@ -5,0 +6,0 @@ /** |
@@ -1,59 +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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (g && (g = 0, op[0] && (_ = 0)), _) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
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 | ||
@@ -63,56 +6,56 @@ // 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) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var args, _a, cacheStrategy, _b, __accelerateInfo, rest, info, _c, __internalParams, query, data; | ||
var _this = this; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
args = params.args; | ||
_a = args, cacheStrategy = _a.cacheStrategy, _b = _a.__accelerateInfo, __accelerateInfo = _b === void 0 ? false : _b, rest = __rest(_a, ["cacheStrategy", "__accelerateInfo"]); | ||
info = null; | ||
_c = params, __internalParams = _c.__internalParams, query = _c.query; | ||
__internalParams.customDataProxyFetch = function (fetch) { | ||
return function (url, options) { return __awaiter(_this, void 0, void 0, function () { | ||
var cacheControl, response; | ||
var _a, _b, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
cacheControl = new Array(); | ||
if (typeof (cacheStrategy === null || cacheStrategy === void 0 ? void 0 : cacheStrategy.ttl) === "number") { | ||
cacheControl.push("max-age=".concat(cacheStrategy.ttl)); | ||
} | ||
if (typeof (cacheStrategy === null || cacheStrategy === void 0 ? void 0 : cacheStrategy.swr) === "number") { | ||
cacheControl.push("stale-while-revalidate=".concat(cacheStrategy.swr)); | ||
} | ||
options.headers = __assign(__assign({}, options.headers), { "cache-control": cacheControl.length > 0 ? cacheControl.join(",") : "no-cache" }); | ||
return [4 /*yield*/, fetch(url, options)]; | ||
case 1: | ||
response = _d.sent(); | ||
info = { | ||
cacheStatus: response.headers["accelerate-cache-status"], | ||
lastModified: new Date(response.headers["last-modified"]), | ||
region: (_b = (_a = response.headers["cf-ray"]) === null || _a === void 0 ? void 0 : _a.split("-")[1]) !== null && _b !== void 0 ? _b : "unspecified", | ||
requestId: (_c = response.headers["cf-ray"]) !== null && _c !== void 0 ? _c : "unspecified", | ||
signature: response.headers["accelerate-signature"] | ||
}; | ||
return [2 /*return*/, response]; | ||
} | ||
}); | ||
}); }; | ||
}; | ||
if (!__accelerateInfo) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, query(rest, __internalParams)]; | ||
case 1: | ||
data = _d.sent(); | ||
return [2 /*return*/, { data: data, info: info }]; | ||
case 2: return [2 /*return*/, query(rest, __internalParams)]; | ||
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 | ||
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); | ||
} | ||
} | ||
export default PrismaDefault.Prisma.defineExtension(function (client) { | ||
var xclient = client.$extends({ | ||
export default PrismaDefault.Prisma.defineExtension((client) => { | ||
const xclient = client.$extends({ | ||
name: EXTENSION_NAME, | ||
@@ -128,5 +71,5 @@ query: { | ||
findUniqueOrThrow: withCacheHeaders, | ||
groupBy: withCacheHeaders | ||
} | ||
} | ||
groupBy: withCacheHeaders, | ||
}, | ||
}, | ||
}); | ||
@@ -139,69 +82,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 = PrismaDefault.Prisma.getExtensionContext(this); | ||
aggregate(args) { | ||
const ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].aggregate(args), { | ||
withAccelerateInfo: function () { | ||
return xclient[ctx.name].aggregate(__assign(__assign({}, args), { __accelerateInfo: true })); | ||
} | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].aggregate({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
count: function (args) { | ||
var ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
count(args) { | ||
const ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].count(args), { | ||
withAccelerateInfo: function () { | ||
return xclient[ctx.name].count(__assign(__assign({}, args), { __accelerateInfo: true })); | ||
} | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].count({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
findFirst: function (args) { | ||
var ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
findFirst(args) { | ||
const ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].findFirst(args), { | ||
withAccelerateInfo: function () { | ||
return xclient[ctx.name].findFirst(__assign(__assign({}, args), { __accelerateInfo: true })); | ||
} | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].findFirst({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
findFirstOrThrow: function (args) { | ||
var ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
findFirstOrThrow(args) { | ||
const ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].findFirstOrThrow(args), { | ||
withAccelerateInfo: function () { | ||
return xclient[ctx.name].findFirstOrThrow(__assign(__assign({}, args), { __accelerateInfo: true })); | ||
} | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].findFirstOrThrow({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
findMany: function (args) { | ||
var ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
findMany(args) { | ||
const ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].findMany(args), { | ||
withAccelerateInfo: function () { | ||
return xclient[ctx.name].findMany(__assign(__assign({}, args), { __accelerateInfo: true })); | ||
} | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].findMany({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
findUnique: function (args) { | ||
var ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
findUnique(args) { | ||
const ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].findUnique(args), { | ||
withAccelerateInfo: function () { | ||
return xclient[ctx.name].findUnique(__assign(__assign({}, args), { __accelerateInfo: true })); | ||
} | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].findUnique({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
findUniqueOrThrow: function (args) { | ||
var ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
findUniqueOrThrow(args) { | ||
const ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].findUniqueOrThrow(args), { | ||
withAccelerateInfo: function () { | ||
return xclient[ctx.name].findUniqueOrThrow(__assign(__assign({}, args), { __accelerateInfo: true })); | ||
} | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].findUniqueOrThrow({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
}, | ||
groupBy: function (args) { | ||
var ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
groupBy(args) { | ||
const ctx = PrismaDefault.Prisma.getExtensionContext(this); | ||
return Object.assign(xclient[ctx.name].groupBy(args), { | ||
withAccelerateInfo: function () { | ||
return xclient[ctx.name].groupBy(__assign(__assign({}, args), { __accelerateInfo: true })); | ||
} | ||
withAccelerateInfo() { | ||
return xclient[ctx.name].groupBy({ | ||
...args, | ||
__accelerateInfo: true, | ||
}); | ||
}, | ||
}); | ||
} | ||
} | ||
} | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); |
{ | ||
"name": "@prisma/extension-accelerate", | ||
"type": "module", | ||
"version": "0.0.0-experimental-4c5dffe", | ||
"version": "0.0.0-experimental-570549d", | ||
"description": "Prisma Client extension for Accelerate", | ||
@@ -26,4 +26,4 @@ "sideEffects": false, | ||
"devDependencies": { | ||
"prisma": "^4.10.0-dev.72", | ||
"@prisma/client": "^4.10.0-dev.72", | ||
"prisma": "^4.11.0", | ||
"@prisma/client": "^4.11.0", | ||
"@tsconfig/esm": "^1.0.2", | ||
@@ -41,4 +41,4 @@ "@tsconfig/node14": "^1.0.3", | ||
"peerDependencies": { | ||
"@prisma/client": ">=4.10.0-dev.72" | ||
"@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
2
28744
563