fix: Do not rely on class names in production bundle (#6212)
This commit is contained in:
@@ -3,6 +3,8 @@ import Model from "./base/Model";
|
||||
import Field from "./decorators/Field";
|
||||
|
||||
class ApiKey extends Model {
|
||||
static modelName = "ApiKey";
|
||||
|
||||
@Field
|
||||
@observable
|
||||
id: string;
|
||||
|
||||
@@ -3,6 +3,8 @@ import Model from "./base/Model";
|
||||
import Field from "./decorators/Field";
|
||||
|
||||
class AuthenticationProvider extends Model {
|
||||
static modelName = "AuthenticationProvider";
|
||||
|
||||
id: string;
|
||||
|
||||
displayName: string;
|
||||
|
||||
@@ -13,6 +13,8 @@ import { client } from "~/utils/ApiClient";
|
||||
import Field from "./decorators/Field";
|
||||
|
||||
export default class Collection extends ParanoidModel {
|
||||
static modelName = "Collection";
|
||||
|
||||
store: CollectionsStore;
|
||||
|
||||
@observable
|
||||
|
||||
@@ -1,14 +1,25 @@
|
||||
import { observable } from "mobx";
|
||||
import { CollectionPermission } from "@shared/types";
|
||||
import Collection from "./Collection";
|
||||
import Group from "./Group";
|
||||
import Model from "./base/Model";
|
||||
import Relation from "./decorators/Relation";
|
||||
|
||||
class CollectionGroupMembership extends Model {
|
||||
static modelName = "CollectionGroupMembership";
|
||||
|
||||
id: string;
|
||||
|
||||
groupId: string;
|
||||
|
||||
@Relation(() => Group, { onDelete: "cascade" })
|
||||
group: Group;
|
||||
|
||||
collectionId: string;
|
||||
|
||||
@Relation(() => Collection, { onDelete: "cascade" })
|
||||
collection: Collection;
|
||||
|
||||
@observable
|
||||
permission: CollectionPermission;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ import Field from "./decorators/Field";
|
||||
import Relation from "./decorators/Relation";
|
||||
|
||||
class Comment extends Model {
|
||||
static modelName = "Comment";
|
||||
|
||||
/**
|
||||
* Map to keep track of which users are currently typing a reply in this
|
||||
* comments thread.
|
||||
|
||||
@@ -22,6 +22,8 @@ type SaveOptions = {
|
||||
};
|
||||
|
||||
export default class Document extends ParanoidModel {
|
||||
static modelName = "Document";
|
||||
|
||||
constructor(fields: Record<string, any>, store: DocumentsStore) {
|
||||
super(fields, store);
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@ import Model from "./base/Model";
|
||||
import Relation from "./decorators/Relation";
|
||||
|
||||
class Event extends Model {
|
||||
static modelName = "Event";
|
||||
|
||||
id: string;
|
||||
|
||||
name: string;
|
||||
|
||||
@@ -5,6 +5,8 @@ import User from "./User";
|
||||
import Model from "./base/Model";
|
||||
|
||||
class FileOperation extends Model {
|
||||
static modelName = "FileOperation";
|
||||
|
||||
id: string;
|
||||
|
||||
@observable
|
||||
|
||||
@@ -3,6 +3,8 @@ import Model from "./base/Model";
|
||||
import Field from "./decorators/Field";
|
||||
|
||||
class Group extends Model {
|
||||
static modelName = "Group";
|
||||
|
||||
@Field
|
||||
@observable
|
||||
id: string;
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
import Group from "./Group";
|
||||
import User from "./User";
|
||||
import Model from "./base/Model";
|
||||
import Relation from "./decorators/Relation";
|
||||
|
||||
class GroupMembership extends Model {
|
||||
id: string;
|
||||
static modelName = "GroupMembership";
|
||||
|
||||
userId: string;
|
||||
|
||||
groupId: string;
|
||||
|
||||
@Relation(() => User, { onDelete: "cascade" })
|
||||
user: User;
|
||||
|
||||
groupId: string;
|
||||
|
||||
@Relation(() => Group, { onDelete: "cascade" })
|
||||
group: Group;
|
||||
}
|
||||
|
||||
export default GroupMembership;
|
||||
|
||||
@@ -8,6 +8,8 @@ import Model from "~/models/base/Model";
|
||||
import Field from "./decorators/Field";
|
||||
|
||||
class Integration<T = unknown> extends Model {
|
||||
static modelName = "Integration";
|
||||
|
||||
id: string;
|
||||
|
||||
type: IntegrationType;
|
||||
|
||||
@@ -3,6 +3,8 @@ import { CollectionPermission } from "@shared/types";
|
||||
import Model from "./base/Model";
|
||||
|
||||
class Membership extends Model {
|
||||
static modelName = "Membership";
|
||||
|
||||
id: string;
|
||||
|
||||
userId: string;
|
||||
|
||||
@@ -15,6 +15,8 @@ import Field from "./decorators/Field";
|
||||
import Relation from "./decorators/Relation";
|
||||
|
||||
class Notification extends Model {
|
||||
static modelName = "Notification";
|
||||
|
||||
@Field
|
||||
@observable
|
||||
id: string;
|
||||
|
||||
@@ -6,6 +6,8 @@ import Field from "./decorators/Field";
|
||||
import Relation from "./decorators/Relation";
|
||||
|
||||
class Pin extends Model {
|
||||
static modelName = "Pin";
|
||||
|
||||
/** The collection ID that the document is pinned to. If empty the document is pinned to home. */
|
||||
collectionId: string;
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ import { observable } from "mobx";
|
||||
import Model from "./base/Model";
|
||||
|
||||
class Policy extends Model {
|
||||
static modelName = "Policy";
|
||||
|
||||
id: string;
|
||||
|
||||
@observable
|
||||
|
||||
@@ -6,6 +6,8 @@ import Model from "./base/Model";
|
||||
import Relation from "./decorators/Relation";
|
||||
|
||||
class Revision extends Model {
|
||||
static modelName = "Revision";
|
||||
|
||||
/** The document ID that the revision is related to */
|
||||
documentId: string;
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ import { client } from "~/utils/ApiClient";
|
||||
import Model from "./base/Model";
|
||||
|
||||
class SearchQuery extends Model {
|
||||
static modelName = "Search";
|
||||
|
||||
id: string;
|
||||
|
||||
query: string;
|
||||
|
||||
@@ -6,6 +6,8 @@ import Field from "./decorators/Field";
|
||||
import Relation from "./decorators/Relation";
|
||||
|
||||
class Share extends Model {
|
||||
static modelName = "Share";
|
||||
|
||||
@Field
|
||||
@observable
|
||||
published: boolean;
|
||||
|
||||
@@ -7,6 +7,8 @@ import Field from "./decorators/Field";
|
||||
import Relation from "./decorators/Relation";
|
||||
|
||||
class Star extends Model {
|
||||
static modelName = "Star";
|
||||
|
||||
/** The sort order of the star */
|
||||
@Field
|
||||
@observable
|
||||
|
||||
@@ -9,6 +9,8 @@ import Relation from "./decorators/Relation";
|
||||
* A subscription represents a request for a user to receive notifications for a document.
|
||||
*/
|
||||
class Subscription extends Model {
|
||||
static modelName = "Subscription";
|
||||
|
||||
/** The user ID subscribing */
|
||||
userId: string;
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ import Model from "./base/Model";
|
||||
import Field from "./decorators/Field";
|
||||
|
||||
class Team extends Model {
|
||||
static modelName = "Team";
|
||||
|
||||
@Field
|
||||
@observable
|
||||
id: string;
|
||||
|
||||
@@ -16,6 +16,8 @@ import ParanoidModel from "./base/ParanoidModel";
|
||||
import Field from "./decorators/Field";
|
||||
|
||||
class User extends ParanoidModel {
|
||||
static modelName = "User";
|
||||
|
||||
@Field
|
||||
@observable
|
||||
id: string;
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
import { action, observable } from "mobx";
|
||||
import Document from "./Document";
|
||||
import User from "./User";
|
||||
import Model from "./base/Model";
|
||||
import Relation from "./decorators/Relation";
|
||||
|
||||
class View extends Model {
|
||||
static modelName = "View";
|
||||
|
||||
id: string;
|
||||
|
||||
documentId: string;
|
||||
|
||||
@Relation(() => Document)
|
||||
document?: Document;
|
||||
|
||||
firstViewedAt: string;
|
||||
|
||||
@observable
|
||||
|
||||
@@ -3,6 +3,8 @@ import Model from "./base/Model";
|
||||
import Field from "./decorators/Field";
|
||||
|
||||
class WebhookSubscription extends Model {
|
||||
static modelName = "WebhookSubscription";
|
||||
|
||||
@Field
|
||||
@observable
|
||||
id: string;
|
||||
|
||||
@@ -5,6 +5,8 @@ import Logger from "~/utils/Logger";
|
||||
import { getFieldsForModel } from "../decorators/Field";
|
||||
|
||||
export default abstract class Model {
|
||||
static modelName: string;
|
||||
|
||||
@observable
|
||||
id: string;
|
||||
|
||||
|
||||
@@ -35,7 +35,9 @@ export const getInverseRelationsForModelClass = (targetClass: typeof Model) => {
|
||||
|
||||
relations.forEach((relation, modelName) => {
|
||||
relation.forEach((properties, propertyName) => {
|
||||
if (properties.relationClassResolver().name === targetClass.name) {
|
||||
if (
|
||||
properties.relationClassResolver().modelName === targetClass.modelName
|
||||
) {
|
||||
inverseRelations.set(propertyName, {
|
||||
...properties,
|
||||
modelName,
|
||||
@@ -66,13 +68,13 @@ export default function Relation<T extends typeof Model>(
|
||||
// this to determine how to update relations when a model is deleted.
|
||||
if (options) {
|
||||
const configForClass =
|
||||
relations.get(target.constructor.name) || new Map();
|
||||
relations.get(target.constructor.modelName) || new Map();
|
||||
configForClass.set(propertyKey, {
|
||||
options,
|
||||
relationClassResolver: classResolver,
|
||||
idKey,
|
||||
});
|
||||
relations.set(target.constructor.name, configForClass);
|
||||
relations.set(target.constructor.modelName, configForClass);
|
||||
}
|
||||
|
||||
Object.defineProperty(target, propertyKey, {
|
||||
@@ -83,9 +85,9 @@ export default function Relation<T extends typeof Model>(
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const relationClassName = classResolver().name;
|
||||
const relationClassName = classResolver().modelName;
|
||||
const store =
|
||||
this.store.rootStore[`${relationClassName.toLowerCase()}s`];
|
||||
this.store.rootStore.getStoreForModelName(relationClassName);
|
||||
invariant(store, `Store for ${relationClassName} not found`);
|
||||
|
||||
return store.get(id);
|
||||
@@ -94,9 +96,9 @@ export default function Relation<T extends typeof Model>(
|
||||
this[idKey] = newValue ? newValue.id : undefined;
|
||||
|
||||
if (newValue) {
|
||||
const relationClassName = classResolver().name;
|
||||
const relationClassName = classResolver().modelName;
|
||||
const store =
|
||||
this.store.rootStore[`${relationClassName.toLowerCase()}s`];
|
||||
this.store.rootStore.getStoreForModelName(relationClassName);
|
||||
invariant(store, `Store for ${relationClassName} not found`);
|
||||
|
||||
store.add(newValue);
|
||||
|
||||
Reference in New Issue
Block a user