@auxilium/datalynk-client 0.9.12 → 1.0.0

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.
package/dist/index.cjs CHANGED
@@ -36,6 +36,10 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
36
36
  return value2;
37
37
  }, space);
38
38
  }
39
+ function flattenArr(arr, result = []) {
40
+ arr.forEach((el) => Array.isArray(el) ? flattenArr(el, result) : result.push(el));
41
+ return result;
42
+ }
39
43
  class PromiseProgress extends Promise {
40
44
  constructor(executor) {
41
45
  super((resolve, reject) => executor(
@@ -1679,7 +1683,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
1679
1683
  * @return {Promise<boolean>} True if system administrator
1680
1684
  */
1681
1685
  async isSysAdmin() {
1682
- return !!(await this.api.slice("sysadmin").select().where("auth_ref", "==", "$viewer").exec().keys()).length;
1686
+ return !!(await this.api.slice("sysadmin").select().where("auth_ref", "==", "$viewer").rows().exec()).length;
1683
1687
  }
1684
1688
  /**
1685
1689
  * Check if user is a table administrator
@@ -1687,7 +1691,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
1687
1691
  * @return {Promise<boolean>} True if table administrator
1688
1692
  */
1689
1693
  async isTableAdmin() {
1690
- return !!(await this.api.slice("tableadmins").select().where("auth_ref", "==", "$viewer").exec().keys()).length;
1694
+ return !!(await this.api.slice("tableadmins").select().where("auth_ref", "==", "$viewer").rows().exec()).length;
1691
1695
  }
1692
1696
  /**
1693
1697
  * Check if user is a user administrator
@@ -1695,7 +1699,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
1695
1699
  * @return {Promise<boolean>} True if user administrator
1696
1700
  */
1697
1701
  async isUserAdmin() {
1698
- return !!(await this.api.slice("useradmins").select().where("auth_ref", "==", "$viewer").exec().keys()).length;
1702
+ return !!(await this.api.slice("useradmins").select().where("auth_ref", "==", "$viewer").rows().exec()).length;
1699
1703
  }
1700
1704
  /**
1701
1705
  * Perform login and save the session token
@@ -1836,7 +1840,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
1836
1840
  })).then(async (files2) => {
1837
1841
  if (associate) {
1838
1842
  let id = typeof associate.row == "number" ? associate.row : associate.row[associate.pk || "id"];
1839
- if (!id) id = await this.api.slice(associate.slice).insert(associate.row).exec().key();
1843
+ if (!id) id = await this.api.slice(associate.slice).insert(associate.row).id();
1840
1844
  await this.associate(files2.map((f2) => f2.id), associate == null ? void 0 : associate.slice, associate == null ? void 0 : associate.row, associate == null ? void 0 : associate.field);
1841
1845
  }
1842
1846
  return files2;
@@ -1996,112 +2000,6 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
1996
2000
  }
1997
2001
  }
1998
2002
  };
1999
- class SlicePromise {
2000
- /**
2001
- * An object to provide helpers for Datalynk results
2002
- *
2003
- * @param slice
2004
- * @param {SliceResponse<T>} promise Datalynk promise to handle
2005
- */
2006
- constructor(slice, promise) {
2007
- this.slice = slice;
2008
- this.promise = promise;
2009
- }
2010
- /**
2011
- * Catch promise errors
2012
- */
2013
- catch(callback) {
2014
- return this.promise.catch(callback);
2015
- }
2016
- /**
2017
- * Count rows or fetch count alias result
2018
- */
2019
- async count() {
2020
- var _a, _b;
2021
- const rows = await this.rows();
2022
- if (typeof ((_a = rows == null ? void 0 : rows[0]) == null ? void 0 : _a["count"]) == "number") return (_b = rows == null ? void 0 : rows[0]) == null ? void 0 : _b["count"];
2023
- return rows.length;
2024
- }
2025
- /**
2026
- * Log the raw result to the console for inspection
2027
- */
2028
- debug() {
2029
- this.promise.then((resp) => console.log(resp));
2030
- return this;
2031
- }
2032
- /**
2033
- * Fields that failed
2034
- */
2035
- async failed() {
2036
- let resp = await this.promise;
2037
- return resp.failed;
2038
- }
2039
- /**
2040
- * Catch promise errors
2041
- */
2042
- finally(callback) {
2043
- return this.promise.finally(callback);
2044
- }
2045
- /**
2046
- * Fields that were ignored (Perms or readonly)
2047
- */
2048
- async ignored() {
2049
- let resp = await this.promise;
2050
- return resp["ignored-fields"];
2051
- }
2052
- /**
2053
- * ID of first affected row
2054
- */
2055
- async key() {
2056
- return (await this.keys())[0];
2057
- }
2058
- /**
2059
- * IDs of all affected rows
2060
- */
2061
- async keys() {
2062
- let resp = await this.promise;
2063
- return resp.keys;
2064
- }
2065
- /**
2066
- * Fields that were affected successfully
2067
- */
2068
- async granted() {
2069
- let resp = await this.promise;
2070
- return resp.granted;
2071
- }
2072
- /**
2073
- * First row of data
2074
- */
2075
- async row() {
2076
- return (await this.rows())[0];
2077
- }
2078
- /**
2079
- * All rows of data
2080
- */
2081
- async rows() {
2082
- let resp = await this.promise;
2083
- return resp.rows;
2084
- }
2085
- /**
2086
- * Handle as normal request
2087
- */
2088
- then(success, error) {
2089
- return this.promise.then(success, error);
2090
- }
2091
- /**
2092
- * Get the raw promise
2093
- */
2094
- toPromise() {
2095
- return this.promise;
2096
- }
2097
- /**
2098
- * Get the transaction ID
2099
- */
2100
- async transaction() {
2101
- let resp = await this.promise;
2102
- return resp.tx;
2103
- }
2104
- }
2105
2003
  const _Slice = class _Slice {
2106
2004
  /**
2107
2005
  * An object to aid in constructing requests
@@ -2111,11 +2009,25 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2111
2009
  */
2112
2010
  constructor(slice, api) {
2113
2011
  __publicField(this, "operation");
2012
+ __publicField(this, "popField");
2114
2013
  __publicField(this, "request", {});
2014
+ /** Log response automatically */
2015
+ __publicField(this, "debugging");
2115
2016
  /** Unsubscribe from changes, undefined if not subscribed */
2116
2017
  __publicField(this, "unsubscribe");
2117
2018
  /** Cached slice data as an observable */
2118
2019
  __publicField(this, "cache$", new BehaviorSubject([]));
2020
+ /**
2021
+ * Whitelist and alias fields. Alias of `fields()`
2022
+ * @example
2023
+ * ```ts
2024
+ * const id: {id: number, field2: any}[] = await new Slice<T>(12345)
2025
+ * .select().alias({id: 'id', field1: 'field2'}).exec().keys();
2026
+ * ```
2027
+ * @param {object} aliasKeyVals List of properties to whitelist and what to rename them to
2028
+ * @return {Slice<T>}
2029
+ */
2030
+ __publicField(this, "alias", this.fields);
2119
2031
  this.slice = slice;
2120
2032
  this.api = api;
2121
2033
  }
@@ -2129,19 +2041,13 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2129
2041
  }
2130
2042
  /** Get raw API request */
2131
2043
  get raw() {
2132
- return { [this.operation]: { slice: this.slice, ...this.request } };
2133
- }
2134
- /**
2135
- * Whitelist and alias fields. Alias for the fields functions
2136
- * @example
2137
- * ```ts
2138
- * const id: {id: number, field2: any}[] = await new Slice<T>(12345)
2139
- * .select().alias({id: 'id', field1: 'field2'}).exec().keys();
2140
- * ```
2141
- * @param {object} aliasKeyVals List of properties to whitelist and what to rename them to
2142
- */
2143
- alias(aliasKeyVals) {
2144
- return this.fields(aliasKeyVals);
2044
+ return clean({
2045
+ [this.operation]: {
2046
+ ...this.request,
2047
+ slice: this.slice
2048
+ },
2049
+ "$pop": this.popField ? this.popField : void 0
2050
+ });
2145
2051
  }
2146
2052
  /**
2147
2053
  * Add an 'AND' condition inside the where argument
@@ -2153,6 +2059,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2153
2059
  * .where({field2: 2, field3: 3})
2154
2060
  * .exec().rows();
2155
2061
  * ```
2062
+ * @return {Slice<T>}
2156
2063
  */
2157
2064
  and() {
2158
2065
  var _a;
@@ -2170,17 +2077,20 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2170
2077
  * .exec().count();
2171
2078
  * ```
2172
2079
  * @param {object | string} arg Count argument
2080
+ * @return {Slice<T>}
2173
2081
  */
2174
2082
  count(arg = "id") {
2175
2083
  this.operation = "$/slice/report";
2176
2084
  this.request.fields = { count: ["$count", typeof arg == "object" ? arg : ["$field", arg]] };
2177
- return this;
2085
+ return this.pop("rows:0:count");
2178
2086
  }
2179
2087
  /**
2180
2088
  * Output the formed request to the console for inspection
2089
+ * @param {boolean} enabled Enable/Disable console logging
2090
+ * @return {Slice<T>}
2181
2091
  */
2182
- debug() {
2183
- console.debug(this.request);
2092
+ debug(enabled = true) {
2093
+ this.debugging = enabled;
2184
2094
  return this;
2185
2095
  }
2186
2096
  /**
@@ -2190,6 +2100,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2190
2100
  * await new Slice(12345).delete(id).exec();
2191
2101
  * ```
2192
2102
  * @param {number | number[]} id ID(s) to delete
2103
+ * @return {Slice<T>}
2193
2104
  */
2194
2105
  delete(id) {
2195
2106
  this.operation = "$/slice/delete";
@@ -2208,6 +2119,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2208
2119
  * .exec().rows();
2209
2120
  * ```
2210
2121
  * @param formula Excel formula to use as where clause
2122
+ * @return {Slice<T>}
2211
2123
  */
2212
2124
  excel(formula) {
2213
2125
  this.where(["$excel", formula]);
@@ -2216,17 +2128,26 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2216
2128
  /**
2217
2129
  * Compile the request and send it
2218
2130
  * @param {ApiRequestOptions} options API Request options
2219
- * @return {SlicePromise<T>}
2131
+ * @return {Promise<T = any>} API response
2220
2132
  */
2221
2133
  exec(options) {
2222
2134
  if (!this.operation) throw new Error("No operation chosen");
2223
- this.request.slice = this.slice;
2224
- return new SlicePromise(this, this.api.request({ [this.operation]: this.request }, options));
2135
+ return this.api.request(this.raw, options).then((resp) => {
2136
+ if (this.debugging) console.log(resp);
2137
+ return resp;
2138
+ });
2225
2139
  }
2226
2140
  fields(keys) {
2227
2141
  this.request.fields = Array.isArray(keys) ? keys.reduce((acc, key) => ({ ...acc, [key]: key }), {}) : keys;
2228
2142
  return this;
2229
2143
  }
2144
+ /**
2145
+ * Unwrap response returning the first ID
2146
+ * @return {Slice<T>}
2147
+ */
2148
+ id() {
2149
+ return this.pop("rows:0:id");
2150
+ }
2230
2151
  /**
2231
2152
  * Set the request type to insert
2232
2153
  * @example
@@ -2237,6 +2158,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2237
2158
  * ]).exec().keys();
2238
2159
  * ```
2239
2160
  * @param {T | T[]} rows Rows to be inserted into the slice
2161
+ * @return {Slice<T>}
2240
2162
  */
2241
2163
  insert(rows) {
2242
2164
  this.operation = "$/slice/xinsert";
@@ -2253,9 +2175,11 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2253
2175
  * .exec().rows();
2254
2176
  * ```
2255
2177
  * @param {number} num Number of rows to return
2178
+ * @return {Slice<T>}
2256
2179
  */
2257
2180
  limit(num) {
2258
2181
  this.request.limit = num;
2182
+ return this;
2259
2183
  }
2260
2184
  /**
2261
2185
  * Add an 'OR' condition inside the where argument
@@ -2269,6 +2193,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2269
2193
  * .where(['$gt', ['$field', field4], 4)
2270
2194
  * .exec().rows();
2271
2195
  * ```
2196
+ * @return {Slice<T>}
2272
2197
  */
2273
2198
  or() {
2274
2199
  var _a;
@@ -2289,12 +2214,36 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2289
2214
  * ```
2290
2215
  * @param {string} field property name
2291
2216
  * @param {boolean} ascending Sort in ascending or descending order
2217
+ * @return {Slice<T>}
2292
2218
  */
2293
2219
  order(field, ascending = true) {
2294
2220
  if (!this.request.order) this.request.order = [];
2295
2221
  this.request.order.push([ascending ? "$asc" : "$desc", ["$field", field]]);
2296
2222
  return this;
2297
2223
  }
2224
+ /**
2225
+ * Unwrap response, returning the field
2226
+ * @param {string} field Colon seperated path: `rows:0`
2227
+ * @return {Slice<T>}
2228
+ */
2229
+ pop(field) {
2230
+ this.popField = field;
2231
+ return this;
2232
+ }
2233
+ /**
2234
+ * Unwrap response returning the first row
2235
+ * @return {Slice<T>}
2236
+ */
2237
+ row() {
2238
+ return this.pop("rows:0");
2239
+ }
2240
+ /**
2241
+ * Unwrap response returning the rows
2242
+ * @return {Slice<T>}
2243
+ */
2244
+ rows() {
2245
+ return this.pop("rows");
2246
+ }
2298
2247
  /**
2299
2248
  * Set the request type to select
2300
2249
  * @example
@@ -2303,6 +2252,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2303
2252
  * const row: T = new Slice<T>(12345).select(id).exec().row();
2304
2253
  * ```
2305
2254
  * @param {number | number[]} id ID(s) to select, leaving blank will return all rows
2255
+ * @return {Slice<T>}
2306
2256
  */
2307
2257
  select(id) {
2308
2258
  this.operation = "$/slice/report";
@@ -2324,10 +2274,10 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2324
2274
  */
2325
2275
  sync(on = true) {
2326
2276
  if (on) {
2327
- new _Slice(this.slice, this.api).select().exec().rows().then((rows) => this.cache = rows);
2277
+ new _Slice(this.slice, this.api).select().rows().exec().then((rows) => this.cache = rows);
2328
2278
  if (!this.unsubscribe) this.unsubscribe = this.api.socket.sliceEvents(this.slice, (event) => {
2329
2279
  const ids = [...event.data.new, ...event.data.changed];
2330
- new _Slice(this.slice, this.api).select(ids).exec().rows().then((rows) => this.cache = [...this.cache.filter((c) => c.id != null && !ids.includes(c.id)), ...rows]);
2280
+ new _Slice(this.slice, this.api).select(ids).rows().exec().then((rows) => this.cache = [...this.cache.filter((c) => c.id != null && !ids.includes(c.id)), ...rows]);
2331
2281
  this.cache = this.cache.filter((v) => v.id && !event.data.lost.includes(v.id));
2332
2282
  });
2333
2283
  return this.cache$;
@@ -2346,6 +2296,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2346
2296
  * ]).exec().keys();
2347
2297
  * ```
2348
2298
  * @param {T | T[]} rows Rows to be updated, each row must have an ID
2299
+ * @return {Slice<T>}
2349
2300
  */
2350
2301
  update(rows) {
2351
2302
  this.operation = "$/slice/xupdate";
@@ -2368,6 +2319,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2368
2319
  * @param {string | object} field property to compare or a map of equality comparisons
2369
2320
  * @param {string} operator Operation to compare with. Accepts JS operators (>=, ==, !=, ...) as well as datalynk styax ($gte, $eq, $is, $not, ...)
2370
2321
  * @param {any} value value to compare against
2322
+ * @return {Slice<T>}
2371
2323
  */
2372
2324
  where(field, operator, value) {
2373
2325
  if (this.request.where && this.request.where[0] != "$or") this.and();
@@ -2556,9 +2508,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2556
2508
  __publicField(this, "bundleOngoing", false);
2557
2509
  /** LocalStorage key for persisting logins */
2558
2510
  __publicField(this, "localStorageKey", "datalynk-token");
2559
- /** Pending requests */
2511
+ /** Pending requests cache */
2560
2512
  __publicField(this, "pending", {});
2561
- // Cache for ongoing requests
2562
2513
  /** Authentication helpers */
2563
2514
  __publicField(this, "auth", new Auth(this));
2564
2515
  /** File helpers */
@@ -2569,9 +2520,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2569
2520
  __publicField(this, "socket");
2570
2521
  /** Superuser helpers */
2571
2522
  __publicField(this, "superuser", new Superuser(this));
2572
- /** API endpoint */
2523
+ /** API URL endpoint */
2573
2524
  __publicField(this, "url");
2574
- // API URL endpoint for requests
2575
2525
  /** API Session token */
2576
2526
  __publicField(this, "token$", new BehaviorSubject(null));
2577
2527
  this.options = options;
@@ -2651,10 +2601,10 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2651
2601
  /**
2652
2602
  * Chain multiple requests to execute together
2653
2603
  * @param {Slice<any>} requests List of requests to chain
2654
- * @return {Promise<SlicePromise[]>} API Response
2604
+ * @return {Promise<any>} API Response
2655
2605
  */
2656
2606
  chain(...requests) {
2657
- return this.request({ "$/tools/action_chain": requests.flatMap((r) => r instanceof Slice ? r.raw : r) });
2607
+ return this.request({ "$/tools/action_chain": flattenArr(requests).map((r) => r instanceof Slice ? r.raw : r) });
2658
2608
  }
2659
2609
  /**
2660
2610
  * Organize multiple requests into a single mapped request
@@ -2663,11 +2613,27 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2663
2613
  */
2664
2614
  chainMap(request) {
2665
2615
  return this.request({ "$/tools/do": [
2666
- ...Object.entries(request).flatMap(([key, r]) => [key, r instanceof Slice ? r.raw : r]),
2616
+ ...Object.entries(request).flatMap(([key, r]) => [key, r instanceof Slice ? r == null ? void 0 : r.raw : r]),
2667
2617
  "returnAllBoilerplate",
2668
2618
  { "$_": "*" }
2669
2619
  ] }, {});
2670
2620
  }
2621
+ /**
2622
+ * Exact same as `request` method, but logs the response in the console automatically
2623
+ *
2624
+ * @param {object | string} data Datalynk request as object or string
2625
+ * @param {ApiRequestOptions} options
2626
+ * @returns {Promise<any>} Datalynk response
2627
+ */
2628
+ debug(data, options = {}) {
2629
+ return this.request(data, options).then((data2) => {
2630
+ console.log(data2);
2631
+ return data2;
2632
+ }).catch((err) => {
2633
+ console.error(err);
2634
+ return err;
2635
+ });
2636
+ }
2671
2637
  /**
2672
2638
  * Send a request to Datalynk
2673
2639
  *
@@ -2682,7 +2648,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2682
2648
  */
2683
2649
  request(data, options = {}) {
2684
2650
  data = typeof data == "string" ? { [data]: {} } : data;
2685
- if (options.force) {
2651
+ if (options.noOptimize) {
2686
2652
  return new Promise((res, rej) => {
2687
2653
  this._request(data, options).then((resp) => {
2688
2654
  (resp == null ? void 0 : resp.error) ? rej(resp) : res(resp);
@@ -2720,27 +2686,11 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2720
2686
  * ```
2721
2687
  *
2722
2688
  * @param {number} slice Slice number the object will target
2723
- * @returns {Slice<T extends SliceMeta>} Object for making requests & caching rows
2689
+ * @returns {Slice<T = any>} Object for making requests & caching rows
2724
2690
  */
2725
2691
  slice(slice) {
2726
2692
  return new Slice(slice, this);
2727
2693
  }
2728
- /**
2729
- * Exact same as `request` method, but logs the response in the console automatically
2730
- *
2731
- * @param {object | string} data Datalynk request as object or string
2732
- * @param {ApiRequestOptions} options
2733
- * @returns {Promise<any>} Datalynk response
2734
- */
2735
- test(data, options = {}) {
2736
- return this.request(data, options).then((data2) => {
2737
- console.log(data2);
2738
- return data2;
2739
- }).catch((err) => {
2740
- console.error(err);
2741
- return err;
2742
- });
2743
- }
2744
2694
  }
2745
2695
  exports2.Api = Api;
2746
2696
  exports2.Auth = Auth;
@@ -2749,7 +2699,6 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2749
2699
  exports2.Pdf = Pdf;
2750
2700
  exports2.Serializer = Serializer;
2751
2701
  exports2.Slice = Slice;
2752
- exports2.SlicePromise = SlicePromise;
2753
2702
  exports2.Socket = Socket;
2754
2703
  exports2.Superuser = Superuser;
2755
2704
  Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });