Skip to content

Commit

Permalink
Make add return ref
Browse files Browse the repository at this point in the history
  • Loading branch information
kossnocorp committed Apr 16, 2020
1 parent 82c349b commit c4d3dc0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ This change log follows the format documented in [Keep a CHANGELOG].

### Changed

- **BREAKING**: `set`, `transaction.set` and `batch.set` now return `Promise<void>` (or `void` in case of `batch.set`) to avoid confusion that the returned data match the current database state which might be not a case when using with field values i.e. `value('serverDate')`.
- **BREAKING**: `add` now return `Ref` instead of `Doc` to avoid confusion that the returned data match the current database state which might be not a case when using with field values i.e. `value('serverDate')`.

- **BREAKING**: `set`, `transaction.set` and `batch.set` now return `Promise<void>` (or `void` in case of `batch.set`). The same reasoning as for the `add` (see above).

- **BREAKING**: `set`, `transaction.set` and `batch.set` now don't accept `merge` option. Instead use the new `upset` function that provides better typing and ensures data consistency.

Expand Down
27 changes: 17 additions & 10 deletions src/add/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import firestore from '../adaptor'
import { Collection } from '../collection'
import { unwrapData } from '../data'
import { doc } from '../doc'
import { ref } from '../ref'
import { AddValue } from '../value'

/**
* Type of the data passed to the set function. It extends the model
* allowing to set server date field value.
*/
export type AddModel<Model> = {
[Key in keyof Model]:
| (Model[Key] extends object ? AddModel<Model[Key]> : Model[Key])
| AddValue<Model[Key]>
}

/**
* Adds a new document with a random id to a collection.
Expand All @@ -13,24 +23,21 @@ import { ref } from '../ref'
* type User = { name: string }
* const users = collection<User>('users')
*
* add(users, { name: 'Sasha' }).then(sasha => {
* console.log(sasha.ref.id)
* //=> '00sHm46UWKObv2W7XK9e'
* console.log(sasha.data)
* //=> { name: 'Sasha' }
* })
* const user = await add(users, { name: 'Sasha' })
* console.log(user.id)
* //=> '00sHm46UWKObv2W7XK9e'
* ```
*
* @param collection - The collection to add to
* @param data - The data to add to
* @returns A promise to the document
* @returns A promise to the ref
*/
export default async function add<Model>(
collection: Collection<Model>,
data: Model
data: AddModel<Model>
) {
const firebaseDoc = await firestore()
.collection(collection.path)
.add(unwrapData(data))
return doc<Model>(ref(collection, firebaseDoc.id), data)
return ref(collection, firebaseDoc.id)
}
11 changes: 6 additions & 5 deletions src/add/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('add', () => {
it('adds document to collection', async () => {
const data = { name: 'Sasha' }
const user = await add(users, data)
const { id } = user.ref
const { id } = user
assert(typeof id === 'string')
const userFromDB = await get(users, id)
assert.deepEqual(userFromDB.data, data)
Expand All @@ -24,10 +24,10 @@ describe('add', () => {
it('supports references', async () => {
const user = await add(users, { name: 'Sasha' })
const post = await add(posts, {
author: user.ref,
author: user,
text: 'Hello!'
})
const postFromDB = await get(posts, post.ref.id)
const postFromDB = await get(posts, post.id)
const userFromDB = await get(users, postFromDB.data.author.id)
assert.deepEqual(userFromDB.data, { name: 'Sasha' })
})
Expand All @@ -40,18 +40,19 @@ describe('add', () => {
text: 'Hello!',
date
})
const postFromDB = await get(posts, post.ref.id)
const postFromDB = await get(posts, post.id)
assert(postFromDB.data.date instanceof Date)
assert(postFromDB.data.date.getTime() === date.getTime())
})

it('supports server dates', async () => {
const userRef = ref(users, '42')
const post = await add(posts, {
const postRef = await add(posts, {
author: userRef,
text: 'Hello!',
date: value('serverDate')
})
const post = await get(postRef)
const now = Date.now()
const returnedDate = post.data.date
assert(returnedDate instanceof Date)
Expand Down
11 changes: 7 additions & 4 deletions src/value/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export type ValueArrayRemove = {
/**
* The server date value type.
*/
export interface ValueServerDate extends Date {
export type ValueServerDate = {
__type__: 'value'
kind: 'serverDate'
}
Expand All @@ -67,6 +67,11 @@ export type UpdateValue<T> = T extends number
? ValueServerDate | AnyUpdateValue
: AnyUpdateValue

/**
* The value types to use for add operation.
*/
export type AddValue<T> = T extends Date ? ValueServerDate : never

/**
* The value types to use for set operation.
*/
Expand Down Expand Up @@ -170,9 +175,7 @@ function value(
return { __type__: 'value', kind: 'arrayRemove', values: payload }

case 'serverDate':
const date = new Date()
Object.assign(date, { __type__: 'value', kind: 'serverDate' })
return date as ValueServerDate
return { __type__: 'value', kind: 'serverDate' }
}
}

Expand Down

0 comments on commit c4d3dc0

Please sign in to comment.