sfobjects-basic-client 1.1.6 → 1.1.8

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.d.ts CHANGED
@@ -160,18 +160,21 @@ export interface ISfConnection {
160
160
  create: (n: string, r: any[], o?: SfDmlOptions) => PromiseLike<SfSaveResult[]>;
161
161
  delete: (n: string, ids: string[], o?: SfDmlOptions) => PromiseLike<SfSaveResult[]>;
162
162
  }
163
- export interface SfSelectActions<OI, N extends KeyOf<OI>, S extends SfRootSelect<OI, N>> {
163
+ export interface SfQueryActions<OI, N extends KeyOf<OI>, S extends SfRootSelect<OI, N>> {
164
164
  soql: () => string;
165
165
  get: (options?: SfQueryOptions) => Promise<SfQueryResult<SfRootSelectProjection<OI, N, S>>>;
166
- limit: (limit: number) => SfSelectActions<OI, N, S>;
166
+ first: (options?: SfQueryOptions) => Promise<SfRootSelectProjection<OI, N, S> | undefined>;
167
+ }
168
+ export interface SfLimitAction<OI, N extends KeyOf<OI>, S extends SfRootSelect<OI, N>> {
169
+ limit: (limit: number) => SfQueryActions<OI, N, S>;
167
170
  }
168
171
  export interface SfOrderByAction<OI, N extends KeyOf<OI>, S extends SfRootSelect<OI, N>> {
169
- orderBy: <R extends SfRootOrderBy<OI, N>>(orderBy: R) => SfSelectActions<OI, N, S>;
172
+ orderBy: <R extends SfRootOrderBy<OI, N>>(orderBy: R) => (SfQueryActions<OI, N, S> & SfLimitAction<OI, N, S>);
170
173
  }
171
174
  export interface SfWhereActions<OI, N extends KeyOf<OI>, S extends SfRootSelect<OI, N>> {
172
- where: <W extends SfRootWhere<OI, N>>(where: W) => (SfSelectActions<OI, N, S> & SfOrderByAction<OI, N, S>);
175
+ where: <W extends SfRootWhere<OI, N>>(where: W) => (SfQueryActions<OI, N, S> & SfOrderByAction<OI, N, S>);
173
176
  }
174
- export interface SfSelectAditionalActions<OI, N extends KeyOf<OI>, S extends SfRootSelect<OI, N>> {
177
+ export interface SfQueryAditionalActions<OI, N extends KeyOf<OI>, S extends SfRootSelect<OI, N>> {
175
178
  find: (id: string, options?: SfQueryOptions) => Promise<SfRootSelectProjection<OI, N, S> | undefined>;
176
179
  retrieve: (id: string, options?: SfQueryOptions) => Promise<SfRootSelectProjection<OI, N, S>>;
177
180
  selection: S[];
@@ -182,12 +185,12 @@ export interface SfObjActions<OI, N extends KeyOf<OI>> {
182
185
  where?: string | W;
183
186
  orderBy?: SfRootOrderBy<OI, N>;
184
187
  limit?: number;
185
- }) => SfSelectActions<OI, N, S>;
188
+ }) => SfQueryActions<OI, N, S>;
186
189
  delete: (ids: string[], options?: SfDmlOptions) => PromiseLike<SfSaveResult[]>;
187
190
  update: (records: SfUpdate<OI[N]>[], options?: SfDmlOptions) => PromiseLike<SfSaveResult[]>;
188
191
  create: (records: SfCreate<OI[N]>[], options?: SfDmlOptions) => PromiseLike<SfSaveResult[]>;
189
192
  upsert: <K extends MandatoryCreateProps<OI[N]>>(records: SfUpsert<OI[N], K>[], key: K, options?: SfDmlOptions) => PromiseLike<SfSaveResult[]>;
190
- select: <S extends SfRootSelect<OI, N>>(select: S[]) => (SfSelectActions<OI, N, S> & SfWhereActions<OI, N, S> & SfSelectAditionalActions<OI, N, S>);
193
+ select: <S extends SfRootSelect<OI, N>>(select: S[]) => (SfQueryActions<OI, N, S> & SfWhereActions<OI, N, S> & SfQueryAditionalActions<OI, N, S>);
191
194
  }
