Skip to content

Commit

Permalink
Merge pull request #109 from tompng/query_without_subquery_bugfix
Browse files Browse the repository at this point in the history
subquery無しの時のバグとか修正
  • Loading branch information
tompng authored May 22, 2020
2 parents e97ea09 + 7b099a5 commit eb4f515
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 25 deletions.
26 changes: 14 additions & 12 deletions core/ArSyncStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ var ArSyncRecord = /** @class */ (function (_super) {
if (request)
_this.initForReload(request);
_this.query = query;
_this.queryAttributes = query.attributes || {};
_this.data = {};
_this.children = {};
_this.replaceData(data);
Expand All @@ -300,8 +301,8 @@ var ArSyncRecord = /** @class */ (function (_super) {
this.data.id = data.id;
}
this.paths = [];
for (var key in this.query.attributes) {
var subQuery = this.query.attributes[key];
for (var key in this.queryAttributes) {
var subQuery = this.queryAttributes[key];
var aliasName = subQuery.as || key;
var subData = data[aliasName];
var child = this.children[aliasName];
Expand Down Expand Up @@ -348,9 +349,9 @@ var ArSyncRecord = /** @class */ (function (_super) {
}
}
}
if (this.query.attributes['*']) {
if (this.queryAttributes['*']) {
for (var key in data) {
if (!this.query.attributes[key] && this.data[key] !== data[key]) {
if (!this.queryAttributes[key] && this.data[key] !== data[key]) {
this.mark();
this.data[key] = data[key];
}
Expand All @@ -361,7 +362,7 @@ var ArSyncRecord = /** @class */ (function (_super) {
ArSyncRecord.prototype.onNotify = function (notifyData, path) {
var _this = this;
var action = notifyData.action, className = notifyData.class_name, id = notifyData.id;
var query = path && this.query.attributes[path];
var query = path && this.queryAttributes[path];
var aliasName = (query && query.as) || path;
if (action === 'remove') {
var child = this.children[aliasName];
Expand Down Expand Up @@ -426,7 +427,7 @@ var ArSyncRecord = /** @class */ (function (_super) {
}
};
ArSyncRecord.prototype.patchQuery = function (key) {
var val = this.query.attributes[key];
var val = this.queryAttributes[key];
if (!val)
return;
var attributes = val.attributes, as = val.as, params = val.params;
Expand All @@ -448,10 +449,10 @@ var ArSyncRecord = /** @class */ (function (_super) {
if (this.reloadQueryCache)
return this.reloadQueryCache;
var reloadQuery = this.reloadQueryCache = { attributes: [] };
for (var key in this.query.attributes) {
for (var key in this.queryAttributes) {
if (key === 'sync_keys')
continue;
var val = this.query.attributes[key];
var val = this.queryAttributes[key];
if (!val || !val.attributes) {
reloadQuery.attributes.push(key);
}
Expand All @@ -463,7 +464,7 @@ var ArSyncRecord = /** @class */ (function (_super) {
};
ArSyncRecord.prototype.update = function (data) {
for (var key in data) {
var subQuery = this.query.attributes[key];
var subQuery = this.queryAttributes[key];
if (subQuery && subQuery.attributes && Object.keys(subQuery.attributes).length > 0)
continue;
if (this.data[key] === data[key])
Expand Down Expand Up @@ -496,6 +497,7 @@ var ArSyncCollection = /** @class */ (function (_super) {
_this.root = root;
_this.path = path;
_this.query = query;
_this.queryAttributes = query.attributes || {};
_this.compactQuery = ArSyncRecord.compactQuery(query);
if (request)
_this.initForReload(request);
Expand Down Expand Up @@ -524,7 +526,7 @@ var ArSyncCollection = /** @class */ (function (_super) {
var limitNumber = (typeof limit === 'number') ? limit : null;
if (limitNumber !== null && key !== 'id')
throw 'limit with custom order key is not supported';
var subQuery = this.query.attributes[key];
var subQuery = this.queryAttributes[key];
this.aliasOrderKey = (subQuery && subQuery.as) || key;
this.order = { limit: limitNumber, mode: mode, key: key };
};
Expand Down Expand Up @@ -557,13 +559,13 @@ var ArSyncCollection = /** @class */ (function (_super) {
for (var _b = 0, collection_1 = collection; _b < collection_1.length; _b++) {
var subData = collection_1[_b];
var model = undefined;
if (typeof (subData) === 'object' && subData && 'id' in subData)
if (typeof (subData) === 'object' && subData && 'sync_keys' in subData)
model = existings.get(subData.id);
var data_1 = subData;
if (model) {
model.replaceData(subData);
}
else if (subData.id) {
else if (subData.sync_keys) {
model = new ArSyncRecord(this.query, subData, null, this.root);
model.parentModel = this;
model.parentKey = subData.id;
Expand Down
28 changes: 16 additions & 12 deletions src/core/ArSyncStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ class ArSyncRecord extends ArSyncContainerBase {
id: number
root
query
queryAttributes
data
children: { [key: string]: ArSyncContainerBase | null }
paths: string[]
Expand All @@ -240,6 +241,7 @@ class ArSyncRecord extends ArSyncContainerBase {
this.root = root
if (request) this.initForReload(request)
this.query = query
this.queryAttributes = query.attributes || {}
this.data = {}
this.children = {}
this.replaceData(data)
Expand All @@ -258,8 +260,8 @@ class ArSyncRecord extends ArSyncContainerBase {
this.data.id = data.id
}
this.paths = []
for (const key in this.query.attributes) {
const subQuery = this.query.attributes[key]
for (const key in this.queryAttributes) {
const subQuery = this.queryAttributes[key]
const aliasName = subQuery.as || key
const subData = data[aliasName]
const child = this.children[aliasName]
Expand Down Expand Up @@ -300,9 +302,9 @@ class ArSyncRecord extends ArSyncContainerBase {
}
}
}
if (this.query.attributes['*']) {
if (this.queryAttributes['*']) {
for (const key in data) {
if (!this.query.attributes[key] && this.data[key] !== data[key]) {
if (!this.queryAttributes[key] && this.data[key] !== data[key]) {
this.mark()
this.data[key] = data[key]
}
Expand All @@ -312,7 +314,7 @@ class ArSyncRecord extends ArSyncContainerBase {
}
onNotify(notifyData: NotifyData, path?: string) {
const { action, class_name: className, id } = notifyData
const query = path && this.query.attributes[path]
const query = path && this.queryAttributes[path]
const aliasName = (query && query.as) || path;
if (action === 'remove') {
const child = this.children[aliasName]
Expand Down Expand Up @@ -359,7 +361,7 @@ class ArSyncRecord extends ArSyncContainerBase {
}
}
patchQuery(key: string) {
const val = this.query.attributes[key]
const val = this.queryAttributes[key]
if (!val) return
let { attributes, as, params } = val
if (attributes && Object.keys(val.attributes).length === 0) attributes = null
Expand All @@ -373,9 +375,9 @@ class ArSyncRecord extends ArSyncContainerBase {
reloadQuery() {
if (this.reloadQueryCache) return this.reloadQueryCache
const reloadQuery = this.reloadQueryCache = { attributes: [] as any[] }
for (const key in this.query.attributes) {
for (const key in this.queryAttributes) {
if (key === 'sync_keys') continue
const val = this.query.attributes[key]
const val = this.queryAttributes[key]
if (!val || !val.attributes) {
reloadQuery.attributes.push(key)
} else if (!val.params && Object.keys(val.attributes).length === 0) {
Expand All @@ -386,7 +388,7 @@ class ArSyncRecord extends ArSyncContainerBase {
}
update(data) {
for (const key in data) {
const subQuery = this.query.attributes[key]
const subQuery = this.queryAttributes[key]
if (subQuery && subQuery.attributes && Object.keys(subQuery.attributes).length > 0) continue
if (this.data[key] === data[key]) continue
this.mark()
Expand All @@ -411,6 +413,7 @@ class ArSyncCollection extends ArSyncContainerBase {
path: string
order: { limit: number | null; key: string; mode: 'asc' | 'desc' } = { limit: null, mode: 'asc', key: 'id' }
query
queryAttributes
compactQuery
data: any[]
children: ArSyncRecord[]
Expand All @@ -420,6 +423,7 @@ class ArSyncCollection extends ArSyncContainerBase {
this.root = root
this.path = path
this.query = query
this.queryAttributes = query.attributes || {}
this.compactQuery = ArSyncRecord.compactQuery(query)
if (request) this.initForReload(request)
if (query.params && (query.params.order || query.params.limit)) {
Expand All @@ -442,7 +446,7 @@ class ArSyncCollection extends ArSyncContainerBase {
}
const limitNumber = (typeof limit === 'number') ? limit : null
if (limitNumber !== null && key !== 'id') throw 'limit with custom order key is not supported'
const subQuery = this.query.attributes[key]
const subQuery = this.queryAttributes[key]
this.aliasOrderKey = (subQuery && subQuery.as) || key
this.order = { limit: limitNumber, mode, key }
}
Expand All @@ -468,11 +472,11 @@ class ArSyncCollection extends ArSyncContainerBase {
const newData: any[] = []
for (const subData of collection) {
let model: ArSyncRecord | undefined = undefined
if (typeof(subData) === 'object' && subData && 'id' in subData) model = existings.get(subData.id)
if (typeof(subData) === 'object' && subData && 'sync_keys' in subData) model = existings.get(subData.id)
let data = subData
if (model) {
model.replaceData(subData)
} else if (subData.id) {
} else if (subData.sync_keys) {
model = new ArSyncRecord(this.query, subData, null, this.root)
model.parentModel = this
model.parentKey = subData.id
Expand Down
2 changes: 2 additions & 0 deletions test/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class User < BaseRecord
sync_has_data :id, :name
sync_has_many :posts
sync_has_one(:postOrNull, type: ->{ [Post, nil] }) { nil }
sync_has_data(:itemWithId) { { id: 1, value: 'data' } }
sync_has_data(:itemsWithId) { [{ id: 1, value: 'data' }] }
end

class Post < BaseRecord
Expand Down
23 changes: 23 additions & 0 deletions test/sync_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,29 @@ class Schema
runner.eval_script 'postModel.release(); postModel = null'
end

tap do # no subquery test
runner.eval_script <<~JAVASCRIPT
global.noSubqueryTestModel = new ArSyncModel({
api: 'currentUser',
query: ['id', 'posts']
})
JAVASCRIPT
runner.assert_script 'noSubqueryTestModel.data'
runner.assert_script 'noSubqueryTestModel.data.posts[0].id >= 1'
end

tap do # object field test
runner.eval_script <<~JAVASCRIPT
global.objectFieldTestModel = new ArSyncModel({
api: 'currentUser',
query: ['itemWithId', 'itemsWithId']
})
JAVASCRIPT
runner.assert_script 'objectFieldTestModel.data'
runner.assert_script 'objectFieldTestModel.data.itemWithId', to_be: { 'id' => 1, 'value' => 'data' }
runner.assert_script 'objectFieldTestModel.data.itemsWithId', to_be: [{ 'id' => 1, 'value' => 'data' }]
end

tap do # wildcard update test
runner.eval_script <<~JAVASCRIPT
global.wildCardTestModel = new ArSyncModel({
Expand Down
2 changes: 1 addition & 1 deletion test/type_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ isOK<IsEqual<typeof data1, { id: number }>>()
const data2 = new ArSyncModel({ api: 'currentUser', query: ['id', 'name'] }).data!
isOK<IsEqual<typeof data2, { id: number; name: string | null }>>()
const data3 = new ArSyncModel({ api: 'currentUser', query: '*' }).data!
isOK<IsEqual<typeof data3, { id: number; name: string | null; sync_keys: string[]; posts: {}[]; postOrNull: {} | null }>>()
isOK<IsEqual<typeof data3, { id: number; name: string | null; sync_keys: string[]; posts: {}[]; postOrNull: {} | null; itemWithId: any; itemsWithId: any }>>()
const data4 = new ArSyncModel({ api: 'currentUser', query: { posts: 'id' } }).data!
isOK<IsEqual<typeof data4, { posts: { id: number }[] }>>()
const data5 = new ArSyncModel({ api: 'currentUser', query: { posts: '*' } }).data!
Expand Down

0 comments on commit eb4f515

Please sign in to comment.