@rpcbase/api 0.50.0 → 0.51.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 +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -66
- package/package.json +2 -4
- package/dist/loadModel.d.ts +0 -4
- package/dist/loadModel.d.ts.map +0 -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,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -16,8 +16,6 @@ import require$$1$4 from "node:net";
|
|
|
16
16
|
import require$$13 from "stream";
|
|
17
17
|
import require$$14 from "util";
|
|
18
18
|
import { initApiClient } from "@rpcbase/client";
|
|
19
|
-
import mongoose from "mongoose";
|
|
20
|
-
import * as frameworkSchemas from "@rpcbase/db";
|
|
21
19
|
const MOCK_TYPE = "MOCK_TYPE";
|
|
22
20
|
var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
|
|
23
21
|
function getDefaultExportFromCjs(x) {
|
|
@@ -21157,70 +21155,7 @@ const initApi = async ({
|
|
|
21157
21155
|
res.status(404).json({ error: `Not Found: ${req.method}:${req.originalUrl}` });
|
|
21158
21156
|
});
|
|
21159
21157
|
};
|
|
21160
|
-
const { APP_NAME, DB_PORT } = process.env;
|
|
21161
|
-
const connections = {};
|
|
21162
|
-
let cachedModels = null;
|
|
21163
|
-
const buildModels = () => {
|
|
21164
|
-
const externalModelModules = globalThis.__rb_model_modules;
|
|
21165
|
-
const modelModules = externalModelModules ?? /* @__PURE__ */ Object.assign({});
|
|
21166
|
-
const appModels = Object.entries(modelModules).flatMap(([key, exports$1]) => {
|
|
21167
|
-
const modelName = key.split("/").pop()?.replace(/\.(t|j)sx?$/, "");
|
|
21168
|
-
assert(modelName, "Model name not found");
|
|
21169
|
-
const schemaEntry = Object.entries(exports$1).find(([key2]) => key2 === `${modelName}Schema`);
|
|
21170
|
-
if (!schemaEntry) return [];
|
|
21171
|
-
const schema = schemaEntry[1];
|
|
21172
|
-
if (!(schema instanceof mongoose.Schema)) return [];
|
|
21173
|
-
return [{
|
|
21174
|
-
name: modelName,
|
|
21175
|
-
model: mongoose.models[modelName] ?? mongoose.model(modelName, schema)
|
|
21176
|
-
}];
|
|
21177
|
-
});
|
|
21178
|
-
const frameworkModels = Object.entries(frameworkSchemas).filter(([_, schema]) => schema instanceof mongoose.Schema).map(([key, schema]) => {
|
|
21179
|
-
const name = key.replace(/Schema$/, "");
|
|
21180
|
-
return {
|
|
21181
|
-
name,
|
|
21182
|
-
model: mongoose.models[name] ?? mongoose.model(name, schema)
|
|
21183
|
-
};
|
|
21184
|
-
});
|
|
21185
|
-
return [...frameworkModels, ...appModels].reduce((acc, { name, model }) => {
|
|
21186
|
-
acc[name] = model;
|
|
21187
|
-
return acc;
|
|
21188
|
-
}, {});
|
|
21189
|
-
};
|
|
21190
|
-
const loadModel = async (modelName, ctx) => {
|
|
21191
|
-
const models = cachedModels ?? (cachedModels = buildModels());
|
|
21192
|
-
const tenantId = ctx.req.session.user?.current_tenant_id || "00000000";
|
|
21193
|
-
assert(tenantId, "Tenant ID is missing from session");
|
|
21194
|
-
const dbName = ["User", "Tenant"].includes(modelName) ? `${APP_NAME}-users-db` : `${APP_NAME}-${tenantId}-db`;
|
|
21195
|
-
const connectionString = `mongodb://localhost:${DB_PORT}/${dbName}`;
|
|
21196
|
-
if (!connections[dbName]) {
|
|
21197
|
-
const connection = mongoose.createConnection(connectionString, {
|
|
21198
|
-
sanitizeFilter: true
|
|
21199
|
-
});
|
|
21200
|
-
await new Promise((resolve, reject) => {
|
|
21201
|
-
connection.once("open", resolve);
|
|
21202
|
-
connection.on("error", reject);
|
|
21203
|
-
});
|
|
21204
|
-
connections[dbName] = connection;
|
|
21205
|
-
} else {
|
|
21206
|
-
const connection = connections[dbName];
|
|
21207
|
-
if (connection.readyState !== 1) {
|
|
21208
|
-
await new Promise((resolve, reject) => {
|
|
21209
|
-
connection.once("open", resolve);
|
|
21210
|
-
connection.on("error", reject);
|
|
21211
|
-
});
|
|
21212
|
-
}
|
|
21213
|
-
}
|
|
21214
|
-
const modelConnection = connections[dbName];
|
|
21215
|
-
const model = models[modelName];
|
|
21216
|
-
assert(model, `Model ${modelName} not registered. Available models: ${Object.keys(models).join(", ")}`);
|
|
21217
|
-
if (!modelConnection.models[modelName]) {
|
|
21218
|
-
modelConnection.model(modelName, model.schema);
|
|
21219
|
-
}
|
|
21220
|
-
return modelConnection.models[modelName];
|
|
21221
|
-
};
|
|
21222
21158
|
export {
|
|
21223
21159
|
MOCK_TYPE,
|
|
21224
|
-
initApi
|
|
21225
|
-
loadModel
|
|
21160
|
+
initApi
|
|
21226
21161
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rpcbase/api",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.51.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -52,9 +52,7 @@
|
|
|
52
52
|
}
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {},
|
|
55
|
-
"peerDependencies": {
|
|
56
|
-
"mongoose": "^8"
|
|
57
|
-
},
|
|
55
|
+
"peerDependencies": {},
|
|
58
56
|
"devDependencies": {
|
|
59
57
|
"bluebird": "3.7.2",
|
|
60
58
|
"express": "5.1.0"
|
package/dist/loadModel.d.ts
DELETED
package/dist/loadModel.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"loadModel.d.ts","sourceRoot":"","sources":["../src/loadModel.ts"],"names":[],"mappings":"AAEA,OAAO,QAAQ,MAAM,UAAU,CAAA;AAG/B,OAAO,EAAE,GAAG,EAAE,MAAM,SAAS,CAAA;AAgD7B,eAAO,MAAM,SAAS,GAAU,WAAW,MAAM,EAAE,KAAK,GAAG,uDAmD1D,CAAA"}
|