Open thegreatzeus opened 2 weeks ago
➤ PM Bot commented:
Jira ticket: RJS-2799
@thegreatzeus Thank you for reporting. We need to investigate.
I've literally been having same issue!
well, same error message
Classes extending Realm.Object cannot define their own
schemastatic, all properties must be defined using TypeScript syntax
in my case I'm just trying to create a basic model like so
class Book extends Realm.Object<Book> {
name!: string;
price?: number;
static schema: ObjectSchema = {
name: 'Book',
properties: {
name: 'string',
price: 'int?',
},
};
}
@thegreatzeus any leads yet?
Same issue here. Using @thegreatzeus class as example, if I just define my scheme as the following, it works fine
class Book extends Realm.Object<Book> {
name?: string;
price?: number;
}
If I add the static scheme: ObjectSchema { ... }
code it always throw the error mentioned here. It seems the only way to set a primaryKey
is through this ObjectSchema
but with this error going on it's impossible.
Is there any work around so far?
import { Realm } from '@realm/react'
import { BeneficiaryType } from '...'
export class Beneficiary extends Realm.Object {
_id!: Realm.BSON.ObjectId;
name!: string;
...
static generate(payload: BeneficiaryType) {
return {
_id: new Realm.BSON.ObjectId(),
name: payload.name,
....
};
}
}
Object.defineProperty(Beneficiary, 'schema', {
value: {
name: 'Beneficiary',
primaryKey: '_id',
properties: {
_id: 'objectId',
name: 'string',
...
},
},
});
this is what I ended up using @jpbast
@favourwright it worked for me! Thanks for your quick reply
@thegreatzeus, it looks like you're using the @realm/babel-plugin. The idea of that plugin is to simplify some of the model/schema creation by removing the need for the explicit static schema
field.
You can either remove the plugin and use the schema as you have defined it, or use the plugin and remove the static schema
.
To index a property using the babel-plugin, use the @index
annotation. To declare a primary key, use static primaryKey = _id
(or whichever key is the pk).
The following is a similar example as the @realm/babel-plugin README:
class Task extends Realm.Object<Task, "description"> {
_id = new Realm.BSON.ObjectId();
description!: string;
@index
isComplete = false;
static primaryKey = "_id";
}
How frequently does the bug occur?
Always
Description
I'm trying to index a
string
property on a Realm model using TypeScript.I'm using Realm's React Native SDK with Realm's Expo example app.
Using the same example from Realm's docs:
Results in the following error:
Stacktrace & log output
Version
"realm": "12.0.0", "@realm/react": "^0.6.0",
What services are you using?
Local Database only
Are you using encryption?
No
Platform OS and version(s)
Android 14
Build environment
Node v18.19.1
package.json
contents:Cocoapods version
No response