@oondemand/oon-core-back 0.4.4 → 0.4.6

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/core/define.js +36 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oondemand/oon-core-back",
3
- "version": "0.4.4",
3
+ "version": "0.4.6",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/oondemand/oon-platform.git",
@@ -7,6 +7,7 @@ const auth = require("./middlewares/auth");
7
7
  const { rbac } = require("./middlewares/rbac");
8
8
  const { requirePermission } = require("./middlewares/permissions");
9
9
  const { mutationAudit } = require("./middlewares/mutationAudit");
10
+ const { dataScope, tenantField, userField } = require("./tenancy");
10
11
 
11
12
  function buildSchema(schemaDef, options = {}) {
12
13
  const mongooseShape = {};
@@ -41,6 +42,37 @@ function buildSchema(schemaDef, options = {}) {
41
42
  return { schema, metaFields, searchableFields };
42
43
  }
43
44
 
45
+ function addDataScopeFields(schema, definition) {
46
+ const scope = dataScope(definition);
47
+ if (!["tenant", "user"].includes(scope)) return schema;
48
+
49
+ const fields = {};
50
+ const scopedTenantField = tenantField(definition);
51
+ if (!schema.path(scopedTenantField)) {
52
+ fields[scopedTenantField] = {
53
+ type: String,
54
+ required: true,
55
+ index: true,
56
+ immutable: true,
57
+ };
58
+ }
59
+
60
+ if (scope === "user") {
61
+ const scopedUserField = userField(definition);
62
+ if (!schema.path(scopedUserField)) {
63
+ fields[scopedUserField] = {
64
+ type: String,
65
+ required: true,
66
+ index: true,
67
+ immutable: true,
68
+ };
69
+ }
70
+ }
71
+
72
+ if (Object.keys(fields).length) schema.add(fields);
73
+ return schema;
74
+ }
75
+
44
76
  function defineModel(definition) {
45
77
  if (!definition?.name) throw new Error("defineModel requer `name`.");
46
78
  if (!definition?.schema) throw new Error(`defineModel("${definition.name}") requer \`schema\`.`);
@@ -49,6 +81,9 @@ function defineModel(definition) {
49
81
  definition.schema,
50
82
  definition.options
51
83
  );
84
+ // Campos de isolamento são internos: fazem parte do schema e dos índices,
85
+ // mas não vazam para metadata/formulários declarativos.
86
+ addDataScopeFields(schema, definition);
52
87
 
53
88
  schema.pre("save", async function (next) {
54
89
  try {
@@ -151,6 +186,7 @@ function defineRoutes(basePath, builder) {
151
186
  }
152
187
 
153
188
  module.exports = {
189
+ addDataScopeFields,
154
190
  defineModel,
155
191
  defineCentral,
156
192
  defineValidation,