如何在不同模式中重用模式/类的属性?

问题描述 投票:0回答:1

考虑以下示例:

Base.ts

import Realm from 'realm';

export type BaseId = Realm.BSON.ObjectId;

export interface BaseEntity {
    id?: BaseId;
    createdAt: string;
    updatedAt: string;
    syncDetails: SyncDetails;
}

export class BaseEntitySchema {
    _id!: Realm.BSON.ObjectId;

    createdAt!: Date;

    updatedAt!: Date;

    static schema: Realm.ObjectSchema = {
        name: 'BaseEntity',
        properties: {
            _id: 'objectId',
            createdAt: 'date',
            updatedAt: 'date',
        },
        primaryKey: '_id',
    };
}

现在,我想使用上面代码片段中提供的属性创建一个新架构。

System.ts

import Realm from 'realm';
import { BaseEntity, BaseEntitySchema } from 'shared/offline-realm/schemas/base';

export interface System extends BaseEntity {
    name: string;
    serialNumber: string;
    locationId: string;
    corporationId: string;
}

export class SystemSchema extends Realm.Object<SystemSchema> {
    name!: string;

    serialNumber!: string;

    locationId!: string;

    corporationId!: string;

    static schema: Realm.ObjectSchema = {
        name: 'System',
        properties: {
            ...BaseEntitySchema.schema.properties,
            name: 'string',
            serialNumber: 'string',
            locationId: 'string',
            corporationId: 'string',
        },
        primaryKey: '_id',
    };
}

我希望可以通过继承/扩展来在 SystemSchema 中使用 BaseEntity 值。

    _id!: Realm.BSON.ObjectId;

    createdAt!: Date;

    updatedAt!: Date;

我不想明确添加它。我宁愿扩展它,因为我有多个模式需要相同的属性,并且我希望 TypeScript 在我尝试访问它时能够识别这些值。

我尝试做类似的事情:

export class BaseEntitySchema extends Realm.Object<BaseEntitySchema> {}

然后:

export class SystemSchema extends BaseEntitySchema

但是 Realm 的

useQuery
在执行类似
Unexpected arguments passed to useQuery
 之类的操作时会崩溃并显示 
useQuery(SystemSchema)

BaseEntitySchema 是一个基本模式,它不像集合一样使用。

目前,我必须在每个模式中不断重复属性值,如下所示:

export class SystemSchema extends Realm.Object<SystemSchema> {
    name!: string;

    serialNumber!: string;

    locationId!: string;

    corporationId!: string;

    _id!: Realm.BSON.ObjectId;

    createdAt!: Date;

    updatedAt!: Date;

    static schema: Realm.ObjectSchema = {
        name: 'System',
        properties: {
            ...BaseEntitySchema.schema.properties,
            name: 'string',
            serialNumber: 'string',
            locationId: 'string',
            corporationId: 'string',
        },
        primaryKey: '_id',
    };
}

有没有更好的方法来处理这个问题?

javascript reactjs typescript react-native realm
1个回答
0
投票

您希望继承 BaseEntity 值。因此您可以为其创建一个单独的界面并创建如下所示的修改以创建扩展模式

export interface BaseEntity {
    _id?: BaseId;
    createdAt: Date;
    updatedAt: Date;
}

export const BaseSchema = {
    _id: 'objectId',
    createdAt: 'date',
    updatedAt: 'date',
};

export function extendSchema<T extends BaseEntity>(name: string, properties: Realm.ObjectSchema['properties']) {
    return class extends Realm.Object<T> {
        _id!: Realm.BSON.ObjectId;
        createdAt!: Date;
        updatedAt!: Date;

        static schema: Realm.ObjectSchema = {
            name,
            properties: {
                ...BaseSchema,
                ...properties,
            },
            primaryKey: '_id',
        };
    };
}
© www.soinside.com 2019 - 2024. All rights reserved.