@budibase/backend-core 2.8.30 → 2.8.31-alpha.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.
Files changed (132) hide show
  1. package/dist/jest.config.js +10 -4
  2. package/dist/jest.config.js.map +1 -1
  3. package/dist/package.json +14 -26
  4. package/dist/src/auth/auth.js.map +1 -1
  5. package/dist/src/cache/appMetadata.d.ts +7 -1
  6. package/dist/src/cache/appMetadata.js +5 -8
  7. package/dist/src/cache/appMetadata.js.map +1 -1
  8. package/dist/src/cache/user.js.map +1 -1
  9. package/dist/src/constants/db.d.ts +1 -39
  10. package/dist/src/constants/db.js +8 -43
  11. package/dist/src/constants/db.js.map +1 -1
  12. package/dist/src/constants/misc.d.ts +2 -0
  13. package/dist/src/constants/misc.js +2 -0
  14. package/dist/src/constants/misc.js.map +1 -1
  15. package/dist/src/db/constants.d.ts +2 -0
  16. package/dist/src/db/constants.js +13 -0
  17. package/dist/src/db/constants.js.map +1 -0
  18. package/dist/src/db/couch/index.d.ts +1 -0
  19. package/dist/src/db/couch/index.js +1 -0
  20. package/dist/src/db/couch/index.js.map +1 -1
  21. package/dist/src/db/searchIndexes/searchIndexes.js.map +1 -1
  22. package/dist/src/db/utils.js +7 -2
  23. package/dist/src/db/utils.js.map +1 -1
  24. package/dist/src/db/views.d.ts +0 -1
  25. package/dist/src/db/views.js +1 -12
  26. package/dist/src/db/views.js.map +1 -1
  27. package/dist/src/environment.d.ts +11 -4
  28. package/dist/src/environment.js +21 -70
  29. package/dist/src/environment.js.map +1 -1
  30. package/dist/src/events/identification.js +3 -3
  31. package/dist/src/events/identification.js.map +1 -1
  32. package/dist/src/logging/index.d.ts +1 -1
  33. package/dist/src/logging/index.js +2 -3
  34. package/dist/src/logging/index.js.map +1 -1
  35. package/dist/src/logging/pino/logger.js +40 -24
  36. package/dist/src/logging/pino/logger.js.map +1 -1
  37. package/dist/src/logging/system.d.ts +9 -0
  38. package/dist/src/logging/system.js +101 -0
  39. package/dist/src/logging/system.js.map +1 -0
  40. package/dist/src/middleware/adminOnly.d.ts +2 -2
  41. package/dist/src/middleware/adminOnly.js +2 -2
  42. package/dist/src/middleware/adminOnly.js.map +1 -1
  43. package/dist/src/middleware/builderOnly.d.ts +2 -2
  44. package/dist/src/middleware/builderOnly.js +16 -2
  45. package/dist/src/middleware/builderOnly.js.map +1 -1
  46. package/dist/src/middleware/builderOrAdmin.d.ts +2 -2
  47. package/dist/src/middleware/builderOrAdmin.js +17 -4
  48. package/dist/src/middleware/builderOrAdmin.js.map +1 -1
  49. package/dist/src/security/permissions.d.ts +31 -18
  50. package/dist/src/security/permissions.js +46 -57
  51. package/dist/src/security/permissions.js.map +1 -1
  52. package/dist/src/security/roles.js +7 -4
  53. package/dist/src/security/roles.js.map +1 -1
  54. package/dist/src/users/db.d.ts +38 -0
  55. package/dist/src/users/db.js +407 -0
  56. package/dist/src/users/db.js.map +1 -0
  57. package/dist/src/users/events.d.ts +5 -0
  58. package/dist/src/users/events.js +169 -0
  59. package/dist/src/users/events.js.map +1 -0
  60. package/dist/src/users/index.d.ts +4 -0
  61. package/dist/src/users/index.js +23 -0
  62. package/dist/src/users/index.js.map +1 -0
  63. package/dist/src/users/lookup.d.ts +13 -0
  64. package/dist/src/users/lookup.js +112 -0
  65. package/dist/src/users/lookup.js.map +1 -0
  66. package/dist/src/{users.d.ts → users/users.d.ts} +4 -2
  67. package/dist/src/{users.js → users/users.js} +24 -4
  68. package/dist/src/users/users.js.map +1 -0
  69. package/dist/src/users/utils.d.ts +14 -0
  70. package/dist/src/users/utils.js +92 -0
  71. package/dist/src/users/utils.js.map +1 -0
  72. package/dist/tests/core/utilities/jestUtils.d.ts +7 -0
  73. package/dist/tests/core/utilities/jestUtils.js +14 -1
  74. package/dist/tests/core/utilities/jestUtils.js.map +1 -1
  75. package/dist/tests/core/utilities/mocks/events.d.ts +0 -1
  76. package/dist/tests/core/utilities/mocks/events.js +0 -1
  77. package/dist/tests/core/utilities/mocks/events.js.map +1 -1
  78. package/dist/tests/core/utilities/mocks/licenses.d.ts +1 -0
  79. package/dist/tests/core/utilities/mocks/licenses.js +8 -4
  80. package/dist/tests/core/utilities/mocks/licenses.js.map +1 -1
  81. package/dist/tests/core/utilities/structures/accounts.js +3 -3
  82. package/dist/tests/core/utilities/structures/accounts.js.map +1 -1
  83. package/dist/tests/core/utilities/structures/scim.js +1 -5
  84. package/dist/tests/core/utilities/structures/scim.js.map +1 -1
  85. package/dist/tests/core/utilities/structures/sso.js +2 -2
  86. package/dist/tests/core/utilities/structures/sso.js.map +1 -1
  87. package/dist/tests/core/utilities/structures/users.d.ts +3 -1
  88. package/dist/tests/core/utilities/structures/users.js +13 -1
  89. package/dist/tests/core/utilities/structures/users.js.map +1 -1
  90. package/dist/tsconfig.build.tsbuildinfo +1 -1
  91. package/jest.config.ts +1 -2
  92. package/package.json +14 -26
  93. package/scripts/test.sh +2 -2
  94. package/src/auth/auth.ts +1 -1
  95. package/src/cache/appMetadata.ts +10 -8
  96. package/src/cache/tests/writethrough.spec.ts +7 -7
  97. package/src/cache/user.ts +1 -1
  98. package/src/constants/db.ts +4 -42
  99. package/src/constants/misc.ts +2 -0
  100. package/src/db/constants.ts +10 -0
  101. package/src/db/couch/index.ts +1 -0
  102. package/src/db/searchIndexes/searchIndexes.ts +1 -1
  103. package/src/db/utils.ts +9 -3
  104. package/src/db/views.ts +0 -11
  105. package/src/environment.ts +24 -4
  106. package/src/events/identification.ts +3 -2
  107. package/src/logging/index.ts +1 -3
  108. package/src/logging/pino/logger.ts +45 -24
  109. package/src/logging/system.ts +81 -0
  110. package/src/logging/tests/system.spec.ts +61 -0
  111. package/src/middleware/adminOnly.ts +4 -6
  112. package/src/middleware/builderOnly.ts +15 -6
  113. package/src/middleware/builderOrAdmin.ts +16 -8
  114. package/src/middleware/tests/builder.spec.ts +180 -0
  115. package/src/security/permissions.ts +5 -21
  116. package/src/security/roles.ts +1 -1
  117. package/src/security/tests/permissions.spec.ts +1 -1
  118. package/src/users/db.ts +460 -0
  119. package/src/users/events.ts +176 -0
  120. package/src/users/index.ts +4 -0
  121. package/src/users/lookup.ts +102 -0
  122. package/src/{users.ts → users/users.ts} +33 -7
  123. package/src/users/utils.ts +55 -0
  124. package/tests/core/utilities/jestUtils.ts +21 -0
  125. package/tests/core/utilities/mocks/events.ts +0 -2
  126. package/tests/core/utilities/mocks/licenses.ts +7 -3
  127. package/tests/core/utilities/structures/accounts.ts +3 -5
  128. package/tests/core/utilities/structures/scim.ts +4 -5
  129. package/tests/core/utilities/structures/sso.ts +2 -2
  130. package/tests/core/utilities/structures/users.ts +19 -0
  131. package/tsconfig.json +2 -2
  132. package/dist/src/users.js.map +0 -1
package/jest.config.ts CHANGED
@@ -1,8 +1,6 @@
1
1
  import { Config } from "@jest/types"
2
- const preset = require("ts-jest/jest-preset")
3
2
 
4
3
  const baseConfig: Config.InitialProjectOptions = {
5
- ...preset,
6
4
  preset: "@trendyol/jest-testcontainers",
7
5
  setupFiles: ["./tests/jestEnv.ts"],
8
6
  setupFilesAfterEnv: ["./tests/jestSetup.ts"],
@@ -11,6 +9,7 @@ const baseConfig: Config.InitialProjectOptions = {
11
9
  },
12
10
  moduleNameMapper: {
13
11
  "@budibase/types": "<rootDir>/../types/src",
12
+ "@budibase/shared-core": ["<rootDir>/../shared-core/src"],
14
13
  },
15
14
  }
16
15
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@budibase/backend-core",
3
- "version": "2.8.30",
3
+ "version": "2.8.31-alpha.0",
4
4
  "description": "Budibase backend core libraries used in server and worker",
5
5
  "main": "dist/src/index.js",
6
6
  "types": "dist/src/index.d.ts",
@@ -16,14 +16,15 @@
16
16
  "prepack": "cp package.json dist",
17
17
  "build": "tsc -p tsconfig.build.json",
