https://github.com/starhoshi/tart
A thin wrapper for Cloud Functions.
https://github.com/starhoshi/tart
cloud-functions firebase firestore npm tyepscript
Last synced: 9 months ago
JSON representation
A thin wrapper for Cloud Functions.
- Host: GitHub
- URL: https://github.com/starhoshi/tart
- Owner: starhoshi
- License: mit
- Created: 2018-03-02T03:53:35.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2021-01-21T13:05:45.000Z (about 5 years ago)
- Last Synced: 2024-11-11T18:46:23.524Z (over 1 year ago)
- Topics: cloud-functions, firebase, firestore, npm, tyepscript
- Language: TypeScript
- Homepage:
- Size: 153 KB
- Stars: 4
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tart
[](https://badge.fury.io/js/%40star__hoshi%2Ftart)
[](https://travis-ci.org/starhoshi/tart)
[](https://www.codacy.com/app/kensuke1751/tart?utm_source=github.com&utm_medium=referral&utm_content=starhoshi/tart&utm_campaign=Badge_Grade)
[](https://opensource.org/licenses/MIT)
Tart is a thin wrapper for Cloud Functions.
Let's define the model and write Cloud Functions with TypesScript.
# Sample
You can write like this.
```ts
import * as Tart from '@star__hoshi/tart'
Tart.initialize(admin.firestore())
interface User extends Tart.Timestamps {
name: string
}
export const updateUser = functions.firestore.document('user/{userId}')
.onCreate(async event => {
const user = new Tart.Snapshot(event.data)
console.log(user.data.name) // => old name
console.log(user.ref) // => DocumentReference
await user.update({ name: 'new name'})
console.log(user.data.name) // => new name
return undefined
})
```
# Installation
```
npm install @star__hoshi/tart --save
yarn add @star__hoshi/tart
```
# Usage
## Initialize
```ts
import * as Tart from '@star__hoshi/tart'
import * as admin from 'firebase-admin'
admin.initializeApp(functions.config().firebase)
// Tart expects timestampsInSnapshots to be 'true'
admin.firestore().settings({ timestampsInSnapshots: true })
// both admin sdk and cloud functions are same interface.
Tart.initialize(admin.firestore())
```
## Define Interface
You have to extend `Tart.Timestamps`.
```ts
interface User extends Tart.Timestamps {
name: string
}
interface Game extends Tart.Timestamps {
price: string
}
```
## Snapshot Type
Snapshot has 2 local variables.
* ref
* DocumentReference
* data
* T extends Tart.Timestamps
```ts
const user = new Tart.Snapshot(snapshot)
console.log(user.data) // => Same as snapshot.data()
console.log(user.ref) // => Same as snapshot.ref
```
## Data Management
### Save
```ts
const data: User = { name: 'test' }
const user = Tart.Snapshot.makeNotSavedSnapshot('user', data)
// Save a document
await user.save()
// Batched writes
const batch = admin.firestore().batch()
user.saveWithBatch(batch)
await batch.commit()
```
### Save Reference Collection
[Reference|Nedted] Collection's description is [here](https://github.com/1amageek/pring#nested-collection--reference-collection).
```ts
const user = Tart.Snapshot.makeNotSavedSnapshot('user', { name: 'test' })
const game = Tart.Snapshot.makeNotSavedSnapshot('game', { price: 1000 })
const batch = admin.firestore().batch()
user.saveWithBatch(batch)
user.saveReferenceCollectionWithBatch(batch, 'games', game.ref)
user.saveNestedCollectionWithBatch(batch, 'nestedgames', game.ref)
game.saveWithBatch(batch)
await batch.commit()
```
### Get
Pass path or ref as argument.
```ts
const savedUser = await Tart.fetch('user', 'id')
const savedUser = await Tart.fetch(userDocumentReference)
```
#### Refresh
Reload the data.
```ts
const savedUser = await Tart.fetch(userDocumentReference)
await savedUser.refresh()
```
### Update
```ts
const savedUser = await Tart.fetch('user', 'id')
// Update a document
await savedUser.update({ name: 'new name' })
// Batched writes
savedUser.saveWithBatch(batch)
savedUser.updateWithBatch(batch, { name: 'new name' })
await savedUser.commit()
```
### Delete
```ts
const savedUser = await Tart.fetch('user', 'id')
// Delete a document
await savedUser.delete()
// Batched writes
savedUser.deleteWithBatch(batch)
await savedUser.commit()
```