@3lineas/d1-orm 1.0.11 → 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.cjs CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  BelongsTo: () => BelongsTo,
24
+ BelongsToMany: () => BelongsToMany,
24
25
  Blueprint: () => Blueprint,
25
26
  Column: () => Column,
26
27
  Connection: () => Connection,
@@ -129,6 +130,10 @@ var QueryBuilder = class {
129
130
  * The where constraints for the query.
130
131
  */
131
132
  wheres = [];
133
+ /**
134
+ * The join clauses for the query.
135
+ */
136
+ joins = [];
132
137
  /**
133
138
  * The bindings for the query.
134
139
  */
@@ -230,6 +235,19 @@ var QueryBuilder = class {
230
235
  this.offsetValue = offset;
231
236
  return this;
232
237
  }
238
+ /**
239
+ * Get the table associated with the query.
240
+ */
241
+ getTable() {
242
+ return this.table;
243
+ }
244
+ /**
245
+ * Add a JOIN clause to the query.
246
+ */
247
+ join(table, first, operator, second) {
248
+ this.joins.push(`JOIN ${table} ON ${first} ${operator} ${second}`);
249
+ return this;
250
+ }
233
251
  /**
234
252
  * Get the SQL representation of the query.
235
253
  *
@@ -237,6 +255,9 @@ var QueryBuilder = class {
237
255
  */
238
256
  toSql() {
239
257
  let sql = `SELECT ${this.columns.join(", ")} FROM ${this.table}`;
258
+ if (this.joins.length > 0) {
259
+ sql += ` ${this.joins.join(" ")}`;
260
+ }
240
261
  if (this.wheres.length > 0) {
241
262
  const constraints = this.wheres.map((w, i) => {
242
263
  if (i === 0) return w.replace(/^OR\s+/, "");
@@ -496,6 +517,39 @@ var BelongsTo = class extends Relationship {
496
517
  }
497
518
  };
498
519
 
520
+ // src/core/relationships/belongs-to-many.ts
521
+ var BelongsToMany = class extends Relationship {
522
+ pivotTable;
523
+ foreignPivotKey;
524
+ relatedPivotKey;
525
+ parentKey;
526
+ relatedKey;
527
+ constructor(query, parent, pivotTable, foreignPivotKey, relatedPivotKey, parentKey, relatedKey) {
528
+ super(query, parent);
529
+ this.pivotTable = pivotTable;
530
+ this.foreignPivotKey = foreignPivotKey;
531
+ this.relatedPivotKey = relatedPivotKey;
532
+ this.parentKey = parentKey;
533
+ this.relatedKey = relatedKey;
534
+ this.addConstraints();
535
+ }
536
+ addConstraints() {
537
+ const parentValue = this.parent.attributes[this.parentKey];
538
+ const relatedTable = this.query.getTable();
539
+ this.query.join(
540
+ this.pivotTable,
541
+ `${relatedTable}.${this.relatedKey}`,
542
+ "=",
543
+ `${this.pivotTable}.${this.relatedPivotKey}`
544
+ );
545
+ this.query.where(
546
+ `${this.pivotTable}.${this.foreignPivotKey}`,
547
+ "=",
548
+ parentValue
549
+ );
550
+ }
551
+ };
552
+
499
553
  // src/models/model.ts
500
554
  var Model = class {
501
555
  /**
@@ -703,6 +757,34 @@ var Model = class {
703
757
  const ok = ownerKey || "id";
704
758
  return new BelongsTo(instance.newQuery(), this, fk, ok);
705
759
  }
760
+ /**
761
+ * Define a many-to-many relationship.
762
+ *
763
+ * @param related - The related model class.
764
+ * @param table - The pivot table name.
765
+ * @param foreignPivotKey - The foreign key of the parent model on the pivot table.
766
+ * @param relatedPivotKey - The foreign key of the related model on the pivot table.
767
+ * @param parentKey - The key on the parent model (usually id).
768
+ * @param relatedKey - The key on the related model (usually id).
769
+ * @returns A BelongsToMany relationship instance.
770
+ */
771
+ belongsToMany(related, table, foreignPivotKey, relatedPivotKey, parentKey, relatedKey) {
772
+ const instance = new related();
773
+ const pivotTable = table || [this.getTable().slice(0, -1), instance.getTable().slice(0, -1)].sort().join("_");
774
+ const fpk = foreignPivotKey || this.getForeignKey();
775
+ const rpk = relatedPivotKey || instance.getForeignKey();
776
+ const pk = parentKey || "id";
777
+ const rk = relatedKey || "id";
778
+ return new BelongsToMany(
779
+ instance.newQuery(),
780
+ this,
781
+ pivotTable,
782
+ fpk,
783
+ rpk,
784
+ pk,
785
+ rk
786
+ );
787
+ }
706
788
  /**
707
789
  * Get the default foreign key name for the model.
708
790
  *
@@ -728,6 +810,7 @@ var Column = class {
728
810
  isNullable = false;
729
811
  isUnique = false;
730
812
  defaultValue = null;
813
+ checkConstraint = null;
731
814
  constructor(name, type) {
732
815
  this.name = name;
733
816
  this.type = type;
@@ -753,6 +836,13 @@ var Column = class {
753
836
  this.defaultValue = value;
754
837
  return this;
755
838
  }
839
+ /**
840
+ * Add a check constraint (primarily for enums).
841
+ */
842
+ check(expression) {
843
+ this.checkConstraint = expression;
844
+ return this;
845
+ }
756
846
  /**
757
847
  * Convert the column definition to SQL.
758
848
  */
@@ -768,6 +858,9 @@ var Column = class {
768
858
  const formattedValue = typeof this.defaultValue === "string" ? `'${this.defaultValue}'` : this.defaultValue;
769
859
  sql += ` DEFAULT ${formattedValue}`;
770
860
  }
861
+ if (this.checkConstraint) {
862
+ sql += ` CHECK (${this.checkConstraint})`;
863
+ }
771
864
  return sql;
772
865
  }
773
866
  };
@@ -775,8 +868,10 @@ var Blueprint = class {
775
868
  table;
776
869
  columns = [];
777
870
  commands = [];
778
- constructor(table) {
871
+ isAlter = false;
872
+ constructor(table, isAlter = false) {
779
873
  this.table = table;
874
+ this.isAlter = isAlter;
780
875
  }
781
876
  /**
782
877
  * Add an auto-incrementing ID column.
@@ -793,6 +888,32 @@ var Blueprint = class {
793
888
  this.columns.push(col);
794
889
  return col;
795
890
  }
891
+ /**
892
+ * Add a text (long string) column.
893
+ */
894
+ text(column) {
895
+ const col = new Column(column, "TEXT");
896
+ this.columns.push(col);
897
+ return col;
898
+ }
899
+ /**
900
+ * Add a JSON column (stored as TEXT).
901
+ */
902
+ json(column) {
903
+ const col = new Column(column, "TEXT");
904
+ this.columns.push(col);
905
+ return col;
906
+ }
907
+ /**
908
+ * Add an enum column (TEXT with CHECK constraint).
909
+ */
910
+ enum(column, values) {
911
+ const col = new Column(column, "TEXT");
912
+ const checkExpr = values.map((v) => `'${v}'`).join(", ");
913
+ col.check(`${column} IN (${checkExpr})`);
914
+ this.columns.push(col);
915
+ return col;
916
+ }
796
917
  /**
797
918
  * Add an integer column.
798
919
  */
@@ -801,6 +922,14 @@ var Blueprint = class {
801
922
  this.columns.push(col);
802
923
  return col;
803
924
  }
925
+ /**
926
+ * Add a float (REAL) column.
927
+ */
928
+ float(column) {
929
+ const col = new Column(column, "REAL");
930
+ this.columns.push(col);
931
+ return col;
932
+ }
804
933
  /**
805
934
  * Add a boolean column.
806
935
  */
@@ -809,6 +938,36 @@ var Blueprint = class {
809
938
  this.columns.push(col);
810
939
  return col;
811
940
  }
941
+ /**
942
+ * Add a date column (TEXT or INTEGER).
943
+ */
944
+ date(column) {
945
+ const col = new Column(column, "TEXT");
946
+ this.columns.push(col);
947
+ return col;
948
+ }
949
+ /**
950
+ * Add a datetime column (TEXT or INTEGER).
951
+ */
952
+ datetime(column) {
953
+ const col = new Column(column, "TEXT");
954
+ this.columns.push(col);
955
+ return col;
956
+ }
957
+ /**
958
+ * Add a blob column.
959
+ */
960
+ blob(column) {
961
+ const col = new Column(column, "BLOB");
962
+ this.columns.push(col);
963
+ return col;
964
+ }
965
+ /**
966
+ * Add a foreign key column (alias for integer).
967
+ */
968
+ foreign(column) {
969
+ return this.integer(column);
970
+ }
812
971
  /**
813
972
  * Add created_at and updated_at timestamp columns.
814
973
  */
@@ -826,6 +985,11 @@ var Blueprint = class {
826
985
  if (typeof col === "string") return col;
827
986
  return col.toSql();
828
987
  });
988
+ if (this.isAlter) {
989
+ return colDefinitions.map(
990
+ (def) => `ALTER TABLE ${this.table} ADD COLUMN ${def}`
991
+ );
992
+ }
829
993
  const cols = colDefinitions.join(", ");
830
994
  return [`CREATE TABLE IF NOT EXISTS ${this.table} (${cols})`];
831
995
  }
@@ -840,6 +1004,11 @@ var Schema = class {
840
1004
  callback(blueprint);
841
1005
  return blueprint.toSql()[0];
842
1006
  }
1007
+ static table(table, callback) {
1008
+ const blueprint = new Blueprint(table, true);
1009
+ callback(blueprint);
1010
+ return blueprint.toSql();
1011
+ }
843
1012
  static dropIfExists(table) {
844
1013
  return `DROP TABLE IF EXISTS ${table}`;
845
1014
  }
@@ -847,6 +1016,7 @@ var Schema = class {
847
1016
  // Annotate the CommonJS export names for ESM import in node:
848
1017
  0 && (module.exports = {
849
1018
  BelongsTo,
1019
+ BelongsToMany,
850
1020
  Blueprint,
851
1021
  Column,
852
1022
  Connection,
package/dist/index.d.cts CHANGED
@@ -99,6 +99,10 @@ declare class QueryBuilder {
99
99
  * The where constraints for the query.
100
100
  */
101
101
  protected wheres: string[];
102
+ /**
103
+ * The join clauses for the query.
104
+ */
105
+ protected joins: string[];
102
106
  /**
103
107
  * The bindings for the query.
104
108
  */
@@ -169,6 +173,14 @@ declare class QueryBuilder {
169
173
  * @returns The QueryBuilder instance.
170
174
  */
171
175
  offset(offset: number): this;
176
+ /**
177
+ * Get the table associated with the query.
178
+ */
179
+ getTable(): string;
180
+ /**
181
+ * Add a JOIN clause to the query.
182
+ */
183
+ join(table: string, first: string, operator: string, second: string): this;
172
184
  /**
173
185
  * Get the SQL representation of the query.
174
186
  *
@@ -278,6 +290,16 @@ declare class BelongsTo<Related extends Model> extends Relationship<Related> {
278
290
  addConstraints(): void;
279
291
  }
280
292
 
293
+ declare class BelongsToMany<Related extends Model> extends Relationship<Related> {
294
+ protected pivotTable: string;
295
+ protected foreignPivotKey: string;
296
+ protected relatedPivotKey: string;
297
+ protected parentKey: string;
298
+ protected relatedKey: string;
299
+ constructor(query: ModelQueryBuilder<Related>, parent: Model, pivotTable: string, foreignPivotKey: string, relatedPivotKey: string, parentKey: string, relatedKey: string);
300
+ addConstraints(): void;
301
+ }
302
+
281
303
  /**
282
304
  * Base Model class for D1 ORM.
283
305
  *
@@ -413,6 +435,18 @@ declare class Model {
413
435
  * @returns A BelongsTo relationship instance.
414
436
  */
415
437
  belongsTo<Related extends Model>(related: new () => Related, foreignKey?: string, ownerKey?: string): BelongsTo<Related>;
438
+ /**
439
+ * Define a many-to-many relationship.
440
+ *
441
+ * @param related - The related model class.
442
+ * @param table - The pivot table name.
443
+ * @param foreignPivotKey - The foreign key of the parent model on the pivot table.
444
+ * @param relatedPivotKey - The foreign key of the related model on the pivot table.
445
+ * @param parentKey - The key on the parent model (usually id).
446
+ * @param relatedKey - The key on the related model (usually id).
447
+ * @returns A BelongsToMany relationship instance.
448
+ */
449
+ belongsToMany<Related extends Model>(related: new () => Related, table?: string, foreignPivotKey?: string, relatedPivotKey?: string, parentKey?: string, relatedKey?: string): BelongsToMany<Related>;
416
450
  /**
417
451
  * Get the default foreign key name for the model.
418
452
  *
@@ -474,6 +508,7 @@ declare class Column {
474
508
  protected isNullable: boolean;
475
509
  protected isUnique: boolean;
476
510
  protected defaultValue: any;
511
+ protected checkConstraint: string | null;
477
512
  constructor(name: string, type: string);
478
513
  /**
479
514
  * Set the column as nullable.
@@ -487,6 +522,10 @@ declare class Column {
487
522
  * Set the default value for the column.
488
523
  */
489
524
  default(value: any): this;
525
+ /**
526
+ * Add a check constraint (primarily for enums).
527
+ */
528
+ check(expression: string): this;
490
529
  /**
491
530
  * Convert the column definition to SQL.
492
531
  */
@@ -499,7 +538,8 @@ declare class Blueprint {
499
538
  protected table: string;
500
539
  protected columns: (Column | string)[];
501
540
  protected commands: string[];
502
- constructor(table: string);
541
+ protected isAlter: boolean;
542
+ constructor(table: string, isAlter?: boolean);
503
543
  /**
504
544
  * Add an auto-incrementing ID column.
505
545
  */
@@ -508,14 +548,46 @@ declare class Blueprint {
508
548
  * Add a string (TEXT) column.
509
549
  */
510
550
  string(column: string): Column;
551
+ /**
552
+ * Add a text (long string) column.
553
+ */
554
+ text(column: string): Column;
555
+ /**
556
+ * Add a JSON column (stored as TEXT).
557
+ */
558
+ json(column: string): Column;
559
+ /**
560
+ * Add an enum column (TEXT with CHECK constraint).
561
+ */
562
+ enum(column: string, values: string[]): Column;
511
563
  /**
512
564
  * Add an integer column.
513
565
  */
514
566
  integer(column: string): Column;
567
+ /**
568
+ * Add a float (REAL) column.
569
+ */
570
+ float(column: string): Column;
515
571
  /**
516
572
  * Add a boolean column.
517
573
  */
518
574
  boolean(column: string): Column;
575
+ /**
576
+ * Add a date column (TEXT or INTEGER).
577
+ */
578
+ date(column: string): Column;
579
+ /**
580
+ * Add a datetime column (TEXT or INTEGER).
581
+ */
582
+ datetime(column: string): Column;
583
+ /**
584
+ * Add a blob column.
585
+ */
586
+ blob(column: string): Column;
587
+ /**
588
+ * Add a foreign key column (alias for integer).
589
+ */
590
+ foreign(column: string): Column;
519
591
  /**
520
592
  * Add created_at and updated_at timestamp columns.
521
593
  */
@@ -528,7 +600,8 @@ declare class Blueprint {
528
600
 
529
601
  declare class Schema {
530
602
  static create(table: string, callback: (table: Blueprint) => void): string;
603
+ static table(table: string, callback: (table: Blueprint) => void): string[];
531
604
  static dropIfExists(table: string): string;
532
605
  }
533
606
 
534
- export { BelongsTo, Blueprint, Column, Connection, type D1Database, type D1ExecResult, type D1PreparedStatement, type D1Result, type D1Value, Database, HasMany, HasOne, Model, ModelQueryBuilder, QueryBuilder, Relationship, Schema };
607
+ export { BelongsTo, BelongsToMany, Blueprint, Column, Connection, type D1Database, type D1ExecResult, type D1PreparedStatement, type D1Result, type D1Value, Database, HasMany, HasOne, Model, ModelQueryBuilder, QueryBuilder, Relationship, Schema };
package/dist/index.d.ts CHANGED
@@ -99,6 +99,10 @@ declare class QueryBuilder {
99
99
  * The where constraints for the query.
100
100
  */
101
101
  protected wheres: string[];
102
+ /**
103
+ * The join clauses for the query.
104
+ */
105
+ protected joins: string[];
102
106
  /**
103
107
  * The bindings for the query.
104
108
  */
@@ -169,6 +173,14 @@ declare class QueryBuilder {
169
173
  * @returns The QueryBuilder instance.
170
174
  */
171
175
  offset(offset: number): this;
176
+ /**
177
+ * Get the table associated with the query.
178
+ */
179
+ getTable(): string;
180
+ /**
181
+ * Add a JOIN clause to the query.
182
+ */
183
+ join(table: string, first: string, operator: string, second: string): this;
172
184
  /**
173
185
  * Get the SQL representation of the query.
174
186
  *
@@ -278,6 +290,16 @@ declare class BelongsTo<Related extends Model> extends Relationship<Related> {
278
290
  addConstraints(): void;
279
291
  }
280
292
 
293
+ declare class BelongsToMany<Related extends Model> extends Relationship<Related> {
294
+ protected pivotTable: string;
295
+ protected foreignPivotKey: string;
296
+ protected relatedPivotKey: string;
297
+ protected parentKey: string;
298
+ protected relatedKey: string;
299
+ constructor(query: ModelQueryBuilder<Related>, parent: Model, pivotTable: string, foreignPivotKey: string, relatedPivotKey: string, parentKey: string, relatedKey: string);
300
+ addConstraints(): void;
301
+ }
302
+
281
303
  /**
282
304
  * Base Model class for D1 ORM.
283
305
  *
@@ -413,6 +435,18 @@ declare class Model {
413
435
  * @returns A BelongsTo relationship instance.
414
436
  */
415
437
  belongsTo<Related extends Model>(related: new () => Related, foreignKey?: string, ownerKey?: string): BelongsTo<Related>;
438
+ /**
439
+ * Define a many-to-many relationship.
440
+ *
441
+ * @param related - The related model class.
442
+ * @param table - The pivot table name.
443
+ * @param foreignPivotKey - The foreign key of the parent model on the pivot table.
444
+ * @param relatedPivotKey - The foreign key of the related model on the pivot table.
445
+ * @param parentKey - The key on the parent model (usually id).
446
+ * @param relatedKey - The key on the related model (usually id).
447
+ * @returns A BelongsToMany relationship instance.
448
+ */
449
+ belongsToMany<Related extends Model>(related: new () => Related, table?: string, foreignPivotKey?: string, relatedPivotKey?: string, parentKey?: string, relatedKey?: string): BelongsToMany<Related>;
416
450
  /**
417
451
  * Get the default foreign key name for the model.
418
452
  *
@@ -474,6 +508,7 @@ declare class Column {
474
508
  protected isNullable: boolean;
475
509
  protected isUnique: boolean;
476
510
  protected defaultValue: any;
511
+ protected checkConstraint: string | null;
477
512
  constructor(name: string, type: string);
478
513
  /**
479
514
  * Set the column as nullable.
@@ -487,6 +522,10 @@ declare class Column {
487
522
  * Set the default value for the column.
488
523
  */
489
524
  default(value: any): this;
525
+ /**
526
+ * Add a check constraint (primarily for enums).
527
+ */
528
+ check(expression: string): this;
490
529
  /**
491
530
  * Convert the column definition to SQL.
492
531
  */
@@ -499,7 +538,8 @@ declare class Blueprint {
499
538
  protected table: string;
500
539
  protected columns: (Column | string)[];
501
540
  protected commands: string[];
502
- constructor(table: string);
541
+ protected isAlter: boolean;
542
+ constructor(table: string, isAlter?: boolean);
503
543
  /**
504
544
  * Add an auto-incrementing ID column.
505
545
  */
@@ -508,14 +548,46 @@ declare class Blueprint {
508
548
  * Add a string (TEXT) column.
509
549
  */
510
550
  string(column: string): Column;
551
+ /**
552
+ * Add a text (long string) column.
553
+ */
554
+ text(column: string): Column;
555
+ /**
556
+ * Add a JSON column (stored as TEXT).
557
+ */
558
+ json(column: string): Column;
559
+ /**
560
+ * Add an enum column (TEXT with CHECK constraint).
561
+ */
562
+ enum(column: string, values: string[]): Column;
511
563
  /**
512
564
  * Add an integer column.
513
565
  */
514
566
  integer(column: string): Column;
567
+ /**
568
+ * Add a float (REAL) column.
569
+ */
570
+ float(column: string): Column;
515
571
  /**
516
572
  * Add a boolean column.
517
573
  */
518
574
  boolean(column: string): Column;
575
+ /**
576
+ * Add a date column (TEXT or INTEGER).
577
+ */
578
+ date(column: string): Column;
579
+ /**
580
+ * Add a datetime column (TEXT or INTEGER).
581
+ */
582
+ datetime(column: string): Column;
583
+ /**
584
+ * Add a blob column.
585
+ */
586
+ blob(column: string): Column;
587
+ /**
588
+ * Add a foreign key column (alias for integer).
589
+ */
590
+ foreign(column: string): Column;
519
591
  /**
520
592
  * Add created_at and updated_at timestamp columns.
521
593
  */
@@ -528,7 +600,8 @@ declare class Blueprint {
528
600
 
529
601
  declare class Schema {
530
602
  static create(table: string, callback: (table: Blueprint) => void): string;
603
+ static table(table: string, callback: (table: Blueprint) => void): string[];
531
604
  static dropIfExists(table: string): string;
532
605
  }
533
606
 
534
- export { BelongsTo, Blueprint, Column, Connection, type D1Database, type D1ExecResult, type D1PreparedStatement, type D1Result, type D1Value, Database, HasMany, HasOne, Model, ModelQueryBuilder, QueryBuilder, Relationship, Schema };
607
+ export { BelongsTo, BelongsToMany, Blueprint, Column, Connection, type D1Database, type D1ExecResult, type D1PreparedStatement, type D1Result, type D1Value, Database, HasMany, HasOne, Model, ModelQueryBuilder, QueryBuilder, Relationship, Schema };