Last active
November 9, 2021 22:03
-
-
Save guyroyse/5b96f4add78d5794fa0b745b3645eb0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Entity, Schema, Client, Repository } from 'redis-om' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Album extends Entity {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let schema = new Schema(Album, { | |
artist: { type: 'string' }, | |
title: { type: 'string' }, | |
year: { type: 'number' }, | |
genres: { type: 'array' }, | |
outOfPublication: { type: 'boolean' } | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let client = new Client() | |
await client.open('redis://localhost:6379') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let repository = new Repository(schema, client) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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