Upgrading RxDB to 10.0.0

In a future post I'll document in a bit more detail how the underlying architecture of Project Beardy Scorebook fits together; not least how the data for games is stored both locally in the application and synchronised to the server.

Suffice for now - the short version is - we use RxDB as the database; and for a large portion of the development too date, have worked with various 9.x.y versions.

In this post however I wanted to document some of the gotchas that had me puzzled for a while, whilst upgrading to the recent (as of July 2021) 10.0.0 release.

The major change in the release centres around the loosening of how tightly coupled RxDB is to Pouchdb. The maintainers of RxDB are moving the tool towards a structure which has little to no opinion on what actually gets used for storage, so with time it can be layered over SQLite, PostgresSQL, MongoDb and others.

Many of the breaking changes - and how to update for them - are documented here, but there's a couple of elements which aren't covered in great detail that others making the change might appreciate having documented - hence this post.

Creating Collections

When creating new collections, I'd previously been using RxDatabase.collection(), but as this has now been deprecated in favour of the addCollections() method this needed updating.

What isn't immediately obvious from the documentation is the signature for the configuration object passed to this new method differs from what the previous method required

It's no longer a flat structured object with at minimum a name and schema properties, changing instead to a more structured property with a key for each collection you want to create. Subtle change but had me scratching around for an hour or so.

As an example - take this code that I had to create the Player collection.

const p: PlayerCollection = await db.collection({
name: "players",
schema: PlayerSchema.schema,
methods: docMethods,
statics: colectionMethods,
autoMigrate: false,
migrationStrategies: migrationStrategies
});

For the purpose of brevity I haven't included the actual code examples for docMethods and collectionMethods properties here - but suffice to say these are objects with appropriate methods defined on them for both document and collection functions.

For the new addCollections method however the object passed to the method changes to look like the below

let result = await db.addCollections({
players: {
schema: PlayerSchema.schema,
statics: colectionMethods,
methods: docMethods,
autoMigrate: false,
migrationStrategies: migrationStrategies
}
});

Note how the name for the collection is now a key in the passed argument rather than the previous incarnation where is was the value for an explicit name property.

The change means that multiple collections can be created at once, which makes for simpler initiation of databases containing multiple collections. Impacts of this change don't stop just at the arguments passed to the method though, as what is returned from the method contains a similar structural difference.

Returned values

The above examples show we're asynchronously awaiting a response from the addCollections method.

In the earlier collections method this would resolve to a direct reference to the newly created collection. Now, because the addCollections allows for the creation of multiple collections at once, we get instead an object with keys that reference each new collection.

// collection method would return reference to the
// Players collection
console.log(p)

// addCollections returns object with a key
// reference to each new collection
console.log(result.players)

Again a subtle change, but one that as of now I cant see documented clearly elsewhere.

Back to Home