Skip to content

Version 0.3.0#25

Merged
D34DPlayer merged 9 commits intomainfrom
dev
Apr 13, 2023
Merged

Version 0.3.0#25
D34DPlayer merged 9 commits intomainfrom
dev

Conversation

@D34DPlayer
Copy link
Owner

@D34DPlayer D34DPlayer commented Apr 13, 2023

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

@D34DPlayer D34DPlayer added this to the v0.3.0 milestone Apr 13, 2023
@D34DPlayer D34DPlayer added the release A pull request concerning a new release label Apr 13, 2023
@D34DPlayer D34DPlayer merged commit 6f9d0bf into main Apr 13, 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