Skip to content

Commit

Permalink
feat: add "fresh" feature (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiaking committed May 14, 2020
1 parent dfb6265 commit fa9d30e
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/connection/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ export class Connection {
this.commit('insert', records)
}

/**
* Commit `fresh` mutation to the store.
*/
fresh(records: Elements): void {
this.commit('fresh', records)
}

/**
* Commit `update` mutation to the store.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/modules/Mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ function insert(state: State, records: Elements): void {
state.data = { ...state.data, ...records }
}

/**
* Commit `fresh` change to the store.
*/
function fresh(state: State, records: Elements): void {
state.data = records
}

/**
* Commit `update` change to the store.
*/
Expand Down Expand Up @@ -47,6 +54,7 @@ function flush(state: State): void {

export const mutations = {
insert,
fresh,
update,
delete: destroy,
flush
Expand Down
2 changes: 1 addition & 1 deletion src/query/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ export interface EagerLoad {

export type EagerLoadConstraint = (query: Query) => void

export type PersistMethod = 'insert' | 'update'
export type PersistMethod = 'insert' | 'fresh' | 'update'
22 changes: 22 additions & 0 deletions src/query/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,26 @@ export class Query<M extends Model = Model> {
return models
}

/**
* Insert the given records to the store by replacing any existing records.
*/
fresh(records: Element | Element[]): Promise<Collections> {
return this.persist('fresh', records)
}

/**
* Insert the given records to the store by replacing any existing records.
*/
async replace<E extends Element>(records: E[]): Promise<Collection<M>>
async replace<E extends Element>(record: E): Promise<M>
async replace(records: any): Promise<any> {
const models = this.hydrate(records)

this.connection.fresh(this.compile(models))

return models
}

/**
* Update the given record to the store.
*/
Expand Down Expand Up @@ -483,6 +503,8 @@ export class Query<M extends Model = Model> {
switch (method) {
case 'insert':
return this.add(mappedRecords)
case 'fresh':
return this.replace(mappedRecords)
case 'update':
return this.merge(mappedRecords)
default:
Expand Down
7 changes: 7 additions & 0 deletions src/repository/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ export class Repository<M extends Model = Model> {
return this.query().insert(records)
}

/**
* Insert the given records to the store by replacing any existing records.
*/
fresh(records: Element | Element[]): Promise<Collections> {
return this.query().fresh(records)
}

/**
* Update records in the store.
*/
Expand Down
62 changes: 62 additions & 0 deletions test/feature/repository/inserts_fresh.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { createStore, fillState, assertState } from 'test/Helpers'
import { Model, Attr, Str } from '@/index'

describe('feature/repository/inserts_fresh', () => {
class User extends Model {
static entity = 'users'

@Attr() id!: any
@Str('') name!: string
}

it('inserts a new record to the store', async () => {
const store = createStore()

await store.$repo(User).fresh({ id: 1, name: 'John Doe' })

assertState(store, {
users: {
1: { id: 1, name: 'John Doe' }
}
})
})

it('inserts multiple records to the store', async () => {
const store = createStore()

await store.$repo(User).fresh([
{ id: 1, name: 'John Doe' },
{ id: 2, name: 'Jane Doe' }
])

assertState(store, {
users: {
1: { id: 1, name: 'John Doe' },
2: { id: 2, name: 'Jane Doe' }
}
})
})

it('replaces existing records', async () => {
const store = createStore()

fillState(store, {
users: {
1: { id: 1, name: 'John Doe' },
2: { id: 2, name: 'Jane Doe' }
}
})

await store.$repo(User).fresh([
{ id: 3, name: 'Johnny Doe' },
{ id: 4, name: 'David Doe' }
])

assertState(store, {
users: {
3: { id: 3, name: 'Johnny Doe' },
4: { id: 4, name: 'David Doe' }
}
})
})
})

0 comments on commit fa9d30e

Please sign in to comment.