ar_sync 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ab145d5094c25600e95e01ab625c0128782b2a0592be95d8c9333539a5128bd1
4
- data.tar.gz: 062fcf8cb8996e145dfa35eb25b31f2ecc2b4a486183bc70678d055db2bf2bc8
3
+ metadata.gz: 8c13bb4ca3915637f310817c9b5c689c12c05f0140cfe2d982da653bc251bf7e
4
+ data.tar.gz: 447f9877703f3ba4cee8d590b6f63d91bc09486a86d2a709169ac245bcd592be
5
5
  SHA512:
6
- metadata.gz: 556a1c954088cea90d44bb61cec1fc250804f1fc5ec4e1c4101ff642343af49bc4ce8870be152de4627802931409d3c5f25707fa34b59ba8e53e9873a99e9b54
7
- data.tar.gz: 28846608f3314be4a117d1ffbf265702f2170755f027696300f991b01a9228302d7e218fce757826c7528c8643d98f66e1073741305c1d851f1fe6602c9f185b
6
+ metadata.gz: dd149d8f9b3ee83ba0e72cc0924bffdcaa9fc144c29a25b544ed9959f270ab65514fb7f18d0410ed1afc88d9db59b2b3f1c964b1005b6eae3057c2d4416d7174
7
+ data.tar.gz: c54c4e3b427c7f4bfec039bc4cacab491a413cc8e29a47307a4078e56266e7473c1171ed99bd5f3281b9afd509db298f293c9625250e040a7ef53f93c6d2f5ed
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ar_sync (1.1.0)
4
+ ar_sync (1.1.1)
5
5
  activerecord
6
6
  ar_serializer
7
7
 
data/core/ArSyncStore.js CHANGED
@@ -156,7 +156,7 @@ var ArSyncContainerBase = /** @class */ (function () {
156
156
  }
157
157
  this.listeners = [];
158
158
  };
