Skip to content

Commit 3fcbf6a

Browse files
committed
feat: add merge method to the repository
1 parent b13357c commit 3fcbf6a

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/repository/Repository.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ export class Repository<M extends Model = Model> {
194194
return this.query().update(records)
195195
}
196196

197+
/**
198+
* Update records in the store without normalization.
199+
*/
200+
merge(records: Element[]): Promise<Collection<M>>
201+
merge(record: Element): Promise<Item<M>>
202+
merge(records: any): Promise<any> {
203+
return this.query().merge(records)
204+
}
205+
197206
/**
198207
* Destroy the models for the given id.
199208
*/

test/feature/repository/updates_update.spec.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,73 @@ describe('feature/repository/updates_update', () => {
5656
}
5757
})
5858
})
59+
60+
it('updates a record without normalization', async () => {
61+
const store = createStore()
62+
63+
fillState(store, {
64+
users: {
65+
1: { id: 1, name: 'John Doe', age: 40 },
66+
2: { id: 2, name: 'Jane Doe', age: 30 },
67+
3: { id: 3, name: 'Johnny Doe', age: 20 }
68+
}
69+
})
70+
71+
await store.$repo(User).merge({ id: 2, age: 50 })
72+
73+
assertState(store, {
74+
users: {
75+
1: { id: 1, name: 'John Doe', age: 40 },
76+
2: { id: 2, name: 'Jane Doe', age: 50 },
77+
3: { id: 3, name: 'Johnny Doe', age: 20 }
78+
}
79+
})
80+
})
81+
82+
it('updates an array of records without normalization', async () => {
83+
const store = createStore()
84+
85+
fillState(store, {
86+
users: {
87+
1: { id: 1, name: 'John Doe', age: 40 },
88+
2: { id: 2, name: 'Jane Doe', age: 30 },
89+
3: { id: 3, name: 'Johnny Doe', age: 20 }
90+
}
91+
})
92+
93+
await store.$repo(User).merge([
94+
{ id: 2, age: 50 },
95+
{ id: 3, age: 60 }
96+
])
97+
98+
assertState(store, {
99+
users: {
100+
1: { id: 1, name: 'John Doe', age: 40 },
101+
2: { id: 2, name: 'Jane Doe', age: 50 },
102+
3: { id: 3, name: 'Johnny Doe', age: 60 }
103+
}
104+
})
105+
})
106+
107+
it('does nothing if no matching record was found', async () => {
108+
const store = createStore()
109+
110+
fillState(store, {
111+
users: {
112+
1: { id: 1, name: 'John Doe', age: 40 },
113+
2: { id: 2, name: 'Jane Doe', age: 30 },
114+
3: { id: 3, name: 'Johnny Doe', age: 20 }
115+
}
116+
})
117+
118+
await store.$repo(User).merge({ id: 4, age: 50 })
119+
120+
assertState(store, {
121+
users: {
122+
1: { id: 1, name: 'John Doe', age: 40 },
123+
2: { id: 2, name: 'Jane Doe', age: 30 },
124+
3: { id: 3, name: 'Johnny Doe', age: 20 }
125+
}
126+
})
127+
})
59128
})

0 commit comments

Comments
 (0)