@h3ravel/database 11.4.11 → 11.5.0-alpha.1

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
@@ -5,16 +5,18 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __commonJS = (cb, mod) => function() {
9
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
10
- };
8
+ var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
11
9
  var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
13
- key = keys[i];
14
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
15
- get: ((k) => from[k]).bind(null, key),
16
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
- });
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
12
+ key = keys[i];
13
+ if (!__hasOwnProp.call(to, key) && key !== except) {
14
+ __defProp(to, key, {
15
+ get: ((k) => from[k]).bind(null, key),
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ }
19
+ }
18
20
  }
19
21
  return to;
20
22
  };
@@ -31,8 +33,9 @@ let __h3ravel_support = require("@h3ravel/support");
31
33
  let node_path = require("node:path");
32
34
  node_path = __toESM(node_path);
33
35
  let __h3ravel_arquebus_migrations = require("@h3ravel/arquebus/migrations");
36
+ let __h3ravel_arquebus_relations = require("@h3ravel/arquebus/relations");
34
37
  let __h3ravel_arquebus = require("@h3ravel/arquebus");
35
- let __h3ravel_core = require("@h3ravel/core");
38
+ let __h3ravel_contracts = require("@h3ravel/contracts");
36
39
 
37
40
  //#region src/Utils/TableGuesser.ts
