diff --git a/core/ArSyncStore.js b/core/ArSyncStore.js index c2952d3..2a19c3f 100644 --- a/core/ArSyncStore.js +++ b/core/ArSyncStore.js @@ -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); @@ -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]; @@ -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]; } @@ -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]; @@ -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; @@ -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); } @@ -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]) @@ -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); @@ -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 }; }; @@ -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; diff --git a/src/core/ArSyncStore.ts b/src/core/ArSyncStore.ts index f4fcac8..6ad0f3b 100644 --- a/src/core/ArSyncStore.ts +++ b/src/core/ArSyncStore.ts @@ -231,6 +231,7 @@ class ArSyncRecord extends ArSyncContainerBase { id: number root query + queryAttributes data children: { [key: string]: ArSyncContainerBase | null } paths: string[] @@ -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) @@ -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] @@ -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] } @@ -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] @@ -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 @@ -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) { @@ -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() @@ -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[] @@ -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)) { @@ -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 } } @@ -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 diff --git a/test/model.rb b/test/model.rb index 5263fe3..8378db3 100644 --- a/test/model.rb +++ b/test/model.rb @@ -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 diff --git a/test/sync_test.rb b/test/sync_test.rb index 77262d0..576d36f 100644 --- a/test/sync_test.rb +++ b/test/sync_test.rb @@ -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({ diff --git a/test/type_test.ts b/test/type_test.ts index 4fb80cd..9508162 100644 --- a/test/type_test.ts +++ b/test/type_test.ts @@ -25,7 +25,7 @@ isOK>() const data2 = new ArSyncModel({ api: 'currentUser', query: ['id', 'name'] }).data! isOK>() const data3 = new ArSyncModel({ api: 'currentUser', query: '*' }).data! -isOK>() +isOK>() const data4 = new ArSyncModel({ api: 'currentUser', query: { posts: 'id' } }).data! isOK>() const data5 = new ArSyncModel({ api: 'currentUser', query: { posts: '*' } }).data!