Skip to content

Commit

Permalink
Merge pull request #361 from funkensturm/native-classes
Browse files Browse the repository at this point in the history
Refactor adapters and serializer to native classes
  • Loading branch information
fsmanuel authored Feb 6, 2024
2 parents 02112d1 + 3a5e879 commit 5bf7359
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 95 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ on:
- master
pull_request: {}


concurrency:
group: ci-${{ github.head_ref || github.ref }}
cancel-in-progress: true

jobs:
test:
name: "Tests"
name: 'Tests'
runs-on: ubuntu-latest
timeout-minutes: 10

Expand All @@ -33,7 +32,7 @@ jobs:
run: yarn test:ember

floating:
name: "Floating Dependencies"
name: 'Floating Dependencies'
runs-on: ubuntu-latest
timeout-minutes: 10

Expand All @@ -50,7 +49,7 @@ jobs:

try-scenarios:
name: ${{ matrix.try-scenario }}
needs: "test"
needs: 'test'
runs-on: ubuntu-latest
timeout-minutes: 10

Expand All @@ -67,7 +66,7 @@ jobs:
- ember-lts-4.8
- ember-4.11
- ember-lts-4.12
- ember-release
# - ember-release
# - ember-beta
# - ember-canary
- embroider-safe
Expand Down
116 changes: 44 additions & 72 deletions addon/adapters/base.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import RSVP from 'rsvp';
import JSONAPIAdapter from '@ember-data/adapter/json-api';
import { get } from '@ember/object';
import { run } from '@ember/runloop';
import { isEmpty, typeOf } from '@ember/utils';
import { computed, get } from '@ember/object';
import JSONAPIAdapter from '@ember-data/adapter/json-api';
import RSVP from 'rsvp';

import {
importData,
exportData,
Expand All @@ -12,43 +13,43 @@ import { _buildKey } from 'ember-local-storage/helpers/storage';
// Ember data ships with ember-inflector
import { singularize, pluralize } from 'ember-inflector';

export default JSONAPIAdapter.extend({
_debug: false,
_indices: computed(function () {
return {};
}),
coalesceFindRequests: false,
export default class BaseAdapter extends JSONAPIAdapter {
constructor() {
super(...arguments);

this._indices = {};
}

// TODO: v2.0 - What are the defaults now? What versions to support?
isNewSerializerAPI: true,
_debug = false;
coalesceFindRequests = false;

// TODO: v2.0 - Can we deprecate or remove that? What are the defaults now? What versions to support?
// Reload behavior
shouldReloadRecord() {
return true;
},
}
shouldReloadAll() {
return true;
},
}
shouldBackgroundReloadRecord() {
return true;
},
}
shouldBackgroundReloadAll() {
return true;
},
}

generateIdForRecord() {
return Math.random().toString(32).slice(2).substr(0, 8);
},
}

// Import & Export
// Import
importData(store, content, options) {
return importData(store, content, options);
},
}

// Export
exportData(store, types, options) {
return exportData(store, types, options);
},
}

// Relationship sugar
createRecord(store, type, snapshot) {
Expand All @@ -64,8 +65,8 @@ export default JSONAPIAdapter.extend({
}
});

return this._super.apply(this, arguments);
},
return super.createRecord(...arguments);
}

deleteRecord(store, type, snapshot) {
snapshot.eachRelationship(function (name, relationship) {
Expand All @@ -88,51 +89,21 @@ export default JSONAPIAdapter.extend({
}
});

return this._super.apply(this, arguments);
},

// Polyfill queryRecord
queryRecord(store, type, query) {
let records = this._super.apply(this, arguments);

if (!records) {
var url = this.buildURL(type.modelName, null, null, 'queryRecord', query);
return super.deleteRecord(...arguments);
}

// TODO: Document why this is needed or remove it!
if (this.sortQueryParams) {
query = this.sortQueryParams(query);
}

records = this.ajax(url, 'GET', { data: query });
}
queryRecord() {
let records = super.queryRecord(...arguments);

return records.then(function (result) {
return { data: result.data[0] || null };
});
},
}

// TODO: v2.0 - What are the defaults now? What versions to support?
// Delegate to _handleStorageRequest
ajax() {
return this._handleStorageRequest.apply(this, arguments);
},

// Delegate to _handleStorageRequest
makeRequest(request) {
return this._handleStorageRequest(request.url, request.method, {
data: request.data,
});
},

// Work arround ds-improved-ajax Feature Flag
_makeRequest() {
return this.makeRequest.apply(this, arguments);
},

// Remove the ajax() deprecation warning
_hasCustomizedAjax() {
return false;
},
}

// Delegate to _handle${type}Request
_handleStorageRequest(url, type, options = {}) {
Expand All @@ -149,7 +120,7 @@ export default JSONAPIAdapter.extend({
run(null, reject, `There is nothing to handle _handle${type}Request`);
}
}, 'DS: LocalStorageAdapter#_handleStorageRequest ' + type + ' to ' + url);
},
}

