mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: allows adapters to define a custom primaryKey (#9254)
This commit is contained in:
@@ -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 });
|
||||
|
||||
@@ -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 });
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user