Skip to content

Commit 02f346e

Browse files
committed
fix: $isPersisted
1 parent 50151cc commit 02f346e

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

src/orm/model.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,18 @@ export default class Model {
105105
}
106106

107107
/**
108-
* Adds $isPersisted and other meta fields to the model by overwriting the fields() method.
109-
* @todo is this a good way to add fields?
108+
* Adds $isPersisted and other meta fields to the model.
109+
*
110+
* TODO: This feels rather hacky currently and may break anytime the internal structure of
111+
* the core changes. Maybe in the future there will be a feature in the core that allows to add
112+
* those meta fields by plugins.
110113
* @param {Model} model
111114
*/
112115
public static augment(model: Model) {
113-
const originalFieldGenerator = model.baseModel.fields.bind(model.baseModel);
114-
115-
model.baseModel.fields = () => {
116-
const originalFields = originalFieldGenerator();
117-
118-
originalFields["$isPersisted"] = model.baseModel.boolean(false);
116+
let baseModel: typeof PatchedModel = model.baseModel;
119117

120-
return originalFields;
121-
};
118+
baseModel.getFields();
119+
baseModel.cachedFields[baseModel.entity]["$isPersisted"] = baseModel.boolean(false);
122120
}
123121

124122
/**

test/integration/plugin.spec.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,17 @@ describe("Plugin GraphQL", () => {
5757

5858
test("is true for single fetched records", async () => {
5959
// @ts-ignore
60-
await User.fetch(1);
60+
await Post.fetch(1);
6161

62-
let user: Data = User.find(1)! as Data;
63-
expect(user.$isPersisted).toBeTruthy();
62+
let post: Data = Post.query().first()! as Data;
63+
expect(post.$isPersisted).toBeTruthy();
6464
});
6565

6666
test("is true for multiple fetched records", async () => {
67-
await User.fetch();
68-
await Tariff.fetch();
69-
70-
let user: Data = User.query().first()! as Data;
71-
expect(user.$isPersisted).toBeTruthy();
67+
await Post.fetch();
7268

73-
let tariff: Data = Tariff.query().first()! as Data;
74-
expect(tariff.$isPersisted).toBeTruthy();
69+
let post: Data = Post.query().first()! as Data;
70+
expect(post.$isPersisted).toBeTruthy();
7571
});
7672
});
7773
});

test/support/helpers.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ export function createStore(entities: Array<Entity>, headers?: any, adapter?: Ad
4242
database: database,
4343
link,
4444
headers,
45-
adapter
45+
adapter,
46+
debug: false
4647
});
4748

4849
const store = new Vuex.Store({

0 commit comments

Comments
 (0)