Skip to content

Commit 8c6d87e

Browse files
authored
Merge pull request #12 from richmolj/master
Allow unpersisted attributes
2 parents cf102e7 + be71078 commit 8c6d87e

File tree

8 files changed

+38
-9
lines changed

8 files changed

+38
-9
lines changed

src/attribute.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ import Config from './configuration';
77
export default class Attribute {
88
name: string;
99

10+
persist: boolean = true;
1011
isAttr: boolean = true;
1112
isRelationship: boolean = false;
1213

14+
constructor(opts?: attributeOptions) {
15+
if (opts && opts.hasOwnProperty('persist')) {
16+
this.persist = opts.persist;
17+
}
18+
}
19+
1320
static applyAll(klass: typeof Model) : void {
1421
this._eachAttribute(klass, (attr) => {
15-
klass.attributeList.push(attr.name);
22+
klass.attributeList[attr.name] = attr;
1623
let descriptor = attr.descriptor();
1724
Object.defineProperty(klass.prototype, attr.name, descriptor);
1825
let instance = new klass();

src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import Attribute from './attribute';
1212
import attrDecorator from './util/attr-decorator';
1313
import { hasMany, hasOne, belongsTo } from './associations';
1414

15-
const attr = function() : any {
16-
return new Attribute();
15+
const attr = function(opts?: attributeOptions) : any {
16+
return new Attribute(opts);
1717
}
1818

1919
export { Config, Model, attr, attrDecorator, hasMany, hasOne, belongsTo, patchExtends };

src/model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default class Model {
4141
_markedForDisassociation: boolean = false;
4242
klass: typeof Model;
4343

44-
static attributeList = [];
44+
static attributeList = {};
4545
static relationList = [];
4646
private static _scope: Scope;
4747

@@ -208,7 +208,7 @@ export default class Model {
208208
assignAttributes(attrs: Object) {
209209
for(var key in attrs) {
210210
let attributeName = camelize(key);
211-
if (key == 'id' || this.klass.attributeList.indexOf(attributeName) >= 0) {
211+
if (key == 'id' || this.klass.attributeList[attributeName]) {
212212
this[attributeName] = attrs[key];
213213
}
214214
}

src/util/deserialize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class Deserializer {
160160
_iterateValidRelationships(instance, relationships, callback) {
161161
for (let key in relationships) {
162162
let relationName = camelize(key);
163-
if (instance.klass.attributeList.indexOf(relationName) >= 0) {
163+
if (instance.klass.attributeList[relationName]) {
164164
let relationData = relationships[key].data;
165165
if(!relationData) continue; // only links, empty, etc
166166
callback(relationName, relationData);

src/util/write-payload.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,10 @@ export default class WritePayload {
194194
private _eachAttribute(callback: Function) : void {
195195
let modelAttrs = this.model.attributes;
196196
Object.keys(modelAttrs).forEach((key) => {
197-
let value = modelAttrs[key];
198-
callback(key, value);
197+
if (this.model.klass.attributeList[key].persist) {
198+
let value = modelAttrs[key];
199+
callback(key, value);
200+
}
199201
});
200202
}
201203
}

test/fixtures.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class Person extends ApplicationRecord {
1414
lastName: string = attr();
1515
}
1616

17+
class PersonWithExtraAttr extends Person {
18+
extraThing: string = attr({ persist: false });
19+
}
20+
1721
// Ensure setup() can be run multiple times with no problems
1822
// putting this here, otherwise relations wont be available.
1923
Config.setup();
@@ -86,6 +90,7 @@ export {
8690
NonJWTOwner,
8791
Author,
8892
Person,
93+
PersonWithExtraAttr,
8994
Book,
9095
Genre,
9196
Bio,

test/integration/persistence-test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, fetchMock } from '../test-helper';
2-
import { Person } from '../fixtures';
2+
import { Person, PersonWithExtraAttr } from '../fixtures';
33

44
let fetchMock = require('fetch-mock');
55

@@ -51,6 +51,17 @@ describe('Model persistence', function() {
5151
});
5252

5353
describe('#save()', function() {
54+
describe('when a unpersisted attr', function() {
55+
it('does not send the attr to server', function(done) {
56+
instance = new PersonWithExtraAttr({ extraThing: 'foo' });
57+
expect(instance.extraThing).to.eq('foo');
58+
instance.save().then(() => {
59+
expect(payloads[0]['data']['attributes']).to.eq(undefined);
60+
done()
61+
});
62+
});
63+
});
64+
5465
describe('when the model is already persisted', function() {
5566
beforeEach(function() {
5667
instance.id = '1';

types/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ interface IResultProxy<T> {
2929
meta: Object
3030
raw: japiDoc
3131
}
32+
33+
interface attributeOptions {
34+
persist: boolean;
35+
}

0 commit comments

Comments
 (0)