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

[bindgen] Refactor namespaces #5376

Merged
merged 20 commits into from
Feb 7, 2023
Merged

Conversation

kraenhansen
Copy link
Member

What, How & Why?

This refactors existing namespace code and adds missing members.

I basically went though out root ./types directory to verify that all we exported before is now also being exported both as named exports and through the Realm namespace.

I noted suggestions for cases where we intentionally break the API in the project's technical design doc (internal to MongoDB employees).

@kraenhansen kraenhansen self-assigned this Feb 6, 2023
@cla-bot cla-bot bot added the cla: yes label Feb 6, 2023
Comment on lines -44 to -57
export type {
CollectionChangeCallback,
CollectionChangeSet,
Configuration,
FlexibleSyncConfiguration,
ObjectChangeCallback,
ObjectChangeSet,
ObjectSchema,
PropertySchema,
PartitionSyncConfiguration,
RealmEventName,
RealmListenerCallback,
SubscriptionOptions,
} from "./internal";
Copy link
Member Author

Choose a reason for hiding this comment

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

@elle-j I remember you mentioned that you had errors when exporting these without the type keyword. Do you remember when that error occurred? This builds for me without issues, so I wonder if it was somewhere when running the tests?

Copy link
Contributor

@elle-j elle-j Feb 6, 2023

Choose a reason for hiding this comment

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

Yes that builds for me as well, but the error occurred when running our unit tests in realm.

cd packages/realm
npm test

Copy link
Member Author

Choose a reason for hiding this comment

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

I've made a few changes and verified that switching to tsx as the TS runtime makes the SDK unit tests run again: #5378

@@ -119,6 +149,8 @@ export class App {
platformVersion: App.PLATFORM_VERSION,
sdkVersion: App.SDK_VERSION, // Used to be "RealmJS/" + SDK_VERSION
transport: createNetworkTransport(),
localAppName: app?.name,
localAppVersion: app?.version,
Copy link
Member Author

Choose a reason for hiding this comment

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

I noticed the implementation for this was missing and since it was literally a 1 minute job, I just did it in this PR 🙈

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

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 effort making these changes! Deprecating the namespace types definitely seems like the way to go. (Also, thanks for fixing some typos here and there in comments.)

Left some questions and suggestions, after that it'll be awesome getting this merged.

@@ -282,3 +275,8 @@ export class Dictionary<T = unknown> extends Collection<string, T, [string, T],
);
}
}

export interface Dictionary<T = unknown> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since class Dictionary is exported, why do we need to export an interface as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually. This change is not strictly needed anymore. The types are merging, but I'll revert this, since thats not 100% obvious.

Comment on lines +184 to +185
// TODO: Decide if we want to deprecate this as well
public static Object = RealmObject;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd think that having all types be imported in the same way (named) would be less confusing (and more consistent), especially if Results, Dictionary, etc. are deprecated. What would be the reason for not deprecating it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Only that Object is a class provided by the runtime globally and import { Object } from "realm" would shadow that (and confuse some static analysis tools, like linters, etc.). In a file where an end-user need access to the Object provided by the runtime and that provided by us, they could rename on import:

import { Object as RealmObject } from "realm";
// Use RealmObject

