Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support full-text index decorator in babel plugin #6218

Merged
merged 2 commits into from
Nov 20, 2023

Conversation

atdyer
Copy link
Contributor

@atdyer atdyer commented Oct 27, 2023

This closes #5800 by adding support for the index("full-text") decorator.

  • Check for index decorator calls that are passed the string "full-text" and subsequently add the { indexed: "full-text" } property to the schema.
  • Update README with description and example of indexing a string field by full text
  • Update tests to verify schemas with full-text indexing are properly generated

- Check for `index` decorator calls that are passed the string "full-text" and subsequently add the `{ indexed: "full-text" }` property to the schema.
- Update README with description and example of indexing a string field by full text
- Update tests to verify schemas with full-text indexing are properly generated
@kneth
Copy link
Contributor

kneth commented Oct 31, 2023

@atdyer Thank you for your contribution. It looks good to me, and I have requested the team to review your PR.

I like that you have taken the time to update README.md and added tests.

Some test suites will fail since you don't have permission to launch some component - don't worry about that.

Once merged, we will update CHANGELOG.md and do a release of the babel plugin.

const indexCall =
indexDecoratorCall
&& types.isStringLiteral(indexDecoratorCall.callExpression.arguments[0])
&& indexDecoratorCall.callExpression.arguments[0].value === 'full-text'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm contemplating if we actually want to pass through any expression here and handle validation at runtime instead 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should just forward whatever is given to the schema. Then at least we get a run-time error.
Unfortunately it's not possible to get type errors here, which would be great.

Comment on lines 420 to 428
it('ignores `@index()` decorators with invalid parameters', () => {
const transformCode = transform({
source: `import Realm, { Types, BSON, List, Set, Dictionary, Mixed } from "realm";
export class Person extends Realm.Object { @Realm.index("fulltext") name: Realm.Types.String; }`,
});
const parsedSchema = extractSchema(transformCode);

expect((parsedSchema?.properties.name as PropertySchema).indexed).toBeUndefined();
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make the code more robust to future changes and allow for the SDK to report this as an error (instead of silently not recognising the users intent) we might want this passed into the schema.

What's your take on this @takameyer?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that an error should be thrown in this case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helping the user understand what's wrong and how to correct it also aligns better with the goals of the updated schema parser 👍

Copy link
Contributor

@takameyer takameyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would approve this after the comments from @kraenhansen are resolved.

Comment on lines 420 to 428
it('ignores `@index()` decorators with invalid parameters', () => {
const transformCode = transform({
source: `import Realm, { Types, BSON, List, Set, Dictionary, Mixed } from "realm";
export class Person extends Realm.Object { @Realm.index("fulltext") name: Realm.Types.String; }`,
});
const parsedSchema = extractSchema(transformCode);

expect((parsedSchema?.properties.name as PropertySchema).indexed).toBeUndefined();
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that an error should be thrown in this case.

const indexCall =
indexDecoratorCall
&& types.isStringLiteral(indexDecoratorCall.callExpression.arguments[0])
&& indexDecoratorCall.callExpression.arguments[0].value === 'full-text'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should just forward whatever is given to the schema. Then at least we get a run-time error.
Unfortunately it's not possible to get type errors here, which would be great.

Copy link
Contributor

@elle-j elle-j left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the time to understand this issue and propose a solution! 🚀

Comment on lines 420 to 428
it('ignores `@index()` decorators with invalid parameters', () => {
const transformCode = transform({
source: `import Realm, { Types, BSON, List, Set, Dictionary, Mixed } from "realm";
export class Person extends Realm.Object { @Realm.index("fulltext") name: Realm.Types.String; }`,
});
const parsedSchema = extractSchema(transformCode);

expect((parsedSchema?.properties.name as PropertySchema).indexed).toBeUndefined();
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helping the user understand what's wrong and how to correct it also aligns better with the goals of the updated schema parser 👍

Removes check that the specific string "full-text" is the only allowable parameter to the `@index` decorator. New behavior is to allow any string to be passed through to the schema. Invalid values in the schema will throw at runtime, alerting user to possible errors.
@atdyer
Copy link
Contributor Author

atdyer commented Nov 6, 2023

@takameyer I believe I've addressed all your comments - now any string passed into the @index decorator as a parameter will be passed through to the final schema. If there's anything I've missed please let me know an I'll fix it up!

Copy link
Member

@kraenhansen kraenhansen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, this just needs an entry in the changelog.

Copy link
Contributor

@takameyer takameyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍🏼

Copy link
Contributor

@elle-j elle-j left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice update!

@kneth kneth merged commit 30592da into realm:main Nov 20, 2023
21 of 31 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update babel plugin with full text search support
5 participants