remult 3.5.0-next.2 → 3.5.0-next.3
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/esm/server/remult-admin-html.js +6 -4
- package/esm/server/remult-admin.js +101 -7
- package/esm/server/remult-api-server.js +6 -3
- package/esm/src/data-providers/array-entity-data-provider.js +3 -3
- package/esm/src/data-providers/sql-database.js +2 -1
- package/esm/src/remult3/RepositoryImplementation.js +1 -1
- package/package.json +1 -1
- package/server/get-remult-admin-html.js +1 -1
- package/server/index.d.ts +2 -1
- package/server/remult-admin-html.js +6 -4
- package/server/remult-admin.d.ts +31 -2
- package/server/remult-admin.js +168 -19
- package/server/remult-api-server.d.ts +14 -0
- package/server/remult-api-server.js +28 -22
- package/src/data-providers/array-entity-data-provider.js +3 -3
- package/src/data-providers/sql-database.js +2 -1
- package/src/remult3/RepositoryImplementation.js +1 -1
- package/src/remult3/remult3.d.ts +3 -3
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { getHtml } from './get-remult-admin-html.js';
|
|
2
2
|
export default function remultAdminHtml(options) {
|
|
3
3
|
const { head, rootPath, requireAuthToken, disableLiveQuery } = options;
|
|
4
|
-
|
|
5
|
-
.replace('<!--PLACE_HERE_HEAD-->', head)
|
|
6
|
-
.replace('<!--PLACE_HERE_BODY-->', `<script>
|
|
4
|
+
const body = `<script>
|
|
7
5
|
window.optionsFromServer = ${JSON.stringify({
|
|
8
6
|
rootPath,
|
|
9
7
|
requireAuthToken,
|
|
10
8
|
disableLiveQuery,
|
|
11
9
|
})}
|
|
12
|
-
</script
|
|
10
|
+
</script>`;
|
|
11
|
+
// replacer functions: a string replacement would expand `$&` and friends
|
|
12
|
+
return getHtml()
|
|
13
|
+
.replace('<!--PLACE_HERE_HEAD-->', () => head)
|
|
14
|
+
.replace('<!--PLACE_HERE_BODY-->', () => body);
|
|
13
15
|
}
|
|
@@ -63,7 +63,7 @@ export function buildEntityInfo(options) {
|
|
|
63
63
|
}
|
|
64
64
|
fields.push({
|
|
65
65
|
key: x.key,
|
|
66
|
-
readOnly:
|
|
66
|
+
readOnly: readOnlyOf(x),
|
|
67
67
|
values: getValueList(x),
|
|
68
68
|
valFieldKey,
|
|
69
69
|
caption: x.label,
|
|
@@ -87,13 +87,11 @@ export function buildEntityInfo(options) {
|
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
if (metadata.apiReadAllowed) {
|
|
90
|
-
let superKey = metadata.key;
|
|
91
|
-
let caption = metadata.label;
|
|
92
90
|
const nbOfEntities = entities.filter((e) => e.key === metadata.key).length;
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
91
|
+
const superKey = adminSuperKey(metadata.key, nbOfEntities);
|
|
92
|
+
const caption = metadata.label + '*'.repeat(nbOfEntities);
|
|
93
|
+
const config = options.entityConfig?.(metadata, options.remult) ?? true;
|
|
94
|
+
const custom = typeof config === 'object' ? config : undefined;
|
|
97
95
|
entities.push({
|
|
98
96
|
superKey,
|
|
99
97
|
key: metadata.key,
|
|
@@ -102,8 +100,104 @@ export function buildEntityInfo(options) {
|
|
|
102
100
|
fields,
|
|
103
101
|
relations,
|
|
104
102
|
defaultOrderBy: metadata.options.defaultOrderBy ?? { id: 'asc' },
|
|
103
|
+
hidden: config === false,
|
|
104
|
+
editMode: custom?.editMode ?? 'inline',
|
|
105
|
+
editModeLocked: custom?.lockEditMode ?? false,
|
|
106
|
+
apiPrefilter: metadata.options.apiPrefilter !== undefined,
|
|
107
|
+
permissions: {
|
|
108
|
+
insert: resolveAdminPermission(options.remult, metadata.options.allowApiInsert),
|
|
109
|
+
update: resolveAdminPermission(options.remult, metadata.options.allowApiUpdate),
|
|
110
|
+
delete: resolveAdminPermission(options.remult, metadata.options.allowApiDelete),
|
|
111
|
+
},
|
|
112
|
+
rules: {
|
|
113
|
+
read: adminRule(metadata.options.allowApiRead),
|
|
114
|
+
insert: adminRule(metadata.options.allowApiInsert),
|
|
115
|
+
update: adminRule(metadata.options.allowApiUpdate),
|
|
116
|
+
delete: adminRule(metadata.options.allowApiDelete),
|
|
117
|
+
},
|
|
105
118
|
});
|
|
106
119
|
}
|
|
107
120
|
}
|
|
108
121
|
return entities;
|
|
109
122
|
}
|
|
123
|
+
/** First occurrence of a key is the key itself, later ones get `_ext_n`. */
|
|
124
|
+
function adminSuperKey(key, nbOfPrevious) {
|
|
125
|
+
return nbOfPrevious > 0 ? key + '_ext_' + nbOfPrevious : key;
|
|
126
|
+
}
|
|
127
|
+
function findAdminEntity(entities, remult, superKey) {
|
|
128
|
+
const seen = {};
|
|
129
|
+
for (const entity of entities) {
|
|
130
|
+
const metadata = remult.repo(entity).metadata;
|
|
131
|
+
if (!metadata.apiReadAllowed)
|
|
132
|
+
continue;
|
|
133
|
+
const nb = seen[metadata.key] ?? 0;
|
|
134
|
+
seen[metadata.key] = nb + 1;
|
|
135
|
+
if (adminSuperKey(metadata.key, nb) === superKey)
|
|
136
|
+
return entity;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/** Keeps `apiPrefilter` only where it narrows rows for this user: an admin whose prefilter is `{}` sees everything. */
|
|
140
|
+
export async function resolveAdminPrefilters(info, entities, remult) {
|
|
141
|
+
for (const e of info) {
|
|
142
|
+
if (!e.apiPrefilter)
|
|
143
|
+
continue;
|
|
144
|
+
const entity = findAdminEntity(entities, remult, e.superKey);
|
|
145
|
+
if (!entity)
|
|
146
|
+
continue;
|
|
147
|
+
const metadata = remult.repo(entity).metadata;
|
|
148
|
+
const prefilter = metadata.options.apiPrefilter;
|
|
149
|
+
try {
|
|
150
|
+
const where = typeof prefilter === 'function' ? await prefilter() : prefilter;
|
|
151
|
+
e.apiPrefilter =
|
|
152
|
+
Object.keys(Filter.entityFilterToJson(metadata, where ?? {}) ?? {})
|
|
153
|
+
.length > 0;
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
// can't tell: say it may narrow
|
|
157
|
+
e.apiPrefilter = true;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return info;
|
|
161
|
+
}
|
|
162
|
+
const needsRow = () => {
|
|
163
|
+
throw new Error('needs the row');
|
|
164
|
+
};
|
|
165
|
+
// any look at this row throws: a rule that never reads it is decided now
|
|
166
|
+
const rowProbe = new Proxy({}, {
|
|
167
|
+
get: needsRow,
|
|
168
|
+
has: needsRow,
|
|
169
|
+
ownKeys: needsRow,
|
|
170
|
+
getOwnPropertyDescriptor: needsRow,
|
|
171
|
+
getPrototypeOf: needsRow,
|
|
172
|
+
});
|
|
173
|
+
function readOnlyOf(field) {
|
|
174
|
+
try {
|
|
175
|
+
return !field.apiUpdateAllowed();
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
// the rule needs the row; the server still enforces it per row
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
export function adminRule(rule) {
|
|
183
|
+
if (Array.isArray(rule)) {
|
|
184
|
+
const kinds = rule.map(adminRule);
|
|
185
|
+
if (kinds.includes('function'))
|
|
186
|
+
return 'function';
|
|
187
|
+
return kinds.includes('role') ? 'roles' : 'bool';
|
|
188
|
+
}
|
|
189
|
+
if (typeof rule === 'function')
|
|
190
|
+
return 'function';
|
|
191
|
+
return typeof rule === 'string' ? 'role' : 'bool';
|
|
192
|
+
}
|
|
193
|
+
export function resolveAdminPermission(remult, rule) {
|
|
194
|
+
if (rule === undefined)
|
|
195
|
+
return false;
|
|
196
|
+
try {
|
|
197
|
+
return remult.isAllowedForInstance(rowProbe, rule) === true;
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
// the rule reads the row (or fails without it): the server decides per row
|
|
201
|
+
return 'row';
|
|
202
|
+
}
|
|
203
|
+
}
|
|
@@ -12,7 +12,7 @@ import { getEntityKey } from '../src/remult3/getEntityRef.js';
|
|
|
12
12
|
import { Action, classBackendMethodsArray } from '../src/server-action.js';
|
|
13
13
|
import { serverActionField } from '../src/server-action-info.js';
|
|
14
14
|
import { remultStatic } from '../src/remult-static.js';
|
|
15
|
-
import { buildEntityInfo } from './remult-admin.js';
|
|
15
|
+
import { buildEntityInfo, resolveAdminPrefilters, } from './remult-admin.js';
|
|
16
16
|
import { isOfType } from '../src/isOfType.js';
|
|
17
17
|
import { initDataProviderOrJson } from './initDataProviderOrJson.js';
|
|
18
18
|
import { modulesFlatAndOrdered } from './module.js';
|
|
@@ -231,10 +231,13 @@ export class RemultServerImplementation {
|
|
|
231
231
|
: this.options.admin;
|
|
232
232
|
if (remult.isAllowed(allowed)) {
|
|
233
233
|
if (orig?.params?.id === '__entities-metadata') {
|
|
234
|
-
res.success(buildEntityInfo({
|
|
234
|
+
res.success(await resolveAdminPrefilters(buildEntityInfo({
|
|
235
235
|
remult,
|
|
236
236
|
entities: this.entities,
|
|
237
|
-
|
|
237
|
+
entityConfig: isOfType(this.options.admin, 'allow')
|
|
238
|
+
? this.options.admin.entities
|
|
239
|
+
: undefined,
|
|
240
|
+
}), this.entities, remult));
|
|
238
241
|
}
|
|
239
242
|
else {
|
|
240
243
|
let head = '<title>Admin</title>';
|
|
@@ -58,7 +58,7 @@ export class ArrayEntityDataProvider {
|
|
|
58
58
|
finishGroup(result) {
|
|
59
59
|
result[key] = {
|
|
60
60
|
...result[key],
|
|
61
|
-
avg: sum / count,
|
|
61
|
+
avg: count ? sum / count : null,
|
|
62
62
|
};
|
|
63
63
|
sum = 0;
|
|
64
64
|
count = 0;
|
|
@@ -76,7 +76,7 @@ export class ArrayEntityDataProvider {
|
|
|
76
76
|
}
|
|
77
77
|
},
|
|
78
78
|
finishGroup(result) {
|
|
79
|
-
result[key] = { ...result[key], min };
|
|
79
|
+
result[key] = { ...result[key], min: min ?? null };
|
|
80
80
|
min = undefined;
|
|
81
81
|
},
|
|
82
82
|
};
|
|
@@ -92,7 +92,7 @@ export class ArrayEntityDataProvider {
|
|
|
92
92
|
}
|
|
93
93
|
},
|
|
94
94
|
finishGroup(result) {
|
|
95
|
-
result[key] = { ...result[key], max };
|
|
95
|
+
result[key] = { ...result[key], max: max ?? null };
|
|
96
96
|
max = undefined;
|
|
97
97
|
},
|
|
98
98
|
};
|
|
@@ -724,7 +724,8 @@ export async function buildGroupBySql(options, e, r, nativeNullSort, limitSyntax
|
|
|
724
724
|
}
|
|
725
725
|
const turnToNumber = x.valueType === Number || operator == 'distinctCount';
|
|
726
726
|
processResultRow.push((sqlResult, theResult) => {
|
|
727
|
-
|
|
727
|
+
// AVG, MIN and MAX are NULL when there is no value; keep it instead of turning it into 0
|
|
728
|
+
if (turnToNumber && (sqlResult != null || operator === 'sum'))
|
|
728
729
|
sqlResult = Number(sqlResult);
|
|
729
730
|
if (operator === 'max' || operator === 'min')
|
|
730
731
|
sqlResult = fromDbColumn(x, sqlResult, jsonColumnStorage);
|
|
@@ -2334,7 +2334,7 @@ class EntityFullInfo {
|
|
|
2334
2334
|
: this.getEntityMetadataWithoutBreakingTheEntity(item).apiDeleteAllowed;
|
|
2335
2335
|
}
|
|
2336
2336
|
apiInsertAllowed(item) {
|
|
2337
|
-
if (this.options.
|
|
2337
|
+
if (this.options.allowApiInsert === undefined)
|
|
2338
2338
|
return false;
|
|
2339
2339
|
return !item
|
|
2340
2340
|
? this.remult.isAllowedForInstance(undefined, this.options.allowApiInsert)
|