realm / realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
https://realm.io
Apache License 2.0
5.79k stars 577 forks source link

Babel Plugin does not add primary key to schema if static primary key value is a variable #5253

Open channeladam opened 1 year ago

channeladam commented 1 year ago

How frequently does the bug occur?

Always

Description

The Babel plugin does not add a primary key to the static schema of the TypeScript class when the value assigned to static primaryKey is a variable - e.g. a constant.

The following example does NOT get a primary key added to the schema by the Babel plugin:

const key = '_id';
export class MyObject extends Realm.Object<MyObject>
  static primaryKey = key;

The following example DOES get a primary key added to the schema by the Babel plugin:

export class MyObject extends Realm.Object<MyObject>
  static primaryKey = '_id';

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

Babel plugin 0.1.1

What services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

Android 11 R API 30 - Pixel 5 emulator

Build environment

Which debugger for React Native: .. Flipper

Cocoapods version

No response

kraenhansen commented 1 year ago

I agree, this is a bug.

Note to the team: We shouldn't check that the value is a string literal here: https://github.com/realm/realm-js/blob/master/packages/babel-plugin/src/plugin/index.ts#L356-L357 .. even better would be to simply leave it alone and adopt the SDKs schema parser to read the static directly.