18
18
  "build:dev": "yarn prebuild && tsc --build --watch --preserveWatchOutput",
19
+ "check:types": "tsc -p tsconfig.json --noEmit --paths null",
19
20
  "test": "bash scripts/test.sh",
20
21
  "test:watch": "jest --watchAll"
21
22
  },
22
23
  "dependencies": {
23
24
  "@budibase/nano": "10.1.2",
24
25
  "@budibase/pouchdb-replication-stream": "1.2.10",
25
- "@budibase/types": "2.8.30",
26
- "@shopify/jest-koa-mocks": "5.0.1",
26
+ "@budibase/shared-core": "2.8.31-alpha.0",
27
+ "@budibase/types": "2.8.31-alpha.0",
27
28
  "@techpass/passport-openidconnect": "0.3.2",
28
29
  "aws-cloudfront-sign": "2.2.0",
29
30
  "aws-sdk": "2.1030.0",
@@ -51,18 +52,20 @@
51
52
  "pouchdb": "7.3.0",
52
53
  "pouchdb-find": "7.2.2",
53
54
  "redlock": "4.2.0",
55
+ "rotating-file-stream": "3.1.0",
54
56
  "sanitize-s3-objectkey": "0.0.1",
55
57
  "semver": "7.3.7",
56
58
  "tar-fs": "2.1.1",
57
59
  "uuid": "8.3.2"
58
60
  },
59
61
  "devDependencies": {
60
- "@jest/test-sequencer": "29.5.0",
61
- "@swc/core": "^1.3.25",
62
- "@swc/jest": "^0.2.24",
62
+ "@jest/test-sequencer": "29.6.2",
63
+ "@shopify/jest-koa-mocks": "5.1.1",
64
+ "@swc/core": "1.3.71",
65
+ "@swc/jest": "0.2.27",
63
66
  "@trendyol/jest-testcontainers": "^2.1.1",
64
67
  "@types/chance": "1.1.3",
65
- "@types/jest": "29.5.0",
68
+ "@types/jest": "29.5.3",
66
69
  "@types/koa": "2.13.4",
67
70
  "@types/lodash": "4.14.180",
68
71
  "@types/node": "14.18.20",
@@ -74,32 +77,17 @@
74
77
  "@types/uuid": "8.3.4",
75
78
  "chance": "1.1.8",
76
79
  "ioredis-mock": "8.7.0",
77
- "jest": "29.5.0",
78
- "jest-environment-node": "29.5.0",
79
- "jest-serial-runner": "^1.2.1",
80
+ "jest": "29.6.2",
81
+ "jest-environment-node": "29.6.2",
82
+ "jest-serial-runner": "1.2.1",
80
83
  "koa": "2.13.4",
81
84
  "nodemon": "2.0.16",
82
85
  "pino-pretty": "10.0.0",
83
86
  "pouchdb-adapter-memory": "7.2.2",
84
87
  "timekeeper": "2.2.0",
85
- "ts-jest": "29.0.5",
86
88
  "ts-node": "10.8.1",
87
89
  "tsconfig-paths": "4.0.0",
88
90
  "typescript": "4.7.3"
89
91
  },
