@dreamtree-org/korm-js 1.0.40 → 1.0.42
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/ControllerWrapper.js +1 -1
- package/README.md +204 -0
- package/clients/mysql/QueryBuilder.js +1 -1
- package/clients/mysql/QueryService.js +1 -1
- package/package.json +1 -1
package/ControllerWrapper.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const mysqlWrapper=require("./clients/mysql"),sqliteWrapper=require("./clients/sqlite"),pgWrapper=require("./clients/pg"),BaseHelperUtility=require("./BaseHelperUtility"),InstanceMapper={mysql2:mysqlWrapper,sqlite:sqliteWrapper,pg:pgWrapper},dbClientMapper={mysql2:"mysql2",mysql:"mysql2",pg:"pg",postgresql:"pg",sqlite:"sqlite",sqlite3:"sqlite"};class ControllerWrapper{static db=null;static dbClient=null;static dbClientClass=null;static schema=null;static resolverPath=null;static dbInstance=null;requestInstance=null;constructor(){this.requestInstance={}}static initializeKORM({db:t,dbClient:e,schema:s,resolverPath:a=null}){this.db=t,this.dbClient=e,this.schema=s,this.resolverPath=a;const n=dbClientMapper[e];if(!n)throw new Error(`Database client ${e} not found`);const r=InstanceMapper[n];if(!r)throw new Error(`Database client ${e} not found`);return this.dbClientClass=r,this}static setSchema(t){this.schema=t;const e=this.dbClientClass;if(!e)throw new Error(`Database client ${dbClient} not found`);return this.dbInstance=new e(this),this}static async processRequest(t,e=null,s=null){return await this.dbInstance.processRequest(t,e,s)}static async syncDatabase(){return await this.dbInstance.syncDatabase()}static async generateSchema(){return await this.dbInstance.generateSchema()}static loadModelClass(t){return this.dbInstance.hookService.loadModelClass(t)}static getModelInstance(t){return this.dbInstance.hookService.getModelInstance(t)}}module.exports=ControllerWrapper;
|
|
1
|
+
const mysqlWrapper=require("./clients/mysql"),sqliteWrapper=require("./clients/sqlite"),pgWrapper=require("./clients/pg"),BaseHelperUtility=require("./BaseHelperUtility"),InstanceMapper={mysql2:mysqlWrapper,sqlite:sqliteWrapper,pg:pgWrapper},dbClientMapper={mysql2:"mysql2",mysql:"mysql2",pg:"pg",postgresql:"pg",sqlite:"sqlite",sqlite3:"sqlite"};class ControllerWrapper{static db=null;static dbClient=null;static dbClientClass=null;static schema=null;static resolverPath=null;static dbInstance=null;requestInstance=null;constructor(){this.requestInstance={}}static initializeKORM({db:t,dbClient:e,schema:s,resolverPath:a=null}){this.db=t,this.dbClient=e,this.schema=s,this.resolverPath=a;const n=dbClientMapper[e];if(!n)throw new Error(`Database client ${e} not found`);const r=InstanceMapper[n];if(!r)throw new Error(`Database client ${e} not found`);return this.dbClientClass=r,this.dbInstance=new r(this),this}static setSchema(t){this.schema=t;const e=this.dbClientClass;if(!e)throw new Error(`Database client ${dbClient} not found`);return this.dbInstance=new e(this),this}static async processRequest(t,e=null,s=null){return await this.dbInstance.processRequest(t,e,s)}static async syncDatabase(){return await this.dbInstance.syncDatabase()}static async generateSchema(){return await this.dbInstance.generateSchema()}static loadModelClass(t){return this.dbInstance.hookService.loadModelClass(t)}static getModelInstance(t){return this.dbInstance.hookService.getModelInstance(t)}}module.exports=ControllerWrapper;
|
package/README.md
CHANGED
|
@@ -500,6 +500,210 @@ POST /api/Users/crud
|
|
|
500
500
|
}
|
|
501
501
|
```
|
|
502
502
|
|
|
503
|
+
## Relational Support with hasRelations
|
|
504
|
+
|
|
505
|
+
### Understanding hasRelations Structure
|
|
506
|
+
|
|
507
|
+
The `hasRelations` object in your schema defines relationships between models. Based on your actual schema file (`../borebell.in/schema/sync.json`), relationships use the following structure:
|
|
508
|
+
|
|
509
|
+
#### One-to-One or Belongs-To Relationship (`type: "one"`)
|
|
510
|
+
|
|
511
|
+
```json
|
|
512
|
+
{
|
|
513
|
+
"UserDetail": {
|
|
514
|
+
"hasRelations": {
|
|
515
|
+
"User": {
|
|
516
|
+
"type": "one",
|
|
517
|
+
"table": "users",
|
|
518
|
+
"localKey": "user_id",
|
|
519
|
+
"foreignKey": "id"
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
#### One-to-Many Relationship (`type: "many"`)
|
|
527
|
+
|
|
528
|
+
```json
|
|
529
|
+
{
|
|
530
|
+
"User": {
|
|
531
|
+
"hasRelations": {
|
|
532
|
+
"Post": {
|
|
533
|
+
"type": "many",
|
|
534
|
+
"table": "posts",
|
|
535
|
+
"localKey": "id",
|
|
536
|
+
"foreignKey": "user_id"
|
|
537
|
+
},
|
|
538
|
+
"Comment": {
|
|
539
|
+
"type": "many",
|
|
540
|
+
"table": "comments",
|
|
541
|
+
"localKey": "id",
|
|
542
|
+
"foreignKey": "user_id"
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
```
|
|
548
|
+
|
|
549
|
+
#### Many-to-Many Relationship (using `through`)
|
|
550
|
+
|
|
551
|
+
For many-to-many relationships, use the `through` key to specify the join/pivot table:
|
|
552
|
+
|
|
553
|
+
```json
|
|
554
|
+
{
|
|
555
|
+
"User": {
|
|
556
|
+
"hasRelations": {
|
|
557
|
+
"Role": {
|
|
558
|
+
"type": "many",
|
|
559
|
+
"table": "roles",
|
|
560
|
+
"localKey": "id",
|
|
561
|
+
"foreignKey": "id",
|
|
562
|
+
"through": "user_roles",
|
|
563
|
+
"throughLocalKey": "user_id",
|
|
564
|
+
"throughForeignKey": "role_id"
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
```
|
|
570
|
+
|
|
571
|
+
**Many-to-Many Structure:**
|
|
572
|
+
- `type`: `"many"` (always use "many" for many-to-many)
|
|
573
|
+
- `table`: The related table name (e.g., `"roles"`)
|
|
574
|
+
- `localKey`: The primary key in the current model (e.g., `"id"`)
|
|
575
|
+
- `foreignKey`: The primary key in the related table (e.g., `"id"`)
|
|
576
|
+
- `through`: **Required** - The join/pivot table name (e.g., `"user_roles"`)
|
|
577
|
+
- `throughLocalKey`: The foreign key in the join table pointing to the current model (e.g., `"user_id"`)
|
|
578
|
+
- `throughForeignKey`: The foreign key in the join table pointing to the related model (e.g., `"role_id"`)
|
|
579
|
+
|
|
580
|
+
### Using Relationships in Queries
|
|
581
|
+
|
|
582
|
+
#### Eager Loading with `with`
|
|
583
|
+
|
|
584
|
+
```javascript
|
|
585
|
+
// Load User with related Posts and Comments
|
|
586
|
+
POST /api/User/crud
|
|
587
|
+
{
|
|
588
|
+
"action": "list",
|
|
589
|
+
"where": { "id": 1 },
|
|
590
|
+
"with": ["Post", "Comment"]
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
// Nested relationships
|
|
594
|
+
POST /api/User/crud
|
|
595
|
+
{
|
|
596
|
+
"action": "list",
|
|
597
|
+
"where": { "id": 1 },
|
|
598
|
+
"with": ["Post.Comment", "Post.Like"]
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
// Multiple relationships
|
|
602
|
+
POST /api/User/crud
|
|
603
|
+
{
|
|
604
|
+
"action": "list",
|
|
605
|
+
"where": { "id": 1 },
|
|
606
|
+
"with": ["UserDetail", "Post", "Comment", "Like"]
|
|
607
|
+
}
|
|
608
|
+
```
|
|
609
|
+
|
|
610
|
+
#### Filtering Related Data with Nested Where
|
|
611
|
+
|
|
612
|
+
```javascript
|
|
613
|
+
// Get users who have posts with specific hashtag
|
|
614
|
+
POST /api/User/crud
|
|
615
|
+
{
|
|
616
|
+
"action": "list",
|
|
617
|
+
"where": {
|
|
618
|
+
"Post.hashtag": "#OrganicFarming"
|
|
619
|
+
},
|
|
620
|
+
"with": ["Post"]
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
// Get posts with comments from specific user
|
|
624
|
+
POST /api/Post/crud
|
|
625
|
+
{
|
|
626
|
+
"action": "list",
|
|
627
|
+
"where": {
|
|
628
|
+
"Comment.user_id": 1
|
|
629
|
+
},
|
|
630
|
+
"with": ["Comment"]
|
|
631
|
+
}
|
|
632
|
+
```
|
|
633
|
+
|
|
634
|
+
### Example: Complete Relationship Structure
|
|
635
|
+
|
|
636
|
+
Based on your actual schema (`../borebell.in/schema/sync.json`):
|
|
637
|
+
|
|
638
|
+
```json
|
|
639
|
+
{
|
|
640
|
+
"User": {
|
|
641
|
+
"table": "users",
|
|
642
|
+
"hasRelations": {
|
|
643
|
+
"UserDetail": {
|
|
644
|
+
"type": "one",
|
|
645
|
+
"table": "user_details",
|
|
646
|
+
"localKey": "id",
|
|
647
|
+
"foreignKey": "user_id"
|
|
648
|
+
},
|
|
649
|
+
"Post": {
|
|
650
|
+
"type": "many",
|
|
651
|
+
"table": "posts",
|
|
652
|
+
"localKey": "id",
|
|
653
|
+
"foreignKey": "user_id"
|
|
654
|
+
},
|
|
655
|
+
"Comment": {
|
|
656
|
+
"type": "many",
|
|
657
|
+
"table": "comments",
|
|
658
|
+
"localKey": "id",
|
|
659
|
+
"foreignKey": "user_id"
|
|
660
|
+
},
|
|
661
|
+
"Like": {
|
|
662
|
+
"type": "many",
|
|
663
|
+
"table": "likes",
|
|
664
|
+
"localKey": "id",
|
|
665
|
+
"foreignKey": "user_id"
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
},
|
|
669
|
+
"Post": {
|
|
670
|
+
"table": "posts",
|
|
671
|
+
"hasRelations": {
|
|
672
|
+
"User": {
|
|
673
|
+
"type": "one",
|
|
674
|
+
"table": "users",
|
|
675
|
+
"localKey": "user_id",
|
|
676
|
+
"foreignKey": "id"
|
|
677
|
+
},
|
|
678
|
+
"Comment": {
|
|
679
|
+
"type": "many",
|
|
680
|
+
"table": "comments",
|
|
681
|
+
"localKey": "id",
|
|
682
|
+
"foreignKey": "post_id"
|
|
683
|
+
},
|
|
684
|
+
"Like": {
|
|
685
|
+
"type": "many",
|
|
686
|
+
"table": "likes",
|
|
687
|
+
"localKey": "id",
|
|
688
|
+
"foreignKey": "post_id"
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
```
|
|
694
|
+
|
|
695
|
+
### Important Notes
|
|
696
|
+
|
|
697
|
+
> **Always refer to your actual schema file** (`../borebell.in/schema/sync.json`) for the exact relationship definitions in your project. The structure shown above matches the format used in your schema.
|
|
698
|
+
|
|
699
|
+
- `type: "one"` = One-to-One or Belongs-To relationship
|
|
700
|
+
- `type: "many"` = One-to-Many relationship
|
|
701
|
+
- `through` = Required for Many-to-Many relationships (specifies the join table)
|
|
702
|
+
- `localKey` = The key in the current model
|
|
703
|
+
- `foreignKey` = The key in the related table
|
|
704
|
+
- `throughLocalKey` = The key in the join table pointing to current model (for many-to-many)
|
|
705
|
+
- `throughForeignKey` = The key in the join table pointing to related model (for many-to-many)
|
|
706
|
+
|
|
503
707
|
## Soft Delete Support
|
|
504
708
|
|
|
505
709
|
### Enabling Soft Delete
|
|
@@ -1 +1 @@
|
|
|
1
|
-
const HelperUtility=require("./HelperUtility");class QueryBuilder{constructor(e,t,r=null){this.db=e,this.utils=t,this.controllerWrapper=r,this.helperUtility=new HelperUtility}getQueryBuilder(e,t=null){let r=this.db(e.table);return t&&(r=t),r._getMyModel=()=>e,r}parseValue(e){return this.helperUtility.parseValue(e)}parseWhereValue(e){return this.helperUtility.parseWhereValue(e)}_applyWhereCondition(e,t,r,i){switch(r){case"between":e.whereBetween(t,i);break;case"notBetween":e.whereNotBetween(t,i);break;case"in":e.whereIn(t,i);break;case"notIn":e.whereNotIn(t,i);break;case"like":e.
|
|
1
|
+
const HelperUtility=require("./HelperUtility");class QueryBuilder{constructor(e,t,r=null){this.db=e,this.utils=t,this.controllerWrapper=r,this.helperUtility=new HelperUtility}getQueryBuilder(e,t=null){let r=this.db(e.table);return t&&(r=t),r._getMyModel=()=>e,r}parseValue(e){return this.helperUtility.parseValue(e)}parseWhereValue(e){return this.helperUtility.parseWhereValue(e)}_applyWhereCondition(e,t,r,i){switch(r){case"between":e.whereBetween(t,i);break;case"notBetween":e.whereNotBetween(t,i);break;case"in":e.whereIn(t,i);break;case"notIn":e.whereNotIn(t,i);break;case"like":e.whereRaw(`\`${t}\` LIKE ?`,[i]);break;default:e.where(t,r,i)}}buildWithTree(e,t=null){return this.helperUtility.dotWalkTree(e,{resolver:({current:e,part:r,source:i})=>t?t({current:e,part:r,source:i}):{}})}async fetchRelatedRows(e,t){if(e.through){let r=await this.db(e.through).whereIn(e.throughLocalKey,t);return await this.db(e.table).whereIn(e.foreignKey,r.map(t=>t[e.throughForeignKey]))}return this.db(e.table).whereIn(e.foreignKey,t)}async fetchAndAttachRelated(e){const{parentRows:t,relName:r,model:i,withTree:l,relation:o}=e;let a=t.map(e=>e[o.localKey]),s=[];s=await this.fetchRelatedRows(o,a);let n=new Map;for(const e of s){let t=e[o.foreignKey];n.has(t)||n.set(t,[]),n.get(t).push(e)}for(const e of t){let t=e[o.localKey];"one"===o?.type&&1==n.get(t)?.length?e[r]=n.get(t)[0]:e[r]=n.get(t)||[]}let h=Object.keys(l);for(const e of h){let i=l[e],o=t.filter(e=>e[r].length>0).map(e=>e[r]).reduce((e,t)=>e.concat(t),[]),a=this.utils.getModel(this.controllerWrapper,r);await this.fetchAndAttachRelated({parentRows:o,relName:e,model:a,withTree:i,relation:a.hasRelations[e]})}return t}filterWhere(e,t=""){return t?Object.keys(e).filter(e=>e.includes(t)).reduce((r,i)=>(r[i.replace(t,"")]=e[i],r),{}):Object.keys(e).filter(e=>!e.includes(".")).reduce((t,r)=>(t[r]=e[r],t),{})}async getQuery(e,t){try{const{where:r={},with:i,withWhere:l,select:o,orderBy:a={column:"id",direction:"asc"},limit:s=10,offset:n=0,page:h,groupBy:c,having:p,distinct:y,join:u,leftJoin:f,rightJoin:d,innerJoin:g,count:w=!1}=t;let b=this.getQueryBuilder(e);o&&(Array.isArray(o)||"string"==typeof o)?b.select(o):b.select("*"),y&&(Array.isArray(y)||"string"==typeof y?b.distinct(y):b.distinct()),u&&this._applyJoins(b,u,"join"),f&&this._applyJoins(b,f,"leftJoin"),d&&this._applyJoins(b,d,"rightJoin"),g&&this._applyJoins(b,g,"innerJoin"),this._applyWhereClause(b,r,i),c&&(Array.isArray(c),b.groupBy(c)),p&&this._applyHavingClause(b,p),a&&this._applyOrderBy(b,a);let W=!1,_=s,m=n,j=1,A=0;h&&s>0&&(j=Math.max(1,parseInt(h)),m=(j-1)*s),s>0&&(b.limit(_),m>0&&b.offset(m));const k=await b;if(i&&i.length>0){const t=this.buildWithTree(i);for(const r of Object.keys(t))await this.fetchAndAttachRelated({parentRows:k,relName:r,model:e,withTree:t[r],relation:e.hasRelations[r]})}let B=null;if(s>0)try{let t=this.getQueryBuilder(e);r&&Object.keys(r).length>0&&this._applyWhereClause(t,r),u&&this._applyJoins(t,u,"join"),f&&this._applyJoins(t,f,"leftJoin"),d&&this._applyJoins(t,d,"rightJoin"),g&&this._applyJoins(t,g,"innerJoin");B=(await t.count("* as cnt").first()).cnt}catch(e){console.warn("Failed to get total count:",e.message),B=k.length}s>0&&null!==B&&(A=Math.ceil(B/s),W=j<A);return{data:k,totalCount:B,...s>0?{pagination:{page:j,limit:_,offset:m,totalPages:A,hasNext:W,hasPrev:j>1,nextPage:W?j+1:null,prevPage:j>1?j-1:null}}:{}}}catch(e){throw console.error("QueryService.getQuery error:",e),new Error(`Failed to execute query: ${e.message}`)}}_applyWhereClause(e,t,r=[]){if(t&&Object.keys(t).length>0){let r=this.helperUtility.getDotWalkQuery(t);console.log({filteredWhere:r}),r=this.helperUtility.objectFilter(r,(e,t)=>"object"!=typeof t||null===t);for(const[t,i]of Object.entries(r))if(null===i)e.whereNull(t);else if(Array.isArray(i))e.whereIn(t,i);else{const{operator:r,value:l}=this.parseWhereValue(i);this._applyWhereCondition(e,t,r,l)}}r&&r.length>0&&this._applyNestedWhere(e,t,r)}_applyNestedWhere(e,t,r){let i=this,l=e._getMyModel();if(r&&r.length>0)for(const o of r){let a=this.helperUtility.getDotWalkQuery(t,o);if(a&&Object.keys(a).length>0){let t=this.utils.getModel(this.controllerWrapper,o),s=l.hasRelations[o],n=r.map(e=>this.helperUtility.pluckDotWalkKey(e,1));e.whereExists(function(){let e=i.getQueryBuilder(t,this.select("*").from(t.table));e.whereRaw(`${s.foreignKey} = ${l.table}.${s.localKey}`),i._applyWhereClause(e,a,n)})}}}_applyJoins(e,t,r){const i=Array.isArray(t)?t:[t];for(const t of i)"string"==typeof t?e[r](t):"object"==typeof t&&(t.table&&t.on?e[r](t.table,t.on):t.table&&t.first&&t.operator&&t.second&&e[r](t.table,t.first,t.operator,t.second))}_applyWithWhere(e,t){try{if(Array.isArray(t))for(const r of t)"string"==typeof r?e.withWhere(r):"object"==typeof r&&e.withWhere(r.column,r.operator,r.value);else if("object"==typeof t)for(const[r,i]of Object.entries(t))e.withWhere(r,i)}catch(e){console.warn("Failed to apply withWhere:",e.message)}}_applyHavingClause(e,t){for(const[r,i]of Object.entries(t))"object"==typeof i&&i.operator?e.having(r,i.operator,i.value):e.having(r,i)}_applyOrderBy(e,t){if(Array.isArray(t))for(const r of t)"string"==typeof r?e.orderBy(r):"object"==typeof r&&e.orderBy(r.column,r.direction||"asc");else"string"==typeof t?e.orderBy(t):"object"==typeof t&&e.orderBy(t.column,t.direction||"asc")}}module.exports=QueryBuilder;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
const QueryBuilder=require("./QueryBuilder");class QueryService{constructor(e,t,r=null){this.db=e,this.utils=t,this.controllerWrapper=r,this.queryBuilder=new QueryBuilder(e,t,r)}async getQuery(e,t){return await this.queryBuilder.getQuery(e,t)}async getSoftDeleteQuery(e,t){return await this.queryBuilder.getQuery(e,{...t,where:{...t.where||{},deleted_at:null}})}async executeShowQuery(e,t){let r=await this.getQuery(e,{...t,limit:1,offset:0});return r.data.length>0?r.data[0]:null}async executeCountQuery(e,t){let r=await this.db(e.table).count();return Object.values(r[0])[0]}async executeCreateQuery(e,t){return await this.db(e.table).insert(t.data).returning("*")}async executeUpdateQuery(e,t){return await this.db(e.table).where(t.where).update(t.data).
|
|
1
|
+
const QueryBuilder=require("./QueryBuilder");class QueryService{constructor(e,t,r=null){this.db=e,this.utils=t,this.controllerWrapper=r,this.queryBuilder=new QueryBuilder(e,t,r)}async getQuery(e,t){return await this.queryBuilder.getQuery(e,t)}async getSoftDeleteQuery(e,t){return await this.queryBuilder.getQuery(e,{...t,where:{...t.where||{},deleted_at:null}})}async executeShowQuery(e,t){let r=await this.getQuery(e,{...t,limit:1,offset:0});return r.data.length>0?r.data[0]:null}async executeCountQuery(e,t){let r=await this.db(e.table).count();return Object.values(r[0])[0]}async executeCreateQuery(e,t){return await this.db(e.table).insert(t.data).returning("*")}async executeUpdateQuery(e,t){return await this.db.transaction(async r=>(await r(e.table).where(t.where).update(t.data),await r(e.table).where(t.where).first()))}async executeDeleteQuery(e,t){return await this.db(e.table).where(t.where).delete()}async executeSoftDeleteQuery(e,t){return await this.db(e.table).where(t.where).update({deleted_at:new Date}).returning("*")}async executeUpsertQuery(e,t){return await this.db(e.table).insert(t.data).onConflict(t.conflict).update(t.data)}async executeReplaceQuery(e,t){return await this.db(e.table).replace(t.data)}async executeSyncQuery(e,t){return{insertOrUpdateQuery:await this.db(e.table).insert(t.data).onConflict(t.conflict).update(t.data),deleteQuery:await this.db(e.table).where(t.where).delete()}}}module.exports=QueryService;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dreamtree-org/korm-js",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.42",
|
|
4
4
|
"description": "Knowledge Object-Relational Mapping - A powerful, modular ORM system for Node.js with dynamic database operations, complex queries, relationships, and nested requests",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Partha Preetham Krishna",
|