forked from geturbackend/urBackend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyncSchema.controller.js
More file actions
230 lines (197 loc) · 6.8 KB
/
Copy pathsyncSchema.controller.js
File metadata and controls
230 lines (197 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
const { z } = require("zod");
const {
Project,
AppError,
ApiResponse,
syncSchemaPayload,
resolveEffectivePlan,
getPlanLimits,
deleteProjectById,
deleteProjectByApiKeyCache,
setProjectById,
getProjectAccessQuery,
} = require("@urbackend/common");
// ── Helpers (mirrored from project.controller.js) ───────────────────────────
const normalizeFieldKey = (key) =>
String(key || "")
.replace(/\uFEFF/g, "")
.trim();
const normalizeFieldType = (type) =>
String(type || "")
.trim()
.toLowerCase();
const isRequiredField = (required) =>
required === true ||
required === 1 ||
String(required).trim().toLowerCase() === "true" ||
String(required).trim() === "1";
const toPlainObject = (value) => {
if (!value || typeof value !== "object") return value;
if (typeof value.toObject === "function") {
return value.toObject({ depopulate: true });
}
if (value._doc && typeof value._doc === "object") {
return { ...value._doc };
}
return value;
};
const sanitizeSchemaFields = (schema = []) => {
if (!Array.isArray(schema)) return [];
return schema
.map((rawField) => {
const field = toPlainObject(rawField);
if (!field || typeof field !== "object") return null;
const normalizedKey = normalizeFieldKey(field.key);
if (!normalizedKey) return null;
const next = { ...field, key: normalizedKey };
if (field.default !== undefined) {
next.default = field.default;
}
if (Array.isArray(field.fields)) {
next.fields = sanitizeSchemaFields(field.fields);
}
if (field.items && typeof field.items === "object") {
next.items = { ...field.items };
if (Array.isArray(field.items.fields)) {
next.items.fields = sanitizeSchemaFields(field.items.fields);
}
}
return next;
})
.filter(Boolean);
};
const getDefaultRlsForCollection = (collectionName, schema = []) => {
const normalizedName = String(collectionName || "").toLowerCase();
const keys = sanitizeSchemaFields(schema).map((f) => f.key);
let ownerField = "userId";
if (normalizedName === "users") {
ownerField = "_id";
} else if (keys.includes("userId")) {
ownerField = "userId";
} else if (keys.includes("ownerId")) {
ownerField = "ownerId";
}
return {
enabled: false,
mode: "public-read",
ownerField,
requireAuthForWrite: true,
};
};
/**
* Validates that a users collection schema contains the required
* `email` (String, required) and `password` (String, required) fields.
*/
const validateUsersSchema = (schema) => {
if (!Array.isArray(schema)) return false;
const sanitized = sanitizeSchemaFields(schema);
const hasEmail = sanitized.find(
(f) =>
normalizeFieldKey(f.key).toLowerCase() === "email" &&
normalizeFieldType(f.type) === "string" &&
isRequiredField(f.required),
);
const hasPassword = sanitized.find(
(f) =>
normalizeFieldKey(f.key).toLowerCase() === "password" &&
normalizeFieldType(f.type) === "string" &&
isRequiredField(f.required),
);
return !!(hasEmail && hasPassword);
};
// ── Controller ──────────────────────────────────────────────────────────────
/**
* PUT /projects/:projectId/sync-schema
*
* Atomically replaces the project's collection schema definitions.
*
* Behaviour:
* - Preserves RLS settings for collections that already exist.
* - Applies safe defaults for newly introduced collections.
* - Enforces plan-based collection limits.
* - Validates the `users` collection contract (email + password required).
* - Does NOT drop underlying MongoDB collections — only updates config.
*/
module.exports.syncSchema = async (req, res, next) => {
try {
// 1. Validate payload
const { collections: incoming } = syncSchemaPayload.parse(req.body);
const { projectId } = req.params;
// 2. Load project (authorizeProject middleware already attached req.project)
const project = req.project;
if (!project) {
return next(new AppError(404, "Project not found or access denied"));
}
// 3. Plan enforcement — check collection count limit
if (req.developer) {
const effectivePlan = resolveEffectivePlan(req.developer);
const limits = getPlanLimits({
plan: effectivePlan,
customLimits: project.customLimits,
});
if (limits.maxCollections !== -1 && incoming.length > limits.maxCollections) {
return next(
new AppError(
403,
`Schema sync would create ${incoming.length} collections, but your plan allows ${limits.maxCollections}. Please upgrade your plan.`,
),
);
}
}
// 4. Build a lookup of existing collections for RLS preservation
const existingByName = new Map();
for (const col of project.collections || []) {
const plain = toPlainObject(col);
existingByName.set(plain.name, plain);
}
// 5. Merge: sanitize fields, preserve RLS, validate users contract
const merged = [];
for (const entry of incoming) {
const sanitizedModel = sanitizeSchemaFields(entry.model || []);
// Enforce users schema contract
if (entry.name.toLowerCase() === "users") {
if (!validateUsersSchema(sanitizedModel)) {
return next(
new AppError(
422,
"The 'users' collection must have required 'email' and 'password' String fields.",
),
);
}
}
const existing = existingByName.get(entry.name);
merged.push({
name: entry.name,
model: sanitizedModel,
rls: existing?.rls || getDefaultRlsForCollection(entry.name, sanitizedModel),
});
}
// 6. Atomic update — single $set, no partial states
const updated = await Project.findOneAndUpdate(
{ _id: projectId, ...getProjectAccessQuery(req.user._id) },
{ $set: { collections: merged } },
{ new: true },
);
if (!updated) {
return next(new AppError(404, "Project not found or access denied"));
}
// 7. Invalidate caches so subsequent reads are fresh
await deleteProjectById(projectId);
await setProjectById(projectId, updated.toObject());
await deleteProjectByApiKeyCache(updated.publishableKey);
await deleteProjectByApiKeyCache(updated.secretKey);
// 8. Respond
return new ApiResponse(
{
synced: merged.length,
collections: merged.map((c) => c.name),
},
`Successfully synced ${merged.length} collection schema(s).`,
).send(res);
} catch (err) {
if (err instanceof z.ZodError) {
return next(new AppError(400, err.issues[0]?.message || "Invalid schema payload"));
}
next(err);
}
};