@salesforce/lds-store-nimbus 1.233.0 → 1.235.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.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { DefaultDurableSegment } from '@luvio/environments';
2
2
 
3
- const { keys, create, assign, entries } = Object;
3
+ const { keys, create, assign, entries, values } = Object;
4
4
  const { stringify, parse } = JSON;
5
5
 
6
6
  function selectColumnsFromTableWhereKeyIn(columnNames, table, keyColumnName, whereIn) {
@@ -34,6 +34,22 @@ class LdsDataTable {
34
34
  }, reject);
35
35
  });
36
36
  }
37
+ getMetadataByKeys(keys) {
38
+ const query = selectColumnsFromTableWhereKeyIn([COLUMN_NAME_KEY$2, COLUMN_NAME_METADATA$1], this.tableName, COLUMN_NAME_KEY$2, keys);
39
+ return new Promise((resolve, reject) => {
40
+ this.plugin.query(query, keys, (results) => {
41
+ resolve(results.rows.reduce((entries, row) => {
42
+ const [key, stringifiedMetadata] = row;
43
+ if (stringifiedMetadata !== undefined) {
44
+ entries[key] = {
45
+ metadata: parse(stringifiedMetadata),
46
+ };
47
+ }
48
+ return entries;
49
+ }, {}));
50
+ }, reject);
51
+ });
52
+ }
37
53
  getAll() {
38
54
  return new Promise((resolve, reject) => {
39
55
  this.plugin.query(this.getAllQuery, [], (x) => {
@@ -60,6 +76,24 @@ class LdsDataTable {
60
76
  }, []),
61
77
  };
62
78
  }
79
+ metadataToUpdateOperations(entries, segment) {
80
+ return {
81
+ type: 'update',
82
+ table: this.tableName,
83
+ keyColumn: COLUMN_NAME_KEY$2,
84
+ context: {
85
+ segment,
86
+ type: 'setMetadata',
87
+ },
88
+ columns: [COLUMN_NAME_METADATA$1],
89
+ values: keys(entries).reduce((values, key) => {
90
+ const { metadata } = entries[key];
91
+ const row = [metadata ? stringify(metadata) : null];
92
+ values[key] = row;
93
+ return values;
94
+ }, {}),
95
+ };
96
+ }
63
97
  mapToDurableEntries(sqliteResult) {
64
98
  return sqliteResult.rows.reduce((entries, row) => {
65
99
  const [key, stringifiedData, stringifiedMetadata] = row;
@@ -106,6 +140,25 @@ class LdsInternalDataTable {
106
140
  }, reject);
107
141
  });
108
142
  }
143
+ getMetadataByKeys(keys, namespace) {
144
+ if (namespace === undefined) {
145
+ throw Error('LdsInternalDataTable requires namespace');
146
+ }
147
+ const query = selectColumnsFromTableWhereKeyInNamespaced([COLUMN_NAME_KEY$1, COLUMN_NAME_METADATA], this.tableName, COLUMN_NAME_KEY$1, keys, COLUMN_NAME_NAMESPACE);
148
+ return new Promise((resolve, reject) => {
149
+ this.plugin.query(query, [namespace].concat(keys), (results) => {
150
+ resolve(results.rows.reduce((entries, row) => {
151
+ const [key, stringifiedMetadata] = row;
152
+ if (stringifiedMetadata !== undefined) {
153
+ entries[key] = {
154
+ metadata: parse(stringifiedMetadata),
155
+ };
156
+ }
157
+ return entries;
158
+ }, {}));
159
+ }, reject);
160
+ });
161
+ }
109
162
  getAll(namespace) {
110
163
  return new Promise((resolve, reject) => {
111
164
  this.plugin.query(this.getAllQuery, [namespace], (x) => {
@@ -139,6 +192,42 @@ class LdsInternalDataTable {
139
192
  }, []),
140
193
  };
141
194
  }
195
+ metadataToUpdateOperations(entries, segment) {
196
+ return {
197
+ type: 'update',
198
+ table: this.tableName,
199
+ keyColumn: COLUMN_NAME_KEY$1,
200
+ context: {
201
+ segment,
202
+ type: 'setMetadata',
203
+ },
204
+ columns: [COLUMN_NAME_METADATA],
205
+ values: keys(entries).reduce((values, key) => {
206
+ const { metadata } = entries[key];
207
+ const row = [metadata ? stringify(metadata) : null];
208
+ values[key] = row;
209
+ return values;
210
+ }, {}),
211
+ };
212
+ }
213
+ metadataToUpdateSQLQueries(entries, segment) {
214
+ return keys(entries).reduce((accu, key) => {
215
+ const { metadata } = entries[key];
216
+ if (metadata !== undefined) {
217
+ accu.push({
218
+ sql: `UPDATE ${this.tableName} SET ${COLUMN_NAME_METADATA} = ? WHERE (${COLUMN_NAME_KEY$1} IS ? AND ${COLUMN_NAME_NAMESPACE} IS ?)`,
219
+ params: [stringify(metadata), key, segment],
220
+ change: {
221
+ ids: [key],
222
+ segment,
223
+ type: 'setMetadata',
224
+ isExternalChange: false,
225
+ },
226
+ });
227
+ }
228
+ return accu;
229
+ }, []);
230
+ }
142
231
  mapToDurableEntries(sqliteResult) {
143
232
  return sqliteResult.rows.reduce((entries, row) => {
144
233
  const [key, stringifiedData, stringifiedMetadata] = row;
@@ -175,9 +264,16 @@ class NimbusSqliteStore {
175
264
  });
176
265
  });
177
266
  }
267
+ batchQuery(queries) {
268
+ const promises = queries.map((q) => this.query(q.sql, q.params));
269
+ return Promise.all(promises);
270
+ }
178
271
  async getEntries(entryIds, segment) {
179
272
  return this.getTable(segment).getByKeys(entryIds, segment);
180
273
  }
274
+ async getMetadata(entryIds, segment) {
275
+ return this.getTable(segment).getMetadataByKeys(entryIds, segment);
276
+ }
181
277
  getAllEntries(segment) {
182
278
  return this.getTable(segment).getAll(segment);
183
279
  }
@@ -186,12 +282,30 @@ class NimbusSqliteStore {
186
282
  const upsertOperation = table.entriesToUpsertOperations(entries, segment);
187
283
  return this.batchOperationAsPromise([upsertOperation]);
188
284
  }
285
+ setMetadata(entries, segment) {
286
+ const table = this.getTable(segment);
287
+ const operation = this.plugin.supportsBatchUpdates === undefined ||
288
+ this.plugin.supportsBatchUpdates() === false
289
+ ? table.entriesToUpsertOperations(entries, segment)
290
+ : table.metadataToUpdateOperations(entries, segment);
291
+ return this.batchOperationAsPromise([operation]);
292
+ }
189
293
  batchOperations(operations) {
190
294
  const sqliteOperations = operations.reduce((acc, cur) => {
191
295
  if (cur.type === 'setEntries') {
192
296
  const table = this.getTable(cur.segment);
193
297
  acc.push(table.entriesToUpsertOperations(cur.entries, cur.segment));
194
298
  }
299
+ else if (cur.type === 'setMetadata') {
300
+ const table = this.getTable(cur.segment);
301
+ if (this.plugin.supportsBatchUpdates === undefined ||
302
+ this.plugin.supportsBatchUpdates() === false) {
303
+ acc.push(table.entriesToUpsertOperations(cur.entries, cur.segment));
304
+ }
305
+ else {
306
+ acc.push(table.metadataToUpdateOperations(cur.entries, cur.segment));
307
+ }
308
+ }
195
309
  else {
196
310
  acc.push(this.idsToDeleteOperation(cur.ids, cur.segment));
197
311
  }
@@ -208,8 +322,15 @@ class NimbusSqliteStore {
208
322
  this.plugin
209
323
  .registerOnChangedListener(async (changes) => {
210
324
  const durableChanges = changes.map((c) => {
325
+ let type = c.type === 'upsert' ? 'setEntries' : 'evictEntries';
326
+ // if our context contains a type then set that as our main level type
327
+ // allows us in the future of updates to specify the segment change happening
328
+ // example being update call on metadata only or updating data
329
+ if (c.type === 'update' && c.context.type !== undefined) {
330
+ type = c.context.type;
331
+ }
211
332
  return {
212
- type: c.type === 'upsert' ? 'setEntries' : 'evictEntries',
333
+ type,
213
334
  ids: c.keys,
214
335
  isExternalChange: false,
215
336
  segment: c.context.segment,
@@ -276,6 +397,10 @@ class AbstractKeyValueDataTable {
276
397
  }, reject);
277
398
  });
278
399
  }
400
+ getMetadataByKeys(_keys) {
401
+ // eslint-disable-next-line @salesforce/lds/no-error-in-production
402
+ throw new Error(`There is no metadata in the ${this.tableName} table.`);
403
+ }
279
404
  getAll() {
280
405
  const getAllQuery = `SELECT ${this.columnNames.join(',')} FROM ${this.tableName}`;
281
406
  return new Promise((resolve, reject) => {
@@ -301,6 +426,10 @@ class AbstractKeyValueDataTable {
301
426
  }, []),
302
427
  };
303
428
  }
429
+ metadataToUpdateOperations(_entries, _segment) {
430
+ // eslint-disable-next-line @salesforce/lds/no-error-in-production
431
+ throw new Error(`There is no metadata in the ${this.tableName} table.`);
432
+ }
304
433
  mapToDurableEntries(sqliteResult) {
305
434
  return sqliteResult.rows.reduce((entries, row) => {
306
435
  const [key, stringifiedData] = row;
@@ -1,7 +1,12 @@
1
1
  import type { SqliteResult, SqliteStore, SqliteType } from '@salesforce/lds-store-sql';
2
2
  import type { SqliteStorePlugin } from '@salesforce/nimbus-plugin-lds';
3
- import type { DurableStore, DurableStoreEntries, DurableStoreOperation, OnDurableStoreChangedListener } from '@luvio/environments';
3
+ import type { DurableStore, DurableStoreChange, DurableStoreEntries, DurableStoreMetadataEntries, DurableStoreOperation, OnDurableStoreChangedListener } from '@luvio/environments';
4
4
  import type { LdsDataTableBase } from './tables';
5
+ export type SQLQuery = {
6
+ sql: string;
7
+ params: SqliteType[];
8
+ change: DurableStoreChange;
9
+ };
5
10
  export declare class NimbusSqliteStore implements SqliteStore, DurableStore {
6
11
  private plugin;
7
12
  private readonly dataTableMap;
@@ -9,9 +14,12 @@ export declare class NimbusSqliteStore implements SqliteStore, DurableStore {
9
14
  constructor(plugin: SqliteStorePlugin, additionalTableMap?: Record<string, LdsDataTableBase>);
10
15
  isEvalSupported(): boolean;
11
16
  query(sql: string, params: SqliteType[]): Promise<SqliteResult>;
17
+ batchQuery(queries: SQLQuery[]): Promise<SqliteResult[]>;
12
18
  getEntries<T>(entryIds: string[], segment: string): Promise<DurableStoreEntries<T> | undefined>;
19
+ getMetadata(entryIds: string[], segment: string): Promise<DurableStoreMetadataEntries | undefined>;
13
20
  getAllEntries<T>(segment: string): Promise<DurableStoreEntries<T> | undefined>;
14
21
  setEntries<T>(entries: DurableStoreEntries<T>, segment: string): Promise<void>;
22
+ setMetadata(entries: DurableStoreMetadataEntries, segment: string): Promise<void>;
15
23
  batchOperations<T>(operations: DurableStoreOperation<T>[]): Promise<void>;
16
24
  evictEntries(entryIds: string[], segment: string): Promise<void>;
17
25
  registerOnChangedListener(listener: OnDurableStoreChangedListener): () => Promise<void>;
@@ -1,5 +1,5 @@
1
1
  import type { SqliteOperation, SqliteStorePlugin } from '@salesforce/nimbus-plugin-lds';
2
- import type { DurableStoreEntries } from '@luvio/environments';
2
+ import type { DurableStoreEntries, DurableStoreMetadataEntries } from '@luvio/environments';
3
3
  import type { LdsDataTableBase } from '../tables/LdsDataTableBase';
4
4
  export declare abstract class AbstractKeyValueDataTable implements LdsDataTableBase {
5
5
  readonly tableName: SqliteOperation['table'];
@@ -8,7 +8,9 @@ export declare abstract class AbstractKeyValueDataTable implements LdsDataTableB
8
8
  private conflictColumnNames;
9
9
  constructor(plugin: SqliteStorePlugin, tableName: SqliteOperation['table']);
10
10
  getByKeys<T>(keys: string[]): Promise<DurableStoreEntries<T>>;
11
+ getMetadataByKeys(_keys: string[]): Promise<DurableStoreMetadataEntries | undefined>;
11
12
  getAll<T>(): Promise<DurableStoreEntries<T>>;
12
13
  entriesToUpsertOperations<T>(entries: DurableStoreEntries<T>, segment: string): SqliteOperation;
14
+ metadataToUpdateOperations(_entries: DurableStoreMetadataEntries, _segment: string): SqliteOperation;
13
15
  private mapToDurableEntries;
14
16
  }
@@ -1,5 +1,5 @@
1
1
  import type { SqliteOperation, SqliteStorePlugin } from '@salesforce/nimbus-plugin-lds';
2
- import type { DurableStoreEntries } from '@luvio/environments';
2
+ import type { DurableStoreEntries, DurableStoreMetadataEntries } from '@luvio/environments';
3
3
  import type { LdsDataTableBase } from '../tables/LdsDataTableBase';
4
4
  export declare class LdsDataTable implements LdsDataTableBase {
5
5
  readonly tableName = "lds_data";
@@ -9,7 +9,9 @@ export declare class LdsDataTable implements LdsDataTableBase {
9
9
  private getAllQuery;
10
10
  constructor(plugin: SqliteStorePlugin);
11
11
  getByKeys<T>(keys: string[]): Promise<DurableStoreEntries<T>>;
12
+ getMetadataByKeys(keys: string[]): Promise<DurableStoreMetadataEntries | undefined>;
12
13
  getAll<T>(): Promise<DurableStoreEntries<T>>;
13
14
  entriesToUpsertOperations<T>(entries: DurableStoreEntries<T>, segment: string): SqliteOperation;
15
+ metadataToUpdateOperations(entries: DurableStoreMetadataEntries, segment: string): SqliteOperation;
14
16
  private mapToDurableEntries;
15
17
  }
@@ -1,8 +1,10 @@
1
- import type { DurableStoreEntries } from '@luvio/environments';
1
+ import type { DurableStoreEntries, DurableStoreMetadataEntries } from '@luvio/environments';
2
2
  import type { SqliteOperation } from '@salesforce/nimbus-plugin-lds';
3
3
  export interface LdsDataTableBase {
4
4
  tableName: SqliteOperation['table'];
5
5
  getByKeys<T>(keys: string[], segment?: string): Promise<DurableStoreEntries<T>>;
6
+ getMetadataByKeys(keys: string[], segment?: string): Promise<DurableStoreMetadataEntries | undefined>;
6
7
  getAll<T>(segment?: string): Promise<DurableStoreEntries<T>>;
7
8
  entriesToUpsertOperations<T>(entries: DurableStoreEntries<T>, segment: string): SqliteOperation;
9
+ metadataToUpdateOperations(entries: DurableStoreMetadataEntries, segment: string): SqliteOperation;
8
10
  }
@@ -1,6 +1,7 @@
1
1
  import type { SqliteOperation, SqliteStorePlugin } from '@salesforce/nimbus-plugin-lds';
2
- import type { DurableStoreEntries } from '@luvio/environments';
2
+ import type { DurableStoreEntries, DurableStoreMetadataEntries } from '@luvio/environments';
3
3
  import type { LdsDataTableBase } from '../tables';
4
+ import type { SQLQuery } from '../NimbusSqliteStore';
4
5
  export declare class LdsInternalDataTable implements LdsDataTableBase {
5
6
  readonly tableName = "lds_internal";
6
7
  readonly plugin: SqliteStorePlugin;
@@ -9,7 +10,10 @@ export declare class LdsInternalDataTable implements LdsDataTableBase {
9
10
  private getAllQuery;
10
11
  constructor(plugin: SqliteStorePlugin);
11
12
  getByKeys<T>(keys: string[], namespace: string): Promise<DurableStoreEntries<T>>;
13
+ getMetadataByKeys(keys: string[], namespace: string): Promise<DurableStoreMetadataEntries | undefined>;
12
14
  getAll<T>(namespace: string): Promise<DurableStoreEntries<T>>;
13
15
  entriesToUpsertOperations<T>(entries: DurableStoreEntries<T>, segment: string): SqliteOperation;
16
+ metadataToUpdateOperations(entries: DurableStoreMetadataEntries, segment: string): SqliteOperation;
17
+ metadataToUpdateSQLQueries(entries: DurableStoreMetadataEntries, segment: string): SQLQuery[];
14
18
  private mapToDurableEntries;
15
19
  }
@@ -14,6 +14,11 @@ declare const keys: {
14
14
  [s: string]: T;
15
15
  } | ArrayLike<T>): [string, T][];
16
16
  (o: {}): [string, any][];
17
+ }, values: {
18
+ <T>(o: {
19
+ [s: string]: T;
20
+ } | ArrayLike<T>): T[];
21
+ (o: {}): any[];
17
22
  };
18
23
  declare const stringify: {
19
24
  (value: any, replacer?: ((this: any, key: string, value: any) => any) | undefined, space?: string | number | undefined): string;
@@ -26,4 +31,4 @@ declare const isArray: (arg: any) => arg is any[], from: {
26
31
  <T_2>(iterable: Iterable<T_2> | ArrayLike<T_2>): T_2[];
27
32
  <T_3, U_1>(iterable: Iterable<T_3> | ArrayLike<T_3>, mapfn: (v: T_3, k: number) => U_1, thisArg?: any): U_1[];
28
33
  };
29
- export { keys as ObjectKeys, create as ObjectCreate, assign as ObjectAssign, entries as ObjectEntries, push as ArrayPrototypePush, join as ArrayPrototypeJoin, slice as ArrayPrototypeSlice, isArray as ArrayIsArray, from as ArrayFrom, stringify as JSONStringify, parse as JSONParse, };
34
+ export { keys as ObjectKeys, create as ObjectCreate, assign as ObjectAssign, entries as ObjectEntries, values as ObjectValues, push as ArrayPrototypePush, join as ArrayPrototypeJoin, slice as ArrayPrototypeSlice, isArray as ArrayIsArray, from as ArrayFrom, stringify as JSONStringify, parse as JSONParse, };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforce/lds-store-nimbus",
3
- "version": "1.233.0",
3
+ "version": "1.235.0",
4
4
  "description": "A nimbus-plugin-based implementation of the Luvio DurableStore and SqliteStore.",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "types": "dist/types/index.d.ts",
@@ -26,7 +26,7 @@
26
26
  },
27
27
  "dependencies": {},
28
28
  "devDependencies": {
29
- "@luvio/environments": "0.145.3",
29
+ "@luvio/environments": "0.147.1",
30
30
  "@salesforce/nimbus-plugin-lds": "*",
31
31
  "@salesforce/lds-store-sql": "*"
32
32
  },
@@ -34,8 +34,8 @@
34
34
  {
35
35
  "path": "./dist/index.js",
36
36
  "maxSize": {
37
- "none": "12 kB",
38
- "min": "7 kB",
37
+ "none": "17 kB",
38
+ "min": "8 kB",
39
39
  "compressed": "3 kB"
40
40
  }
41
41
  }