realm-js icon indicating copy to clipboard operation
realm-js copied to clipboard

Fixed babel plugin add extra name property

Open XHMM opened this issue 2 years ago • 3 comments

What, How & Why?

When defining schema using class with a custom static property name, the generated schema code containes two name fields: one is class name and another is the static property. The expected result should only contain one name field

☑️ ToDos

  • [ ] 📝 Changelog entry
  • [ ] 📝 Compatibility label is updated or copied from previous entry
  • [ ] 📝 Update COMPATIBILITY.md
  • [ ] 🚦 Tests
  • [ ] 📦 Updated internal package version in consuming package.jsons (if updating internal packages)
  • [ ] 📱 Check the React Native/other sample apps work if necessary
  • [ ] 💥 Breaking label has been applied or is not necessary

XHMM avatar Nov 26 '23 15:11 XHMM

There is no doc for how to running test locally and I am not familier with wireit, so I skipped writing tests :(

XHMM avatar Nov 26 '23 16:11 XHMM

@XHMM can you share an example of code that would trigger this?

There is no doc for how to running test locally

It should be as simple as:

  • Checking out your fork of the repo
  • git submodule update --init --recursive
  • npm install
  • npm test --workspace @realm/babel-plugin

kraenhansen avatar Dec 20 '23 10:12 kraenhansen

Here is a code sample:

schema defination using ts:



export class RecordModel extends Realm.Object<RecordModel, "recordFrom"> {
  // @ts-ignore
  static name = "Record";
  static primaryKey = "_id";

  _id: Realm.Types.ObjectId = new Realm.BSON.ObjectId();
  @Realm.index recordAt: Realm.Types.Date = new Date();
  recordFrom!: string;
}

after babel transform with current plugin: (some unrelated code was omitted)

(0, _defineProperty2.default)(RecordModel, "name", "Record");
RecordModel.primaryKey = "_id";
RecordModel.schema = {
  name: "RecordModel", // class name
  properties: {
    _id: {
      type: "objectId",
      default: function _default() {
        return new _realm.default.BSON.ObjectId();
      },
    },
    recordAt: {
      type: "date",
      default: function _default() {
        return new Date();
      },
      indexed: true,
    },
    recordFrom: { type: "string" },
  },
  name: "Record", // custom static name
  primaryKey: "_id",
};

As above showed , name property appeared twice, although this won't caused unexpected behavior due to how js language works.

This pull request just checked if user defined custom static name, it will remove the default class name

XHMM avatar Dec 21 '23 02:12 XHMM