query-core 0.5.1 → 0.6.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/src/services.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { attributes, buildToDelete, buildToInsert, buildToUpdate, exist, metadata, select, version } from "./build"
2
2
  import { Attribute, Attributes, Statement, StringMap } from "./metadata"
3
+ import { LikeType } from "./query"
4
+ import { SearchBuilder } from "./SearchBuilder"
3
5
  // import { SearchResult } from "./search"
4
6
 
5
7
  export interface Filter {
@@ -474,7 +476,7 @@ export class SqlWriter<T> {
474
476
  obj2 = this.toDB(obj)
475
477
  }
476
478
  const stmt = buildToInsert(obj2, this.table, this.attributes, this.param, this.version)
477
- if (stmt) {
479
+ if (stmt.query.length > 0) {
478
480
  return this.exec(stmt.query, stmt.params, ctx).catch((err) => {
479
481
  if (err && err.error === "duplicate") {
480
482
  return 0
@@ -492,7 +494,7 @@ export class SqlWriter<T> {
492
494
  obj2 = this.toDB(obj)
493
495
  }
494
496
  const stmt = buildToUpdate(obj2, this.table, this.attributes, this.param, this.version)
495
- if (stmt) {
497
+ if (stmt.query.length > 0) {
496
498
  return this.exec(stmt.query, stmt.params, ctx)
497
499
  } else {
498
500
  return Promise.resolve(0)
@@ -527,6 +529,156 @@ export class CRUDRepository<T, ID> extends SqlWriter<T> {
527
529
  const sql = `select * from ${this.table}`
528
530
  return this.query(sql, [], this.map)
529
531
  }
532
+ load(id: ID, ctx?: any): Promise<T | null> {
533
+ const stmt = select<ID>(id, this.table, this.primaryKeys, this.param)
534
+ if (stmt.query.length === 0) {
535
+ throw new Error("cannot build query by id")
536
+ }
537
+ const fn = this.fromDB
538
+ if (fn) {
539
+ return this.query<T>(stmt.query, stmt.params, this.map, ctx).then((res) => {
540
+ if (!res || res.length === 0) {
541
+ return null
542
+ } else {
543
+ const obj = res[0]
544
+ return fn(obj)
545
+ }
546
+ })
547
+ } else {
548
+ return this.query<T>(stmt.query, stmt.params, this.map).then((res) => (!res || res.length === 0 ? null : res[0]))
549
+ }
550
+ }
551
+ exist(id: ID, ctx?: any): Promise<boolean> {
552
+ const field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name
553
+ const stmt = exist<ID>(id, this.table, this.primaryKeys, this.param, field)
554
+ if (stmt.query.length === 0) {
555
+ throw new Error("cannot build query by id")
556
+ }
557
+ return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then((res) => (!res || res.length === 0 ? false : true))
558
+ }
559
+ delete(id: ID, ctx?: any): Promise<number> {
560
+ const stmt = buildToDelete<ID>(id, this.table, this.primaryKeys, this.param)
561
+ if (stmt.query.length > 0) {
562
+ return this.exec(stmt.query, stmt.params, ctx)
563
+ } else {
564
+ return Promise.resolve(0)
565
+ }
566
+ }
567
+ }
568
+
569
+ export class SqlSearchWriter<T, S> extends SearchBuilder<T, S> {
570
+ protected version?: string
571
+ protected exec: (sql: string, args?: any[], ctx?: any) => Promise<number>
572
+ constructor(
573
+ db: DB,
574
+ table: string,
575
+ protected attributes: Attributes,
576
+ buildQ?: (
577
+ s: S,
578
+ param: (i: number) => string,
579
+ sort?: string,
580
+ buildSort3?: (sort?: string, map?: Attributes | StringMap) => string,
581
+ attrs?: Attributes,
582
+ table?: string,
583
+ fields?: string[],
584
+ sq?: string,
585
+ strExcluding?: string,
586
+ likeType?: LikeType
587
+ ) => Statement | undefined,
588
+ protected toDB?: (v: T) => T,
589
+ fromDB?: (v: T) => T,
590
+ sort?: string,
591
+ q?: string,
592
+ excluding?: string,
593
+ buildSort?: (sort?: string, map?: Attributes | StringMap) => string,
594
+ buildParam?: (i: number) => string,
595
+ total?: string,
596
+ ) {
597
+ super(db.query, table, attributes, db.driver, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total)
598
+ this.exec = db.exec
599
+ const x = version(attributes)
600
+ if (x) {
601
+ this.version = x.name
602
+ }
603
+ this.create = this.create.bind(this)
604
+ this.update = this.update.bind(this)
605
+ this.patch = this.patch.bind(this)
606
+ }
607
+ create(obj: T, ctx?: any): Promise<number> {
608
+ let obj2 = obj
609
+ if (this.toDB) {
610
+ obj2 = this.toDB(obj)
611
+ }
612
+ const stmt = buildToInsert(obj2, this.table, this.attributes, this.param, this.version)
613
+ if (stmt.query.length > 0) {
614
+ return this.exec(stmt.query, stmt.params, ctx).catch((err) => {
615
+ if (err && err.error === "duplicate") {
616
+ return 0
617
+ } else {
618
+ throw err
619
+ }
620
+ })
621
+ } else {
622
+ return Promise.resolve(0)
623
+ }
624
+ }
625
+ update(obj: T, ctx?: any): Promise<number> {
626
+ let obj2 = obj
627
+ if (this.toDB) {
628
+ obj2 = this.toDB(obj)
629
+ }
630
+ const stmt = buildToUpdate(obj2, this.table, this.attributes, this.param, this.version)
631
+ if (stmt.query.length > 0) {
632
+ return this.exec(stmt.query, stmt.params, ctx)
633
+ } else {
634
+ return Promise.resolve(0)
635
+ }
636
+ }
637
+ patch(obj: Partial<T>, ctx?: any): Promise<number> {
638
+ return this.update(obj as any, ctx)
639
+ }
640
+ }
641
+ // tslint:disable-next-line:max-classes-per-file
642
+ export class SqlRepository<T, ID, S> extends SqlSearchWriter<T, S> {
643
+ constructor(
644
+ db: DB,
645
+ table: string,
646
+ protected attributes: Attributes,
647
+ buildQ?: (
648
+ s: S,
649
+ param: (i: number) => string,
650
+ sort?: string,
651
+ buildSort3?: (sort?: string, map?: Attributes | StringMap) => string,
652
+ attrs?: Attributes,
653
+ table?: string,
654
+ fields?: string[],
655
+ sq?: string,
656
+ strExcluding?: string,
657
+ likeType?: LikeType
658
+ ) => Statement | undefined,
659
+ protected toDB?: (v: T) => T,
660
+ fromDB?: (v: T) => T,
661
+ sort?: string,
662
+ q?: string,
663
+ excluding?: string,
664
+ buildSort?: (sort?: string, map?: Attributes | StringMap) => string,
665
+ buildParam?: (i: number) => string,
666
+ total?: string,
667
+ ) {
668
+ super(db, table, attributes, buildQ, toDB, fromDB, sort, q, excluding, buildSort, buildParam, total)
669
+ this.metadata = this.metadata.bind(this)
670
+ this.all = this.all.bind(this)
671
+ this.load = this.load.bind(this)
672
+ this.exist = this.exist.bind(this)
673
+ this.delete = this.delete.bind(this)
674
+ }
675
+ metadata(): Attributes {
676
+ return this.attributes
677
+ }
678
+ all(): Promise<T[]> {
679
+ const sql = `select * from ${this.table}`
680
+ return this.query(sql, [], this.map)
681
+ }
530
682
  load(id: ID, ctx?: any): Promise<T | null> {
531
683
  const stmt = select<ID>(id, this.table, this.primaryKeys, this.param)
532
684
  if (!stmt) {
@@ -563,3 +715,81 @@ export class CRUDRepository<T, ID> extends SqlWriter<T> {
563
715
  }
564
716
  }
565
717
  }
718
+ // tslint:disable-next-line:max-classes-per-file
719
+ export class Query<T, ID, S> extends SearchBuilder<T, S> {
720
+ primaryKeys: Attribute[]
721
+ map?: StringMap
722
+ // attributes: Attributes;
723
+ bools?: Attribute[]
724
+ constructor(
725
+ query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
726
+ table: string,
727
+ attributes: Attributes,
728
+ buildQ?: (
729
+ s: S,
730
+ param: (i: number) => string,
731
+ sort?: string,
732
+ buildSort3?: (sort?: string, map?: Attributes | StringMap) => string,
733
+ attrs?: Attributes,
734
+ table?: string,
735
+ fields?: string[],
736
+ sq?: string,
737
+ strExcluding?: string,
738
+ likeType?: LikeType
739
+ ) => Statement | undefined,
740
+ provider?: string,
741
+ fromDB?: (v: T) => T,
742
+ sort?: string,
743
+ q?: string,
744
+ excluding?: string,
745
+ buildSort?: (sort?: string, map?: Attributes | StringMap) => string,
746
+ buildParam?: (i: number) => string,
747
+ total?: string,
748
+ ) {
749
+ super(query, table, attributes, provider, buildQ, fromDB, sort, q, excluding, buildSort, buildParam, total)
750
+ const m = metadata(attributes)
751
+ this.primaryKeys = m.keys
752
+ this.map = m.map
753
+ this.bools = m.bools
754
+ if (this.metadata) {
755
+ this.metadata = this.metadata.bind(this)
756
+ }
757
+ this.all = this.all.bind(this)
758
+ this.load = this.load.bind(this)
759
+ this.exist = this.exist.bind(this)
760
+ }
761
+ metadata?(): Attributes | undefined {
762
+ return this.attrs
763
+ }
764
+ all(): Promise<T[]> {
765
+ const sql = `select * from ${this.table}`
766
+ return this.query(sql, [], this.map)
767
+ }
768
+ load(id: ID, ctx?: any): Promise<T | null> {
769
+ const stmt = select<ID>(id, this.table, this.primaryKeys, this.param)
770
+ if (!stmt) {
771
+ throw new Error("cannot build query by id")
772
+ }
773
+ const fn = this.fromDB
774
+ if (fn) {
775
+ return this.query<T>(stmt.query, stmt.params, this.map, ctx).then((res) => {
776
+ if (!res || res.length === 0) {
777
+ return null
778
+ } else {
779
+ const obj = res[0]
780
+ return fn(obj)
781
+ }
782
+ })
783
+ } else {
784
+ return this.query<T>(stmt.query, stmt.params, this.map).then((res) => (!res || res.length === 0 ? null : res[0]))
785
+ }
786
+ }
787
+ exist(id: ID, ctx?: any): Promise<boolean> {
788
+ const field = this.primaryKeys[0].column ? this.primaryKeys[0].column : this.primaryKeys[0].name
789
+ const stmt = exist<ID>(id, this.table, this.primaryKeys, this.param, field)
790
+ if (!stmt) {
791
+ throw new Error("cannot build query by id")
792
+ }
793
+ return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then((res) => (!res || res.length === 0 ? false : true))
794
+ }
795
+ }