Or alternatively import us as a namespace (which won't be deprecated):

import * as Realm from `realm`;
// Use Realm.Object

The latter could be a bit confusing since Realm in that case wouldn't be a constructor.

By not deprecating on the line we're discussing, we would allow:

import { Realm } from "realm";
// Use Realm.Object
// Realm is still a constructor

I suspect this is the reason the namespaced API got introduced in the first case.

Copy link
Contributor

@elle-j elle-j Feb 7, 2023

Choose a reason for hiding this comment

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

Gotcha, thanks for the explanation. One more thing, in index.ts we export e.g. RealmObject as Object, could we not export it as RealmObject instead (but not in v12)?

Copy link
Member Author

Choose a reason for hiding this comment

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

could we not export it as RealmObject

We could .. We should probably think long an hard about that tough. The package name ("realm") is already a namespace (not the TS kind of namespace ...), so in general I tend to opt towards not include a name twice (both in the thing and the thing / namespace that contains it)

public static User = User;
public static Credentials = Credentials;
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this be a breaking change? (Removing Credentials from Realm and its namespace, and adding it to App)

Copy link
Member Author

@kraenhansen kraenhansen Feb 6, 2023

Choose a reason for hiding this comment

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

Great catch. Because of https://github.com/realm/realm-js/tree/bindgen/types#L266-L269 I thought they were exported on App only, but looking more closely, this should definitely not be removed.

@@ -107,20 +165,54 @@ type InternalConfig = {
};

export class Realm {
Copy link
Contributor

Choose a reason for hiding this comment

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

So much easier to read and navigate when the members are in alphabetical order, thanks 😃

Comment on lines 1254 to 1258
/** @deprecated Please use named imports */
CanonicalObjectSchemaProperty,
/** @deprecated Please use named imports */
CanonicalPropertiesTypes,
/** @deprecated Please use named imports */
Copy link
Contributor

@elle-j elle-j Feb 6, 2023

Choose a reason for hiding this comment

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

I saw that the old API exports CanonicalObjectSchemaProperty and CanonicalPropertiesTypes (and CanonicalObjectSchema, this last one is missing from this namespace), but I'm not sure why we're exporting the canonical types as opposed to only the "regular/relaxed" ones.

Would it not be preferable to remove those 3 canonical public exports in v13 (meaning, not only from the namespace but also not providing them as named exports)? (And in this case mention this in the deprecation comments.)

The only differences between the publicly exported..

  • (A) CanonicalObjectSchema vs. (B) ObjectSchema:
    • (A) properties: The values use object notation.
    • (B) properties: The values use either object or shorthand notation.
  • (A) CanonicalObjectSchemaProperty vs. (B) ObjectSchemaProperty:
    • (A) name: The name is provided.
    • (B) name: The name is not provided (it's just the same as the property name).
  • (A) CanonicalPropertiesTypes vs. (B) PropertiesTypes:
    • (A) The values use object notation.
    • (B) The values use either object or shorthand notation.

Then, for methods that return canonical types (e.g. Realm.schema) we could change those to return the relaxed ones.

Copy link
Member Author

Choose a reason for hiding this comment

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

for methods that return canonical types (e.g. Realm.schema) we could change those to return the relaxed ones.

From my work on Studio I found it nicer to program against the canonical types. Granted, most end-user applications doesn't do schema reflection like Studio does, but for a CMS like front-end to a database, having the canonical types as well as having the Realm#schema be canonical is actually a massive upside, since the consuming code needs to consider less edge-cases.

Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting, point well taken! :) (ps. I believe CanonicalObjectSchema needs to be added there as well then)

@@ -52,12 +52,14 @@ Timestamp.prototype.toDate = function () {
return new Date(Number(this.seconds) * 1000 + this.nanoseconds / 1000_000);
};

/** @internal */
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Comment on lines 121 to 124
/**
* @deprecated Use the another {@link ClientResetMode} than {@link ClientResetMode.Manual}.
*/
export class ClientResetError extends SyncError {
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the correlation between ClientResetMode.Manual and ClientResetError in this case? Perhaps it could be clarified a bit here.

Copy link
Member Author

Choose a reason for hiding this comment

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

To be honest, I'm lacking context myself, but I've added links to the changelog and original PR adding the deprecation in the first place. We could probably revisit this.

@kneth do you remember why this got deprecated? Is it mainly to discourage the use of the manual client reset mode?

@@ -18,44 +18,110 @@

export {
Copy link
Contributor

Choose a reason for hiding this comment

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

These types could be helpful for users as well:

  • PropertySchemaStrict
  • PropertyTypeName
  • PrimitivePropertyTypeName
  • CollectionPropertyTypeName
  • RelationshipPropertyTypeName
  • UserTypeName

Comment on lines -44 to -57
export type {
CollectionChangeCallback,
CollectionChangeSet,
Configuration,
FlexibleSyncConfiguration,
ObjectChangeCallback,
ObjectChangeSet,
ObjectSchema,
PropertySchema,
PartitionSyncConfiguration,
RealmEventName,
RealmListenerCallback,
SubscriptionOptions,
} from "./internal";
Copy link
Contributor

@elle-j elle-j Feb 6, 2023

Choose a reason for hiding this comment

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

Yes that builds for me as well, but the error occurred when running our unit tests in realm.

cd packages/realm
npm test

@@ -18,7 +18,8 @@

import { expect } from "chai";

import { Realm } from "../index";
import { Realm, Results } from "../index";
Copy link
Contributor

Choose a reason for hiding this comment

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

I've noticed that the tests usually import from individual files rather than from internal.ts. Should we start using internal.ts for the tests as well or does it not matter much in this case?

Copy link
Member Author

Choose a reason for hiding this comment

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

That would probably be good, but then again, I expect to delete these or partly migrate them to integration tests soon.

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.

Great 💯

Comment on lines 1258 to 1259
/** @deprecated Please use named imports */
CanonicalObjectSchemaProperty,
Copy link
Contributor

Choose a reason for hiding this comment

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

We can refer the user here to the now-called CanonicalPropertySchema (also needs to be imported at the top).

Suggested change
/** @deprecated Please use named imports */
CanonicalObjectSchemaProperty,
/** @deprecated Will be removed in v13.0.0. Please use {@link CanonicalPropertySchema} as a named import */
CanonicalObjectSchemaProperty,
/** @deprecated Please use named imports */
CanonicalPropertySchema,

Feel free to rearrange/reformulate.

@kraenhansen kraenhansen merged commit 712a277 into bindgen Feb 7, 2023
@kraenhansen kraenhansen deleted the kh/bindgen/refactor-namespaces branch February 7, 2023 12:15
papafe added a commit that referenced this pull request Feb 7, 2023
* bindgen:
  [bindgen] Migrated `tsm` → `tsx` (#5378)
  [bindgen] Refactor namespaces (#5376)
  Fixed space
  Small correction to docs

# Conflicts:
#	packages/realm/src/Dictionary.ts
kraenhansen added a commit that referenced this pull request Mar 1, 2023
* WIP on improving the type-tests

* Removed a stray comment start

* Fixed missing @ts-expect-error in bundle.d.ts

* Propagate local app name and version

* Renamed AuthClients to Auth

This is to avoid breaking the API just yet.

* Refactored namespaces

* Fixed namespaced Types export

* Fixed InsertionModel types

* Fixed a type-o

* Ensuring named exports are correct

* Updated unit tests to import from ../index

* Fixed some exported types

* Merged Dictionary interface and class

* Reverting removal of `Realm.Credentials`

* Reordered and exporting MutableSubscriptionSet

* Updated AppConfiguration#baseUrl docs

* Adding links to ClientResetError deprecation

* Exporting types pr review

* Adding "CanonicalObjectSchema" to Realm namespace

* Export CanonicalPropertySchema

And added a link to it on the CanonicalObjectSchemaProperty.
kraenhansen added a commit that referenced this pull request Mar 3, 2023
* WIP on improving the type-tests

* Removed a stray comment start

* Fixed missing @ts-expect-error in bundle.d.ts

* Propagate local app name and version

* Renamed AuthClients to Auth

This is to avoid breaking the API just yet.

* Refactored namespaces

* Fixed namespaced Types export

* Fixed InsertionModel types

* Fixed a type-o

* Ensuring named exports are correct

* Updated unit tests to import from ../index

* Fixed some exported types

* Merged Dictionary interface and class

* Reverting removal of `Realm.Credentials`

* Reordered and exporting MutableSubscriptionSet

* Updated AppConfiguration#baseUrl docs

* Adding links to ClientResetError deprecation

* Exporting types pr review

* Adding "CanonicalObjectSchema" to Realm namespace

* Export CanonicalPropertySchema

And added a link to it on the CanonicalObjectSchemaProperty.
@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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants