@3lineas/d1-orm 1.0.10 → 1.0.12

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
@@ -21,6 +21,10 @@ var QueryBuilder = class {
21
21
  * The where constraints for the query.
22
22
  */
23
23
  wheres = [];
24
+ /**
25
+ * The join clauses for the query.
26
+ */
27
+ joins = [];
24
28
  /**
25
29
  * The bindings for the query.
26
30
  */
@@ -122,6 +126,19 @@ var QueryBuilder = class {
122
126
  this.offsetValue = offset;
123
127
  return this;
124
128
  }
129
+ /**
130
+ * Get the table associated with the query.
131
+ */
132
+ getTable() {
133
+ return this.table;
134
+ }
135
+ /**
136
+ * Add a JOIN clause to the query.
137
+ */
138
+ join(table, first, operator, second) {
139
+ this.joins.push(`JOIN ${table} ON ${first} ${operator} ${second}`);
140
+ return this;
141
+ }
125
142
  /**
126
143
  * Get the SQL representation of the query.
127
144
  *
@@ -129,6 +146,9 @@ var QueryBuilder = class {
129
146
  */
130
147
  toSql() {
131
148
  let sql = `SELECT ${this.columns.join(", ")} FROM ${this.table}`;
149
+ if (this.joins.length > 0) {
150
+ sql += ` ${this.joins.join(" ")}`;
151
+ }
132
152
  if (this.wheres.length > 0) {
133
153
  const constraints = this.wheres.map((w, i) => {
134
154
  if (i === 0) return w.replace(/^OR\s+/, "");
@@ -341,6 +361,39 @@ var BelongsTo = class extends Relationship {
341
361
  }
342
362
  };
343
363
 
364
+ // src/core/relationships/belongs-to-many.ts
365
+ var BelongsToMany = class extends Relationship {
366
+ pivotTable;
367
+ foreignPivotKey;
368
+ relatedPivotKey;
369
+ parentKey;
370
+ relatedKey;
371
+ constructor(query, parent, pivotTable, foreignPivotKey, relatedPivotKey, parentKey, relatedKey) {
372
+ super(query, parent);
373
+ this.pivotTable = pivotTable;
374
+ this.foreignPivotKey = foreignPivotKey;
375
+ this.relatedPivotKey = relatedPivotKey;
376
+ this.parentKey = parentKey;
377
+ this.relatedKey = relatedKey;
378
+ this.addConstraints();
379
+ }
380
+ addConstraints() {
381
+ const parentValue = this.parent.attributes[this.parentKey];
382
+ const relatedTable = this.query.getTable();
383
+ this.query.join(
384
+ this.pivotTable,
385
+ `${relatedTable}.${this.relatedKey}`,
386
+ "=",
387
+ `${this.pivotTable}.${this.relatedPivotKey}`
388
+ );
389
+ this.query.where(
390
+ `${this.pivotTable}.${this.foreignPivotKey}`,
391
+ "=",
392
+ parentValue
393
+ );
394
+ }
395
+ };
396
+
344
397
  // src/models/model.ts
345
398
  var Model = class {
346
399
  /**
@@ -548,6 +601,34 @@ var Model = class {
548
601
  const ok = ownerKey || "id";
549
602
  return new BelongsTo(instance.newQuery(), this, fk, ok);
550
603
  }
604
+ /**
605
+ * Define a many-to-many relationship.
606
+ *
607
+ * @param related - The related model class.
608
+ * @param table - The pivot table name.
609
+ * @param foreignPivotKey - The foreign key of the parent model on the pivot table.
610
+ * @param relatedPivotKey - The foreign key of the related model on the pivot table.
611
+ * @param parentKey - The key on the parent model (usually id).
612
+ * @param relatedKey - The key on the related model (usually id).
613
+ * @returns A BelongsToMany relationship instance.
614
+ */
615
+ belongsToMany(related, table, foreignPivotKey, relatedPivotKey, parentKey, relatedKey) {
616
+ const instance = new related();
617
+ const pivotTable = table || [this.getTable().slice(0, -1), instance.getTable().slice(0, -1)].sort().join("_");
618
+ const fpk = foreignPivotKey || this.getForeignKey();
619
+ const rpk = relatedPivotKey || instance.getForeignKey();
620
+ const pk = parentKey || "id";
621
+ const rk = relatedKey || "id";
622
+ return new BelongsToMany(
623
+ instance.newQuery(),
624
+ this,
625
+ pivotTable,
626
+ fpk,
627
+ rpk,
628
+ pk,
629
+ rk
630
+ );
631
+ }
551
632
  /**
552
633
  * Get the default foreign key name for the model.
553
634
  *
@@ -573,6 +654,7 @@ var Column = class {
573
654
  isNullable = false;
574
655
  isUnique = false;
575
656
  defaultValue = null;
657
+ checkConstraint = null;
576
658
  constructor(name, type) {
577
659
  this.name = name;
578
660
  this.type = type;
@@ -598,6 +680,13 @@ var Column = class {
598
680
  this.defaultValue = value;
599
681
  return this;
600
682
  }
683
+ /**
684
+ * Add a check constraint (primarily for enums).
685
+ */
686
+ check(expression) {
687
+ this.checkConstraint = expression;
688
+ return this;
689
+ }
601
690
  /**
602
691
  * Convert the column definition to SQL.
603
692
  */
@@ -613,6 +702,9 @@ var Column = class {
613
702
  const formattedValue = typeof this.defaultValue === "string" ? `'${this.defaultValue}'` : this.defaultValue;
614
703
  sql += ` DEFAULT ${formattedValue}`;
615
704
  }
705
+ if (this.checkConstraint) {
706
+ sql += ` CHECK (${this.checkConstraint})`;
707
+ }
616
708
  return sql;
617
709
  }
618
710
  };
@@ -620,8 +712,10 @@ var Blueprint = class {
620
712
  table;
621
713
  columns = [];
622
714
  commands = [];
623
- constructor(table) {
715
+ isAlter = false;
716
+ constructor(table, isAlter = false) {
624
717
  this.table = table;
718
+ this.isAlter = isAlter;
625
719
  }
626
720
  /**
627
721
  * Add an auto-incrementing ID column.
@@ -638,6 +732,32 @@ var Blueprint = class {
638
732
  this.columns.push(col);
639
733
  return col;
640
734
  }
735
+ /**
736
+ * Add a text (long string) column.
737
+ */
738
+ text(column) {
739
+ const col = new Column(column, "TEXT");
740
+ this.columns.push(col);
741
+ return col;
742
+ }
743
+ /**
744
+ * Add a JSON column (stored as TEXT).
745
+ */
746
+ json(column) {
747
+ const col = new Column(column, "TEXT");
748
+ this.columns.push(col);
749
+ return col;
750
+ }
751
+ /**
752
+ * Add an enum column (TEXT with CHECK constraint).
753
+ */
754
+ enum(column, values) {
755
+ const col = new Column(column, "TEXT");
756
+ const checkExpr = values.map((v) => `'${v}'`).join(", ");
757
+ col.check(`${column} IN (${checkExpr})`);
758
+ this.columns.push(col);
759
+ return col;
760
+ }
641
761
  /**
642
762
  * Add an integer column.
643
763
  */
@@ -646,6 +766,14 @@ var Blueprint = class {
646
766
  this.columns.push(col);
647
767
  return col;
648
768
  }
769
+ /**
770
+ * Add a float (REAL) column.
771
+ */
772
+ float(column) {
773
+ const col = new Column(column, "REAL");
774
+ this.columns.push(col);
775
+ return col;
776
+ }
649
777
  /**
650
778
  * Add a boolean column.
651
779
  */
@@ -654,6 +782,36 @@ var Blueprint = class {
654
782
  this.columns.push(col);
655
783
  return col;
656
784
  }
785
+ /**
786
+ * Add a date column (TEXT or INTEGER).
787
+ */
788
+ date(column) {
789
+ const col = new Column(column, "TEXT");
790
+ this.columns.push(col);
791
+ return col;
792
+ }
793
+ /**
794
+ * Add a datetime column (TEXT or INTEGER).
795
+ */
796
+ datetime(column) {
797
+ const col = new Column(column, "TEXT");
798
+ this.columns.push(col);
799
+ return col;
800
+ }
801
+ /**
802
+ * Add a blob column.
803
+ */
804
+ blob(column) {
805
+ const col = new Column(column, "BLOB");
806
+ this.columns.push(col);
807
+ return col;
808
+ }
809
+ /**
810
+ * Add a foreign key column (alias for integer).
811
+ */
812
+ foreign(column) {
813
+ return this.integer(column);
814
+ }
657
815
  /**
658
816
  * Add created_at and updated_at timestamp columns.
659
817
  */
@@ -671,6 +829,11 @@ var Blueprint = class {
671
829
  if (typeof col === "string") return col;
672
830
  return col.toSql();
673
831
  });
832
+ if (this.isAlter) {
833
+ return colDefinitions.map(
834
+ (def) => `ALTER TABLE ${this.table} ADD COLUMN ${def}`
835
+ );
836
+ }
674
837
  const cols = colDefinitions.join(", ");
675
838
  return [`CREATE TABLE IF NOT EXISTS ${this.table} (${cols})`];
676
839
  }
@@ -685,12 +848,18 @@ var Schema = class {
685
848
  callback(blueprint);
686
849
  return blueprint.toSql()[0];
687
850
  }
851
+ static table(table, callback) {
852
+ const blueprint = new Blueprint(table, true);
853
+ callback(blueprint);
854
+ return blueprint.toSql();
855
+ }
688
856
  static dropIfExists(table) {
689
857
  return `DROP TABLE IF EXISTS ${table}`;
690
858
  }
691
859
  };
692
860
  export {
693
861
  BelongsTo,
862
+ BelongsToMany,
694
863
  Blueprint,
695
864
  Column,
696
865
  Connection,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@3lineas/d1-orm",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "A lightweight and powerful ORM for Cloudflare D1, inspired by Laravel Eloquent.",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",