159
- ArSyncContainerBase.compactQuery = function (query) {
159
+ ArSyncContainerBase.compactQueryAttributes = function (query) {
160
160
  function compactAttributes(attributes) {
161
161
  var attrs = {};
162
162
  var keys = [];
@@ -201,6 +201,8 @@ var ArSyncContainerBase = /** @class */ (function () {
201
201
  return result;
202
202
  }
203
203
  var result = compactQuery(query);
204
+ if (typeof result === 'object' && 'attributes' in result)
205
+ return result.attributes;
204
206
  return result === true ? {} : result;
205
207
  };
206
208
  ArSyncContainerBase.parseQuery = function (query, attrsonly) {
@@ -247,17 +249,17 @@ var ArSyncContainerBase = /** @class */ (function () {
247
249
  ArSyncContainerBase._load = function (_a, root) {
248
250
  var api = _a.api, id = _a.id, params = _a.params, query = _a.query;
249
251
  var parsedQuery = ArSyncRecord.parseQuery(query);
250
- var compactQuery = ArSyncRecord.compactQuery(parsedQuery);
252
+ var compactQueryAttributes = ArSyncRecord.compactQueryAttributes(parsedQuery);
251
253
  if (id != null) {
252
- return modelBatchRequest.fetch(api, compactQuery, id).then(function (data) {
254
+ return modelBatchRequest.fetch(api, compactQueryAttributes, id).then(function (data) {
253
255
  if (!data)
254
256
  throw { retry: false };
255
- var request = { api: api, id: id, query: compactQuery };
257
+ var request = { api: api, id: id, query: compactQueryAttributes };
256
258
  return new ArSyncRecord(parsedQuery, data, request, root);
257
259
  });
258
260
  }
259
261
  else {
260
- var request_1 = { api: api, query: compactQuery, params: params };
262
+ var request_1 = { api: api, query: compactQueryAttributes, params: params };
261
263
  return ArSyncApi_1.default.syncFetch(request_1).then(function (response) {
262
264
  if (!response) {
263
265
  throw { retry: false };
@@ -393,7 +395,7 @@ var ArSyncRecord = /** @class */ (function (_super) {
393
395
  else if (action === 'add') {
394
396
  if (this.data[aliasName] && this.data[aliasName].id === id)
395
397
  return;
396
- modelBatchRequest.fetch(className, ArSyncRecord.compactQuery(query), id).then(function (data) {
398
+ modelBatchRequest.fetch(className, ArSyncRecord.compactQueryAttributes(query), id).then(function (data) {
397
399
  if (!data || !_this.data)
398
400
  return;
399
401
  var model = new ArSyncRecord(query, data, null, _this.root);
@@ -444,40 +446,30 @@ var ArSyncRecord = /** @class */ (function (_super) {
444
446
  }
445
447
  };
446
448
  ArSyncRecord.prototype.patchQuery = function (key) {
447
- var val = this.queryAttributes[key];
448
- if (!val)
449
- return;
450
- var attributes = val.attributes, as = val.as, params = val.params;
451
- if (attributes && Object.keys(val.attributes).length === 0)
452
- attributes = null;
453
- if (!attributes && !as && !params)
454
- return key;
455
- var result = {};
456
- if (attributes)
457
- result.attributes = attributes;
458
- if (as)
459
- result.as = as;
460
- if (params)
461
- result.params = params;
462
- return result;
449
+ var _a;
450
+ var subQuery = this.queryAttributes[key];
451
+ if (subQuery)
452
+ return _a = {}, _a[key] = subQuery, _a;
463
453
  };
464
454
  ArSyncRecord.prototype.reloadQuery = function () {
465
- var _a;
466
455
  if (this.reloadQueryCache)
467
456
  return this.reloadQueryCache;
468
- var reloadQuery = this.reloadQueryCache = { attributes: [] };
457
+ var arrayQuery = [];
458
+ var hashQuery = {};
469
459
  for (var key in this.queryAttributes) {
470
460
  if (key === 'sync_keys')
471
461
  continue;
472
462
  var val = this.queryAttributes[key];
473
463
  if (!val || !val.attributes) {
474
- reloadQuery.attributes.push(key);
464
+ arrayQuery === null || arrayQuery === void 0 ? void 0 : arrayQuery.push(key);
465
+ hashQuery[key] = true;
475
466
  }
476
467
  else if (!val.params && Object.keys(val.attributes).length === 0) {
477
- reloadQuery.attributes.push((_a = {}, _a[key] = val, _a));
468
+ arrayQuery = null;
469
+ hashQuery[key] = val;
478
470
  }
479
471
  }
480
- return reloadQuery;
472
+ return this.reloadQueryCache = arrayQuery || hashQuery;
481
473
  };
482
474
  ArSyncRecord.prototype.update = function (data) {
483
475
  for (var key in data) {
@@ -515,7 +507,7 @@ var ArSyncCollection = /** @class */ (function (_super) {
515
507
  _this.path = path;
516
508
  _this.query = query;
517
509
  _this.queryAttributes = query.attributes || {};
518
- _this.compactQuery = ArSyncRecord.compactQuery(query);
510
+ _this.compactQueryAttributes = ArSyncRecord.compactQueryAttributes(query);
519
511
  if (request)
520
512
  _this.initForReload(request);
521
513
  if (query.params) {
@@ -638,7 +630,7 @@ var ArSyncCollection = /** @class */ (function (_super) {
638
630
  }
639
631
  }
640
632
  }
641
- modelBatchRequest.fetch(className, this.compactQuery, id).then(function (data) {
633
+ modelBatchRequest.fetch(className, this.compactQueryAttributes, id).then(function (data) {
642
634
  if (!data || !_this.data)
643
635
  return;
644
636
  var model = new ArSyncRecord(_this.query, data, null, _this.root);
data/lib/ar_sync/rails.rb CHANGED
@@ -47,8 +47,8 @@ module ArSync
47
47
  end
48
48
 
49
49
  def sync_call
50
- _api_call :sync do |model, current_user, query|
51
- ArSync.sync_serialize model, current_user, query
50
+ _api_call :sync do |schema, current_user, query|
51
+ ArSync.sync_serialize schema, current_user, query
52
52
  end
53
53
  end
54
54
 
@@ -69,15 +69,8 @@ module ArSync
69
69
  end
70
70
 
71
71
  def static_call
72
- _api_call :static do |model, current_user, query|
73
- case model
74
- when ArSync::Collection, ActiveRecord::Relation, Array
75
- ArSerializer.serialize model.to_a, query, context: current_user
76
- when ArSerializer::Serializable
77
- ArSerializer.serialize model, query, context: current_user
78
- else
79
- model
80
- end
72
+ _api_call :static do |schema, current_user, query|
73
+ ArSerializer.serialize schema, query, context: current_user
81
74
  end
82
75
  end
83
76
 
@@ -87,15 +80,19 @@ module ArSync
87
80
  if respond_to?(ArSync.config.current_user_method)
88
81
  current_user = send ArSync.config.current_user_method
89
82
  end
83
+ sch = schema
90
84
  responses = params[:requests].map do |request|
91
85
  begin
92
86
  api_name = request[:api]
93
- sch = schema
94
- info = sch.class._serializer_field_info api_name
95
- raise ArSync::ApiNotFound, "#{type.to_s.capitalize} API named `#{api_name}` not configured" unless info
96
- api_params = (request[:params].as_json || {}).transform_keys(&:to_sym)
97
- model = sch.instance_exec(current_user, **api_params, &info.data_block)
98
- { data: yield(model, current_user, request[:query].as_json) }
87
+ raise ArSync::ApiNotFound, "#{type.to_s.capitalize} API named `#{api_name}` not configured" unless sch.class._serializer_field_info api_name
88
+ query = {
89
+ api_name => {
90
+ as: :data,
91
+ params: request[:params].as_json,
92
+ attributes: request[:query].as_json
93
+ }
94
+ }
95
+ yield schema, current_user, query
99
96
  rescue StandardError => e
100
97
  { error: handle_exception(e) }
101
98
  end
@@ -1,3 +1,3 @@
1
1
  module ArSync
2
- VERSION = '1.1.0'
2
+ VERSION = '1.1.1'
3
3
  end
@@ -124,7 +124,7 @@ class ArSyncContainerBase {
124
124
  for (const l of this.listeners) l.unsubscribe()
125
125
  this.listeners = []
126
126
  }
127
- static compactQuery(query: ParsedQuery) {
127
+ static compactQueryAttributes(query: ParsedQuery) {
128
128
  function compactAttributes(attributes: Record<string, ParsedQuery>): [ParsedQuery, boolean] {
129
129
  const attrs = {}
130
130
  const keys: string[] = []
@@ -160,6 +160,7 @@ class ArSyncContainerBase {
160
160
  return result
161
161
  }
162
162
  const result = compactQuery(query)
163
+ if (typeof result === 'object' && 'attributes' in result) return result.attributes
163
164
  return result === true ? {} : result
164
165
  }
165
166
  static parseQuery(query, attrsonly: true): Record<string, ParsedQuery>
@@ -198,15 +199,15 @@ class ArSyncContainerBase {
198
199
  }
199
200
  static _load({ api, id, params, query }, root) {
200
201
  const parsedQuery = ArSyncRecord.parseQuery(query)
201
- const compactQuery = ArSyncRecord.compactQuery(parsedQuery)
202
+ const compactQueryAttributes = ArSyncRecord.compactQueryAttributes(parsedQuery)
202
203
  if (id != null) {
203
- return modelBatchRequest.fetch(api, compactQuery, id).then(data => {
204
+ return modelBatchRequest.fetch(api, compactQueryAttributes, id).then(data => {
204
205
  if (!data) throw { retry: false }
205
- const request = { api, id, query: compactQuery }
206
+ const request = { api, id, query: compactQueryAttributes }
206
207
  return new ArSyncRecord(parsedQuery, data, request, root)
207
208
  })
208
209
  } else {
209
- const request = { api, query: compactQuery, params }
210
+ const request = { api, query: compactQueryAttributes, params }
210
211
  return ArSyncApi.syncFetch(request).then((response: any) => {
211
212
  if (!response) {
212
213
  throw { retry: false }
@@ -338,7 +339,7 @@ class ArSyncRecord extends ArSyncContainerBase {
338
339
  this.onChange([aliasName], null)
339
340
  } else if (action === 'add') {
340
341
  if (this.data[aliasName] && this.data[aliasName].id === id) return
341
- modelBatchRequest.fetch(className, ArSyncRecord.compactQuery(query), id).then(data => {
342
+ modelBatchRequest.fetch(className, ArSyncRecord.compactQueryAttributes(query), id).then(data => {
342
343
  if (!data || !this.data) return
343
344
  const model = new ArSyncRecord(query, data, null, this.root)
344
345
  const child = this.children[aliasName]
@@ -374,30 +375,25 @@ class ArSyncRecord extends ArSyncContainerBase {
374
375
  }
375
376
  }
376
377
  patchQuery(key: string) {
377
- const val = this.queryAttributes[key]
378
- if (!val) return
379
- let { attributes, as, params } = val
380
- if (attributes && Object.keys(val.attributes).length === 0) attributes = null
381
- if (!attributes && !as && !params) return key
382
- const result: { attributes?; as?; params? } = {}
383
- if (attributes) result.attributes = attributes
384
- if (as) result.as = as
385
- if (params) result.params = params
386
- return result
378
+ const subQuery = this.queryAttributes[key]
379
+ if (subQuery) return { [key]: subQuery }
387
380
  }
388
381
  reloadQuery() {
389
382
  if (this.reloadQueryCache) return this.reloadQueryCache
390
- const reloadQuery = this.reloadQueryCache = { attributes: [] as any[] }
383
+ let arrayQuery = [] as string[] | null
384
+ const hashQuery = {}
391
385
  for (const key in this.queryAttributes) {
392
386
  if (key === 'sync_keys') continue
393
387
  const val = this.queryAttributes[key]
394
388
  if (!val || !val.attributes) {
395
- reloadQuery.attributes.push(key)
389
+ arrayQuery?.push(key)
390
+ hashQuery[key] = true
396
391
  } else if (!val.params && Object.keys(val.attributes).length === 0) {
397
- reloadQuery.attributes.push({ [key]: val })
392
+ arrayQuery = null
393
+ hashQuery[key] = val
398
394
  }
399
395
  }
400
- return reloadQuery
396
+ return this.reloadQueryCache = arrayQuery || hashQuery
401
397
  }
402
398
  update(data) {
403
399
  for (const key in data) {
@@ -428,7 +424,7 @@ class ArSyncCollection extends ArSyncContainerBase {
428
424
  ordering: Ordering = { orderBy: 'id', direction: 'asc' }
429
425
  query
430
426
  queryAttributes
431
- compactQuery
427
+ compactQueryAttributes
432
428
  data: any[]
433
429
  children: ArSyncRecord[]
434
430
  aliasOrderKey = 'id'
@@ -438,7 +434,7 @@ class ArSyncCollection extends ArSyncContainerBase {
438
434
  this.path = path
439
435
  this.query = query
440
436
  this.queryAttributes = query.attributes || {}
441
- this.compactQuery = ArSyncRecord.compactQuery(query)
437
+ this.compactQueryAttributes = ArSyncRecord.compactQueryAttributes(query)
442
438
  if (request) this.initForReload(request)
443
439
  if (query.params) {
444
440
  this.setOrdering(query.params)
@@ -528,7 +524,7 @@ class ArSyncCollection extends ArSyncContainerBase {
528
524
  }
529
525
  }
530
526
  }
531
- modelBatchRequest.fetch(className, this.compactQuery, id).then((data: any) => {
527
+ modelBatchRequest.fetch(className, this.compactQueryAttributes, id).then((data: any) => {
532
528
  if (!data || !this.data) return
533
529
  const model = new ArSyncRecord(this.query, data, null, this.root)
534
530
  model.parentModel = this
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_sync
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - tompng
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-17 00:00:00.000000000 Z
11
+ date: 2022-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord