@rpcbase/db 0.1.0 → 0.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/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +82 -1
- package/dist/loadModel.d.ts +13 -0
- package/dist/loadModel.d.ts.map +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA;AACxB,cAAc,UAAU,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA;AACxB,cAAc,UAAU,CAAA;AACxB,cAAc,aAAa,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { Schema, isValidObjectId, Types, SchemaTypes } from "mongoose";
|
|
1
|
+
import mongoose, { Schema, isValidObjectId, Types, SchemaTypes } from "mongoose";
|
|
2
2
|
import { z, ZodMap, ZodRecord, ZodAny, ZodUnion, ZodNullable, ZodOptional, ZodDefault, ZodDate, ZodEnum, ZodBoolean, ZodArray, ZodObject, ZodNumber, ZodString } from "zod";
|
|
3
3
|
export * from "zod";
|
|
4
4
|
import { z as z2 } from "zod";
|
|
5
|
+
import assert from "assert";
|
|
5
6
|
const ZUser = z.object({
|
|
6
7
|
email: z.string().email().optional(),
|
|
7
8
|
password: z.string(),
|
|
@@ -20,6 +21,11 @@ const UserSchema = new Schema({
|
|
|
20
21
|
email_verification_code: { type: String, required: false },
|
|
21
22
|
email_verification_expires_at: { type: Date, required: false }
|
|
22
23
|
});
|
|
24
|
+
const __vite_glob_0_1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
25
|
+
__proto__: null,
|
|
26
|
+
UserSchema,
|
|
27
|
+
ZUser
|
|
28
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
23
29
|
const ZTenant = z.object({
|
|
24
30
|
tenant_id: z.string(),
|
|
25
31
|
parent_tenant_id: z.string().optional(),
|
|
@@ -30,6 +36,18 @@ const TenantSchema = new Schema({
|
|
|
30
36
|
parent_tenant_id: { type: String },
|
|
31
37
|
name: { type: String }
|
|
32
38
|
});
|
|
39
|
+
const __vite_glob_0_0 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
40
|
+
__proto__: null,
|
|
41
|
+
TenantSchema,
|
|
42
|
+
ZTenant
|
|
43
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
44
|
+
const frameworkSchemas = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
45
|
+
__proto__: null,
|
|
46
|
+
TenantSchema,
|
|
47
|
+
UserSchema,
|
|
48
|
+
ZTenant,
|
|
49
|
+
ZUser
|
|
50
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
33
51
|
const zmAssert$4 = {
|
|
34
52
|
string(f) {
|
|
35
53
|
return f.constructor.name === "ZodString";
|
|
@@ -635,12 +653,75 @@ function parseMixed(required = true, def) {
|
|
|
635
653
|
required
|
|
636
654
|
};
|
|
637
655
|
}
|
|
656
|
+
const { APP_NAME, DB_PORT } = process.env;
|
|
657
|
+
const connections = {};
|
|
658
|
+
let cachedModels = null;
|
|
659
|
+
const buildModels = () => {
|
|
660
|
+
const externalModelModules = globalThis.__rb_model_modules;
|
|
661
|
+
const modelModules = externalModelModules ?? /* @__PURE__ */ Object.assign({ "/src/models/Tenant.ts": __vite_glob_0_0, "/src/models/User.ts": __vite_glob_0_1, "/src/models/index.ts": frameworkSchemas });
|
|
662
|
+
const appModels = Object.entries(modelModules).flatMap(([key, exports$1]) => {
|
|
663
|
+
const modelName = key.split("/").pop()?.replace(/\.(t|j)sx?$/, "");
|
|
664
|
+
assert(modelName, "Model name not found");
|
|
665
|
+
const schemaEntry = Object.entries(exports$1).find(([key2]) => key2 === `${modelName}Schema`);
|
|
666
|
+
if (!schemaEntry) return [];
|
|
667
|
+
const schema = schemaEntry[1];
|
|
668
|
+
if (!(schema instanceof mongoose.Schema)) return [];
|
|
669
|
+
return [{
|
|
670
|
+
name: modelName,
|
|
671
|
+
model: mongoose.models[modelName] ?? mongoose.model(modelName, schema)
|
|
672
|
+
}];
|
|
673
|
+
});
|
|
674
|
+
const frameworkModels = Object.entries(frameworkSchemas).filter(([_, schema]) => schema instanceof mongoose.Schema).map(([key, schema]) => {
|
|
675
|
+
const name = key.replace(/Schema$/, "");
|
|
676
|
+
return {
|
|
677
|
+
name,
|
|
678
|
+
model: mongoose.models[name] ?? mongoose.model(name, schema)
|
|
679
|
+
};
|
|
680
|
+
});
|
|
681
|
+
return [...frameworkModels, ...appModels].reduce((acc, { name, model }) => {
|
|
682
|
+
acc[name] = model;
|
|
683
|
+
return acc;
|
|
684
|
+
}, {});
|
|
685
|
+
};
|
|
686
|
+
const loadModel = async (modelName, ctx) => {
|
|
687
|
+
const models = cachedModels ?? (cachedModels = buildModels());
|
|
688
|
+
const tenantId = ctx.req.session?.user?.current_tenant_id || "00000000";
|
|
689
|
+
assert(tenantId, "Tenant ID is missing from session");
|
|
690
|
+
const dbName = ["User", "Tenant"].includes(modelName) ? `${APP_NAME}-users-db` : `${APP_NAME}-${tenantId}-db`;
|
|
691
|
+
const connectionString = `mongodb://localhost:${DB_PORT}/${dbName}`;
|
|
692
|
+
if (!connections[dbName]) {
|
|
693
|
+
const connection = mongoose.createConnection(connectionString, {
|
|
694
|
+
sanitizeFilter: true
|
|
695
|
+
});
|
|
696
|
+
await new Promise((resolve, reject) => {
|
|
697
|
+
connection.once("open", resolve);
|
|
698
|
+
connection.on("error", reject);
|
|
699
|
+
});
|
|
700
|
+
connections[dbName] = connection;
|
|
701
|
+
} else {
|
|
702
|
+
const connection = connections[dbName];
|
|
703
|
+
if (connection.readyState !== 1) {
|
|
704
|
+
await new Promise((resolve, reject) => {
|
|
705
|
+
connection.once("open", resolve);
|
|
706
|
+
connection.on("error", reject);
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
const modelConnection = connections[dbName];
|
|
711
|
+
const model = models[modelName];
|
|
712
|
+
assert(model, `Model ${modelName} not registered. Available models: ${Object.keys(models).join(", ")}`);
|
|
713
|
+
if (!modelConnection.models[modelName]) {
|
|
714
|
+
modelConnection.model(modelName, model.schema);
|
|
715
|
+
}
|
|
716
|
+
return modelConnection.models[modelName];
|
|
717
|
+
};
|
|
638
718
|
export {
|
|
639
719
|
TenantSchema,
|
|
640
720
|
UserSchema,
|
|
641
721
|
ZTenant,
|
|
642
722
|
ZUser,
|
|
643
723
|
extendZod,
|
|
724
|
+
loadModel,
|
|
644
725
|
z2 as z,
|
|
645
726
|
zId,
|
|
646
727
|
zUUID,
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { default as mongoose } from '../../vite/node_modules/mongoose';
|
|
2
|
+
type LoadModelCtx = {
|
|
3
|
+
req: {
|
|
4
|
+
session?: {
|
|
5
|
+
user?: {
|
|
6
|
+
current_tenant_id?: string;
|
|
7
|
+
};
|
|
8
|
+
} | null;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export declare const loadModel: (modelName: string, ctx: LoadModelCtx) => Promise<mongoose.Model<any, {}, {}, {}, any, any>>;
|
|
12
|
+
export type { LoadModelCtx };
|
|
13
|
+
//# sourceMappingURL=loadModel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loadModel.d.ts","sourceRoot":"","sources":["../src/loadModel.ts"],"names":[],"mappings":"AAEA,OAAO,QAAQ,MAAM,UAAU,CAAA;AAK/B,KAAK,YAAY,GAAG;IAClB,GAAG,EAAE;QACH,OAAO,CAAC,EAAE;YACR,IAAI,CAAC,EAAE;gBACL,iBAAiB,CAAC,EAAE,MAAM,CAAC;aAC5B,CAAA;SACF,GAAG,IAAI,CAAC;KACV,CAAC;CACH,CAAC;AA+CF,eAAO,MAAM,SAAS,GAAU,WAAW,MAAM,EAAE,KAAK,YAAY,uDA2CnE,CAAA;AAED,YAAY,EAAE,YAAY,EAAE,CAAA"}
|