1amageek / pring.ts

Cloud Firestore model framework for TypeScript - Google
https://firebase.google.com/docs/firestore/
MIT License
108 stars 7 forks source link

Fix NestedCollection.insert() and ReferenceCollection.insert() #3

Closed starhoshi closed 6 years ago

starhoshi commented 6 years ago

NestedCollection.insert() and ReferenceCollection.insert() can update count. But collections are not created... I fix it.

1amageek commented 6 years ago

@starhoshi

以下の処理では同じ結果を期待していたんだけど 使いにくかった?

保存するする前にinsertする場合

let item = new Item()
let user = new User()

user.items.insert(item)
user.save()   // batch処理で保持しているitemを全て保存する

保存した後にinsertした場合

let item = new Item()
let user = new User()
user.save().then( () => {
  user.items.insert(item) // countの処理ためにtransaction処理が必要
}
1amageek commented 6 years ago

https://github.com/1amageek/pring.ts/blob/master/pring.ts#L238

バッチ処理はpack functionを使ってやってます。

starhoshi commented 6 years ago

現状の仕組みだとうまく動いてくれる場合と動かない場合があるんですよね...

動く

    const otherAddress = new Address()
    otherAddress.name = 'other'
    await newUser.addresses.insert(otherAddress)
    await newUser.save()

正常に動いている

動かない

    const otherAddress = new Address()
    otherAddress.name = 'other'
    let newUser = new User()
    await newUser.save().then(() => {
      newUser.addresses.insert(otherAddress)
    })

動かない2

    const otherAddress = new Address()
    otherAddress.name = 'other'
    otherAddress.save()
    let newUser = new User()
    await newUser.save()
    await newUser.addresses.insert(otherAddress)
1amageek commented 6 years ago

Objectがセーブされてたらプロミスで処理、されてなかったらただのArrayに入れる

const otherAddress = new Address()
    otherAddress.name = 'other'
    let newUser = new User()
    await newUser.save().then(() => {
      newUser.addresses.insert(otherAddress)  // ここにプロミスいるのでは?
    })

Javascriptのメモリ解放の仕組みわかんないけど なんか処理中に解放されてそうなやつだ

starhoshi commented 6 years ago
    const otherAddress = new Address()
    otherAddress.name = 'other'
    let newUser = new User()
    await newUser.save().then(async () => {
      await newUser.addresses.insert(otherAddress) 
    })

await しても結果は変わらなかったですね :cry: