Skip to content

Collections ​

db.collections.get returns read-only snapshots for collections. You can keep chaining filters or use normal array methods.

These examples use a small fixture that resets for every run. Your application still uses its own Realm database.

all ​

Returns all matching collections in a regular array.

ts
return db.collections.get
  .all()
  .map(item => ({
    name: item.Name,
    beatmaps: item.BeatmapMD5Hashes.length,
  }))

toArray ​

Returns all matching collections in a regular array.

ts
return db.collections.get
  .toArray()
  .map(item => ({
    name: item.Name,
    beatmaps: item.BeatmapMD5Hashes.length,
  }))

first ​

Returns the first matching collection, or undefined.

ts
return db.collections.get
  .first()?.Name

count ​

Counts all matching collections, even when a limit is set.

ts
return db.collections.get
  .count()

limit ​

Limits how many collections elements are accessed.

ts
return db.collections.get
  .limit(1)
  .map(item => ({
    name: item.Name,
    beatmaps: item.BeatmapMD5Hashes.length,
  }))

sortedBy ​

Sorts collections by a database field.

ts
return db.collections.get
  .sortedBy('Name')
  .map(item => ({
    name: item.Name,
    beatmaps: item.BeatmapMD5Hashes.length,
  }))

autoEdit ​

Starts an edit session that buffers changes to matching collections. Commit or roll them back when you are done.

ts
const session = db.collections.get
  .limit(1)
  .autoEdit()
const length = session.length
session.rollback()
return length

commit ​

Saves the pending changes.

ts
const session = db.collections.get
  .limit(1)
  .autoEdit()
const length = session.length
session.commit()
return length

rollback ​

Discards the pending changes.

ts
const session = db.collections.get
  .limit(1)
  .autoEdit()
const length = session.length
session.rollback()
return length

write.delete ​

Deletes every matching collection in one transaction.

ts
return db.collections.get
  .limit(1)
  .write.delete()

write.update ​

Applies a typed patch to every matching collection.

ts
return db.collections.get
  .limit(1)
  .write.update({ Name: 'Example' })

byId ​

Finds collections where ID matches the supplied value. You can chain more filters on the result.

ts
return db.collections.get
  .byId('66666666-6666-4666-8666-666666666666')
  .map(item => ({
    name: item.Name,
    beatmaps: item.BeatmapMD5Hashes.length,
  }))

byLastModifiedAfter ​

Finds collections where last modified is after the supplied date. You can chain more filters on the result.

ts
return db.collections.get
  .byLastModifiedAfter(new Date('2024-01-01'))
  .map(item => ({
    name: item.Name,
    beatmaps: item.BeatmapMD5Hashes.length,
  }))

byLastModifiedBefore ​

Finds collections where last modified is before the supplied date. You can chain more filters on the result.

ts
return db.collections.get
  .byLastModifiedBefore(new Date('2024-01-01'))
  .map(item => ({
    name: item.Name,
    beatmaps: item.BeatmapMD5Hashes.length,
  }))

byName ​

Finds collections where name matches the supplied value. You can chain more filters on the result.

ts
return db.collections.get
  .byName('Favorites')
  .map(item => ({
    name: item.Name,
    beatmaps: item.BeatmapMD5Hashes.length,
  }))

byNameContains ​

Finds collections where name contains the supplied value. You can chain more filters on the result.

ts
return db.collections.get
  .byNameContains('Fav')
  .map(item => ({
    name: item.Name,
    beatmaps: item.BeatmapMD5Hashes.length,
  }))

sortedByLastModified ​

Returns collections ordered by last modified.

ts
return db.collections.get
  .sortedByLastModified(true)
  .map(item => ({
    name: item.Name,
    beatmaps: item.BeatmapMD5Hashes.length,
  }))

sortedByName ​

Returns collections ordered by name.

ts
return db.collections.get
  .sortedByName(true)
  .map(item => ({
    name: item.Name,
    beatmaps: item.BeatmapMD5Hashes.length,
  }))

withBeatmap ​

Finds collections related to the supplied beatmap.

ts
return db.collections.get
  .withBeatmap('b'.repeat(32))
  .map(item => ({
    name: item.Name,
    beatmaps: item.BeatmapMD5Hashes.length,
  }))

Released under the MIT License.