Skip to content

Instantly share code, notes, and snippets.

@guyroyse
Created November 10, 2021 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guyroyse/4737f9f3453dc5c677714250da5204c0 to your computer and use it in GitHub Desktop.
Save guyroyse/4737f9f3453dc5c677714250da5204c0 to your computer and use it in GitHub Desktop.
import { Entity, Schema, Client, Repository } from 'redis-om'
class Album extends Entity {}
let schema = new Schema(Album, {
artist: { type: 'string' },
title: { type: 'string' },
year: { type: 'number' },
genres: { type: 'array' },
outOfPublication: { type: 'boolean' }
})
let client = new Client()
await client.open('redis://localhost:6379')
let repository = new Repository(schema, client)
let album, id
// create an entity and save it
album = repository.createEntity()
album.artist = "Mushroomhead"
album.title = "The Righteous & The Butterfly"
album.year = 2014
album.genres = [ 'metal' ]
album.outOfPublication = true
id = await repository.save(album) // '01FJYWEYRHYFT8YTEGQBABJ43J'
// read an entity
album = await repository.fetch('01FJYWEYRHYFT8YTEGQBABJ43J')
// update an entity
album.genres = [ 'metal', 'nu metal', 'avantgarde' ]
album.outOfPublication = false
id = await repository.save(album) // '01FJYWEYRHYFT8YTEGQBABJ43J'
// delete an entity
await repository.remove('01FJYWEYRHYFT8YTEGQBABJ43J')
let albums = await repository.search()
.where('artist').equals('Mushroomhead')
.and('title').matches('butterfly')
.and('year').is.greaterThan(2000).returnAll()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment