foreign-keys.cjs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var foreign_keys_exports = {};
  20. __export(foreign_keys_exports, {
  21. ForeignKey: () => ForeignKey,
  22. ForeignKeyBuilder: () => ForeignKeyBuilder,
  23. foreignKey: () => foreignKey
  24. });
  25. module.exports = __toCommonJS(foreign_keys_exports);
  26. var import_entity = require("../entity.cjs");
  27. var import_table_utils = require("../table.utils.cjs");
  28. class ForeignKeyBuilder {
  29. static [import_entity.entityKind] = "PgForeignKeyBuilder";
  30. /** @internal */
  31. reference;
  32. /** @internal */
  33. _onUpdate = "no action";
  34. /** @internal */
  35. _onDelete = "no action";
  36. constructor(config, actions) {
  37. this.reference = () => {
  38. const { name, columns, foreignColumns } = config();
  39. return { name, columns, foreignTable: foreignColumns[0].table, foreignColumns };
  40. };
  41. if (actions) {
  42. this._onUpdate = actions.onUpdate;
  43. this._onDelete = actions.onDelete;
  44. }
  45. }
  46. onUpdate(action) {
  47. this._onUpdate = action === void 0 ? "no action" : action;
  48. return this;
  49. }
  50. onDelete(action) {
  51. this._onDelete = action === void 0 ? "no action" : action;
  52. return this;
  53. }
  54. /** @internal */
  55. build(table) {
  56. return new ForeignKey(table, this);
  57. }
  58. }
  59. class ForeignKey {
  60. constructor(table, builder) {
  61. this.table = table;
  62. this.reference = builder.reference;
  63. this.onUpdate = builder._onUpdate;
  64. this.onDelete = builder._onDelete;
  65. }
  66. static [import_entity.entityKind] = "PgForeignKey";
  67. reference;
  68. onUpdate;
  69. onDelete;
  70. getName() {
  71. const { name, columns, foreignColumns } = this.reference();
  72. const columnNames = columns.map((column) => column.name);
  73. const foreignColumnNames = foreignColumns.map((column) => column.name);
  74. const chunks = [
  75. this.table[import_table_utils.TableName],
  76. ...columnNames,
  77. foreignColumns[0].table[import_table_utils.TableName],
  78. ...foreignColumnNames
  79. ];
  80. return name ?? `${chunks.join("_")}_fk`;
  81. }
  82. }
  83. function foreignKey(config) {
  84. function mappedConfig() {
  85. const { name, columns, foreignColumns } = config;
  86. return {
  87. name,
  88. columns,
  89. foreignColumns
  90. };
  91. }
  92. return new ForeignKeyBuilder(mappedConfig);
  93. }
  94. // Annotate the CommonJS export names for ESM import in node:
  95. 0 && (module.exports = {
  96. ForeignKey,
  97. ForeignKeyBuilder,
  98. foreignKey
  99. });
  100. //# sourceMappingURL=foreign-keys.cjs.map