Skip to content

v0.3.0

Choose a tag to compare

@D34DPlayer D34DPlayer released this 13 Apr 21:25
· 56 commits to main since this release
117cac1

Documentation
NPM
Full Changelog

Features

Query.update

Bulk update the instances in a query. The provided fields will be changed on every matched item. (filters, limits and offsets apply)

await Test.filter({ age: t => t < 25 }).update({ name: 'test2' })

Model.update

Allows updating fields on an instance with type hints, note that saving still needs to happen in a separate step.

const test = Test.get(1)
test.update({
  name: "Joe",
})
await test.save()

Query.forEach

Closes #9

Allows doing complex operations in a query, if the other methods aren't enough. Actually they could all be implemented with this system and already share most of the logic.
The callback receives two arguments, the current instance and the ongoing transaction. The transaction needs to be used if extra changes to the database need to be done in the loop.
If at some point you want to end the loop early, returning true will do it. No return or a false return will keep it going until there's no more items.

// Simple usage
const results = []
await Test.orderBy('-age').forEach(async (instance) => {
  results.push(instance.age)
})
// Complex usage
await Test.filter({ age: t => t < 25 }).forEach(async (t, tx) => {
  // If true is returned, the loop is stopped
  if (t.name == 'test2') return true

  t.update({ name: 'test2' })
  await t.save(tx)
}, 'readwrite')

New Error types

Closes #8

Three new error types have been added:

  • WormError: Base error for all the errors produced by this library
  • ConnectionError: Whenever an operation is tried against a closed database
  • ModelError: Any Model validation error (eg. not nullable field set to null)

Decorator-less definitions

Closes #20

Besides the "decorator" way of using this library, there's now a new way to define a tables fields:

class Test extends Model {
  id!: number
  unique!: string
}
defineModel(Test, {
  id: { primaryKey: true },
  unique: { unique: true },
})

This will allow using this library in a non-TS environment.

Global namespace

Now all the module's exports can be found in globalThis.WORM.

"Script" usage

Closes #21

This library can now be used without any compiler, by simply including a script tag in the HTML:

<script src="https://unpkg.com/@d34d/w-orm@latest/dist/index.umd.min.js"></script>
<script>
    class Test extends WORM.Model { }
    WORM.defineModel(Test, {
      id: { primaryKey: true },
    })

    WORM.init('test1', 1).then(() => console.log(WORM.db))
</script>

Other

  • dist builds are now smaller, with a minified version available (closes #24)