FIX: allows adapters to define a custom primaryKey (#9254)

This commit is contained in:
Joffrey JAFFEUX
2020-03-30 15:23:59 +02:00
committed by GitHub
parent 92e81d2ae5
commit 2b78bd01ab
5 changed files with 68 additions and 24 deletions

View File

@@ -5,12 +5,21 @@ import TopicListAdapter from "discourse/adapters/topic-list";
import TopicTrackingState from "discourse/models/topic-tracking-state";
import { buildResolver } from "discourse-common/resolver";
const CatAdapter = RestAdapter.extend({
primaryKey: "cat_id"
});
export default function(customLookup = () => {}) {
const resolver = buildResolver("discourse").create();
return Store.create({
register: {
lookup(type) {
if (type === "adapter:cat") {
this._catAdapter =
this._catAdapter || CatAdapter.create({ owner: this });
return this._catAdapter;
}
if (type === "adapter:rest") {
if (!this._restAdapter) {
this._restAdapter = RestAdapter.create({ owner: this });

View File

@@ -26,9 +26,23 @@ const colors = [
{ id: 3, name: "Yellow" }
];
const cats = [
{
cat_id: 1,
name: "souna"
}
];
export default function(helpers) {
const { response, success, parsePostData } = helpers;
this.get("/cats", function() {
return response({
__rest_serializer: "1",
cats
});
});
this.get("/fruits/:id", function(request) {
const fruit = fruits.find(f => f.id === parseInt(request.params.id, 10));
return response({ __rest_serializer: "1", fruit, farmers, colors });

View File

@@ -182,3 +182,9 @@ QUnit.test("findAll embedded", async assert => {
assert.equal(fruits.objectAt(2).get("farmer.name"), "Luke Skywalker");
});
QUnit.test("custom primaryKey", async assert => {
const store = createStore();
const cats = await store.findAll("cat");
assert.equal(cats.objectAt(0).name, "souna");
});