-
Notifications
You must be signed in to change notification settings - Fork 576
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
Conversation
export type { | ||
CollectionChangeCallback, | ||
CollectionChangeSet, | ||
Configuration, | ||
FlexibleSyncConfiguration, | ||
ObjectChangeCallback, | ||
ObjectChangeSet, | ||
ObjectSchema, | ||
PropertySchema, | ||
PartitionSyncConfiguration, | ||
RealmEventName, | ||
RealmListenerCallback, | ||
SubscriptionOptions, | ||
} from "./internal"; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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 🙈
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
This is to avoid breaking the API just yet.
3750aa5
to
92ba2a9
Compare
There was a problem hiding this 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.
packages/realm/src/Dictionary.ts
Outdated
@@ -282,3 +275,8 @@ export class Dictionary<T = unknown> extends Collection<string, T, [string, T], | |||
); | |||
} | |||
} | |||
|
|||
export interface Dictionary<T = unknown> { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
// TODO: Decide if we want to deprecate this as well | ||
public static Object = RealmObject; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
)
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 😃
packages/realm/src/Realm.ts
Outdated
/** @deprecated Please use named imports */ | ||
CanonicalObjectSchemaProperty, | ||
/** @deprecated Please use named imports */ | ||
CanonicalPropertiesTypes, | ||
/** @deprecated Please use named imports */ |
There was a problem hiding this comment.
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)
- (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)
- (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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
packages/realm/src/errors.ts
Outdated
/** | ||
* @deprecated Use the another {@link ClientResetMode} than {@link ClientResetMode.Manual}. | ||
*/ | ||
export class ClientResetError extends SyncError { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
export type { | ||
CollectionChangeCallback, | ||
CollectionChangeSet, | ||
Configuration, | ||
FlexibleSyncConfiguration, | ||
ObjectChangeCallback, | ||
ObjectChangeSet, | ||
ObjectSchema, | ||
PropertySchema, | ||
PartitionSyncConfiguration, | ||
RealmEventName, | ||
RealmListenerCallback, | ||
SubscriptionOptions, | ||
} from "./internal"; |
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great 💯
packages/realm/src/Realm.ts
Outdated
/** @deprecated Please use named imports */ | ||
CanonicalObjectSchemaProperty, |
There was a problem hiding this comment.
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).
/** @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.
And added a link to it on the CanonicalObjectSchemaProperty.
* 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.
* 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.
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 theRealm
namespace.I noted suggestions for cases where we intentionally break the API in the project's technical design doc (internal to MongoDB employees).