Skip to content

Commit

Permalink
Add primaryKey to models so we can use different attributes than id
Browse files Browse the repository at this point in the history
  • Loading branch information
masylum committed Mar 19, 2017
1 parent ba0c226 commit d9967ec
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ export default class Model {
return toJS(this.attributes)
}

/**
* Determine what attribute do you use
* as a primary id
*
* @abstract
*/
get primaryKey (): string {
return 'id'
}

/**
* Return the base url used in
* the `url` method
Expand Down Expand Up @@ -70,7 +80,7 @@ export default class Model {
if (this.isNew) {
return urlRoot
} else {
return `${urlRoot}/${this.get('id')}`
return `${urlRoot}/${this.get(this.primaryKey)}`
}
}

Expand All @@ -88,11 +98,10 @@ export default class Model {
* Wether the resource is new or not
*
* We determine this asking if it contains
* the `id` attribute (set by the server).
* the `primaryKey` attribute (set by the server).
*/
@computed
get isNew (): boolean {
return !this.has('id')
@computed get isNew (): boolean {
return !this.has(this.primaryKey)
}

/**
Expand Down Expand Up @@ -127,8 +136,8 @@ export default class Model {
* the backend assigned one or the client.
*/
get id (): Id {
return this.has('id')
? this.get('id')
return this.has(this.primaryKey)
? this.get(this.primaryKey)
: this.optimisticId
}

Expand Down Expand Up @@ -178,19 +187,18 @@ export default class Model {
/**
* Saves the resource on the backend.
*
* If the item has an `id` it updates it,
* If the item has a `primaryKey` it updates it,
* otherwise it creates the new resource.
*
* It supports optimistic and patch updates.
*
* TODO: Add progress
*/
@action
async save (
@action async save (
attributes: {},
{ optimistic = true, patch = true }: SaveOptions = {}
): Promise<*> {
if (!this.has('id')) {
if (!this.has(this.primaryKey)) {
this.set(Object.assign({}, attributes))
if (this.collection) {
return this.collection.create(this, { optimistic })
Expand Down Expand Up @@ -305,11 +313,10 @@ export default class Model {
* requests the backend to delete it there
* too
*/
@action
async destroy (
@action async destroy (
{ optimistic = true }: DestroyOptions = {}
): Promise<*> {
if (!this.has('id') && this.collection) {
if (!this.has(this.primaryKey) && this.collection) {
this.collection.remove([this.optimisticId], { optimistic })
return Promise.resolve()
}
Expand Down

0 comments on commit d9967ec

Please sign in to comment.