drizzle-multitenant 1.1.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -8
- package/dist/cli/index.js +2001 -2442
- package/dist/{context-Vki959ri.d.ts → context-BBLPNjmk.d.ts} +1 -1
- package/dist/cross-schema/index.js +1 -426
- package/dist/export/index.d.ts +395 -0
- package/dist/export/index.js +9 -0
- package/dist/index.d.ts +5 -4
- package/dist/index.js +149 -2437
- package/dist/integrations/express.d.ts +3 -3
- package/dist/integrations/express.js +1 -110
- package/dist/integrations/fastify.d.ts +3 -3
- package/dist/integrations/fastify.js +1 -236
- package/dist/integrations/hono.js +0 -3
- package/dist/integrations/nestjs/index.d.ts +1 -1
- package/dist/integrations/nestjs/index.js +3 -10759
- package/dist/lint/index.d.ts +475 -0
- package/dist/lint/index.js +5 -0
- package/dist/metrics/index.d.ts +530 -0
- package/dist/metrics/index.js +3 -0
- package/dist/migrator/index.d.ts +1087 -270
- package/dist/migrator/index.js +149 -970
- package/dist/migrator-B7oPKe73.d.ts +1067 -0
- package/dist/scaffold/index.d.ts +330 -0
- package/dist/scaffold/index.js +277 -0
- package/dist/{types-BhK96FPC.d.ts → types-CGqsPe2Q.d.ts} +49 -1
- package/package.json +18 -1
- package/dist/cli/index.js.map +0 -1
- package/dist/cross-schema/index.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/integrations/express.js.map +0 -1
- package/dist/integrations/fastify.js.map +0 -1
- package/dist/integrations/hono.js.map +0 -1
- package/dist/integrations/nestjs/index.js.map +0 -1
- package/dist/migrator/index.js.map +0 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Request, Response, NextFunction, RequestHandler } from 'express';
|
|
2
|
-
import { T as TenantManager } from '../types-
|
|
3
|
-
import { T as TenantContextData, a as TenantContext } from '../context-
|
|
4
|
-
export { c as createTenantContext } from '../context-
|
|
2
|
+
import { T as TenantManager } from '../types-CGqsPe2Q.js';
|
|
3
|
+
import { T as TenantContextData, a as TenantContext } from '../context-BBLPNjmk.js';
|
|
4
|
+
export { c as createTenantContext } from '../context-BBLPNjmk.js';
|
|
5
5
|
import 'pg';
|
|
6
6
|
import 'drizzle-orm/node-postgres';
|
|
7
7
|
|
|
@@ -1,110 +1 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
// src/context.ts
|
|
4
|
-
function createTenantContext(manager) {
|
|
5
|
-
const storage = new AsyncLocalStorage();
|
|
6
|
-
function getTenantOrNull() {
|
|
7
|
-
return storage.getStore();
|
|
8
|
-
}
|
|
9
|
-
function getTenant() {
|
|
10
|
-
const context = getTenantOrNull();
|
|
11
|
-
if (!context) {
|
|
12
|
-
throw new Error(
|
|
13
|
-
"[drizzle-multitenant] No tenant context found. Make sure you are calling this within runWithTenant()."
|
|
14
|
-
);
|
|
15
|
-
}
|
|
16
|
-
return context;
|
|
17
|
-
}
|
|
18
|
-
function getTenantId() {
|
|
19
|
-
return getTenant().tenantId;
|
|
20
|
-
}
|
|
21
|
-
function getTenantDb() {
|
|
22
|
-
const tenantId = getTenantId();
|
|
23
|
-
return manager.getDb(tenantId);
|
|
24
|
-
}
|
|
25
|
-
function getSharedDb() {
|
|
26
|
-
return manager.getSharedDb();
|
|
27
|
-
}
|
|
28
|
-
function isInTenantContext() {
|
|
29
|
-
return getTenantOrNull() !== void 0;
|
|
30
|
-
}
|
|
31
|
-
function runWithTenant(context, callback) {
|
|
32
|
-
if (!context.tenantId) {
|
|
33
|
-
throw new Error("[drizzle-multitenant] tenantId is required in context");
|
|
34
|
-
}
|
|
35
|
-
return storage.run(context, callback);
|
|
36
|
-
}
|
|
37
|
-
return {
|
|
38
|
-
runWithTenant,
|
|
39
|
-
getTenant,
|
|
40
|
-
getTenantOrNull,
|
|
41
|
-
getTenantId,
|
|
42
|
-
getTenantDb,
|
|
43
|
-
getSharedDb,
|
|
44
|
-
isInTenantContext
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// src/integrations/express.ts
|
|
49
|
-
var TenantNotFoundError = class extends Error {
|
|
50
|
-
constructor(message = "Tenant not found") {
|
|
51
|
-
super(message);
|
|
52
|
-
this.name = "TenantNotFoundError";
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
var TenantValidationError = class extends Error {
|
|
56
|
-
constructor(message = "Tenant validation failed") {
|
|
57
|
-
super(message);
|
|
58
|
-
this.name = "TenantValidationError";
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
function createExpressMiddleware(options) {
|
|
62
|
-
const { manager, extractTenantId, validateTenant, enrichContext, onError } = options;
|
|
63
|
-
const tenantContext = createTenantContext(manager);
|
|
64
|
-
const defaultErrorHandler = (error, _req, res, _next) => {
|
|
65
|
-
if (error instanceof TenantNotFoundError) {
|
|
66
|
-
res.status(400).json({ error: "Tenant ID is required" });
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
if (error instanceof TenantValidationError) {
|
|
70
|
-
res.status(403).json({ error: "Invalid tenant" });
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
res.status(500).json({ error: "Internal server error" });
|
|
74
|
-
};
|
|
75
|
-
const errorHandler = onError ?? defaultErrorHandler;
|
|
76
|
-
const middleware = async (req, res, next) => {
|
|
77
|
-
try {
|
|
78
|
-
const tenantId = await extractTenantId(req);
|
|
79
|
-
if (!tenantId) {
|
|
80
|
-
throw new TenantNotFoundError("Tenant ID not found in request");
|
|
81
|
-
}
|
|
82
|
-
if (validateTenant) {
|
|
83
|
-
const isValid = await validateTenant(tenantId, req);
|
|
84
|
-
if (!isValid) {
|
|
85
|
-
throw new TenantValidationError(`Tenant ${tenantId} validation failed`);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
const customContext = enrichContext ? await enrichContext(tenantId, req) : {};
|
|
89
|
-
const context = {
|
|
90
|
-
tenantId,
|
|
91
|
-
...customContext
|
|
92
|
-
};
|
|
93
|
-
req.tenantContext = context;
|
|
94
|
-
await tenantContext.runWithTenant(context, async () => {
|
|
95
|
-
await new Promise((resolve, reject) => {
|
|
96
|
-
next();
|
|
97
|
-
res.on("finish", resolve);
|
|
98
|
-
res.on("error", reject);
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
} catch (error) {
|
|
102
|
-
errorHandler(error, req, res, next);
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
return Object.assign(middleware, { context: tenantContext });
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export { TenantNotFoundError, TenantValidationError, createExpressMiddleware, createTenantContext };
|
|
109
|
-
//# sourceMappingURL=express.js.map
|
|
110
|
-
//# sourceMappingURL=express.js.map
|
|
1
|
+
import {AsyncLocalStorage}from'async_hooks';function C(a){let e=new AsyncLocalStorage;function s(){return e.getStore()}function i(){let t=s();if(!t)throw new Error("[drizzle-multitenant] No tenant context found. Make sure you are calling this within runWithTenant().");return t}function T(){return i().tenantId}function x(){let t=T();return a.getDb(t)}function d(){return a.getSharedDb()}function g(){return s()!==void 0}function h(t,n){if(!t.tenantId)throw new Error("[drizzle-multitenant] tenantId is required in context");return e.run(t,n)}return {runWithTenant:h,getTenant:i,getTenantOrNull:s,getTenantId:T,getTenantDb:x,getSharedDb:d,isInTenantContext:g}}var c=class extends Error{constructor(e="Tenant not found"){super(e),this.name="TenantNotFoundError";}},m=class extends Error{constructor(e="Tenant validation failed"){super(e),this.name="TenantValidationError";}};function b(a){let{manager:e,extractTenantId:s,validateTenant:i,enrichContext:T,onError:x}=a,d=C(e),h=x??((n,u,o,r)=>{if(n instanceof c){o.status(400).json({error:"Tenant ID is required"});return}if(n instanceof m){o.status(403).json({error:"Invalid tenant"});return}o.status(500).json({error:"Internal server error"});});return Object.assign(async(n,u,o)=>{try{let r=await s(n);if(!r)throw new c("Tenant ID not found in request");if(i&&!await i(r,n))throw new m(`Tenant ${r} validation failed`);let w=T?await T(r,n):{},S={tenantId:r,...w};n.tenantContext=S,await d.runWithTenant(S,async()=>{await new Promise((l,f)=>{o(),u.on("finish",l),u.on("error",f);});});}catch(r){h(r,n,u,o);}},{context:d})}export{c as TenantNotFoundError,m as TenantValidationError,b as createExpressMiddleware,C as createTenantContext};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { FastifyRequest, FastifyReply, FastifyPluginAsync } from 'fastify';
|
|
2
|
-
import { T as TenantManager } from '../types-
|
|
3
|
-
import { T as TenantContextData, a as TenantContext } from '../context-
|
|
4
|
-
export { c as createTenantContext } from '../context-
|
|
2
|
+
import { T as TenantManager } from '../types-CGqsPe2Q.js';
|
|
3
|
+
import { T as TenantContextData, a as TenantContext } from '../context-BBLPNjmk.js';
|
|
4
|
+
export { c as createTenantContext } from '../context-BBLPNjmk.js';
|
|
5
5
|
import 'pg';
|
|
6
6
|
import 'drizzle-orm/node-postgres';
|
|
7
7
|
|
|
@@ -1,236 +1 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
var __create = Object.create;
|
|
4
|
-
var __defProp = Object.defineProperty;
|
|
5
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
-
var __commonJS = (cb, mod) => function __require() {
|
|
10
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
__defProp(target, "default", { value: mod, enumerable: true }) ,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
|
|
29
|
-
// node_modules/fastify-plugin/lib/getPluginName.js
|
|
30
|
-
var require_getPluginName = __commonJS({
|
|
31
|
-
"node_modules/fastify-plugin/lib/getPluginName.js"(exports$1, module) {
|
|
32
|
-
var fpStackTracePattern = /at\s(?:.*\.)?plugin\s.*\n\s*(.*)/;
|
|
33
|
-
var fileNamePattern = /(\w*(\.\w*)*)\..*/;
|
|
34
|
-
module.exports = function getPluginName(fn) {
|
|
35
|
-
if (fn.name.length > 0) return fn.name;
|
|
36
|
-
const stackTraceLimit = Error.stackTraceLimit;
|
|
37
|
-
Error.stackTraceLimit = 10;
|
|
38
|
-
try {
|
|
39
|
-
throw new Error("anonymous function");
|
|
40
|
-
} catch (e) {
|
|
41
|
-
Error.stackTraceLimit = stackTraceLimit;
|
|
42
|
-
return extractPluginName(e.stack);
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
function extractPluginName(stack) {
|
|
46
|
-
const m = stack.match(fpStackTracePattern);
|
|
47
|
-
return m ? m[1].split(/[/\\]/).slice(-1)[0].match(fileNamePattern)[1] : "anonymous";
|
|
48
|
-
}
|
|
49
|
-
module.exports.extractPluginName = extractPluginName;
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
// node_modules/fastify-plugin/lib/toCamelCase.js
|
|
54
|
-
var require_toCamelCase = __commonJS({
|
|
55
|
-
"node_modules/fastify-plugin/lib/toCamelCase.js"(exports$1, module) {
|
|
56
|
-
module.exports = function toCamelCase(name) {
|
|
57
|
-
if (name[0] === "@") {
|
|
58
|
-
name = name.slice(1).replace("/", "-");
|
|
59
|
-
}
|
|
60
|
-
return name.replace(/-(.)/g, function(match, g1) {
|
|
61
|
-
return g1.toUpperCase();
|
|
62
|
-
});
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
// node_modules/fastify-plugin/plugin.js
|
|
68
|
-
var require_plugin = __commonJS({
|
|
69
|
-
"node_modules/fastify-plugin/plugin.js"(exports$1, module) {
|
|
70
|
-
var getPluginName = require_getPluginName();
|
|
71
|
-
var toCamelCase = require_toCamelCase();
|
|
72
|
-
var count = 0;
|
|
73
|
-
function plugin(fn, options = {}) {
|
|
74
|
-
let autoName = false;
|
|
75
|
-
if (fn.default !== void 0) {
|
|
76
|
-
fn = fn.default;
|
|
77
|
-
}
|
|
78
|
-
if (typeof fn !== "function") {
|
|
79
|
-
throw new TypeError(
|
|
80
|
-
`fastify-plugin expects a function, instead got a '${typeof fn}'`
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
if (typeof options === "string") {
|
|
84
|
-
options = {
|
|
85
|
-
fastify: options
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
if (typeof options !== "object" || Array.isArray(options) || options === null) {
|
|
89
|
-
throw new TypeError("The options object should be an object");
|
|
90
|
-
}
|
|
91
|
-
if (options.fastify !== void 0 && typeof options.fastify !== "string") {
|
|
92
|
-
throw new TypeError(`fastify-plugin expects a version string, instead got '${typeof options.fastify}'`);
|
|
93
|
-
}
|
|
94
|
-
if (!options.name) {
|
|
95
|
-
autoName = true;
|
|
96
|
-
options.name = getPluginName(fn) + "-auto-" + count++;
|
|
97
|
-
}
|
|
98
|
-
fn[/* @__PURE__ */ Symbol.for("skip-override")] = options.encapsulate !== true;
|
|
99
|
-
fn[/* @__PURE__ */ Symbol.for("fastify.display-name")] = options.name;
|
|
100
|
-
fn[/* @__PURE__ */ Symbol.for("plugin-meta")] = options;
|
|
101
|
-
if (!fn.default) {
|
|
102
|
-
fn.default = fn;
|
|
103
|
-
}
|
|
104
|
-
const camelCase = toCamelCase(options.name);
|
|
105
|
-
if (!autoName && !fn[camelCase]) {
|
|
106
|
-
fn[camelCase] = fn;
|
|
107
|
-
}
|
|
108
|
-
return fn;
|
|
109
|
-
}
|
|
110
|
-
module.exports = plugin;
|
|
111
|
-
module.exports.default = plugin;
|
|
112
|
-
module.exports.fastifyPlugin = plugin;
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
// src/integrations/fastify.ts
|
|
117
|
-
var import_fastify_plugin = __toESM(require_plugin());
|
|
118
|
-
function createTenantContext(manager) {
|
|
119
|
-
const storage = new AsyncLocalStorage();
|
|
120
|
-
function getTenantOrNull() {
|
|
121
|
-
return storage.getStore();
|
|
122
|
-
}
|
|
123
|
-
function getTenant() {
|
|
124
|
-
const context = getTenantOrNull();
|
|
125
|
-
if (!context) {
|
|
126
|
-
throw new Error(
|
|
127
|
-
"[drizzle-multitenant] No tenant context found. Make sure you are calling this within runWithTenant()."
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
return context;
|
|
131
|
-
}
|
|
132
|
-
function getTenantId() {
|
|
133
|
-
return getTenant().tenantId;
|
|
134
|
-
}
|
|
135
|
-
function getTenantDb() {
|
|
136
|
-
const tenantId = getTenantId();
|
|
137
|
-
return manager.getDb(tenantId);
|
|
138
|
-
}
|
|
139
|
-
function getSharedDb() {
|
|
140
|
-
return manager.getSharedDb();
|
|
141
|
-
}
|
|
142
|
-
function isInTenantContext() {
|
|
143
|
-
return getTenantOrNull() !== void 0;
|
|
144
|
-
}
|
|
145
|
-
function runWithTenant(context, callback) {
|
|
146
|
-
if (!context.tenantId) {
|
|
147
|
-
throw new Error("[drizzle-multitenant] tenantId is required in context");
|
|
148
|
-
}
|
|
149
|
-
return storage.run(context, callback);
|
|
150
|
-
}
|
|
151
|
-
return {
|
|
152
|
-
runWithTenant,
|
|
153
|
-
getTenant,
|
|
154
|
-
getTenantOrNull,
|
|
155
|
-
getTenantId,
|
|
156
|
-
getTenantDb,
|
|
157
|
-
getSharedDb,
|
|
158
|
-
isInTenantContext
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// src/integrations/fastify.ts
|
|
163
|
-
var TenantNotFoundError = class extends Error {
|
|
164
|
-
constructor(message = "Tenant not found") {
|
|
165
|
-
super(message);
|
|
166
|
-
this.name = "TenantNotFoundError";
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
var TenantValidationError = class extends Error {
|
|
170
|
-
constructor(message = "Tenant validation failed") {
|
|
171
|
-
super(message);
|
|
172
|
-
this.name = "TenantValidationError";
|
|
173
|
-
}
|
|
174
|
-
};
|
|
175
|
-
function createFastifyPlugin(options) {
|
|
176
|
-
const { manager, extractTenantId, validateTenant, enrichContext, onError } = options;
|
|
177
|
-
const tenantContext = createTenantContext(manager);
|
|
178
|
-
const defaultErrorHandler = async (error, _req, reply) => {
|
|
179
|
-
if (error instanceof TenantNotFoundError) {
|
|
180
|
-
reply.status(400).send({ error: "Tenant ID is required" });
|
|
181
|
-
return;
|
|
182
|
-
}
|
|
183
|
-
if (error instanceof TenantValidationError) {
|
|
184
|
-
reply.status(403).send({ error: "Invalid tenant" });
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
reply.status(500).send({ error: "Internal server error" });
|
|
188
|
-
};
|
|
189
|
-
const errorHandler = onError ?? defaultErrorHandler;
|
|
190
|
-
const pluginImpl = async (fastify) => {
|
|
191
|
-
fastify.decorateRequest("tenantContext", void 0);
|
|
192
|
-
fastify.addHook("preHandler", async (req, reply) => {
|
|
193
|
-
let tenantId;
|
|
194
|
-
try {
|
|
195
|
-
tenantId = await extractTenantId(req);
|
|
196
|
-
} catch (error) {
|
|
197
|
-
await errorHandler(error, req, reply);
|
|
198
|
-
throw error;
|
|
199
|
-
}
|
|
200
|
-
if (!tenantId) {
|
|
201
|
-
const error = new TenantNotFoundError("Tenant ID not found in request");
|
|
202
|
-
await errorHandler(error, req, reply);
|
|
203
|
-
throw error;
|
|
204
|
-
}
|
|
205
|
-
if (validateTenant) {
|
|
206
|
-
try {
|
|
207
|
-
const isValid = await validateTenant(tenantId, req);
|
|
208
|
-
if (!isValid) {
|
|
209
|
-
const error = new TenantValidationError(`Tenant ${tenantId} validation failed`);
|
|
210
|
-
await errorHandler(error, req, reply);
|
|
211
|
-
throw error;
|
|
212
|
-
}
|
|
213
|
-
} catch (error) {
|
|
214
|
-
if (!(error instanceof TenantValidationError)) {
|
|
215
|
-
await errorHandler(error, req, reply);
|
|
216
|
-
}
|
|
217
|
-
throw error;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
const customContext = enrichContext ? await enrichContext(tenantId, req) : {};
|
|
221
|
-
const context = {
|
|
222
|
-
tenantId,
|
|
223
|
-
...customContext
|
|
224
|
-
};
|
|
225
|
-
req.tenantContext = context;
|
|
226
|
-
});
|
|
227
|
-
};
|
|
228
|
-
const plugin = (0, import_fastify_plugin.default)(pluginImpl, {
|
|
229
|
-
name: "drizzle-multitenant"
|
|
230
|
-
});
|
|
231
|
-
return { plugin, context: tenantContext };
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
export { TenantNotFoundError, TenantValidationError, createFastifyPlugin, createTenantContext };
|
|
235
|
-
//# sourceMappingURL=fastify.js.map
|
|
236
|
-
//# sourceMappingURL=fastify.js.map
|
|
1
|
+
import {AsyncLocalStorage}from'async_hooks';var N=Object.create;var k=Object.defineProperty;var z=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var j=Object.getPrototypeOf,H=Object.prototype.hasOwnProperty;var h=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports);var L=(e,t,n,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of A(t))!H.call(e,r)&&r!==n&&k(e,r,{get:()=>t[r],enumerable:!(a=z(t,r))||a.enumerable});return e};var M=(e,t,n)=>(n=e!=null?N(j(e)):{},L(k(n,"default",{value:e,enumerable:true}),e));var b=h((G,y)=>{var O=/at\s(?:.*\.)?plugin\s.*\n\s*(.*)/,W=/(\w*(\.\w*)*)\..*/;y.exports=function(t){if(t.name.length>0)return t.name;let n=Error.stackTraceLimit;Error.stackTraceLimit=10;try{throw new Error("anonymous function")}catch(a){return Error.stackTraceLimit=n,D(a.stack)}};function D(e){let t=e.match(O);return t?t[1].split(/[/\\]/).slice(-1)[0].match(W)[1]:"anonymous"}y.exports.extractPluginName=D;});var I=h((J,P)=>{P.exports=function(t){return t[0]==="@"&&(t=t.slice(1).replace("/","-")),t.replace(/-(.)/g,function(n,a){return a.toUpperCase()})};});var F=h((K,f)=>{var $=b(),B=I(),V=0;function C(e,t={}){let n=false;if(e.default!==void 0&&(e=e.default),typeof e!="function")throw new TypeError(`fastify-plugin expects a function, instead got a '${typeof e}'`);if(typeof t=="string"&&(t={fastify:t}),typeof t!="object"||Array.isArray(t)||t===null)throw new TypeError("The options object should be an object");if(t.fastify!==void 0&&typeof t.fastify!="string")throw new TypeError(`fastify-plugin expects a version string, instead got '${typeof t.fastify}'`);t.name||(n=true,t.name=$(e)+"-auto-"+V++),e[Symbol.for("skip-override")]=t.encapsulate!==true,e[Symbol.for("fastify.display-name")]=t.name,e[Symbol.for("plugin-meta")]=t,e.default||(e.default=e);let a=B(t.name);return !n&&!e[a]&&(e[a]=e),e}f.exports=C;f.exports.default=C;f.exports.fastifyPlugin=C;});var E=M(F());function p(e){let t=new AsyncLocalStorage;function n(){return t.getStore()}function a(){let i=n();if(!i)throw new Error("[drizzle-multitenant] No tenant context found. Make sure you are calling this within runWithTenant().");return i}function r(){return a().tenantId}function g(){let i=r();return e.getDb(i)}function x(){return e.getSharedDb()}function w(){return n()!==void 0}function u(i,S){if(!i.tenantId)throw new Error("[drizzle-multitenant] tenantId is required in context");return t.run(i,S)}return {runWithTenant:u,getTenant:a,getTenantOrNull:n,getTenantId:r,getTenantDb:g,getSharedDb:x,isInTenantContext:w}}var l=class extends Error{constructor(t="Tenant not found"){super(t),this.name="TenantNotFoundError";}},m=class extends Error{constructor(t="Tenant validation failed"){super(t),this.name="TenantValidationError";}};function Z(e){let{manager:t,extractTenantId:n,validateTenant:a,enrichContext:r,onError:g}=e,x=p(t),u=g??(async(T,s,c)=>{if(T instanceof l){c.status(400).send({error:"Tenant ID is required"});return}if(T instanceof m){c.status(403).send({error:"Invalid tenant"});return}c.status(500).send({error:"Internal server error"});});return {plugin:(0, E.default)(async T=>{T.decorateRequest("tenantContext",void 0),T.addHook("preHandler",async(s,c)=>{let d;try{d=await n(s);}catch(o){throw await u(o,s,c),o}if(!d){let o=new l("Tenant ID not found in request");throw await u(o,s,c),o}if(a)try{if(!await a(d,s)){let R=new m(`Tenant ${d} validation failed`);throw await u(R,s,c),R}}catch(o){throw o instanceof m||await u(o,s,c),o}let q=r?await r(d,s):{},v={tenantId:d,...q};s.tenantContext=v;});},{name:"drizzle-multitenant"}),context:x}}export{l as TenantNotFoundError,m as TenantValidationError,Z as createFastifyPlugin,p as createTenantContext};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as _nestjs_common from '@nestjs/common';
|
|
2
2
|
import { ModuleMetadata, Type, InjectionToken, DynamicModule, CanActivate, ExecutionContext, NestInterceptor, CallHandler, Provider } from '@nestjs/common';
|
|
3
3
|
import { Request } from 'express';
|
|
4
|
-
import { C as Config, a as TenantDb, T as TenantManager, S as SharedDb } from '../../types-
|
|
4
|
+
import { C as Config, a as TenantDb, T as TenantManager, S as SharedDb } from '../../types-CGqsPe2Q.js';
|
|
5
5
|
import { Reflector } from '@nestjs/core';
|
|
6
6
|
import { Observable } from 'rxjs';
|
|
7
7
|
import 'pg';
|