@rpcbase/api 0.14.0 → 0.16.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/package.json +1 -1
- package/src/initApi.ts +17 -4
- package/src/loadModel.ts +4 -4
package/package.json
CHANGED
package/src/initApi.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import assert from "assert"
|
|
2
|
-
import
|
|
2
|
+
import env from "@rpcbase/env"
|
|
3
3
|
import BBPromise from "bluebird"
|
|
4
4
|
import { Application , json } from "express"
|
|
5
5
|
|
|
@@ -9,10 +9,11 @@ import { Api, ApiHandler, Payload, Result, Middleware } from "./types"
|
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
// dotenv setup, merge process env with vite process.env file
|
|
12
|
+
|
|
12
13
|
process.env = {
|
|
13
14
|
...__vite_env__,
|
|
14
15
|
...process.env,
|
|
15
|
-
...
|
|
16
|
+
...env.config().parsed
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
|
|
@@ -68,10 +69,22 @@ export const initApi = async (app: Application) => {
|
|
|
68
69
|
|
|
69
70
|
const routeEntries = Object.entries(routes) as RouteModule[]
|
|
70
71
|
|
|
71
|
-
const loadRoute = async([routePath, loadFn]: RouteModule) => {
|
|
72
|
+
const loadRoute = async ([routePath, loadFn]: RouteModule) => {
|
|
72
73
|
const routeModule = await loadFn()
|
|
73
74
|
routeModule.default(api)
|
|
74
|
-
|
|
75
|
+
|
|
76
|
+
let loggedPath = routePath.startsWith("../../")
|
|
77
|
+
? routePath.replace("../../", "@rpcbase/")
|
|
78
|
+
: routePath
|
|
79
|
+
|
|
80
|
+
// Colorize output: gray for @rpcbase paths, default (white) otherwise
|
|
81
|
+
const GRAY = "\x1b[90m"
|
|
82
|
+
const RESET = "\x1b[0m"
|
|
83
|
+
const coloredPath = loggedPath.startsWith("@rpcbase/")
|
|
84
|
+
? `${GRAY}${loggedPath}${RESET}`
|
|
85
|
+
: loggedPath
|
|
86
|
+
|
|
87
|
+
console.log("loaded api route", coloredPath)
|
|
75
88
|
}
|
|
76
89
|
|
|
77
90
|
await BBPromise.map(routeEntries, loadRoute)
|
package/src/loadModel.ts
CHANGED
|
@@ -2,7 +2,7 @@ import assert from "assert"
|
|
|
2
2
|
import mongoose from "mongoose"
|
|
3
3
|
import { Ctx } from "./types"
|
|
4
4
|
|
|
5
|
-
const { APP_NAME,
|
|
5
|
+
const { APP_NAME, MONGO_PORT } = process.env
|
|
6
6
|
const connections: Record<string, mongoose.Connection> = {}
|
|
7
7
|
|
|
8
8
|
// @ts-ignore: Property 'glob' does not exist on type 'ImportMeta'.
|
|
@@ -39,10 +39,10 @@ export const loadModel = async (modelName: string, ctx: Ctx) => {
|
|
|
39
39
|
assert(tenantId, "Tenant ID is missing from session")
|
|
40
40
|
|
|
41
41
|
const dbName = ["User", "Tenant"].includes(modelName)
|
|
42
|
-
? `${APP_NAME}-users
|
|
43
|
-
: `${APP_NAME}-${tenantId}
|
|
42
|
+
? `${APP_NAME}-users-db`
|
|
43
|
+
: `${APP_NAME}-${tenantId}-db`
|
|
44
44
|
|
|
45
|
-
const connectionString = `mongodb://localhost
|
|
45
|
+
const connectionString = `mongodb://localhost:${MONGO_PORT}/${dbName}`
|
|
46
46
|
|
|
47
47
|
if (!connections[dbName]) {
|
|
48
48
|
const connection = mongoose.createConnection(connectionString, {
|