90
- "nx": {
91
- "targets": {
92
- "build": {
93
- "dependsOn": [
94
- {
95
- "projects": [
96
- "@budibase/types"
97
- ],
98
- "target": "build"
99
- }
100
- ]
101
- }
102
- }
103
- },
104
- "gitHead": "0bb17a32b1120c5b2526983870dbec4ee8cd6086"
92
+ "gitHead": "38e2c52eb34c0fae42da6e7f346996c379cd281f"
105
93
  }
package/scripts/test.sh CHANGED
@@ -8,6 +8,6 @@ then
8
8
  jest --coverage --runInBand --forceExit
9
9
  else
10
10
  # --maxWorkers performs better in development
11
- echo "jest --coverage --forceExit"
12
- jest --coverage --forceExit
11
+ echo "jest --coverage --detectOpenHandles"
12
+ jest --coverage --detectOpenHandles
13
13
  fi
package/src/auth/auth.ts CHANGED
@@ -159,7 +159,7 @@ export async function updateUserOAuth(userId: string, oAuthConfig: any) {
159
159
 
160
160
  try {
161
161
  const db = getGlobalDB()
162
- const dbUser = await db.get(userId)
162
+ const dbUser = await db.get<any>(userId)
163
163
 
164
164
  //Do not overwrite the refresh token if a valid one is not provided.
165
165
  if (typeof details.refreshToken !== "string") {
@@ -2,9 +2,14 @@ import { getAppClient } from "../redis/init"
2
2
  import { doWithDB, DocumentType } from "../db"
3
3
  import { Database, App } from "@budibase/types"
4
4
 
5
- const AppState = {
6
- INVALID: "invalid",
5
+ export enum AppState {
6
+ INVALID = "invalid",
7
7
  }
8
+
9
+ export interface DeletedApp {
10
+ state: AppState
11
+ }
12
+
8
13
  const EXPIRY_SECONDS = 3600
9
14
 
10
15
  /**
@@ -31,7 +36,7 @@ function isInvalid(metadata?: { state: string }) {
31
36
  * @param {string} appId the id of the app to get metadata from.
32
37
  * @returns {object} the app metadata.
33
38
  */
34
- export async function getAppMetadata(appId: string) {
39
+ export async function getAppMetadata(appId: string): Promise<App | DeletedApp> {
35
40
  const client = await getAppClient()
36
41
  // try cache
37
42
  let metadata = await client.get(appId)
@@ -61,11 +66,8 @@ export async function getAppMetadata(appId: string) {
61
66
  }
62
67
  await client.store(appId, metadata, expiry)
63
68
  }
64
- // we've stored in the cache an object to tell us that it is currently invalid
65
- if (isInvalid(metadata)) {
66
- throw { status: 404, message: "No app metadata found" }
67
- }
68
- return metadata as App
69
+
70
+ return metadata
69
71
  }
70
72
 
71
73
  /**
@@ -36,7 +36,7 @@ describe("writethrough", () => {
36
36
  _id: docId,
37
37
  value: 1,
38
38
  })
39
- const output = await db.get(response.id)
39
+ const output = await db.get<any>(response.id)
40
40
  current = output
41
41
  expect(output.value).toBe(1)
42
42
  })
@@ -45,7 +45,7 @@ describe("writethrough", () => {
45
45
  it("second put shouldn't update DB", async () => {
46
46
  await config.doInTenant(async () => {
47
47
  const response = await writethrough.put({ ...current, value: 2 })
48
- const output = await db.get(response.id)
48
+ const output = await db.get<any>(response.id)
49
49
  expect(current._rev).toBe(output._rev)
50
50
  expect(output.value).toBe(1)
51
51
  })
@@ -55,7 +55,7 @@ describe("writethrough", () => {
55
55
  await config.doInTenant(async () => {
56
56
  tk.freeze(Date.now() + DELAY + 1)
57
57
  const response = await writethrough.put({ ...current, value: 3 })
58
- const output = await db.get(response.id)
58
+ const output = await db.get<any>(response.id)
59
59
  expect(response.rev).not.toBe(current._rev)
60
60
  expect(output.value).toBe(3)
61
61
 
@@ -79,7 +79,7 @@ describe("writethrough", () => {
79
79
  expect.arrayContaining([current._rev, current._rev, newRev])
80
80
  )
81
81
 
82
- const output = await db.get(current._id)
82
+ const output = await db.get<any>(current._id)
83
83
  expect(output.value).toBe(4)
84
84
  expect(output._rev).toBe(newRev)
85
85
 
@@ -107,7 +107,7 @@ describe("writethrough", () => {
107
107
  })
108
108
  expect(res.ok).toBe(true)
109
109
 
110
- const output = await db.get(id)
110
+ const output = await db.get<any>(id)
111
111
  expect(output.value).toBe(3)
112
112
  expect(output._rev).toBe(res.rev)
113
113
  })
@@ -130,8 +130,8 @@ describe("writethrough", () => {
130
130
  const resp2 = await writethrough2.put({ _id: "db1", value: "second" })
131
131
  expect(resp1.rev).toBeDefined()
132
132
  expect(resp2.rev).toBeDefined()
133
- expect((await db.get("db1")).value).toBe("first")
134
- expect((await db2.get("db1")).value).toBe("second")
133
+ expect((await db.get<any>("db1")).value).toBe("first")
134
+ expect((await db2.get<any>("db1")).value).toBe("second")
135
135
  })
136
136
  })
137
137
  })
package/src/cache/user.ts CHANGED
@@ -12,7 +12,7 @@ const EXPIRY_SECONDS = 3600
12
12
  */
13
13
  async function populateFromDB(userId: string, tenantId: string) {
14
14
  const db = tenancy.getTenantDB(tenantId)
15
- const user = await db.get(userId)
15
+ const user = await db.get<any>(userId)
16
16
  user.budibaseAccess = true
17
17
  if (!env.SELF_HOSTED && !env.DISABLE_ACCOUNT_PORTAL) {
18
18
  const account = await accounts.getAccount(user.email)
@@ -1,5 +1,5 @@
1
- export const SEPARATOR = "_"
2
- export const UNICODE_MAX = "\ufff0"
1
+ import { prefixed, DocumentType } from "@budibase/types"
2
+ export { SEPARATOR, UNICODE_MAX, DocumentType } from "@budibase/types"
3
3
 
4
4
  /**
5
5
  * Can be used to create a few different forms of querying a view.
@@ -14,8 +14,6 @@ export enum ViewName {
14
14
  USER_BY_APP = "by_app",
15
15
  USER_BY_EMAIL = "by_email2",
16
16
  BY_API_KEY = "by_api_key",
17
- /** @deprecated - could be deleted */
18
- USER_BY_BUILDERS = "by_builders",
19
17
  LINK = "by_link",
20
18
  ROUTING = "screen_routes",
21
19
  AUTOMATION_LOGS = "automation_logs",
@@ -36,42 +34,6 @@ export enum InternalTable {
36
34
  USER_METADATA = "ta_users",
37
35
  }
38
36
 
39
- export enum DocumentType {
40
- USER = "us",
41
- GROUP = "gr",
42
- WORKSPACE = "workspace",
43
- CONFIG = "config",
44
- TEMPLATE = "template",
45
- APP = "app",
46
- DEV = "dev",
47
- APP_DEV = "app_dev",
48
- APP_METADATA = "app_metadata",
49
- ROLE = "role",
50
- MIGRATIONS = "migrations",
51
- DEV_INFO = "devinfo",
52
- AUTOMATION_LOG = "log_au",
53
- ACCOUNT_METADATA = "acc_metadata",
54
- PLUGIN = "plg",
55
- DATASOURCE = "datasource",
56
- DATASOURCE_PLUS = "datasource_plus",
57
- APP_BACKUP = "backup",
58
- TABLE = "ta",
59
- ROW = "ro",
60
- AUTOMATION = "au",
61
- LINK = "li",
62
- WEBHOOK = "wh",
63
- INSTANCE = "inst",
64
- LAYOUT = "layout",
65
- SCREEN = "screen",
66
- QUERY = "query",
67
- DEPLOYMENTS = "deployments",
68
- METADATA = "metadata",
69
- MEM_VIEW = "view",
70
- USER_FLAG = "flag",
71
- AUTOMATION_METADATA = "meta_au",
72
- AUDIT_LOG = "al",
73
- }
74
-
75
37
  export const StaticDatabases = {
76
38
  GLOBAL: {
77
39
  name: "global-db",
@@ -95,7 +57,7 @@ export const StaticDatabases = {
95
57
  },
96
58
  }
97
59
 
98
- export const APP_PREFIX = DocumentType.APP + SEPARATOR
99
- export const APP_DEV = DocumentType.APP_DEV + SEPARATOR
60
+ export const APP_PREFIX = prefixed(DocumentType.APP)
61
+ export const APP_DEV = prefixed(DocumentType.APP_DEV)
100
62
  export const APP_DEV_PREFIX = APP_DEV
101
63
  export const BUDIBASE_DATASOURCE_TYPE = "budibase"
@@ -20,6 +20,8 @@ export enum Header {
20
20
  TYPE = "x-budibase-type",
21
21
  PREVIEW_ROLE = "x-budibase-role",
22
22
  TENANT_ID = "x-budibase-tenant-id",
23
+ VERIFICATION_CODE = "x-budibase-verification-code",
24
+ RETURN_VERIFICATION_CODE = "x-budibase-return-verification-code",
23
25
  TOKEN = "x-budibase-token",
24
26
  CSRF_TOKEN = "x-csrf-token",
25
27
  CORRELATION_ID = "x-budibase-correlation-id",
@@ -0,0 +1,10 @@
1
+ export const CONSTANT_INTERNAL_ROW_COLS = [
2
+ "_id",
3
+ "_rev",
4
+ "type",
5
+ "createdAt",
6
+ "updatedAt",
7
+ "tableId",
8
+ ] as const
9
+
10
+ export const CONSTANT_EXTERNAL_ROW_COLS = ["_id", "_rev", "tableId"] as const
@@ -2,3 +2,4 @@ export * from "./connections"
2
2
  export * from "./DatabaseImpl"
3
3
  export * from "./utils"
4
4
  export { init, getPouch, getPouchDB, closePouchDB } from "./pouchDB"
5
+ export * from "../constants"
@@ -5,7 +5,7 @@ export async function createUserIndex() {
5
5
  const db = getGlobalDB()
6
6
  let designDoc
7
7
  try {
8
- designDoc = await db.get("_design/database")
8
+ designDoc = await db.get<any>("_design/database")
9
9
  } catch (err: any) {
10
10
  if (err.status === 404) {
11
11
  designDoc = { _id: "_design/database" }
package/src/db/utils.ts CHANGED
@@ -2,7 +2,7 @@ import env from "../environment"
2
2
  import { DEFAULT_TENANT_ID, SEPARATOR, DocumentType } from "../constants"
3
3
  import { getTenantId, getGlobalDBName } from "../context"
4
4
  import { doWithDB, directCouchAllDbs } from "./db"
5
- import { getAppMetadata } from "../cache/appMetadata"
5
+ import { AppState, DeletedApp, getAppMetadata } from "../cache/appMetadata"
6
6
  import { isDevApp, isDevAppID, getProdAppID } from "../docIds/conversions"
7
7
  import { App, Database } from "@budibase/types"
8
8
  import { getStartEndKeyURL } from "../docIds"
@@ -101,7 +101,9 @@ export async function getAllApps({
101
101
  const response = await Promise.allSettled(appPromises)
102
102
  const apps = response
103
103
  .filter(
104
- (result: any) => result.status === "fulfilled" && result.value != null
104
+ (result: any) =>
105
+ result.status === "fulfilled" &&
106
+ result.value?.state !== AppState.INVALID
105
107
  )
106
108
  .map(({ value }: any) => value)
107
109
  if (!all) {
@@ -126,7 +128,11 @@ export async function getAppsByIDs(appIds: string[]) {
126
128
  )
127
129
  // have to list the apps which exist, some may have been deleted
128
130
  return settled
129
- .filter(promise => promise.status === "fulfilled")
131
+ .filter(
132
+ promise =>
133
+ promise.status === "fulfilled" &&
134
+ (promise.value as DeletedApp).state !== AppState.INVALID
135
+ )
130
136
  .map(promise => (promise as PromiseFulfilledResult<App>).value)
131
137
  }
132
138
 
package/src/db/views.ts CHANGED
@@ -105,16 +105,6 @@ export const createApiKeyView = async () => {
105
105
  await createView(db, viewJs, ViewName.BY_API_KEY)
106
106
  }
107
107
 
108
- export const createUserBuildersView = async () => {
109
- const db = getGlobalDB()
110
- const viewJs = `function(doc) {
111
- if (doc.builder && doc.builder.global === true) {
112
- emit(doc._id, doc._id)
113
- }
114
- }`
115
- await createView(db, viewJs, ViewName.USER_BY_BUILDERS)
116
- }
117
-
118
108
  export interface QueryViewOptions {
119
109
  arrayResponse?: boolean
120
110
  }
@@ -223,7 +213,6 @@ export const queryPlatformView = async <T>(
223
213
  const CreateFuncByName: any = {
224
214
  [ViewName.USER_BY_EMAIL]: createNewUserEmailView,
225
215
  [ViewName.BY_API_KEY]: createApiKeyView,
226
- [ViewName.USER_BY_BUILDERS]: createUserBuildersView,
227
216
  [ViewName.USER_BY_APP]: createUserAppView,
228
217
  }
229
218
 
@@ -1,4 +1,5 @@
1
1
  import { existsSync, readFileSync } from "fs"
2
+ import { ServiceType } from "@budibase/types"
2
3
 
3
4
  function isTest() {
4
5
  return isCypress() || isJest()
@@ -47,7 +48,10 @@ function httpLogging() {
47
48
  return process.env.HTTP_LOGGING
48
49
  }
49
50
 
50
- function findVersion() {
51
+ function getPackageJsonFields(): {
52
+ VERSION: string
53
+ SERVICE_NAME: string
54
+ } {
51
55
  function findFileInAncestors(
52
56
  fileName: string,
53
57
  currentDir: string
@@ -69,17 +73,31 @@ function findVersion() {
69
73
  try {
70
74
  const packageJsonFile = findFileInAncestors("package.json", process.cwd())
71
75
  const content = readFileSync(packageJsonFile!, "utf-8")
72
- return JSON.parse(content).version
76
+ const parsedContent = JSON.parse(content)
77
+ return {
78
+ VERSION: parsedContent.version,
79
+ SERVICE_NAME: parsedContent.name,
80
+ }
73
81
  } catch {
74
82
  // throwing an error here is confusing/causes backend-core to be hard to import
75
- return undefined
83
+ return { VERSION: "", SERVICE_NAME: "" }
76
84
  }
77
85
  }
78
86
 
87
+ function isWorker() {
88
+ return environment.SERVICE_TYPE === ServiceType.WORKER
89
+ }
90
+
91
+ function isApps() {
92
+ return environment.SERVICE_TYPE === ServiceType.APPS
93
+ }
94
+
79
95
  const environment = {
80
96
  isTest,
81
97
  isJest,
82
98
  isDev,
99
+ isWorker,
100
+ isApps,
83
101
  isProd: () => {
84
102
  return !isDev()
85
103
  },
@@ -146,6 +164,7 @@ const environment = {
146
164
  SMTP_FROM_ADDRESS: process.env.SMTP_FROM_ADDRESS,
147
165
  DISABLE_JWT_WARNING: process.env.DISABLE_JWT_WARNING,
148
166
  BLACKLIST_IPS: process.env.BLACKLIST_IPS,
167
+ SERVICE_TYPE: "unknown",
149
168
  /**
150
169
  * Enable to allow an admin user to login using a password.
151
170
  * This can be useful to prevent lockout when configuring SSO.
@@ -154,7 +173,7 @@ const environment = {
154
173
  ENABLE_SSO_MAINTENANCE_MODE: selfHosted
155
174
  ? process.env.ENABLE_SSO_MAINTENANCE_MODE
156
175
  : false,
157
- VERSION: findVersion(),
176
+ ...getPackageJsonFields(),
158
177
  DISABLE_PINO_LOGGER: process.env.DISABLE_PINO_LOGGER,
159
178
  OFFLINE_MODE: process.env.OFFLINE_MODE,
160
179
  _set(key: any, value: any) {
@@ -162,6 +181,7 @@ const environment = {
162
181
  // @ts-ignore
163
182
  environment[key] = value
164
183
  },
184
+ ROLLING_LOG_MAX_SIZE: process.env.ROLLING_LOG_MAX_SIZE || "10M",
165
185
  }
166
186
 
167
187
  // clean up any environment variable edge cases
@@ -21,6 +21,7 @@ import { processors } from "./processors"
21
21
  import { newid } from "../utils"
22
22
  import * as installation from "../installation"
23
23
  import * as configs from "../configs"
24
+ import * as users from "../users"
24
25
  import { withCache, TTL, CacheKey } from "../cache/generic"
25
26
 
26
27
  /**
@@ -164,8 +165,8 @@ const identifyUser = async (
164
165
  const id = user._id as string
165
166
  const tenantId = await getEventTenantId(user.tenantId)
166
167
  const type = IdentityType.USER
167
- let builder = user.builder?.global || false
168
- let admin = user.admin?.global || false
168
+ let builder = users.hasBuilderPermissions(user)
169
+ let admin = users.hasAdminPermissions(user)
169
170
  let providerType
170
171
  if (isSSOUser(user)) {
171
172
  providerType = user.providerType
@@ -1,6 +1,4 @@
1
1
  export * as correlation from "./correlation/correlation"
2
2
  export { logger } from "./pino/logger"
3
3
  export * from "./alerts"
4
-
5
- // turn off or on context logging i.e. tenantId, appId etc
6
- export let LOG_CONTEXT = true
4
+ export * as system from "./system"
@@ -1,37 +1,60 @@
1
- import env from "../../environment"
2
1
  import pino, { LoggerOptions } from "pino"
2
+ import pinoPretty from "pino-pretty"
3
+
4
+ import { IdentityType } from "@budibase/types"
5
+ import env from "../../environment"
3
6
  import * as context from "../../context"
4
7
  import * as correlation from "../correlation"
5
- import { IdentityType } from "@budibase/types"
6
- import { LOG_CONTEXT } from "../index"
8
+
9
+ import { localFileDestination } from "../system"
7
10
 
8
11
  // LOGGER
9
12
 
10
13
  let pinoInstance: pino.Logger | undefined
11
14
  if (!env.DISABLE_PINO_LOGGER) {
15
+ const level = env.LOG_LEVEL
12
16
  const pinoOptions: LoggerOptions = {
13
- level: env.LOG_LEVEL,
17
+ level,
14
18
  formatters: {
15
- level: label => {
16
- return { level: label.toUpperCase() }
19
+ level: level => {
20
+ return { level: level.toUpperCase() }
17
21
  },
18
22
  bindings: () => {
19
- return {}
23
+ if (env.SELF_HOSTED) {
24
+ // "service" is being injected in datadog using the pod names,
25
+ // so we should leave it blank to allow the default behaviour if it's not running self-hosted
26
+ return {
27
+ service: env.SERVICE_NAME,
28
+ }
29
+ } else {
30
+ return {}
31
+ }
20
32
  },
21
33
  },
22
34
  timestamp: () => `,"timestamp":"${new Date(Date.now()).toISOString()}"`,
23
35
  }
24
36
 
25
- if (env.isDev()) {
26
- pinoOptions.transport = {
27
- target: "pino-pretty",
28
- options: {
29
- singleLine: true,
30
- },
31
- }
37
+ const destinations: pino.StreamEntry[] = []
38
+
39
+ destinations.push(
40
+ env.isDev()
41
+ ? {
42
+ stream: pinoPretty({ singleLine: true }),
43
+ level: level as pino.Level,
44
+ }
45
+ : { stream: process.stdout, level: level as pino.Level }
46
+ )
47
+
48
+ if (env.SELF_HOSTED) {
49
+ destinations.push({
50
+ stream: localFileDestination(),
51
+ level: level as pino.Level,
52
+ })
32
53
  }
33
54
 
34
- pinoInstance = pino(pinoOptions)
55
+ pinoInstance = destinations.length
56
+ ? pino(pinoOptions, pino.multistream(destinations))
57
+ : pino(pinoOptions)
35
58
 
36
59
  // CONSOLE OVERRIDES
37
60
 
@@ -83,15 +106,13 @@ if (!env.DISABLE_PINO_LOGGER) {
83
106
 
84
107
  let contextObject = {}
85
108
 
86
- if (LOG_CONTEXT) {
87
- contextObject = {
88
- tenantId: getTenantId(),
89
- appId: getAppId(),
90
- automationId: getAutomationId(),
91
- identityId: identity?._id,
92
- identityType: identity?.type,
93
- correlationId: correlation.getId(),
94
- }
109
+ contextObject = {
110
+ tenantId: getTenantId(),
111
+ appId: getAppId(),
112
+ automationId: getAutomationId(),
113
+ identityId: identity?._id,
114
+ identityType: identity?.type,
115
+ correlationId: correlation.getId(),
95
116
  }
96
117
 
97
118
  const mergingObject: any = {