5
5
*/
6
6
import {
7
7
FeathersVuexOptions ,
8
+ Id ,
8
9
ModelInstanceOptions ,
10
+ Model ,
9
11
ModelStatic ,
10
- ModelInstanceClone
12
+ ModelInstanceClone ,
13
+ ModelClone
11
14
} from './types'
12
15
import { globalModels , prepareAddModel } from './global-models'
13
- import { mergeWithAccessors , checkNamespace , getId } from '../utils'
16
+ import { mergeWithAccessors , checkNamespace , getId , Params } from '../utils'
14
17
import _merge from 'lodash/merge'
15
18
import _get from 'lodash/get'
16
19
import { EventEmitter } from 'events'
17
20
import { FeathersVuexStoreState , FeathersVuexGlobalModels } from '..'
21
+ import { ModelSetupContext } from './types'
18
22
import { Store } from 'vuex'
19
23
20
24
// A hack to prevent error with this.constructor.preferUpdate
@@ -64,14 +68,14 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
64
68
public static namespace : string
65
69
public static keepCopiesInStore = options . keepCopiesInStore
66
70
// eslint-disable-next-line
67
- public static instanceDefaults ( data , { models , store } ) {
71
+ public static instanceDefaults ( data : Partial < D > , ctx : ModelSetupContext ) {
68
72
return data
69
73
}
70
74
// eslint-disable-next-line
71
- public static setupInstance ( data , { models , store } ) {
75
+ public static setupInstance ( data : Partial < D > , ctx : ModelSetupContext ) {
72
76
return data
73
77
}
74
- public static diffOnPatch ( data ) {
78
+ public static diffOnPatch ( data : Partial < D > ) {
75
79
return data
76
80
}
77
81
@@ -84,7 +88,10 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
84
88
public static serverAlias : string = options . serverAlias
85
89
86
90
public static readonly models = globalModels as FeathersVuexGlobalModels // Can access other Models here
87
- public static copiesById = { }
91
+ public static copiesById : {
92
+ [ key : string ] : ModelClone < D > | undefined
93
+ [ key : number ] : ModelClone < D > | undefined
94
+ } = { }
88
95
89
96
public __id : string
90
97
public __isClone : boolean
@@ -93,7 +100,7 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
93
100
public static merge = mergeWithAccessors
94
101
public static modelName = 'BaseModel'
95
102
96
- public constructor ( data , options : ModelInstanceOptions ) {
103
+ public constructor ( data : Partial < D > , options : ModelInstanceOptions ) {
97
104
// You have to pass at least an empty object to get a tempId.
98
105
data = data || { }
99
106
options = Object . assign ( { } , defaultOptions , options )
@@ -182,23 +189,23 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
182
189
return getId ( record , idField )
183
190
}
184
191
185
- public static find ( params ) {
192
+ public static find ( params ?: Params ) {
186
193
return this . _dispatch ( 'find' , params )
187
194
}
188
195
189
- public static findInStore ( params ) {
196
+ public static findInStore ( params ?: Params ) {
190
197
return this . _getters ( 'find' , params )
191
198
}
192
199
193
- public static get ( id , params ) {
200
+ public static get ( id : Id , params ?: Params ) {
194
201
if ( params ) {
195
202
return this . _dispatch ( 'get' , [ id , params ] )
196
203
} else {
197
204
return this . _dispatch ( 'get' , id )
198
205
}
199
206
}
200
207
201
- public static getFromStore ( id , params ?) {
208
+ public static getFromStore ( id : Id , params ?: Params ) {
202
209
return this . _getters ( 'get' , id , params )
203
210
}
204
211
@@ -261,7 +268,7 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
261
268
/**
262
269
* clone the current record using the `createCopy` mutation
263
270
*/
264
- public clone ( data ) {
271
+ public clone ( data : Partial < D > ) : ModelClone < D > {
265
272
const { idField, tempIdField } = this . constructor as typeof BaseModel
266
273
if ( this . __isClone ) {
267
274
throw new Error ( 'You cannot clone a copy' )
@@ -294,7 +301,7 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
294
301
/**
295
302
* Reset a clone to match the instance in the store.
296
303
*/
297
- public reset ( ) {
304
+ public reset ( ) : this {
298
305
const { idField, tempIdField, _commit } = this
299
306
. constructor as typeof BaseModel
300
307
@@ -313,7 +320,7 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
313
320
/**
314
321
* Update a store instance to match a clone.
315
322
*/
316
- public commit ( ) {
323
+ public commit ( ) : Model < D > {
317
324
const { idField, tempIdField, _commit, _getters } = this
318
325
. constructor as typeof BaseModel
319
326
if ( this . __isClone ) {
@@ -333,7 +340,7 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
333
340
* A shortcut to either call create or patch/update
334
341
* @param params
335
342
*/
336
- public save ( params ) {
343
+ public save ( params ?: Params ) : Promise < this > {
337
344
const { idField, preferUpdate } = this . constructor as typeof BaseModel
338
345
const id = getId ( this , idField )
339
346
if ( id != null ) {
@@ -346,7 +353,7 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
346
353
* Calls service create with the current instance data
347
354
* @param params
348
355
*/
349
- public create ( params ) {
356
+ public create ( params ?: Params ) : Promise < this > {
350
357
const { _dispatch } = this . constructor as typeof BaseModel
351
358
const data = Object . assign ( { } , this )
352
359
if ( data [ options . idField ] === null ) {
@@ -359,7 +366,7 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
359
366
* Calls service patch with the current instance data
360
367
* @param params
361
368
*/
362
- public patch ( params ?) {
369
+ public patch ( params ?: Params ) : Promise < this > {
363
370
const { idField, _dispatch } = this . constructor as typeof BaseModel
364
371
const id = getId ( this , idField )
365
372
@@ -376,7 +383,7 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
376
383
* Calls service update with the current instance data
377
384
* @param params
378
385
*/
379
- public update ( params ) {
386
+ public update ( params ?: Params ) : Promise < this > {
380
387
const { idField, _dispatch } = this . constructor as typeof BaseModel
381
388
const id = getId ( this , idField )
382
389
@@ -393,7 +400,7 @@ export default function makeBaseModel(options: Required<FeathersVuexOptions>) {
393
400
* Calls service remove with the current instance id
394
401
* @param params
395
402
*/
396
- public remove ( params ) {
403
+ public remove ( params ?: Params ) : Promise < this > {
397
404
const { idField, tempIdField, _dispatch, _commit } = this
398
405
. constructor as typeof BaseModel
399
406
const id = getId ( this , idField )
0 commit comments