38
41
  var TableGuesser = class TableGuesser {
@@ -52,11 +55,12 @@ var TableGuesser = class TableGuesser {
52
55
  };
53
56
 
54
57
  //#endregion
55
- //#region ../../node_modules/.pnpm/dayjs@1.11.18/node_modules/dayjs/dayjs.min.js
56
- var require_dayjs_min = /* @__PURE__ */ __commonJS({ "../../node_modules/.pnpm/dayjs@1.11.18/node_modules/dayjs/dayjs.min.js": ((exports, module) => {
58
+ //#region ../../node_modules/.pnpm/dayjs@1.11.19/node_modules/dayjs/dayjs.min.js
59
+ var require_dayjs_min = /* @__PURE__ */ __commonJSMin(((exports, module) => {
57
60
  (function(t, e) {
58
61
  "object" == typeof exports && "undefined" != typeof module ? module.exports = e() : "function" == typeof define && define.amd ? define(e) : (t = "undefined" != typeof globalThis ? globalThis : t || self).dayjs = e();
59
62
  })(exports, (function() {
63
+ "use strict";
60
64
  var t = 1e3, e = 6e4, n = 36e5, r = "millisecond", i = "second", s = "minute", u = "hour", a = "day", o = "week", c = "month", f = "quarter", h = "year", d = "date", l = "Invalid Date", $ = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/, y = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g, M = {
61
65
  name: "en",
62
66
  weekdays: "Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),
@@ -347,7 +351,7 @@ var require_dayjs_min = /* @__PURE__ */ __commonJS({ "../../node_modules/.pnpm/d
347
351
  return O(1e3 * t$1);
348
352
  }, O.en = D[g], O.Ls = D, O.p = {}, O;
349
353
  }));
350
- }) });
354
+ }));
351
355
 
352
356
  //#endregion
353
357
  //#region src/Commands/MakeCommand.ts
@@ -803,16 +807,87 @@ const arquebusConfig = (config$1) => {
803
807
 
804
808
  //#endregion
805
809
  //#region src/Model.ts
806
- var Model = class extends __h3ravel_arquebus.Model {
810
+ var Model = class Model extends (0, __h3ravel_shared.mix)(__h3ravel_contracts.UrlRoutable, __h3ravel_arquebus.Model) {
807
811
  /**
808
812
  * Retrieve the model for a bound value.
809
813
  *
810
- * @param {any} value
811
- * @param {String|null} field
812
- * @returns
814
+ * @param value
815
+ * @param field
816
+ */
817
+ resolveRouteBinding(value, field) {
818
+ return this.resolveRouteBindingQuery(this.newQuery(), value, field).first();
819
+ }
820
+ /**
821
+ * Retrieve the model for a bound value.
822
+ *
823
+ * @param query
824
+ * @param value
825
+ * @param field
826
+ */
827
+ resolveRouteBindingQuery(query, value, field = null) {
828
+ return query.where(field ?? this.getRouteKeyName(), value);
829
+ }
830
+ /**
831
+ * Retrieve the model for a bound value.
832
+ *
833
+ * @param value
834
+ * @param field
835
+ */
836
+ resolveSoftDeletableRouteBinding(value, field) {
837
+ return this.resolveRouteBindingQuery(this.newQuery(), value, field).withTrashed().first();
838
+ }
839
+ /**
840
+ * Retrieve the child model for a bound value.
841
+ *
842
+ * @param childType
843
+ * @param value
844
+ * @param field
845
+ */
846
+ resolveChildRouteBinding(childType, value, field) {
847
+ return this.resolveChildRouteBindingQuery(childType, value, field).first();
848
+ }
849
+ /**
850
+ * Retrieve the child model for a bound value.
851
+ *
852
+ * @param childType
853
+ * @param value
854
+ * @param field
855
+ */
856
+ resolveSoftDeletableChildRouteBinding(childType, value, field) {
857
+ return this.resolveChildRouteBindingQuery(childType, value, field).withTrashed().first();
858
+ }
859
+ /**
860
+ * Retrieve the child model query for a bound value.
861
+ *
862
+ * @param childType
863
+ * @param value
864
+ * @param field
865
+ */
866
+ resolveChildRouteBindingQuery(childType, value, field) {
867
+ const relationship = this[this.childRouteBindingRelationshipName(childType)]();
868
+ field = field || relationship.getRelated().getRouteKeyName();
869
+ if (relationship instanceof __h3ravel_arquebus_relations.HasManyThrough || relationship instanceof __h3ravel_arquebus_relations.BelongsToMany) field = relationship.getRelated().qualifyColumn(field);
870
+ return relationship instanceof Model ? relationship.resolveRouteBindingQuery(relationship.newQuery(), value, field) : relationship.getRelated().resolveRouteBindingQuery(relationship, value, field);
871
+ }
872
+ /**
873
+ * Retrieve the child route model binding relationship name for the given child type.
874
+ *
875
+ * @param childType
876
+ */
877
+ childRouteBindingRelationshipName(childType) {
878
+ return __h3ravel_support.Str.plural(__h3ravel_support.Str.camel(childType));
879
+ }
880
+ /**
881
+ * Get the value of the model's route key.
882
+ */
883
+ getRouteKey() {
884
+ return this.getAttribute(this.getRouteKeyName());
885
+ }
886
+ /**
887
+ * Get the route key for the model.
813
888
  */
814
- resolveRouteBinding(value, field = null) {
815
- return this.newQuery().where(field ?? "ids", value).firstOrFail();
889
+ getRouteKeyName() {
890
+ return this.getKeyName();
816
891
  }
817
892
  };
818
893
 
@@ -826,12 +901,13 @@ var Model = class extends __h3ravel_arquebus.Model {
826
901
  * Register migration and seeder commands.
827
902
  *
828
903
  */
829
- var DatabaseServiceProvider = class extends __h3ravel_core.ServiceProvider {
904
+ var DatabaseServiceProvider = class extends __h3ravel_support.ServiceProvider {
830
905
  static priority = 994;
831
906
  register() {
832
907
  const config$1 = this.app.make("config");
833
908
  const connection = Object.entries(arquebusConfig(config$1.get("database"))).find(([client]) => client === config$1.get("database.default"))?.at(1);
834
909
  if (connection) __h3ravel_arquebus.arquebus.addConnection(connection);
910
+ this.app.singleton("db", () => __h3ravel_arquebus.arquebus.fire());
835
911
  /** Register Musket Commands */
836
912
  this.registerCommands([
837
913
  MigrateCommand,
package/dist/index.d.ts CHANGED
@@ -4,8 +4,10 @@ import * as _h3ravel_arquebus0 from "@h3ravel/arquebus";
4
4
  import { Model as Model$1, QueryBuilder, Seeder as Seeder$1 } from "@h3ravel/arquebus";
5
5
  import * as knex0 from "knex";
6
6
  import { Knex } from "knex";
7
- import { Application, ServiceProvider } from "@h3ravel/core";
8
- import { IQueryBuilder } from "@h3ravel/arquebus/types";
7
+ import { IBuilder, IQueryBuilder, Relation } from "@h3ravel/arquebus/types";
8
+ import { UrlRoutable } from "@h3ravel/contracts";
9
+ import { ServiceProvider } from "@h3ravel/support";
10
+ import { Application } from "@h3ravel/core";
9
11
 
10
12
  //#region src/Commands/MakeCommand.d.ts
11
13
  declare class MakeCommand extends Command {
@@ -207,15 +209,68 @@ type TConfig = TBaseConfig & ({
207
209
  declare const arquebusConfig: (config: any) => TBaseConfig;
208
210
  //#endregion
209
211
  //#region src/Model.d.ts
210
- declare class Model<M extends Model$1 = any> extends Model$1 {
212
+ declare const Model_base: typeof UrlRoutable & typeof Model$1 & (new (...args: any[]) => UrlRoutable & Model$1);
213
+ declare class Model<M extends Model$1 = any> extends Model_base {
211
214
  /**
212
215
  * Retrieve the model for a bound value.
213
216
  *
214
- * @param {any} value
215
- * @param {String|null} field
216
- * @returns
217
+ * @param value
218
+ * @param field
219
+ */
220
+ resolveRouteBinding(value: any, field?: string): Promise<M>;
221
+ /**
222
+ * Retrieve the model for a bound value.
223
+ *
224
+ * @param query
225
+ * @param value
226
+ * @param field
227
+ */
228
+ resolveRouteBindingQuery(query: IBuilder<M>, value: any, field?: undefined | string | null): IBuilder<M>;
229
+ /**
230
+ * Retrieve the model for a bound value.
231
+ *
232
+ * @param value
233
+ * @param field
234
+ */
235
+ resolveSoftDeletableRouteBinding(value: any, field?: string): Promise<M | null | undefined>;
236
+ /**
237
+ * Retrieve the child model for a bound value.
238
+ *
239
+ * @param childType
240
+ * @param value
241
+ * @param field
242
+ */
243
+ resolveChildRouteBinding(childType: string, value: any, field: string): Promise<M | null | undefined>;
244
+ /**
245
+ * Retrieve the child model for a bound value.
246
+ *
247
+ * @param childType
248
+ * @param value
249
+ * @param field
250
+ */
251
+ resolveSoftDeletableChildRouteBinding(childType: string, value: any, field: string): Promise<M | null | undefined>;
252
+ /**
253
+ * Retrieve the child model query for a bound value.
254
+ *
255
+ * @param childType
256
+ * @param value
257
+ * @param field
258
+ */
259
+ protected resolveChildRouteBindingQuery(childType: string, value: any, field: string): Relation<Model<M>>;
260
+ /**
261
+ * Retrieve the child route model binding relationship name for the given child type.
262
+ *
263
+ * @param childType
264
+ */
265
+ protected childRouteBindingRelationshipName(childType: string): keyof typeof this;
266
+ /**
267
+ * Get the value of the model's route key.
268
+ */
269
+ getRouteKey(): any;
270
+ /**
271
+ * Get the route key for the model.
217
272
  */
218
- resolveRouteBinding(value: any, field?: undefined | string | null): Promise<M>;
273
+ getRouteKeyName(): string;
219
274
  }
220
275
  //#endregion
221
276
  //#region src/Providers/DatabaseServiceProvider.d.ts
@@ -247,7 +302,7 @@ declare class DB {
247
302
  /**
248
303
  * Builder transaction instance
249
304
  */
250
- static transaction<C>(callback: (...args: C[]) => any): Promise<knex0.Knex.Transaction<any, any[]>> | undefined;
305
+ static transaction<C>(callback: (...args: C[]) => any): Promise<knex0.Knex.Transaction<any, any[]>> | null | undefined;
251
306
  private builder;
252
307
  }
253
308
  //#endregion
package/dist/index.js CHANGED
@@ -1,12 +1,13 @@
1
1
  import "node:module";
2
- import { FileSystem, Logger, Resolver, TaskManager } from "@h3ravel/shared";
2
+ import { FileSystem, Logger, Resolver, TaskManager, mix } from "@h3ravel/shared";
3
3
  import { mkdir, readFile, writeFile } from "node:fs/promises";
4
4
  import { Command } from "@h3ravel/musket";
5
- import { Arr, Str } from "@h3ravel/support";
5
+ import { Arr, ServiceProvider, Str } from "@h3ravel/support";
6
6
  import npath from "node:path";
7
7
  import { Migrate, MigrationCreator } from "@h3ravel/arquebus/migrations";
8
+ import { BelongsToMany, HasManyThrough } from "@h3ravel/arquebus/relations";
8
9
  import { Model as Model$1, Seeder as Seeder$1, arquebus } from "@h3ravel/arquebus";
9
- import { ServiceProvider } from "@h3ravel/core";
10
+ import { UrlRoutable } from "@h3ravel/contracts";
10
11
 
11
12
  //#region rolldown:runtime
12
13
  var __create = Object.create;
@@ -15,16 +16,18 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
15
16
  var __getOwnPropNames = Object.getOwnPropertyNames;
16
17
  var __getProtoOf = Object.getPrototypeOf;
17
18
  var __hasOwnProp = Object.prototype.hasOwnProperty;
18
- var __commonJS = (cb, mod) => function() {
19
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
20
- };
19
+ var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
21
20
  var __copyProps = (to, from, except, desc) => {
22
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
23
- key = keys[i];
24
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
25
- get: ((k) => from[k]).bind(null, key),
26
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
27
- });
21
+ if (from && typeof from === "object" || typeof from === "function") {
22
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
23
+ key = keys[i];
24
+ if (!__hasOwnProp.call(to, key) && key !== except) {
25
+ __defProp(to, key, {
26
+ get: ((k) => from[k]).bind(null, key),
27
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
28
+ });
29
+ }
30
+ }
28
31
  }
29
32
  return to;
30
33
  };
@@ -52,11 +55,12 @@ var TableGuesser = class TableGuesser {
52
55
  };
53
56
 
54
57
  //#endregion
55
- //#region ../../node_modules/.pnpm/dayjs@1.11.18/node_modules/dayjs/dayjs.min.js
56
- var require_dayjs_min = /* @__PURE__ */ __commonJS({ "../../node_modules/.pnpm/dayjs@1.11.18/node_modules/dayjs/dayjs.min.js": ((exports, module) => {
58
+ //#region ../../node_modules/.pnpm/dayjs@1.11.19/node_modules/dayjs/dayjs.min.js
59
+ var require_dayjs_min = /* @__PURE__ */ __commonJSMin(((exports, module) => {
57
60
  (function(t, e) {
58
61
  "object" == typeof exports && "undefined" != typeof module ? module.exports = e() : "function" == typeof define && define.amd ? define(e) : (t = "undefined" != typeof globalThis ? globalThis : t || self).dayjs = e();
59
62
  })(exports, (function() {
63
+ "use strict";
60
64
  var t = 1e3, e = 6e4, n = 36e5, r = "millisecond", i = "second", s = "minute", u = "hour", a = "day", o = "week", c = "month", f = "quarter", h = "year", d = "date", l = "Invalid Date", $ = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/, y = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g, M = {
61
65
  name: "en",
62
66
  weekdays: "Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),
@@ -347,7 +351,7 @@ var require_dayjs_min = /* @__PURE__ */ __commonJS({ "../../node_modules/.pnpm/d
347
351
  return O(1e3 * t$1);
348
352
  }, O.en = D[g], O.Ls = D, O.p = {}, O;
349
353
  }));
350
- }) });
354
+ }));
351
355
 
352
356
  //#endregion
353
357
  //#region src/Commands/MakeCommand.ts
@@ -803,16 +807,87 @@ const arquebusConfig = (config$1) => {
803
807
 
804
808
  //#endregion
805
809
  //#region src/Model.ts
806
- var Model = class extends Model$1 {
810
+ var Model = class Model extends mix(UrlRoutable, Model$1) {
807
811
  /**
808
812
  * Retrieve the model for a bound value.
809
813
  *
810
- * @param {any} value
811
- * @param {String|null} field
812
- * @returns
814
+ * @param value
815
+ * @param field
816
+ */
817
+ resolveRouteBinding(value, field) {
818
+ return this.resolveRouteBindingQuery(this.newQuery(), value, field).first();
819
+ }
820
+ /**
821
+ * Retrieve the model for a bound value.
822
+ *
823
+ * @param query
824
+ * @param value
825
+ * @param field
826
+ */
827
+ resolveRouteBindingQuery(query, value, field = null) {
828
+ return query.where(field ?? this.getRouteKeyName(), value);
829
+ }
830
+ /**
831
+ * Retrieve the model for a bound value.
832
+ *
833
+ * @param value
834
+ * @param field
835
+ */
836
+ resolveSoftDeletableRouteBinding(value, field) {
837
+ return this.resolveRouteBindingQuery(this.newQuery(), value, field).withTrashed().first();
838
+ }
839
+ /**
840
+ * Retrieve the child model for a bound value.
841
+ *
842
+ * @param childType
843
+ * @param value
844
+ * @param field
845
+ */
846
+ resolveChildRouteBinding(childType, value, field) {
847
+ return this.resolveChildRouteBindingQuery(childType, value, field).first();
848
+ }
849
+ /**
850
+ * Retrieve the child model for a bound value.
851
+ *
852
+ * @param childType
853
+ * @param value
854
+ * @param field
855
+ */
856
+ resolveSoftDeletableChildRouteBinding(childType, value, field) {
857
+ return this.resolveChildRouteBindingQuery(childType, value, field).withTrashed().first();
858
+ }
859
+ /**
860
+ * Retrieve the child model query for a bound value.
861
+ *
862
+ * @param childType
863
+ * @param value
864
+ * @param field
865
+ */
866
+ resolveChildRouteBindingQuery(childType, value, field) {
867
+ const relationship = this[this.childRouteBindingRelationshipName(childType)]();
868
+ field = field || relationship.getRelated().getRouteKeyName();
869
+ if (relationship instanceof HasManyThrough || relationship instanceof BelongsToMany) field = relationship.getRelated().qualifyColumn(field);
870
+ return relationship instanceof Model ? relationship.resolveRouteBindingQuery(relationship.newQuery(), value, field) : relationship.getRelated().resolveRouteBindingQuery(relationship, value, field);
871
+ }
872
+ /**
873
+ * Retrieve the child route model binding relationship name for the given child type.
874
+ *
875
+ * @param childType
876
+ */
877
+ childRouteBindingRelationshipName(childType) {
878
+ return Str.plural(Str.camel(childType));
879
+ }
880
+ /**
881
+ * Get the value of the model's route key.
882
+ */
883
+ getRouteKey() {
884
+ return this.getAttribute(this.getRouteKeyName());
885
+ }
886
+ /**
887
+ * Get the route key for the model.
813
888
  */
814
- resolveRouteBinding(value, field = null) {
815
- return this.newQuery().where(field ?? "ids", value).firstOrFail();
889
+ getRouteKeyName() {
890
+ return this.getKeyName();
816
891
  }
817
892
  };
818
893
 
@@ -832,6 +907,7 @@ var DatabaseServiceProvider = class extends ServiceProvider {
832
907
  const config$1 = this.app.make("config");
833
908
  const connection = Object.entries(arquebusConfig(config$1.get("database"))).find(([client]) => client === config$1.get("database.default"))?.at(1);
834
909
  if (connection) arquebus.addConnection(connection);
910
+ this.app.singleton("db", () => arquebus.fire());
835
911
  /** Register Musket Commands */
836
912
  this.registerCommands([
837
913
  MigrateCommand,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@h3ravel/database",
3
- "version": "11.4.11",
3
+ "version": "11.5.0-alpha.1",
4
4
  "description": "Modeling data and migration system for H3ravel.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -41,21 +41,20 @@
41
41
  "laravel"
42
42
  ],
43
43
  "dependencies": {
44
- "@h3ravel/arquebus": "^0.6.14",
45
- "@h3ravel/musket": "^0.3.10",
44
+ "@h3ravel/arquebus": "^0.7.6",
45
+ "@h3ravel/musket": "^0.6.10",
46
46
  "sqlite3": "5.1.7"
47
47
  },
48
48
  "peerDependencies": {
49
- "@h3ravel/core": "^1.21.6",
50
- "@h3ravel/shared": "^0.27.7",
51
- "@h3ravel/support": "^0.15.6",
52
- "@h3ravel/filesystem": "^0.4.13"
49
+ "@h3ravel/core": "^1.22.0-alpha.1",
50
+ "@h3ravel/shared": "^0.29.0-alpha.1",
51
+ "@h3ravel/support": "^0.17.0-alpha.1",
52
+ "@h3ravel/filesystem": "^0.4.15-alpha.1"
53
53
  },
54
54
  "devDependencies": {
55
55
  "typescript": "^5.9.2"
56
56
  },
57
57
  "scripts": {
58
- "barrel": "barrelsby --directory src --delete --singleQuotes",
59
58
  "build": "tsdown --config-loader unconfig",
60
59
  "dev": "tsx watch src/index.ts",
61
60
  "start": "node dist/index.js",