@rebasepro/common 0.0.1-canary.2 → 0.0.1-canary.4d4fb3e
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/README.md +1 -1
- package/dist/collections/CollectionRegistry.d.ts +2 -2
- package/dist/data/buildRebaseData.d.ts +14 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.es.js +398 -815
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +418 -835
- package/dist/index.umd.js.map +1 -1
- package/dist/util/builders.d.ts +6 -13
- package/dist/util/collections.d.ts +2 -2
- package/dist/util/entities.d.ts +16 -8
- package/dist/util/index.d.ts +1 -11
- package/dist/util/navigation_from_path.d.ts +10 -5
- package/dist/util/navigation_utils.d.ts +1 -12
- package/dist/util/permissions.d.ts +4 -4
- package/dist/util/references.d.ts +1 -1
- package/dist/util/relations.d.ts +4 -4
- package/dist/util/resolutions.d.ts +10 -12
- package/dist/util/storage.d.ts +4 -4
- package/package.json +11 -16
- package/src/collections/CollectionRegistry.ts +96 -33
- package/src/data/buildRebaseData.ts +221 -0
- package/src/index.ts +1 -0
- package/src/types/json-logic-js.d.ts +8 -0
- package/src/util/builders.ts +8 -18
- package/src/util/callbacks.ts +3 -4
- package/src/util/collections.ts +4 -4
- package/src/util/conditions.ts +4 -4
- package/src/util/entities.ts +37 -12
- package/src/util/index.ts +1 -11
- package/src/util/navigation_from_path.ts +8 -7
- package/src/util/navigation_utils.ts +4 -56
- package/src/util/parent_references_from_path.ts +1 -1
- package/src/util/permissions.test.ts +24 -28
- package/src/util/permissions.ts +14 -14
- package/src/util/references.ts +16 -2
- package/src/util/relations.ts +23 -19
- package/src/util/resolutions.ts +31 -62
- package/src/util/storage.ts +5 -5
- package/dist/util/arrays.d.ts +0 -1
- package/dist/util/dates.d.ts +0 -1
- package/dist/util/entity_actions.d.ts +0 -2
- package/dist/util/fields.d.ts +0 -2
- package/dist/util/flatten_object.d.ts +0 -5
- package/dist/util/hash.d.ts +0 -1
- package/dist/util/names.d.ts +0 -22
- package/dist/util/objects.d.ts +0 -26
- package/dist/util/os.d.ts +0 -2
- package/dist/util/plurals.d.ts +0 -16
- package/dist/util/regexp.d.ts +0 -7
- package/dist/util/strings.d.ts +0 -7
- package/src/util/arrays.ts +0 -3
- package/src/util/dates.ts +0 -1
- package/src/util/entity_actions.ts +0 -28
- package/src/util/fields.ts +0 -28
- package/src/util/flatten_object.ts +0 -45
- package/src/util/hash.ts +0 -11
- package/src/util/names.ts +0 -30
- package/src/util/objects.ts +0 -376
- package/src/util/os.ts +0 -13
- package/src/util/plurals.ts +0 -188
- package/src/util/regexp.ts +0 -32
- package/src/util/strings.ts +0 -84
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { canCreateEntity, canEditEntity, canDeleteEntity, canReadCollection } from "./permissions";
|
|
2
|
-
import { EntityCollection, AuthController, Entity, User } from "@rebasepro/types";
|
|
2
|
+
import { EntityCollection, AuthController, Entity, User, SecurityRule } from "@rebasepro/types";
|
|
3
3
|
|
|
4
4
|
describe("Permissions Evaluator", () => {
|
|
5
5
|
|
|
@@ -25,7 +25,7 @@ describe("Permissions Evaluator", () => {
|
|
|
25
25
|
roles: ["admin"]
|
|
26
26
|
};
|
|
27
27
|
|
|
28
|
-
const mockAuthController: AuthController<
|
|
28
|
+
const mockAuthController: AuthController<User> = {
|
|
29
29
|
user: mockUser,
|
|
30
30
|
initialLoading: false,
|
|
31
31
|
authLoading: false,
|
|
@@ -33,32 +33,32 @@ describe("Permissions Evaluator", () => {
|
|
|
33
33
|
signOut: async () => { },
|
|
34
34
|
getAuthToken: async () => "token",
|
|
35
35
|
setExtra: () => { },
|
|
36
|
-
extra:
|
|
36
|
+
extra: undefined
|
|
37
37
|
};
|
|
38
38
|
|
|
39
|
-
const adminAuthController: AuthController<
|
|
39
|
+
const adminAuthController: AuthController<User> = {
|
|
40
40
|
...mockAuthController,
|
|
41
41
|
user: adminUser
|
|
42
42
|
};
|
|
43
43
|
|
|
44
|
-
const unauthenticatedController: AuthController<
|
|
44
|
+
const unauthenticatedController: AuthController<User> = {
|
|
45
45
|
...mockAuthController,
|
|
46
46
|
user: null
|
|
47
47
|
};
|
|
48
48
|
|
|
49
|
-
const createMockCollection = (rules?:
|
|
50
|
-
|
|
49
|
+
const createMockCollection = (rules?: SecurityRule[]): EntityCollection => ({
|
|
50
|
+
slug: "test",
|
|
51
51
|
name: "Test",
|
|
52
|
+
table: "test",
|
|
52
53
|
properties: {},
|
|
53
54
|
securityRules: rules
|
|
54
|
-
}
|
|
55
|
+
});
|
|
55
56
|
|
|
56
|
-
const createMockEntity = (values: Record<string,
|
|
57
|
+
const createMockEntity = (values: Record<string, unknown>): Entity => ({
|
|
57
58
|
id: "entity-123",
|
|
58
59
|
path: "test",
|
|
59
60
|
values,
|
|
60
|
-
|
|
61
|
-
} as any);
|
|
61
|
+
});
|
|
62
62
|
|
|
63
63
|
// ─── Section 1: Core Defaults ─────────────────────────────────────────────────
|
|
64
64
|
|
|
@@ -75,19 +75,11 @@ describe("Permissions Evaluator", () => {
|
|
|
75
75
|
expect(canReadCollection(collection, mockAuthController)).toBe(true);
|
|
76
76
|
});
|
|
77
77
|
|
|
78
|
-
test("3.
|
|
79
|
-
const collection = createMockCollection(
|
|
78
|
+
test("3. Undefined securityRules falls back to true", () => {
|
|
79
|
+
const collection = createMockCollection(undefined);
|
|
80
80
|
expect(canReadCollection(collection, mockAuthController)).toBe(true);
|
|
81
81
|
});
|
|
82
82
|
|
|
83
|
-
test("4. Collection group ALWAYS blocks create, any other operation fine", () => {
|
|
84
|
-
const collection = { ...createMockCollection(), collectionGroup: true };
|
|
85
|
-
expect(canCreateEntity(collection, mockAuthController, "test", null)).toBe(false);
|
|
86
|
-
expect(canReadCollection(collection, mockAuthController)).toBe(true);
|
|
87
|
-
expect(canEditEntity(collection, mockAuthController, "test", null)).toBe(true);
|
|
88
|
-
expect(canDeleteEntity(collection, mockAuthController, "test", null)).toBe(true);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
83
|
// ─── Section 2: Operation Filtering ──────────────────────────────────────────
|
|
92
84
|
|
|
93
85
|
test("5. Only rules for matching operation are applied", () => {
|
|
@@ -138,7 +130,7 @@ describe("Permissions Evaluator", () => {
|
|
|
138
130
|
|
|
139
131
|
test("8. Unknown operation name in rule is silently ignored", () => {
|
|
140
132
|
const collection = createMockCollection([
|
|
141
|
-
{ operation: "read" as
|
|
133
|
+
{ operation: "read" as never, access: "public" } // "read" not a valid op
|
|
142
134
|
]);
|
|
143
135
|
// "read" does not match "select", so it is never applied → denied
|
|
144
136
|
expect(canReadCollection(collection, mockAuthController)).toBe(false);
|
|
@@ -319,13 +311,17 @@ describe("Permissions Evaluator", () => {
|
|
|
319
311
|
expect(canEditEntity(collection, mockAuthController, "test", createMockEntity({ author_uid: "other" }))).toBe(false);
|
|
320
312
|
});
|
|
321
313
|
|
|
322
|
-
test("28. AST: complex SQL with AND/OR
|
|
314
|
+
test("28. AST: complex SQL with AND/OR is evaluated by the parser", () => {
|
|
323
315
|
const collection = createMockCollection([
|
|
324
316
|
{ operation: "update", using: "status IN ('published', 'draft') AND author_id = current_setting('app.user_id')" }
|
|
325
317
|
]);
|
|
326
|
-
//
|
|
327
|
-
const
|
|
328
|
-
expect(canEditEntity(collection, mockAuthController, "test",
|
|
318
|
+
// IN clause is optimistic true, but AND requires author_id to match user uid
|
|
319
|
+
const mismatchEntity = createMockEntity({ status: "archived", author_id: "someone-else" });
|
|
320
|
+
expect(canEditEntity(collection, mockAuthController, "test", mismatchEntity)).toBe(false);
|
|
321
|
+
|
|
322
|
+
// When author_id matches the current user, AND passes
|
|
323
|
+
const matchingEntity = createMockEntity({ status: "published", author_id: "user-123" });
|
|
324
|
+
expect(canEditEntity(collection, mockAuthController, "test", matchingEntity)).toBe(true);
|
|
329
325
|
});
|
|
330
326
|
|
|
331
327
|
test("29. AST: null entity ALWAYS optimistically passes SQL evaluation", () => {
|
|
@@ -615,7 +611,7 @@ describe("Permissions Evaluator", () => {
|
|
|
615
611
|
providerId: "test", isAnonymous: false, photoURL: null,
|
|
616
612
|
roles: ["editor"]
|
|
617
613
|
};
|
|
618
|
-
const editorAuth: AuthController<
|
|
614
|
+
const editorAuth: AuthController<User> = { ...mockAuthController, user: editorUser };
|
|
619
615
|
|
|
620
616
|
const collection = createMockCollection([
|
|
621
617
|
{ operation: "select", roles: ["admin", "editor", "author"] }, // read all three
|
|
@@ -701,7 +697,7 @@ describe("Permissions Evaluator", () => {
|
|
|
701
697
|
|
|
702
698
|
test("58. Missing authController ('blankAuth') handled gracefully", () => {
|
|
703
699
|
const collection = createMockCollection([{ operation: "insert", roles: ["admin"] }]);
|
|
704
|
-
const blankAuth = { user: null } as
|
|
700
|
+
const blankAuth = { user: null } as unknown as AuthController<User>;
|
|
705
701
|
expect(canCreateEntity(collection, blankAuth, "test", null)).toBe(false);
|
|
706
702
|
});
|
|
707
703
|
|
package/src/util/permissions.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { AuthController, Entity, EntityCollection, SecurityRule, User } from "@rebasepro/types";
|
|
1
|
+
import { AuthController, Entity, EntityCollection, isPostgresCollection, SecurityRule, User } from "@rebasepro/types";
|
|
2
2
|
|
|
3
|
-
function evaluateAST(sqlString: string, auth: AuthController
|
|
3
|
+
function evaluateAST<USER extends User, M extends Record<string, unknown>>(sqlString: string, auth: AuthController<USER>, entity: Entity<M> | null): boolean {
|
|
4
4
|
// This is a client-side SQL evaluator used *only* for optimistic UI updates.
|
|
5
5
|
// It parses basic AND / OR statements to evaluate RLS without backend roundtrips.
|
|
6
6
|
if (!entity) return true;
|
|
@@ -111,7 +111,7 @@ function evaluateAST(sqlString: string, auth: AuthController, entity: Entity<any
|
|
|
111
111
|
return true; // Optimistic fallback for anything else
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
-
function evaluateRule(rule: SecurityRule, auth: AuthController
|
|
114
|
+
function evaluateRule<USER extends User, M extends Record<string, unknown>>(rule: SecurityRule, auth: AuthController<USER>, entity: Entity<M> | null): boolean {
|
|
115
115
|
|
|
116
116
|
if (rule.access === "public") return true;
|
|
117
117
|
|
|
@@ -135,20 +135,21 @@ function evaluateRule(rule: SecurityRule, auth: AuthController, entity: Entity<a
|
|
|
135
135
|
return true;
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
-
function checkOperation(
|
|
139
|
-
collection: EntityCollection<
|
|
140
|
-
authController: AuthController<
|
|
141
|
-
entity: Entity<
|
|
138
|
+
function checkOperation<M extends Record<string, unknown>, USER extends User>(
|
|
139
|
+
collection: EntityCollection<M>,
|
|
140
|
+
authController: AuthController<USER>,
|
|
141
|
+
entity: Entity<M> | null,
|
|
142
142
|
targetOperation: "select" | "insert" | "update" | "delete"
|
|
143
143
|
): boolean {
|
|
144
|
-
|
|
144
|
+
const securityRules = isPostgresCollection(collection) ? collection.securityRules : undefined;
|
|
145
|
+
if (!securityRules || securityRules.length === 0) {
|
|
145
146
|
// According to our plan: Postgres RLS implicitly denies if enabled without rules.
|
|
146
147
|
// But for Rebase we default to true if securityRules is undefined,
|
|
147
148
|
// so as not to break everything without rules. Let's assume true for now.
|
|
148
149
|
return true;
|
|
149
150
|
}
|
|
150
151
|
|
|
151
|
-
const applicableRules =
|
|
152
|
+
const applicableRules = securityRules.filter((r: SecurityRule) =>
|
|
152
153
|
r.operation === targetOperation ||
|
|
153
154
|
r.operation === "all" ||
|
|
154
155
|
r.operations?.includes(targetOperation) ||
|
|
@@ -195,7 +196,7 @@ function checkOperation(
|
|
|
195
196
|
}
|
|
196
197
|
}
|
|
197
198
|
|
|
198
|
-
export function canReadCollection<M extends Record<string,
|
|
199
|
+
export function canReadCollection<M extends Record<string, unknown>, USER extends User>
|
|
199
200
|
(
|
|
200
201
|
collection: EntityCollection<M>,
|
|
201
202
|
authController: AuthController<USER>
|
|
@@ -203,7 +204,7 @@ export function canReadCollection<M extends Record<string, any>, USER extends Us
|
|
|
203
204
|
return checkOperation(collection, authController, null, "select");
|
|
204
205
|
}
|
|
205
206
|
|
|
206
|
-
export function canEditEntity<M extends Record<string,
|
|
207
|
+
export function canEditEntity<M extends Record<string, unknown>, USER extends User>
|
|
207
208
|
(
|
|
208
209
|
collection: EntityCollection<M>,
|
|
209
210
|
authController: AuthController<USER>,
|
|
@@ -213,18 +214,17 @@ export function canEditEntity<M extends Record<string, any>, USER extends User>
|
|
|
213
214
|
return checkOperation(collection, authController, entity, "update");
|
|
214
215
|
}
|
|
215
216
|
|
|
216
|
-
export function canCreateEntity<M extends Record<string,
|
|
217
|
+
export function canCreateEntity<M extends Record<string, unknown>, USER extends User>
|
|
217
218
|
(
|
|
218
219
|
collection: EntityCollection<M>,
|
|
219
220
|
authController: AuthController<USER>,
|
|
220
221
|
path: string,
|
|
221
222
|
entity: Entity<M> | null
|
|
222
223
|
): boolean {
|
|
223
|
-
if (collection.collectionGroup) return false;
|
|
224
224
|
return checkOperation(collection, authController, entity, "insert");
|
|
225
225
|
}
|
|
226
226
|
|
|
227
|
-
export function canDeleteEntity<M extends Record<string,
|
|
227
|
+
export function canDeleteEntity<M extends Record<string, unknown>, USER extends User>
|
|
228
228
|
(
|
|
229
229
|
collection: EntityCollection<M>,
|
|
230
230
|
authController: AuthController<USER>,
|
package/src/util/references.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EntityCollection } from "@rebasepro/types";
|
|
2
2
|
|
|
3
|
-
export function getEntityImagePreviewPropertyKey<M extends
|
|
3
|
+
export function getEntityImagePreviewPropertyKey<M extends Record<string, unknown>>(collection: EntityCollection<M>): string | undefined {
|
|
4
4
|
|
|
5
5
|
// find first storage property of type image
|
|
6
6
|
for (const key in collection.properties) {
|
|
@@ -26,7 +26,21 @@ export function getEntityImagePreviewPropertyKey<M extends object>(collection: E
|
|
|
26
26
|
// and arrays of URL properties with image preview type
|
|
27
27
|
for (const key in collection.properties) {
|
|
28
28
|
const property = collection.properties[key];
|
|
29
|
-
if (property.type === "array" && property.of
|
|
29
|
+
if (property.type === "array" && property.of && !Array.isArray(property.of) && property.of.type === "string" && property.of.url === "image") {
|
|
30
|
+
return key;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
// fallback: any storage property without explicit acceptedFiles (e.g. a generic "picture" field)
|
|
34
|
+
for (const key in collection.properties) {
|
|
35
|
+
const property = collection.properties[key];
|
|
36
|
+
if (property.type === "string" && property.storage && !property.storage.acceptedFiles) {
|
|
37
|
+
return key;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
// fallback: any array of storage properties without explicit acceptedFiles
|
|
41
|
+
for (const key in collection.properties) {
|
|
42
|
+
const property = collection.properties[key];
|
|
43
|
+
if (property.type === "array" && !Array.isArray(property.of) && property.of?.type === "string" && property.of.storage && !property.of.storage.acceptedFiles) {
|
|
30
44
|
return key;
|
|
31
45
|
}
|
|
32
46
|
}
|
package/src/util/relations.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { EntityCollection, Property, Relation } from "@rebasepro/types";
|
|
2
|
-
import { toSnakeCase } from "
|
|
3
|
-
import { generateForeignKeyName } from "
|
|
1
|
+
import { EntityCollection, isPostgresCollection, PostgresCollection, Property, Relation } from "@rebasepro/types";
|
|
2
|
+
import { toSnakeCase } from "@rebasepro/utils";
|
|
3
|
+
import { generateForeignKeyName } from "@rebasepro/utils";
|
|
4
4
|
|
|
5
|
-
export function sanitizeRelation(relation: Partial<Relation>, sourceCollection:
|
|
5
|
+
export function sanitizeRelation(relation: Partial<Relation>, sourceCollection: PostgresCollection): Relation {
|
|
6
6
|
if (!relation.target) {
|
|
7
7
|
throw new Error("Relation is missing a `target` collection.");
|
|
8
8
|
}
|
|
@@ -10,9 +10,9 @@ export function sanitizeRelation(relation: Partial<Relation>, sourceCollection:
|
|
|
10
10
|
|
|
11
11
|
const newRelation: Partial<Relation> = { ...relation };
|
|
12
12
|
|
|
13
|
-
// 1. Default relationName from target collection slug
|
|
13
|
+
// 1. Default relationName from target collection slug
|
|
14
14
|
if (!newRelation.relationName) {
|
|
15
|
-
newRelation.relationName = toSnakeCase(targetCollection.slug
|
|
15
|
+
newRelation.relationName = toSnakeCase(targetCollection.slug);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
// 2. Infer or default direction if absent
|
|
@@ -41,15 +41,14 @@ export function sanitizeRelation(relation: Partial<Relation>, sourceCollection:
|
|
|
41
41
|
|
|
42
42
|
try {
|
|
43
43
|
// Look for an owning relation on the target that points back to this collection
|
|
44
|
-
const targetRelations = targetCollection.relations || [];
|
|
44
|
+
const targetRelations = (targetCollection as PostgresCollection).relations || [];
|
|
45
45
|
for (const targetRel of targetRelations) {
|
|
46
46
|
if (targetRel.direction === "owning" &&
|
|
47
47
|
targetRel.cardinality === "one" &&
|
|
48
48
|
targetRel.localKey) {
|
|
49
49
|
try {
|
|
50
50
|
const targetRelTarget = targetRel.target();
|
|
51
|
-
if (targetRelTarget.slug === sourceCollection.slug
|
|
52
|
-
targetRelTarget.dbPath === sourceCollection.dbPath) {
|
|
51
|
+
if (targetRelTarget.slug === sourceCollection.slug) {
|
|
53
52
|
// Found the corresponding owning relation, use its localKey
|
|
54
53
|
newRelation.foreignKeyOnTarget = targetRel.localKey;
|
|
55
54
|
foundForeignKey = true;
|
|
@@ -82,12 +81,14 @@ export function sanitizeRelation(relation: Partial<Relation>, sourceCollection:
|
|
|
82
81
|
// Try to determine if this is a many-to-many inverse relation
|
|
83
82
|
if (newRelation.inverseRelationName && !newRelation.foreignKeyOnTarget) {
|
|
84
83
|
try {
|
|
85
|
-
// Look for a corresponding owning many-to-many relation on the target collection
|
|
86
|
-
|
|
84
|
+
// Look for a corresponding owning many-to-many relation on the target collection.
|
|
85
|
+
// Note: we intentionally do NOT require `through` here because the raw (unsanitized)
|
|
86
|
+
// relations won't have `through` populated yet — sanitizeRelation fills it in later.
|
|
87
|
+
// `cardinality: "many" + direction: "owning"` is sufficient to identify owning M2M.
|
|
88
|
+
const targetRelations = (targetCollection as PostgresCollection).relations || [];
|
|
87
89
|
for (const targetRel of targetRelations) {
|
|
88
90
|
if (targetRel.cardinality === "many" &&
|
|
89
|
-
targetRel.direction === "owning" &&
|
|
90
|
-
targetRel.through &&
|
|
91
|
+
(targetRel.direction === "owning" || !targetRel.direction) &&
|
|
91
92
|
(targetRel.relationName === newRelation.inverseRelationName)) {
|
|
92
93
|
// Found a corresponding owning many-to-many relation
|
|
93
94
|
isManyToManyInverse = true;
|
|
@@ -124,7 +125,7 @@ export function sanitizeRelation(relation: Partial<Relation>, sourceCollection:
|
|
|
124
125
|
if (newRelation.cardinality === "one" && newRelation.direction === "inverse" && !newRelation.foreignKeyOnTarget && !newRelation.joinPath) {
|
|
125
126
|
throw new Error(`Configuration Error in relation from '${sourceCollection.name}': An 'inverse' one-to-one relation requires a 'foreignKeyOnTarget'. Check the relation config for '${newRelation.relationName}'`);
|
|
126
127
|
}
|
|
127
|
-
if (newRelation.cardinality === "many" && newRelation.direction === "inverse" && !newRelation.foreignKeyOnTarget && !newRelation.joinPath) {
|
|
128
|
+
if (newRelation.cardinality === "many" && newRelation.direction === "inverse" && !newRelation.foreignKeyOnTarget && !newRelation.joinPath && !newRelation.inverseRelationName) {
|
|
128
129
|
throw new Error(`Configuration Error in relation from '${sourceCollection.name}': An 'inverse' one-to-many relation requires a 'foreignKeyOnTarget'. Check the relation config for '${newRelation.relationName}'`);
|
|
129
130
|
}
|
|
130
131
|
|
|
@@ -132,7 +133,7 @@ export function sanitizeRelation(relation: Partial<Relation>, sourceCollection:
|
|
|
132
133
|
}
|
|
133
134
|
|
|
134
135
|
export function resolveCollectionRelations(
|
|
135
|
-
collection:
|
|
136
|
+
collection: PostgresCollection,
|
|
136
137
|
): Record<string, Relation> {
|
|
137
138
|
const relations: Record<string, Relation> = {};
|
|
138
139
|
|
|
@@ -178,13 +179,13 @@ export function resolvePropertyRelation({
|
|
|
178
179
|
}: {
|
|
179
180
|
propertyKey: string;
|
|
180
181
|
property: Property;
|
|
181
|
-
sourceCollection:
|
|
182
|
+
sourceCollection: PostgresCollection;
|
|
182
183
|
}): Relation | undefined {
|
|
183
184
|
if (property.type !== "relation") return undefined;
|
|
184
185
|
|
|
185
|
-
const relation = sourceCollection.relations
|
|
186
|
+
const relation = (sourceCollection.relations ?? []).find((rel: Relation) => rel.relationName === property.relationName)
|
|
186
187
|
if (!relation) {
|
|
187
|
-
console.warn(`Unrecognized relation format for property '${propertyKey}' in collection '${sourceCollection.slug
|
|
188
|
+
console.warn(`Unrecognized relation format for property '${propertyKey}' in collection '${sourceCollection.slug}'`);
|
|
188
189
|
return undefined;
|
|
189
190
|
}
|
|
190
191
|
|
|
@@ -193,7 +194,10 @@ export function resolvePropertyRelation({
|
|
|
193
194
|
}
|
|
194
195
|
|
|
195
196
|
export function getTableName(collection: EntityCollection): string {
|
|
196
|
-
|
|
197
|
+
if (isPostgresCollection(collection)) {
|
|
198
|
+
return collection.table ?? toSnakeCase(collection.slug) ?? toSnakeCase(collection.name);
|
|
199
|
+
}
|
|
200
|
+
return toSnakeCase(collection.slug) ?? toSnakeCase(collection.name);
|
|
197
201
|
}
|
|
198
202
|
|
|
199
203
|
export function getTableVarName(tableName: string): string {
|
package/src/util/resolutions.ts
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
1
|
import {
|
|
2
2
|
ArrayProperty,
|
|
3
3
|
AuthController,
|
|
4
|
-
CustomizationController,
|
|
5
|
-
EntityAction,
|
|
6
4
|
EntityCollection,
|
|
7
|
-
EntityCustomView,
|
|
8
5
|
EnumValueConfig,
|
|
9
6
|
EnumValues,
|
|
10
7
|
NumberProperty,
|
|
11
8
|
Properties,
|
|
12
9
|
Property,
|
|
13
|
-
PropertyConfig,
|
|
14
10
|
Relation,
|
|
15
11
|
RelationProperty,
|
|
16
|
-
StringProperty
|
|
12
|
+
StringProperty,
|
|
13
|
+
isPostgresCollection,
|
|
14
|
+
isFirebaseCollection,
|
|
17
15
|
} from "@rebasepro/types";
|
|
18
|
-
|
|
16
|
+
|
|
17
|
+
type PropertyConfig = { property: unknown; [key: string]: unknown };
|
|
19
18
|
import { isPropertyBuilder } from "./entities";
|
|
20
|
-
import { getIn, mergeDeep } from "./objects";
|
|
21
19
|
import { enumToObjectEntries } from "./enums";
|
|
22
20
|
import { DEFAULT_ONE_OF_TYPE } from "./common";
|
|
21
|
+
import { isDefaultFieldConfigId } from "@rebasepro/utils";
|
|
22
|
+
import { getIn, mergeDeep } from "@rebasepro/utils";
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
25
|
* Resolve property builders, enums and arrays.
|
|
26
26
|
*/
|
|
27
27
|
|
|
28
|
-
export type ResolvePropertyProps<M extends Record<string,
|
|
28
|
+
export type ResolvePropertyProps<M extends Record<string, unknown> = Record<string, unknown>> = {
|
|
29
29
|
property: Property
|
|
30
30
|
propertyKey?: string,
|
|
31
31
|
values?: Partial<M>,
|
|
@@ -38,7 +38,7 @@ export type ResolvePropertyProps<M extends Record<string, any> = any> = {
|
|
|
38
38
|
authController: AuthController;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
export function resolveProperty<M extends Record<string,
|
|
41
|
+
export function resolveProperty<M extends Record<string, unknown> = Record<string, unknown>>(props: ResolvePropertyProps<M>): Property | null {
|
|
42
42
|
|
|
43
43
|
const {
|
|
44
44
|
property,
|
|
@@ -117,7 +117,8 @@ export function resolveProperty<M extends Record<string, any> = any>(props: Reso
|
|
|
117
117
|
return resolvedProperty;
|
|
118
118
|
}
|
|
119
119
|
if (customField.property) {
|
|
120
|
-
const
|
|
120
|
+
const restConfigProperty = { ...customField.property } as Record<string, unknown>;
|
|
121
|
+
delete restConfigProperty.propertyConfig;
|
|
121
122
|
const customFieldProperty = resolveProperty({
|
|
122
123
|
property: { name: "", ...restConfigProperty } as Property,
|
|
123
124
|
ignoreMissingFields,
|
|
@@ -165,7 +166,7 @@ export function resolvePropertyEnum(property: StringProperty | NumberProperty):
|
|
|
165
166
|
* @param properties
|
|
166
167
|
* @param value
|
|
167
168
|
*/
|
|
168
|
-
export function resolveProperties<M extends Record<string,
|
|
169
|
+
export function resolveProperties<M extends Record<string, unknown>>({
|
|
169
170
|
propertyKey,
|
|
170
171
|
properties,
|
|
171
172
|
ignoreMissingFields,
|
|
@@ -268,7 +269,7 @@ export function resolveArrayProperties<M>({
|
|
|
268
269
|
}).filter(e => Boolean(e)) as Property[]
|
|
269
270
|
: [];
|
|
270
271
|
return resolvedProperties;
|
|
271
|
-
} else if (!property.Field) {
|
|
272
|
+
} else if (!("Field" in property && property.Field)) {
|
|
272
273
|
throw Error(`The array property (${propertyKey}) needs to declare an 'of' or a 'oneOf' property, or provide a custom \`Field\` component`);
|
|
273
274
|
} else {
|
|
274
275
|
return [];
|
|
@@ -328,56 +329,24 @@ export function resolveEnumValues(input: EnumValues): EnumValueConfig[] | undefi
|
|
|
328
329
|
}
|
|
329
330
|
}
|
|
330
331
|
|
|
331
|
-
export function resolveEntityView(entityView: string | EntityCustomView<any>, contextEntityViews?: EntityCustomView<any>[]): EntityCustomView<any> | undefined {
|
|
332
|
-
if (typeof entityView === "string") {
|
|
333
|
-
return contextEntityViews?.find((entry) => entry.key === entityView);
|
|
334
|
-
} else {
|
|
335
|
-
return entityView;
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
export function resolveEntityAction<M extends Record<string, any>>(
|
|
340
|
-
entityAction: string | EntityAction<M>,
|
|
341
|
-
contextEntityActions?: EntityAction<M>[]
|
|
342
|
-
): EntityAction<M> | undefined {
|
|
343
|
-
if (typeof entityAction === "string") {
|
|
344
|
-
return contextEntityActions?.find((entry) => entry.key === entityAction);
|
|
345
|
-
} else {
|
|
346
|
-
return entityAction;
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
export function resolvedSelectedEntityView<M extends Record<string, any>>(
|
|
351
|
-
customViews: (string | EntityCustomView<M>)[] | undefined,
|
|
352
|
-
customizationController: CustomizationController,
|
|
353
|
-
selectedTab?: string,
|
|
354
|
-
canEdit?: boolean,
|
|
355
|
-
) {
|
|
356
|
-
const resolvedEntityViews = customViews ? customViews
|
|
357
|
-
.map(e => resolveEntityView(e, customizationController.entityViews))
|
|
358
|
-
.filter((e): e is EntityCustomView<M> => Boolean(e))
|
|
359
|
-
// .filter((e) => canEdit || !e.includeActions)
|
|
360
|
-
: [];
|
|
361
332
|
|
|
362
|
-
const selectedEntityView = resolvedEntityViews.find(e => e.key === selectedTab);
|
|
363
|
-
const selectedSecondaryForm = customViews
|
|
364
|
-
&& resolvedEntityViews.filter(e => e.includeActions).find(e => e.key === selectedTab);
|
|
365
|
-
return {
|
|
366
|
-
resolvedEntityViews,
|
|
367
|
-
selectedEntityView,
|
|
368
|
-
selectedSecondaryForm
|
|
369
|
-
};
|
|
370
|
-
}
|
|
371
333
|
|
|
372
|
-
export function getSubcollections<M extends Record<string,
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
334
|
+
export function getSubcollections<M extends Record<string, unknown> = Record<string, unknown>>(collection: EntityCollection<M>): EntityCollection<Record<string, unknown>>[] {
|
|
335
|
+
if (collection.childCollections) {
|
|
336
|
+
return collection.childCollections() ?? [];
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (isFirebaseCollection(collection) && collection.subcollections) {
|
|
340
|
+
return collection.subcollections() ?? [];
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (isPostgresCollection(collection) && collection.relations) {
|
|
344
|
+
const manyRelations = collection.relations.filter(r => r.cardinality === "many");
|
|
345
|
+
return manyRelations.map(r => {
|
|
346
|
+
const target = r.target();
|
|
347
|
+
return r.overrides ? mergeDeep(target, r.overrides) : target;
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
return [];
|
|
383
352
|
}
|
package/src/util/storage.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ArrayProperty, EntityValues, StorageConfig, StringProperty, UploadedFileContext } from "@rebasepro/types";
|
|
2
|
-
import { randomString } from "
|
|
2
|
+
import { randomString } from "@rebasepro/utils";
|
|
3
3
|
|
|
4
|
-
interface ResolveFilenameStringParams<M extends
|
|
4
|
+
interface ResolveFilenameStringParams<M extends Record<string, unknown>> {
|
|
5
5
|
input: string | ((context: UploadedFileContext) => (Promise<string> | string));
|
|
6
6
|
storage: StorageConfig;
|
|
7
7
|
values: EntityValues<M>;
|
|
@@ -12,7 +12,7 @@ interface ResolveFilenameStringParams<M extends object> {
|
|
|
12
12
|
propertyKey: string;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export async function resolveStorageFilenameString<M extends
|
|
15
|
+
export async function resolveStorageFilenameString<M extends Record<string, unknown>>(
|
|
16
16
|
{
|
|
17
17
|
input,
|
|
18
18
|
storage,
|
|
@@ -53,7 +53,7 @@ export async function resolveStorageFilenameString<M extends object>(
|
|
|
53
53
|
return result;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
interface ResolveStoragePathStringParams<M extends
|
|
56
|
+
interface ResolveStoragePathStringParams<M extends Record<string, unknown>> {
|
|
57
57
|
input: string | ((context: UploadedFileContext) => string);
|
|
58
58
|
storage: StorageConfig;
|
|
59
59
|
values: EntityValues<M>;
|
|
@@ -64,7 +64,7 @@ interface ResolveStoragePathStringParams<M extends object> {
|
|
|
64
64
|
propertyKey: string;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
export function resolveStoragePathString<M extends
|
|
67
|
+
export function resolveStoragePathString<M extends Record<string, unknown>>(
|
|
68
68
|
{
|
|
69
69
|
input,
|
|
70
70
|
storage,
|
package/dist/util/arrays.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function toArray<T>(input?: T | T[]): T[];
|
package/dist/util/dates.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const defaultDateFormat = "MMMM dd, yyyy, HH:mm:ss";
|
package/dist/util/fields.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export declare function flattenObject(obj: Record<string, unknown>, parentKey?: string): {
|
|
2
|
-
[key: string]: unknown;
|
|
3
|
-
};
|
|
4
|
-
export type ArrayValuesCount = Record<string, number>;
|
|
5
|
-
export declare function getArrayValuesCount(array: Record<string, unknown>[]): ArrayValuesCount;
|
package/dist/util/hash.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function hashString(str: string): number;
|
package/dist/util/names.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generates a foreign key column name from a given string, typically a collection slug or name.
|
|
3
|
-
* It converts the name to snake_case, attempts to singularize it by removing a trailing 's'
|
|
4
|
-
* (a common convention for collection names), and appends '_id'.
|
|
5
|
-
*
|
|
6
|
-
* @param name The base name to convert to a foreign key.
|
|
7
|
-
* @returns A foreign key name in the format 'singular_name_id'.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* // returns "user_id"
|
|
11
|
-
* generateForeignKeyName("users")
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* // returns "post_id"
|
|
15
|
-
* generateForeignKeyName("posts")
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* // returns "product_id"
|
|
19
|
-
* generateForeignKeyName("Product")
|
|
20
|
-
*
|
|
21
|
-
*/
|
|
22
|
-
export declare function generateForeignKeyName(name: string): string;
|
package/dist/util/objects.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
/** @private is the value an empty array? */
|
|
2
|
-
export declare const isEmptyArray: (value?: unknown) => boolean;
|
|
3
|
-
/** @private is the given object a Function? */
|
|
4
|
-
export declare const isFunction: (obj: unknown) => obj is Function;
|
|
5
|
-
/** @private is the given object an integer? */
|
|
6
|
-
export declare const isInteger: (obj: unknown) => boolean;
|
|
7
|
-
/** @private is the given object a NaN? */
|
|
8
|
-
export declare const isNaN: (obj: unknown) => boolean;
|
|
9
|
-
/**
|
|
10
|
-
* Deeply get a value from an object via its path.
|
|
11
|
-
*/
|
|
12
|
-
export declare function getIn(obj: Record<string, unknown> | unknown[] | unknown, key: string | string[], def?: unknown, p?: number): unknown;
|
|
13
|
-
export declare function setIn<T>(obj: T, path: string, value: unknown): T;
|
|
14
|
-
export declare function clone<T>(value: T): T;
|
|
15
|
-
export declare const pick: <T extends Record<string, unknown>>(obj: T, ...args: (keyof T)[]) => Partial<T>;
|
|
16
|
-
export declare function isObject(item: unknown): item is Record<string, unknown>;
|
|
17
|
-
export declare function isPlainObject(obj: unknown): obj is Record<string, unknown>;
|
|
18
|
-
export declare function mergeDeep<T extends Record<string, any>, U extends Record<string, any>>(target: T, source: U, ignoreUndefined?: boolean): T & U;
|
|
19
|
-
export declare function getValueInPath(o: object | undefined, path: string): unknown;
|
|
20
|
-
export declare function removeInPath(o: object, path: string): object | undefined;
|
|
21
|
-
export declare function removeFunctions(o: unknown): unknown;
|
|
22
|
-
export declare function getHashValue<T>(v: T): string | null;
|
|
23
|
-
export declare function removeUndefined(value: unknown, removeEmptyStrings?: boolean): unknown;
|
|
24
|
-
export declare function removeNulls(value: unknown): unknown;
|
|
25
|
-
export declare function isEmptyObject(obj: object): boolean;
|
|
26
|
-
export declare function removePropsIfExisting(source: Record<string, unknown> | unknown[], comparison: Record<string, unknown> | unknown[]): Record<string, unknown> | unknown[];
|
package/dist/util/os.d.ts
DELETED
package/dist/util/plurals.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns the plural of an English word.
|
|
3
|
-
*
|
|
4
|
-
* @param {string} word
|
|
5
|
-
* @param {number} [amount]
|
|
6
|
-
* @returns {string}
|
|
7
|
-
*/
|
|
8
|
-
export declare function plural(word: string, amount?: number): string;
|
|
9
|
-
/**
|
|
10
|
-
* Returns the singular of an English word.
|
|
11
|
-
*
|
|
12
|
-
* @param {string} word
|
|
13
|
-
* @param {number} [amount]
|
|
14
|
-
* @returns {string}
|
|
15
|
-
*/
|
|
16
|
-
export declare function singular(word: string, amount?: number): string;
|
package/dist/util/regexp.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export declare function serializeRegExp(input: RegExp): string;
|
|
2
|
-
/**
|
|
3
|
-
* Get a RegExp out of a serialized string
|
|
4
|
-
* @param input
|
|
5
|
-
*/
|
|
6
|
-
export declare function hydrateRegExp(input?: string): RegExp | undefined;
|
|
7
|
-
export declare function isValidRegExp(input: string): boolean;
|