@samet-it/be-db-common 1.0.11 → 1.1.2

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.
@@ -1,12 +1,11 @@
1
- import type { DbConnectionLike, DbExecOpt, DbQueryResultMore, DbQueryResultOne, DbConnOpt, DbMeta, DbOnAfter, DbConnProps } from "./index.types";
1
+ import { DbConnectionLike, DbExecOpt, DbQueryResultMore, DbQueryResultOne, DbConnOpt, DbMeta, DbOnAfter, DbConnProps, DbRepoLink } from "./index.types";
2
2
  import { LoggerLike } from "@samet-it/be-base-common";
3
3
  import type { DbLines } from "../line";
4
- import type { DbRepoDef } from "../repo";
5
4
  import type { Opt } from "@leyyo/common";
6
5
  /**
7
6
  * DB connection abstract class
8
7
  * */
9
- export declare abstract class DbConnection<REPO extends DbRepoDef, META extends DbMeta, OPT extends DbExecOpt> implements DbConnectionLike<REPO, META, OPT> {
8
+ export declare abstract class DbConnection<LINK extends DbRepoLink, META extends DbMeta, OPT extends DbExecOpt> implements DbConnectionLike<LINK, META, OPT> {
10
9
  /**
11
10
  * Logger
12
11
  * */
@@ -60,9 +59,9 @@ export declare abstract class DbConnection<REPO extends DbRepoDef, META extends
60
59
  /** {@inheritDoc} */
61
60
  exec<T>(fn: Promise<T>, p1?: string | Omit<OPT, 'printSql'>): Promise<T>;
62
61
  /** {@inheritDoc} */
63
- abstract more<T>(repo: REPO, sql: string | DbLines, name?: string | DbExecOpt): Promise<DbQueryResultMore<T, META>>;
62
+ abstract more<T>(link: LINK, sql: string | DbLines, name?: string | DbExecOpt): Promise<DbQueryResultMore<T, META>>;
64
63
  /** {@inheritDoc} */
65
- abstract one<T>(repo: REPO, sql: string | DbLines, name?: string | DbExecOpt): Promise<DbQueryResultOne<T, META>>;
64
+ abstract one<T>(link: LINK, sql: string | DbLines, name?: string | DbExecOpt): Promise<DbQueryResultOne<T, META>>;
66
65
  /**
67
66
  * It will be called during module based systems
68
67
  * */
@@ -1,7 +1,6 @@
1
1
  import type { DbLines } from "../line";
2
- import type { DbRepoDef } from "../repo";
3
2
  import type { DbErrorOmit } from "../error";
4
- import { Opt } from "@leyyo/common";
3
+ import type { Opt } from "@leyyo/common";
5
4
  /**
6
5
  * DB query result
7
6
  * */
@@ -129,7 +128,7 @@ export interface DbConnectionBase<OPT extends DbExecOpt = DbExecOpt> {
129
128
  /**
130
129
  * DB connection interface
131
130
  * */
132
- export interface DbConnectionLike<REPO extends DbRepoDef, META extends DbMeta, OPT extends DbExecOpt> extends DbConnectionBase {
131
+ export interface DbConnectionLike<LINK extends DbRepoLink, META extends DbMeta, OPT extends DbExecOpt> extends DbConnectionBase {
133
132
  /**
134
133
  * Props on connection
135
134
  * */
@@ -171,23 +170,23 @@ export interface DbConnectionLike<REPO extends DbRepoDef, META extends DbMeta, O
171
170
  /**
172
171
  * Execute a sql and return rows
173
172
  *
174
- * @param {DbRepoLike} repo - repository instance
173
+ * @param {DbRepoLink} link - repository link
175
174
  * @param {(string|DbLines)} sql - sql or sql lines
176
175
  * @param {(DbExecOpt|string)?} opt - option or query name
177
176
  * @return {Promise<DbQueryResultMore>} - rows
178
177
  * @async
179
178
  * */
180
- more<T>(repo: REPO, sql: string | DbLines, opt?: string | OPT): Promise<DbQueryResultMore<T, META>>;
179
+ more<T>(link: LINK, sql: string | DbLines, opt?: string | OPT): Promise<DbQueryResultMore<T, META>>;
181
180
  /**
182
181
  * Execute a sql and return a row
183
182
  *
184
- * @param {DbRepoLike} repo - repository instance
183
+ * @param {DbRepoLink} link - repository link
185
184
  * @param {(string|DbLines)} sql - sql or sql lines
186
185
  * @param {(DbExecOpt|string)?} opt - option or query name
187
186
  * @return {Promise<DbQueryResultOne>} - row
188
187
  * @async
189
188
  * */
190
- one<T>(repo: REPO, sql: string | DbLines, opt?: string | OPT): Promise<DbQueryResultOne<T, META>>;
189
+ one<T>(link: LINK, sql: string | DbLines, opt?: string | OPT): Promise<DbQueryResultOne<T, META>>;
191
190
  /**
192
191
  * It will be called during module based systems
193
192
  *
@@ -292,3 +291,7 @@ export type DbOnAfter = () => Promise<void>;
292
291
  * DB connection events
293
292
  * */
294
293
  export type DbEvent = 'connected' | 'disconnected' | 'first-connected';
294
+ /** Repository Link */
295
+ export interface DbRepoLink {
296
+ path: string;
297
+ }
@@ -1,4 +1,4 @@
1
- import type { DbCheckKeysResult, DbRepoProps, DbRepoLike, DbRepoOpt, DbRepoDef, DbCheckKeysTuple } from "./index.types";
1
+ import type { DbCheckKeysResult, DbRepoProps, DbRepoLike, DbRepoOpt, DbCheckKeysTuple } from "./index.types";
2
2
  import type { DefDims, Entity, IdDocLike, LoggerLike, Pair, Portion, UrnDocLike, UrnTuple, View } from "@samet-it/be-base-common";
3
3
  import type { DbConnectionBase, DbExecOpt } from "../connection";
4
4
  import type { KeyValue, StrKey } from "@leyyo/common";
@@ -39,8 +39,6 @@ export declare abstract class DbRepo<CONN extends DbConnectionBase, OPT extends
39
39
  /** @inheritDoc */
40
40
  get props(): Readonly<DbRepoProps<CONN, ID, ENTITY>>;
41
41
  /** @inheritDoc */
42
- get $def(): DbRepoDef;
43
- /** @inheritDoc */
44
42
  $cast<T>(): T;
45
43
  protected _keyToUrn(key: KeyValue): string;
46
44
  /** @inheritDoc */
@@ -233,10 +233,6 @@ class DbRepo {
233
233
  return this._props;
234
234
  }
235
235
  /** @inheritDoc */
236
- get $def() {
237
- return this;
238
- }
239
- /** @inheritDoc */
240
236
  $cast() {
241
237
  return this;
242
238
  }
@@ -3,7 +3,7 @@ import type { DbConnectionBase, DbExecOpt } from "../connection";
3
3
  import type { DefDims, Portion, View } from "@samet-it/be-base-common";
4
4
  import type { QueryAny, QueryRegular } from "@leyyo/query";
5
5
  import type { CacheConnectionLike, CacheChannelLike } from "@samet-it/be-cache-common";
6
- import type { Fnc, KeyValue, StrKey } from "@leyyo/common";
6
+ import type { KeyValue, StrKey } from "@leyyo/common";
7
7
  /**
8
8
  * Urn delimiter literals (options)
9
9
  * */
@@ -16,10 +16,6 @@ export interface DbRepoLike<CONN extends DbConnectionBase, OPT extends DbExecOpt
16
16
  * Props on repository
17
17
  * */
18
18
  get props(): Readonly<DbRepoProps<CONN, ID, ENTITY>>;
19
- /**
20
- * Converts current instance to flatten instance with ignoring generics
21
- * */
22
- get $def(): DbRepoDef;
23
19
  /**
24
20
  * Casts current instance to given type
25
21
  * */
@@ -391,7 +387,6 @@ export interface DbRepoLike<CONN extends DbConnectionBase, OPT extends DbExecOpt
391
387
  *
392
388
  * @param {Entity} doc - document object
393
389
  * @param {string?} dim - dimension, default: `def`
394
- * @param {boolean?} ignoreCheck - ignores to check input
395
390
  * @return {Promise<IdDocLike>} - dim object
396
391
  * @async
397
392
  * */
@@ -487,7 +482,6 @@ export interface DbRepoLike<CONN extends DbConnectionBase, OPT extends DbExecOpt
487
482
  * */
488
483
  listPortions(keys: Array<KeyValue>, ignoreCheck?: boolean): Promise<Array<PORTION>>;
489
484
  }
490
- export type DbRepoDef = DbRepoLike<DbConnectionBase, DbExecOpt, KeyValue, Entity, UrnDocLike, keyof Entity, DefDims, Pair, View, Portion>;
491
485
  /**
492
486
  * DB repository option
493
487
  * */
@@ -501,20 +495,22 @@ export interface DbRepoOpt<ID extends KeyValue, ENTITY extends Entity<ID>> {
501
495
  /**
502
496
  * Model version
503
497
  *
504
- * @default `1.0.0`
505
- * @type {string}
498
+ * @default 1
499
+ * @type {number}
506
500
  * */
507
501
  version?: number;
508
502
  /**
509
503
  * Urn delimiter
510
504
  *
511
- * @default `,` -- use comma
505
+ * @default ","
512
506
  * @type {UrnDelimiter}
513
507
  * */
514
508
  urnDelimiter?: UrnDelimiter;
515
509
  /**
516
510
  * Urn prefix, like domain:subdomain
517
511
  *
512
+ * @example domain:subdomain
513
+ *
518
514
  * @type {string}
519
515
  * */
520
516
  urnPrefix?: string;
@@ -522,7 +518,8 @@ export interface DbRepoOpt<ID extends KeyValue, ENTITY extends Entity<ID>> {
522
518
  * Id format, number or string
523
519
  * - if yes, it evaluates as integer
524
520
  * - if no, it evaluates as string, uuid etc
525
- * @default `false`
521
+ *
522
+ * @default false
526
523
  * */
527
524
  useNumericId?: boolean;
528
525
  /**
@@ -531,7 +528,7 @@ export interface DbRepoOpt<ID extends KeyValue, ENTITY extends Entity<ID>> {
531
528
  * - if no, it generates number - timestamp
532
529
  *
533
530
  * @fields `createdAt`, `updatedAt`
534
- * @default `false`
531
+ * @default false
535
532
  * */
536
533
  useIsoDate?: boolean;
537
534
  /**
@@ -539,8 +536,8 @@ export interface DbRepoOpt<ID extends KeyValue, ENTITY extends Entity<ID>> {
539
536
  * - if yes, id is same with urn
540
537
  * - if no, id is not same with urn
541
538
  *
542
- * @fields `_urn`
543
- * @default `false`
539
+ * @fields `_urn`, `id`
540
+ * @default false
544
541
  * */
545
542
  idSame?: boolean;
546
543
  /**
@@ -549,7 +546,7 @@ export interface DbRepoOpt<ID extends KeyValue, ENTITY extends Entity<ID>> {
549
546
  * - if no, it validates trash case
550
547
  *
551
548
  * @fields `_trashId`
552
- * @default `false`
549
+ * @default false
553
550
  * */
554
551
  useSoftDelete?: boolean;
555
552
  /**
@@ -558,7 +555,7 @@ export interface DbRepoOpt<ID extends KeyValue, ENTITY extends Entity<ID>> {
558
555
  * - if no, it ignores `createdAt`
559
556
  *
560
557
  * @fields `createdAt`
561
- * @default `false`
558
+ * @default false
562
559
  * */
563
560
  useCreatedAt?: boolean;
564
561
  /**
@@ -567,7 +564,7 @@ export interface DbRepoOpt<ID extends KeyValue, ENTITY extends Entity<ID>> {
567
564
  * - if no, it ignores `createdBy`
568
565
  *
569
566
  * @fields `createdBy`
570
- * @default `false`
567
+ * @default false
571
568
  * */
572
569
  useCreatedBy?: boolean;
573
570
  /**
@@ -576,7 +573,7 @@ export interface DbRepoOpt<ID extends KeyValue, ENTITY extends Entity<ID>> {
576
573
  * - if no, it builds updatedAt
577
574
  *
578
575
  * @fields `updatedAt`
579
- * @default `false`
576
+ * @default false
580
577
  * */
581
578
  useUpdatedAt?: boolean;
582
579
  /**
@@ -585,7 +582,7 @@ export interface DbRepoOpt<ID extends KeyValue, ENTITY extends Entity<ID>> {
585
582
  * - if no, it ignores `updatedBy`
586
583
  *
587
584
  * @fields `updatedBy`
588
- * @default `false`
585
+ * @default false
589
586
  * */
590
587
  useUpdatedBy?: boolean;
591
588
  /**
@@ -594,7 +591,7 @@ export interface DbRepoOpt<ID extends KeyValue, ENTITY extends Entity<ID>> {
594
591
  * - if no, it ignores `_ver`
595
592
  *
596
593
  * @fields `_ver`
597
- * @default `false`
594
+ * @default false
598
595
  * */
599
596
  useVersion?: boolean;
600
597
  /**
@@ -603,7 +600,7 @@ export interface DbRepoOpt<ID extends KeyValue, ENTITY extends Entity<ID>> {
603
600
  * - if no, it ignores `_rev`
604
601
  *
605
602
  * @fields `_rev`
606
- * @default `false`
603
+ * @default false
607
604
  * */
608
605
  useRevision?: boolean;
609
606
  /**
@@ -612,27 +609,33 @@ export interface DbRepoOpt<ID extends KeyValue, ENTITY extends Entity<ID>> {
612
609
  * - if no, it ignores `_alpha`
613
610
  *
614
611
  * @fields `_alpha`
615
- * @default `false`
612
+ *
613
+ * @param {Entity} doc
614
+ * @async
616
615
  * */
617
- alphaFn?: Fnc;
616
+ alphaFn?: DbRepoExtensionLambda<ID, ENTITY>;
618
617
  /**
619
618
  * Manages `search` case, to power search
620
619
  * - if yes, it builds `_search`
621
620
  * - if no, it ignores `_search`
622
621
  *
623
- * @fields `_alpha`
624
- * @default `false`
622
+ * @fields `_search`
623
+ *
624
+ * @param {Entity} doc
625
+ * @async
625
626
  * */
626
- searchFn?: Fnc;
627
+ searchFn?: DbRepoExtensionLambda<ID, ENTITY>;
627
628
  /**
628
629
  * Manages `irregular` case, to store irregular field which is not in model
629
630
  * - if yes, it builds `_irregular`
630
631
  * - if no, it ignores `_irregular`
631
632
  *
632
633
  * @fields `_irregular`
633
- * @default `false`
634
+ *
635
+ * @param {Entity} doc
636
+ * @async
634
637
  * */
635
- irregularFn?: Fnc;
638
+ irregularFn?: DbRepoExtensionLambda<ID, ENTITY>;
636
639
  /**
637
640
  * Field list which can not be set (update/replace)
638
641
  *
@@ -683,19 +686,62 @@ export interface DbCheckKeysResult<ID extends KeyValue> {
683
686
  urns: Array<string>;
684
687
  ordered: Array<DbCheckKeysTuple<ID>>;
685
688
  }
689
+ /**
690
+ * Response model during key is being checked
691
+ * @tuple
692
+ *
693
+ * Options
694
+ * - if primary(urn) => `[value (string), '_urn'];
695
+ * - if secondary(id) => `[value (string|number), 'id'];
696
+ * */
686
697
  export type DbCheckKeysTuple<ID extends KeyValue> = [string, '_urn'] | [ID, 'id'];
687
698
  /**
688
699
  * DB `$toUrnTuple` lambda
689
- * @function
700
+ *
701
+ * @see {@link DbRepoLike#$toUrnTuple}
702
+ * @method
703
+ *
704
+ * @param {UrnDocLike} urnRec - an object which will be a source to generate an urn
705
+ * @return {UrnTuple} - tuple
690
706
  * */
691
707
  export type DbRepoToUrnTuple<URN extends UrnDocLike = UrnDocLike> = (urnRec: URN) => UrnTuple;
692
708
  /**
693
709
  * DB `toDim` lambda
694
- * @function
710
+ *
711
+ * @see {@link DbRepoLike#$toDim}
712
+ * @method
713
+ *
714
+ * @param {Entity} doc - document
715
+ * @param {DefDims} dim - dimension
695
716
  * */
696
717
  export type DbRepoToDim<R, ID extends KeyValue, ENTITY extends Entity<ID>, DIMS extends DefDims = DefDims> = (doc: ENTITY, dim: DIMS | undefined) => Promise<R>;
718
+ /**
719
+ * Default model which contains keys
720
+ * */
697
721
  export interface DbIdLike {
722
+ /**
723
+ * Secondary key for an entity
724
+ *
725
+ * @type {KeyValue}
726
+ */
698
727
  id?: KeyValue;
699
- urn?: string;
728
+ /**
729
+ * Primary key for an entity
730
+ *
731
+ * @type {string}
732
+ */
700
733
  _urn?: string;
734
+ /**
735
+ * Alternative usage for {@link #_urn}
736
+ *
737
+ * @type {string}
738
+ */
739
+ urn?: string;
701
740
  }
741
+ /**
742
+ * Lambda to handle several extensions, ie: search, ...
743
+ *
744
+ * @param {Entity} doc - document
745
+ * @async
746
+ * */
747
+ export type DbRepoExtensionLambda<ID extends KeyValue, ENTITY extends Entity<ID>> = (doc: ENTITY) => Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@samet-it/be-db-common",
3
- "version": "1.0.11",
3
+ "version": "1.1.2",
4
4
  "description": "Backend DB Common",
5
5
  "repository": {
6
6
  "type": "git",
@@ -63,8 +63,8 @@
63
63
  }
64
64
  },
65
65
  "dependencies": {
66
+ "@leyyo/query": "^1.2.2",
66
67
  "@leyyo/common": "^1.2.1",
67
- "@leyyo/query": "^1.2.1",
68
68
  "@leyyo/type": "^1.1.1",
69
69
  "@samet-it/be-base-common": "^1.1.1",
70
70
  "@samet-it/be-cache-common": "^1.1.1"