Skip to content

v1.3.0

Compare
Choose a tag to compare
@haltcase haltcase released this 25 Aug 04:45
· 272 commits to master since this release

This release brings a pair of exciting features!

First up is the ability to define getters and setters when creating models.
These are useful for things like formatting after selects and conforming
data before inserts.

const people = await db.model('people', {
  name: String,
  username: {
    type: String,
    get (username) {
      return `username is: ${username}`
    },
    set (username) {
      // remove anything that isn't alphanumeric or underscore
      return username.replace(/[^\w]/gi, '')
    }
  }
})

await people.create({
  name: 'Bo Lingen',
  username: 'ci.ty]ci[d/e'
})
// -> { name: 'Bo Lingen', username: 'citycide' }

await people.get('username', { name: 'Bo Lingen' })
// -> 'username is: citycide'

// can use `getRaw()` and `setRaw()` to bypass getters / setters
// other methods may also accept a `raw` property in their options object
await people.getRaw('username', { name: 'Bo Lingen' })
// -> 'citycide'

There's also a new memory-only mode - if the file path is exactly ':memory:',
no file will be created and an in-memory store will be used. This doesn't
persist any data but is useful for speed and performance reasons. For example,
most of trilogy's tests now use in-memory databases to avoid tons of disk usage.

const db = new Trilogy(':memory:')
BUG FIXES
  • always return promises in async functions (009f079)
FEATURES
  • add support for in-memory database (8587f4b)
  • add getters & setters (ab5cd7b)
  • add getRaw() & setRaw() (5fe1f80)
PERFORMANCE
  • model: use assignments in constructor (73e6ebd)
  • util: optimize each() and map() (040084d)