Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pull:285 fix update of record with new id #664

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/connections/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ export default class Connection {
this.state.data = { ...this.state.data, ...records }
}

/**
* Updates the given record.
*/
update(newId: String, record: Record): void {
delete this.state.data[record.$id]
record.$id = newId
this.state.data = { ...this.state.data, [record.$id]: record }
}

/**
* Delete records that matches the given id.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/modules/RootMutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ function insertRecords(this: Store<any>, state: RootState, payload: any): void {
new Connection(this, state.$name, entity).insertRecords(records)
}

/**
* Update the given record.
*/
function update(this: Store<any>, state: RootState, payload: any): void {
const { entity, record, newId } = payload
new Connection(this, state.$name, entity).update(newId, record)
}

/**
* Delete records from the store. The actual name for this mutation is
* `delete`, but named `destroy` here because `delete` can't be declared at
Expand All @@ -46,6 +54,7 @@ const RootMutations: MutationsContract = {
$mutate,
insert,
insertRecords,
update,
delete: destroy
}

Expand Down
39 changes: 11 additions & 28 deletions src/query/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1001,8 +1001,6 @@ export default class Query<T extends Model = Model> {
* Commit `update` to the state.
*/
private performUpdate(models: Data.Instances<T>): Data.Collection<T> {
models = this.updateIndexes(models)

const beforeHooks = this.buildHooks(
'beforeUpdate'
) as Contracts.MutationHook[]
Expand All @@ -1021,7 +1019,10 @@ export default class Query<T extends Model = Model> {
continue
}

this.commitInsert(model.$getAttributes())
this.commitUpdate(
String(model.$getIndexIdFromAttributes()),
model.$getAttributes()
)

afterHooks.forEach((hook) => {
hook(model, null, this.entity)
Expand All @@ -1033,31 +1034,6 @@ export default class Query<T extends Model = Model> {
return updated
}

/**
* Update the key of the instances. This is needed when a user updates
* record's primary key. We must then update the index key to
* correspond with new id value.
*/
private updateIndexes(instances: Data.Instances<T>): Data.Instances<T> {
return Object.keys(instances).reduce<Data.Instances<T>>(
(instances, key) => {
const instance = instances[key]
const id = String(this.model.getIndexIdFromRecord(instance))

if (key !== id) {
instance.$id = id

instances[id] = instance

delete instances[key]
}

return instances
},
instances
)
}

/**
* Insert or update given data to the state. Unlike `insert`, this method
* will not replace existing data within the state, but it will update only
Expand Down Expand Up @@ -1262,6 +1238,13 @@ export default class Query<T extends Model = Model> {
this.commit('insert', { record })
}

/**
* Commit update mutation.
*/
private commitUpdate(newId: String, record: Data.Record): void {
this.commit('update', { newId, record })
}

/**
* Commit insert records mutation.
*/
Expand Down
2 changes: 2 additions & 0 deletions test/feature/basics/Update.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,9 +625,11 @@ describe('Feature – Basics – Update', () => {
})

const user: any = User.find(2)
const count: any = User.query().count()

expect(user.$id).toBe('2')
expect(user.id).toBe(2)
expect(count).toBe(1)
})

it('returns a updated object', async () => {
Expand Down