Skip to content

Conversation

@D34DPlayer
Copy link
Owner

@D34DPlayer D34DPlayer commented Apr 21, 2023

Features

Disable indexing

Closes #28

Fields allow now to disable the automatic index creation (eg. for non indexable types like Blob)

class User extends Model {
  @Field({ index: false })
  profilePic: Blob
}

Migrations

Closes #1 and closes #31

Sometimes, changes to the way existing data is stored are required for an update, to cope with this W-ORM provides an intuitive migration system.

Migrations are defined as list of functions to be executed, depending on the current and target DB versions.
The key is the target version number, and the value is the migration callback.

Eg. { 2: (migration) => { ... } } will execute the migration callback when the current database version is smaller than 2.

A migration callback receives a MigrationContext object as its only argument.
This object contains a transaction to be used for the migration.

It is expected for the callback to create Model classes that represent the table's state in between these two versions.
The fields aren't actually used by W-ORM in this scenario, and only serve to improve the typing within the migration.

The Model methods can be then used to manipulate the data.
It is very important to use the transaction provided by the migration context, otherwise the migration will hang forever.

const migrations: MigrationList = {
  2: async (migration) => {
    class User extends Model {
      id!: number
      name!: string
    }

    const users = await User.all()
    for (const user of users) {
      user.name = `${user.id} name`
      await user.save(migration.tx)
    }
    // Or with the `forEach method`
    await User.forEach(async (instance, tx) => {
      instance.name = `${instance.id} name`
      await instance.save(tx)
    }, migration.tx)

    const specificUser = await User.get(69, migration.tx)
    await specificUser?.delete(migration.tx)
  },
}

Model now allows extra fields

The Typescript signature of Model has been changed so that you can provide and get extra, not defined fields from it. This is a feature supported by IndexedDB so it's only natural to support it here as well.

class User extends Model {
  id!: string
  name!: string 
}

const user = await User.create({
  id: "joe",
  name: "Joe",
  lastName: "Mama", // This would have caused an error before, it is allowed now.
})

const otherUser = await User.get("tom")
console.log(user.extraField) // This would have caused an error as well, you'll still need to check that it is defined

Query cloning

Closes #18

Queries can now be cloned, to allow branching and allow code deduplication.

// Regular pagination
const userPage = User.orderBy('id').offset(10).limit(10)

// Filtered pagination
const johnPage = userPage.clone().filter({ name: 'John' })
const janePage = userPage.clone().filter({ name: 'Jane' })

Model.keys

Closes #17

We can now get a list with all the keys in a table (same as Model.all but with keys instead of instances)

const keys = await User.keys()
// equivalent to (even though more performant)
const keys = (await User.all()).map(u => u.keys)

Bug fixes

  • After an upgrade, changed indexes will now be recreated and unused indexes will be removed
  • Errors during an upgrade will now bubble up properly to the init call
  • Upgrades would fail silently because the automatic table creation would try to create already existing tables

Other

  • Errors are now displayed in their own section in the documentation
  • Minified dist would break because the Model class wasn't called Model anymore
  • Documentation improved overall (closes Document examples for every method #16)
  • Improve test coverage (closes Test all connection errors #19)
  • UMD dist exposes the whole module under globalThis.WORM, for the ESM version, you'll have to import WORM from "https://unpkg.com/browse/@d34d/w-orm@0.4.0/dist/index.esm.js"

@D34DPlayer D34DPlayer added this to the v0.4.0 milestone Apr 21, 2023
@D34DPlayer D34DPlayer added the release A pull request concerning a new release label Apr 21, 2023
@D34DPlayer D34DPlayer merged commit 58900d6 into main Apr 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release A pull request concerning a new release

Projects

None yet

1 participant