192
195
  export declare class SfBasicClientSaveError extends Error {
193
196
  errors: SfSaveError[];
@@ -201,7 +204,7 @@ export declare function isPlainObject(value: unknown): value is Record<string, a
201
204
  export declare function constructSoql<OI>(_cfg: SfObjCfgIndex<OI>): (objName: string, from: string, select: (string | {})[], where?: string | Record<string, any>, orderBy?: Record<string, any>, limit?: number) => string;
202
205
  export declare function getSfObject<OI>(_cfg: SfObjCfgIndex<OI>, o?: SfClientOptions): <N extends KeyOf<OI>>(from: N, _conn: ISfConnection) => SfObjActions<OI, N>;
203
206
  export declare const getSfObjects: <OI>(cfg: SfObjCfgIndex<OI>) => (conn: ISfConnection, options?: SfClientOptions) => { [N in KeyOf<OI>]: SfObjActions<OI, N>; } & {
204
- __getObject: <N_1 extends KeyOf<OI>>(from: N_1, _conn: ISfConnection) => SfObjActions<OI, N_1>;
207
+ __getObject: <N_1 extends KeyOf<OI>>(from: N_1) => SfObjActions<OI, N_1>;
205
208
  };
206
209
  export declare const sfObject: <OI, N extends KeyOf<OI>>(cfg: SfObjCfgIndex<OI>, n: N) => {
207
210
  info: SfObjCfgIndex<OI>[N];
package/dist/index.js CHANGED
@@ -252,14 +252,18 @@ function processSaveResult(sr, breakOnError) {
252
252
  function getSfObject(_cfg, o) {
253
253
  function _query(conn, from, select, where, orderBy, limit) {
254
254
  const soql = () => constructSoql(_cfg)(from, from, select, where, orderBy, limit);
255
+ const get = (...args_1) => __awaiter(this, [...args_1], void 0, function* (options = o === null || o === void 0 ? void 0 : o.query) { return yield conn.query(soql(), options); }); // to get rid of promiselike,
255
256
  return ({
256
257
  soql,
257
- get: (...args_1) => __awaiter(this, [...args_1], void 0, function* (options = o === null || o === void 0 ? void 0 : o.query) { return yield conn.query(soql(), options); }), // to get rid of promiselike,
258
- limit: (limit) => _query(conn, from, select, where, orderBy, limit)
258
+ get,
259
+ first: (...args_1) => __awaiter(this, [...args_1], void 0, function* (options = o === null || o === void 0 ? void 0 : o.query) {
260
+ const result = (yield get(options)).records;
261
+ return result.length ? result[0] : undefined;
262
+ })
259
263
  });
260
264
  }
261
265
  function _queryOne(conn, from, select, id, options = o === null || o === void 0 ? void 0 : o.query) {
262
- return _query(conn, from, select, `Id = '${id}'`).limit(1).get(options);
266
+ return _query(conn, from, select, `Id = '${id}'`, undefined, 1).first(options);
263
267
  }
264
268
  return (from, _conn) => {
265
269
  return ({
@@ -271,24 +275,21 @@ function getSfObject(_cfg, o) {
271
275
  update: (records_1, ...args_1) => __awaiter(this, [records_1, ...args_1], void 0, function* (records, options = o === null || o === void 0 ? void 0 : o.dml) { return processSaveResult(yield _conn.update(from, records, options), o === null || o === void 0 ? void 0 : o.breakOnError); }),
272
276
  create: (records_1, ...args_1) => __awaiter(this, [records_1, ...args_1], void 0, function* (records, options = o === null || o === void 0 ? void 0 : o.dml) { return processSaveResult(yield _conn.create(from, records, options), o === null || o === void 0 ? void 0 : o.breakOnError); }),
273
277
  upsert: (records_1, key_1, ...args_1) => __awaiter(this, [records_1, key_1, ...args_1], void 0, function* (records, key, options = o === null || o === void 0 ? void 0 : o.dml) { return processSaveResult(yield _conn.upsert(from, records, key, options), o === null || o === void 0 ? void 0 : o.breakOnError); }),
274
- select: (select) => (Object.assign(Object.assign({}, _query(_conn, from, select)), { orderBy: (orderBy) => _query(_conn, from, select, undefined, orderBy), where: (where) => (Object.assign(Object.assign({}, _query(_conn, from, select, where)), { orderBy: (orderBy) => _query(_conn, from, select, where, orderBy) })), find: (id_1, ...args_1) => __awaiter(this, [id_1, ...args_1], void 0, function* (id, options = o === null || o === void 0 ? void 0 : o.query) {
275
- const result = yield _queryOne(_conn, from, select, id, options);
276
- if (result.records.length) {
277
- return result.records[0];
278
- }
278
+ select: (select) => (Object.assign(Object.assign({}, _query(_conn, from, select)), { orderBy: (orderBy) => (Object.assign(Object.assign({}, _query(_conn, from, select, undefined, orderBy)), { limit: (limit) => _query(_conn, from, select, undefined, orderBy, limit) })), limit: (limit) => _query(_conn, from, select, undefined, undefined, limit), where: (where) => (Object.assign(Object.assign({}, _query(_conn, from, select, where)), { orderBy: (orderBy) => (Object.assign(Object.assign({}, _query(_conn, from, select, where, orderBy)), { limit: (limit) => _query(_conn, from, select, where, orderBy, limit) })), limit: (limit) => _query(_conn, from, select, where, undefined, limit) })), find: (id_1, ...args_1) => __awaiter(this, [id_1, ...args_1], void 0, function* (id, options = o === null || o === void 0 ? void 0 : o.query) {
279
+ return _queryOne(_conn, from, select, id, options);
279
280
  }), retrieve: (id_1, ...args_1) => __awaiter(this, [id_1, ...args_1], void 0, function* (id, options = o === null || o === void 0 ? void 0 : o.query) {
280
281
  const result = yield _queryOne(_conn, from, select, id, options);
281
- if (!result.records.length) {
282
+ if (!result) {
282
283
  throw new SfBasicClientReadError(`Record with id '${id}' is not found in '${from}'.`);
283
284
  }
284
- return result.records[0];
285
+ return result;
285
286
  }), selection: select }))
286
287
  });
287
288
  };
288
289
  }
289
290
  const getSfObjects = (cfg) => (conn, options) => {
290
- const __getObject = getSfObject(cfg, options);
291
- return Object.assign(Object.assign({}, Object.keys(cfg).reduce((p, n) => (Object.assign(Object.assign({}, p), { [n]: __getObject(n, conn) })), {})), { __getObject });
291
+ const __getObject = (from) => getSfObject(cfg, options)(from, conn);
292
+ return Object.assign(Object.assign({}, Object.keys(cfg).reduce((p, n) => (Object.assign(Object.assign({}, p), { [n]: __getObject(n) })), {})), { __getObject });
292
293
  };
293
294
  exports.getSfObjects = getSfObjects;
294
295
  const sfObject = (cfg, n) => ({ info: cfg[n], select: (s) => s });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sfobjects-basic-client",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "description": "Basic Salesforce Client to use together with sfobjects-to-typescript lib",
5
5
  "license": "ISC",
6
6
  "author": "Murad Aliyev",
package/src/index.ts CHANGED
@@ -258,34 +258,38 @@ export interface ISfConnection {
258
258
  delete: (n: string, ids: string[], o?: SfDmlOptions) => PromiseLike<SfSaveResult[]>
259
259
  }
260
260
 
261
- export interface SfSelectActions<OI, N extends KeyOf<OI>, S extends SfRootSelect<OI, N>> {
261
+ export interface SfQueryActions<OI, N extends KeyOf<OI>, S extends SfRootSelect<OI, N>> {
262
262
  soql: () => string;
263
263
  get: (options?: SfQueryOptions) => Promise<SfQueryResult<SfRootSelectProjection<OI, N, S>>>;
264
- limit: (limit: number) => SfSelectActions<OI, N, S>;
264
+ first: (options?: SfQueryOptions) => Promise<SfRootSelectProjection<OI, N, S> | undefined>;
265
+ //limit: (limit: number) => SfSelectActions<OI, N, S>;
266
+ }
267
+
268
+ export interface SfLimitAction<OI, N extends KeyOf<OI>, S extends SfRootSelect<OI, N>> {
269
+ limit: (limit: number) => SfQueryActions<OI, N, S>;
265
270
  }
266
271
 
267
272
  export interface SfOrderByAction<OI, N extends KeyOf<OI>, S extends SfRootSelect<OI, N>> {
268
- orderBy: <R extends SfRootOrderBy<OI, N>>(orderBy: R) => SfSelectActions<OI, N, S>;
273
+ orderBy: <R extends SfRootOrderBy<OI, N>>(orderBy: R) => (SfQueryActions<OI, N, S> & SfLimitAction<OI, N, S>);
269
274
  }
270
275
 
271
276
  export interface SfWhereActions<OI, N extends KeyOf<OI>, S extends SfRootSelect<OI, N>> {
272
- where: <W extends SfRootWhere<OI, N>>(where: W) => (SfSelectActions<OI, N, S> & SfOrderByAction<OI, N, S>);
277
+ where: <W extends SfRootWhere<OI, N>>(where: W) => (SfQueryActions<OI, N, S> & SfOrderByAction<OI, N, S>);
273
278
  }
274
279
 
275
- export interface SfSelectAditionalActions<OI, N extends KeyOf<OI>, S extends SfRootSelect<OI, N>> {
280
+ export interface SfQueryAditionalActions<OI, N extends KeyOf<OI>, S extends SfRootSelect<OI, N>> {
276
281
  find: (id: string, options?: SfQueryOptions) => Promise<SfRootSelectProjection<OI, N, S> | undefined>;
277
282
  retrieve: (id: string, options?: SfQueryOptions) => Promise<SfRootSelectProjection<OI, N, S>>;
278
283
  selection: S[];
279
- //S extends SfRootSelect<OI, N>
280
284
  }
281
285
 
282
286
  export interface SfObjActions<OI, N extends KeyOf<OI>> {
283
- query: <S extends SfRootSelect<OI, N>, W extends SfRootWhere<OI, N>>(q: { select: S[], where?: string | W, orderBy?: SfRootOrderBy<OI, N>, limit?: number }) => SfSelectActions<OI, N, S>;
287
+ query: <S extends SfRootSelect<OI, N>, W extends SfRootWhere<OI, N>>(q: { select: S[], where?: string | W, orderBy?: SfRootOrderBy<OI, N>, limit?: number }) => SfQueryActions<OI, N, S>;
284
288
  delete: (ids: string[], options?: SfDmlOptions) => PromiseLike<SfSaveResult[]>;
285
289
  update: (records: SfUpdate<OI[N]>[], options?: SfDmlOptions) => PromiseLike<SfSaveResult[]>;
286
290
  create: (records: SfCreate<OI[N]>[], options?: SfDmlOptions) => PromiseLike<SfSaveResult[]>;
287
291
  upsert: <K extends MandatoryCreateProps<OI[N]>>(records: SfUpsert<OI[N], K>[], key: K, options?: SfDmlOptions) => PromiseLike<SfSaveResult[]>;
288
- select: <S extends SfRootSelect<OI, N>>(select: S[]) => (SfSelectActions<OI, N, S> & SfWhereActions<OI, N, S> & SfSelectAditionalActions<OI, N, S>);
292
+ select: <S extends SfRootSelect<OI, N>>(select: S[]) => (SfQueryActions<OI, N, S> & SfWhereActions<OI, N, S> & SfQueryAditionalActions<OI, N, S>);
289
293
  }
290
294
 
291
295
  export class SfBasicClientSaveError extends Error {
@@ -564,14 +568,19 @@ export function getSfObject<OI>(_cfg: SfObjCfgIndex<OI>, o?: SfClientOptions) {
564
568
  where?: string | W,
565
569
  orderBy?: R,
566
570
  limit?: number
567
- ): SfSelectActions<OI, N, S> {
571
+ ): SfQueryActions<OI, N, S> {
568
572
 
569
573
  const soql = () => constructSoql(_cfg)(from, from, select, where, orderBy, limit);
570
574
 
575
+ const get = async (options: SfQueryOptions | undefined = o?.query) => await conn.query<SfRootSelectProjection<OI, N, S>>(soql(), options); // to get rid of promiselike,
576
+
571
577
  return ({
572
578
  soql,
573
- get: async (options: SfQueryOptions | undefined = o?.query) => await conn.query<SfRootSelectProjection<OI, N, S>>(soql(), options), // to get rid of promiselike,
574
- limit: (limit: number) => _query(conn, from, select, where, orderBy, limit)
579
+ get,
580
+ first: async (options: SfQueryOptions | undefined = o?.query) => {
581
+ const result = (await get(options)).records;
582
+ return result.length ? result[0] : undefined;
583
+ }
575
584
  });
576
585
  }
577
586
 
@@ -582,7 +591,7 @@ export function getSfObject<OI>(_cfg: SfObjCfgIndex<OI>, o?: SfClientOptions) {
582
591
  id: string,
583
592
  options: SfQueryOptions | undefined = o?.query
584
593
  ) {
585
- return _query(conn, from, select, `Id = '${id}'`).limit(1).get(options);
594
+ return _query(conn, from, select, `Id = '${id}'`, undefined, 1).first(options);
586
595
  }
587
596
 
588
597
  return <N extends KeyOf<OI>>(from: N, _conn: ISfConnection): SfObjActions<OI, N> => {
@@ -606,34 +615,40 @@ export function getSfObject<OI>(_cfg: SfObjCfgIndex<OI>, o?: SfClientOptions) {
606
615
 
607
616
  ..._query(_conn, from, select),
608
617
 
609
- orderBy: <R extends SfRootOrderBy<OI, N>>(orderBy: R) => _query(_conn, from, select, undefined, orderBy),
618
+ orderBy: <R extends SfRootOrderBy<OI, N>>(orderBy: R) => ({
619
+ ..._query(_conn, from, select, undefined, orderBy),
620
+ limit: (limit: number) => _query(_conn, from, select, undefined, orderBy, limit)
621
+ }),
622
+
623
+ limit: (limit: number) => _query(_conn, from, select, undefined, undefined, limit),
610
624
 
611
625
  where: <W extends SfRootWhere<OI, N>>(where: string | W) => ({
612
626
 
613
627
  ..._query(_conn, from, select, where),
614
628
 
615
- orderBy: <R extends SfRootOrderBy<OI, N>>(orderBy: R) => _query(_conn, from, select, where, orderBy),
629
+ orderBy: <R extends SfRootOrderBy<OI, N>>(orderBy: R) => ({
630
+ ..._query(_conn, from, select, where, orderBy),
631
+ limit: (limit: number) => _query(_conn, from, select, where, orderBy, limit)
632
+ }),
633
+
634
+ limit: (limit: number) => _query(_conn, from, select, where, undefined, limit)
616
635
 
617
636
  }),
618
637
 
619
638
  find: async (id: string, options: SfQueryOptions | undefined = o?.query) => {
620
639
 
621
- const result = await _queryOne(_conn, from, select, id, options);
622
-
623
- if (result.records.length) {
624
- return result.records[0];
625
- }
640
+ return _queryOne(_conn, from, select, id, options);
626
641
  },
627
642
 
628
643
  retrieve: async (id: string, options: SfQueryOptions | undefined = o?.query) => {
629
644
 
630
645
  const result = await _queryOne(_conn, from, select, id, options);
631
646
 
632
- if (!result.records.length) {
647
+ if (!result) {
633
648
  throw new SfBasicClientReadError(`Record with id '${id}' is not found in '${from}'.`);
634
649
  }
635
650
 
636
- return result.records[0];
651
+ return result;
637
652
  },
638
653
 
639
654
  selection: select
@@ -645,8 +660,13 @@ export function getSfObject<OI>(_cfg: SfObjCfgIndex<OI>, o?: SfClientOptions) {
645
660
 
646
661
 
647
662
  export const getSfObjects = <OI>(cfg: SfObjCfgIndex<OI>) => (conn: ISfConnection, options?: SfClientOptions) => {
648
- const __getObject = getSfObject<OI>(cfg, options);
649
- return { ...(Object.keys(cfg) as KeyOf<OI>[]).reduce((p, n) => ({ ...p, [n]: __getObject(n, conn) }), {} as { [N in KeyOf<OI>]: SfObjActions<OI, N> }), __getObject };
663
+
664
+ const __getObject = <N extends KeyOf<OI>>(from: N) => getSfObject<OI>(cfg, options)(from, conn);
665
+
666
+ return {
667
+ ...(Object.keys(cfg) as KeyOf<OI>[]).reduce((p, n) => ({ ...p, [n]: __getObject(n) }), {} as { [N in KeyOf<OI>]: SfObjActions<OI, N> }),
668
+ __getObject
669
+ };
650
670
  }
651
671
 
652
672
  export const sfObject = <OI, N extends KeyOf<OI>>(cfg: SfObjCfgIndex<OI>, n: N) => ({ info: cfg[n], select: <S extends SfRootSelect<OI, N>>(s: S[]) => s });