_handleGETRequest(url, query) {
const { type, id } = this._urlParts(url);
Expand Down Expand Up @@ -180,7 +151,7 @@ export default JSONAPIAdapter.extend({
}

return records;
},
}

_handlePOSTRequest(url, record) {
const { type, id } = record.data;
Expand All @@ -190,7 +161,7 @@ export default JSONAPIAdapter.extend({
get(this, '_storage')[storageKey] = JSON.stringify(record.data);

return null;
},
}

_handlePATCHRequest(url, record) {
const { type, id } = record.data;
Expand All @@ -200,7 +171,7 @@ export default JSONAPIAdapter.extend({
get(this, '_storage')[storageKey] = JSON.stringify(record.data);

return null;
},
}

_handleDELETERequest(url) {
const { type, id } = this._urlParts(url);
Expand All @@ -210,7 +181,7 @@ export default JSONAPIAdapter.extend({
delete get(this, '_storage')[storageKey];

return null;
},
}

// TODO: Extract into utility functions in private/query.js
_queryFilter(data, serializer, query = {}) {
Expand Down Expand Up @@ -288,15 +259,16 @@ export default JSONAPIAdapter.extend({
});
}
}
},
}

// TODO: Extract into utility function
_matches(recordValue, queryValue) {
if (typeOf(queryValue) === 'regexp') {
return queryValue.test(recordValue);
}

return recordValue === queryValue;
},
}

_urlParts(url) {
const parts = url.split('/');
Expand All @@ -316,27 +288,27 @@ export default JSONAPIAdapter.extend({
type: type,
id: id,
};
},
}

_storageKey(type, id) {
return _buildKey(this, type + '-' + id);
},
}

// Should be overwriten
// Signature: _getIndex(type)
_getIndex() {},
_getIndex() {}

_indexHasKey(type, id) {
return this._getIndex(type).indexOf(id) !== -1;
},
}

_addToIndex(type, id) {
if (!this._indexHasKey(type, id)) {
this._getIndex(type).addObject(id);
}
},
}

_removeFromIndex(type, id) {
this._getIndex(type).removeObject(id);
},
});
}
}
8 changes: 4 additions & 4 deletions addon/adapters/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { getStorage, _buildKey } from '../helpers/storage';
import StorageArray from '../local/array';
import { getOwner } from '@ember/application';

export default BaseAdapter.extend({
_storage: getStorage('local'),
export default class LocalStorageAdapter extends BaseAdapter {
_storage = getStorage('local');

_getIndex(type) {
const indices = get(this, '_indices');
Expand All @@ -19,5 +19,5 @@ export default BaseAdapter.extend({
}

return indices[type];
},
});
}
}
8 changes: 4 additions & 4 deletions addon/adapters/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { getStorage, _buildKey } from '../helpers/storage';
import StorageArray from '../session/array';
import { getOwner } from '@ember/application';

export default BaseAdapter.extend({
_storage: getStorage('session'),
export default class SessionStorageAdapter extends BaseAdapter {
_storage = getStorage('session');

_getIndex(type) {
const indices = get(this, '_indices');
Expand All @@ -19,5 +19,5 @@ export default BaseAdapter.extend({
}

return indices[type];
},
});
}
}
20 changes: 10 additions & 10 deletions addon/serializers/serializer.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import JSONAPISerializer from '@ember-data/serializer/json-api';

export default JSONAPISerializer.extend({
export default class Serializer extends JSONAPISerializer {
shouldSerializeHasMany() {
return true;
},
}

serializeBelongsTo() {
this._fixSerializeBelongsTo(...arguments);
},

serializeHasMany() {
this._fixSerializeHasMany(...arguments);
},
}

_fixSerializeBelongsTo(snapshot, json, relationship) {
let key = relationship.key;
Expand Down Expand Up @@ -43,7 +39,11 @@ export default JSONAPISerializer.extend({
json.relationships[payloadKey] = { data };
}
}
},
}

serializeHasMany() {
this._fixSerializeHasMany(...arguments);
}

_fixSerializeHasMany(snapshot, json, relationship) {
let key = relationship.key;
Expand Down Expand Up @@ -78,5 +78,5 @@ export default JSONAPISerializer.extend({
json.relationships[payloadKey] = { data };
}
}
},
});
}
}
2 changes: 2 additions & 0 deletions config/ember-try.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ module.exports = async function () {
'ember-auto-import': '^2.2.0',
'ember-data': 'latest',
'ember-source': await getChannelURL('release'),
'ember-resolver': '10.0.0',
'@ember/string': '3.0.1',
},
},
},
Expand Down

0 comments on commit 5bf7359

Please sign in to comment.