0.3.14 • Published 2 years ago

rxfirestorm v0.3.14

Weekly downloads
9
License
ISC
Repository
github
Last release
2 years ago

RxFireSTORM

This combines Firestore with an ORM and RxJS.

Simple example

// models.ts
import Model from "rxfirestorm"

export class Article extends Model {
  static collection = "articles"

  public title: string
  public body: string

  constructor(init: { title?: string, body?: string }) {
    super(init)
    this.title = init.title ?? ""
    this.body = init.body ?? ""
  }
}

And then somewhere you could do this:

import { Article } from "../models"

const article$ = Article.query().where("title", "==", "Hello World!").first()

// If you want to listen to snapshots, you can subscribe to it
// like you're used to with Observables from RxJS
const subscription = article$.subscribe(
  article => console.log(article.body)
)

// Or if you just want a single data object, you can await it.
const article = await article$
// This will subscribe to it, wait for the first snapshot, and then unsubscribe.

Or in a Svelte component you'd write it like this of course:

import { Article } from "../models"

const article = Article.query().first()

$: console.log($article.body)

More complex example

// models.ts
import Model, {
  HasMany,
  BelongsTo,
  ModelQuery,
  CollectionQuery
} from "rxfirestorm"

export class User extends Model {
  static collection = "users"

  public email = ""
  public name = ""

  constructor(init: { email?: string, name?: string }) {
    super(init)
    Object.assign(this, init)
  }
}

export class Comment extends Model {
  static collection = "comments"

  public body: string

  @BelongsTo(User)
  public author!: ModelQuery<typeof User>

  constructor(init: { body: string, author: User }) {
    super(init)
    Object.assign(this, init)
  }
}

export class Article extends Model {
  static collection = "articles"

  public title = ""
  public body = ""

  @BelongsTo(User) public author!: ModelQuery<typeof User>
  @HasMany(Comment) public comments!: CollectionQuery<typeof Comment>

  constructor(init: { title?: string, body?: string, author: User }) {
    super(init)
    Object.assign(this, init)
  }

  async addComment(comment: { body: string, author: User }): Promise<void> {
    await this.comments.add(comment)
  }
}

And then somewhere else:

const article = await Article.query().first()

console.log(article.author.name) // will print a name

let comments = await article.comments // lazy loading
// you can also add arbitrary query constraints
comments = await article.comments.orderBy("createdAt")

comments.forEach(comment => console.log(comment.body))

Check out the tests for more examples!

0.3.6

2 years ago

0.3.5

2 years ago

0.3.8

2 years ago

0.3.7

2 years ago

0.3.9

2 years ago

0.3.14

2 years ago

0.3.13

2 years ago

0.3.12

2 years ago

0.3.11

2 years ago

0.3.10

2 years ago

0.3.0

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.4

2 years ago

0.2.5

2 years ago

0.3.3

2 years ago

0.2.3

3 years ago

0.2.4

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.28

3 years ago

0.1.29

3 years ago

0.1.27

3 years ago

0.1.26

3 years ago

0.1.25

3 years ago

0.1.24

3 years ago

0.1.23

3 years ago

0.1.22

3 years ago

0.1.20

3 years ago

0.1.21

3 years ago

0.1.13

3 years ago

0.1.14

3 years ago

0.1.15

3 years ago

0.1.16

3 years ago

0.1.17

3 years ago

0.1.18

3 years ago

0.1.19

3 years ago

0.1.11

3 years ago

0.1.12

3 years ago

0.1.10

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.9

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago