@h3ravel/arquebus 2.0.0 → 3.0.0

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.
Files changed (45) hide show
  1. package/bin/index.cjs +580 -451
  2. package/bin/index.js +566 -437
  3. package/dist/browser/index.cjs +54 -51
  4. package/dist/browser/index.d.ts +804 -737
  5. package/dist/browser/index.js +48 -44
  6. package/dist/concerns/index.cjs +152 -143
  7. package/dist/concerns/index.d.ts +849 -776
  8. package/dist/concerns/index.js +143 -133
  9. package/dist/index.cjs +186 -185
  10. package/dist/index.d.ts +856 -782
  11. package/dist/index.js +179 -177
  12. package/dist/inspector/index.cjs +175 -174
  13. package/dist/inspector/index.d.ts +0 -1
  14. package/dist/inspector/index.js +161 -159
  15. package/dist/migrations/index.cjs +183 -182
  16. package/dist/migrations/index.d.ts +799 -727
  17. package/dist/migrations/index.js +172 -170
  18. package/dist/relations/index.cjs +152 -143
  19. package/dist/relations/index.d.ts +835 -762
  20. package/dist/relations/index.js +143 -133
  21. package/dist/seeders/index.cjs +7 -5
  22. package/dist/seeders/index.d.ts +797 -724
  23. package/dist/seeders/index.js +8 -6
  24. package/dist/{migrations/stubs/stubs → stubs}/migration-js.stub +1 -0
  25. package/dist/{migrations/stubs/stubs → stubs}/migration-ts.stub +1 -0
  26. package/dist/{migrations/stubs/stubs → stubs}/migration.create-js.stub +1 -0
  27. package/dist/{migrations/stubs/stubs → stubs}/migration.create-ts.stub +1 -0
  28. package/dist/{migrations/stubs/stubs → stubs}/migration.update-js.stub +1 -0
  29. package/dist/{migrations/stubs/stubs → stubs}/migration.update-ts.stub +1 -0
  30. package/dist/stubs/model-ts.stub +9 -0
  31. package/package.json +31 -31
  32. package/types/builder.ts +1 -1
  33. package/types/database.ts +69 -0
  34. package/types/index.ts +3 -0
  35. package/types/model-builder.ts +132 -0
  36. package/types/query-builder.ts +0 -1
  37. package/types/query-methods.ts +0 -1
  38. package/types/schema.ts +92 -0
  39. package/types/utils.ts +1 -40
  40. package/dist/stubs/stubs/model-ts.stub +0 -5
  41. /package/dist/stubs/{stubs/arquebus.config-js.stub → arquebus.config-js.stub} +0 -0
  42. /package/dist/stubs/{stubs/arquebus.config-ts.stub → arquebus.config-ts.stub} +0 -0
  43. /package/dist/stubs/{stubs/model-js.stub → model-js.stub} +0 -0
  44. /package/dist/stubs/{stubs/seeder-js.stub → seeder-js.stub} +0 -0
  45. /package/dist/stubs/{stubs/seeder-ts.stub → seeder-ts.stub} +0 -0
@@ -1,7 +1,7 @@
1
- import collect$1, { Collection as Collection$1, collect } from "collect.js";
2
- import { assign, camel, dash, diff, flat, isArray, isEmpty, omit, pick, snake, trim } from "radashi";
3
- import advancedFormat from "dayjs/plugin/advancedFormat.js";
4
- import dayjs from "dayjs";
1
+ import { Collection as Collection$1, collect } from "@h3ravel/collect.js";
2
+ import { Arr, Obj, Str } from "@h3ravel/support";
3
+ import * as dayjsModule from "dayjs";
4
+ import * as advancedFormatModule from "dayjs/plugin/advancedFormat.js";
5
5
  import pluralize from "pluralize";
6
6
  //#region \0rolldown/runtime.js
7
7
  var __defProp = Object.defineProperty;
@@ -135,29 +135,33 @@ function compose$1(Base, ...mixins) {
135
135
  }, Base);
136
136
  }
137
137
  //#endregion
138
- //#region src/utils.ts
138
+ //#region src/dayjs.ts
139
+ const dayjs = dayjsModule.default ?? dayjsModule;
140
+ const advancedFormat = advancedFormatModule.default ?? advancedFormatModule;
139
141
  dayjs.extend(advancedFormat);
142
+ //#endregion
143
+ //#region src/utils.ts
140
144
  const now = (format = "YYYY-MM-DD HH:mm:ss") => dayjs().format(format);
141
145
  const getRelationName = (relationMethod) => {
142
- return snake(relationMethod.substring(8));
146
+ return Str.snake(relationMethod.substring(8));
143
147
  };
144
148
  const getScopeName = (scopeMethod) => {
145
- return snake(scopeMethod.substring(5));
149
+ return Str.snake(scopeMethod.substring(5));
146
150
  };
147
151
  const getRelationMethod = (relation) => {
148
- return camel(`relation_${relation}`);
152
+ return Str.camel(`relation_${relation}`);
149
153
  };
150
154
  const getScopeMethod = (scope) => {
151
- return camel(`scope_${scope}`);
155
+ return Str.camel(`scope_${scope}`);
152
156
  };
153
157
  const getAttrMethod = (attr) => {
154
- return camel(`attribute_${attr}`);
158
+ return Str.camel(`attribute_${attr}`);
155
159
  };
156
160
  const getGetterMethod = (attr) => {
157
- return camel(`get_${attr}_attribute`);
161
+ return Str.camel(`get_${attr}_attribute`);
158
162
  };
159
163
  const getSetterMethod = (attr) => {
160
- return camel(`set_${attr}_attribute`);
164
+ return Str.camel(`set_${attr}_attribute`);
161
165
  };
162
166
  const getAttrName = (attrMethod) => {
163
167
  return attrMethod.substring(3, attrMethod.length - 9).toLowerCase();
@@ -176,8 +180,8 @@ const tap = (instance, callback) => {
176
180
  const { compose } = mixin_exports;
177
181
  const flatten = (arr) => arr.flat();
178
182
  const flattenDeep = (arr) => Array.isArray(arr) ? arr.reduce((a, b) => a.concat(flattenDeep(b)), []) : [arr];
179
- const kebabCase = (str) => trim(dash(str.replace(/[^a-zA-Z0-9_-]/g, "-")), "_-");
180
- const snakeCase = (str) => trim(snake(str.replace(/[^a-zA-Z0-9_-]/g, "-")), "_-");
183
+ const kebabCase = (str) => Str.trim(Str.kebab(str.replace(/[^a-zA-Z0-9_-]/g, "-")), "_-").replace(/-+/g, "-");
184
+ const snakeCase = (str) => Str.trim(Str.snake(str.replace(/[^a-zA-Z0-9_-]/g, "-")), "_-").replace(/_+/g, "_");
181
185
  const defineConfig = (config) => {
182
186
  return config;
183
187
  };
@@ -265,8 +269,7 @@ const HasAttributes = (Model) => {
265
269
  return this;
266
270
  }
267
271
  normalizeCastClassResponse(key, value) {
268
- var _value$constructor;
269
- return (value === null || value === void 0 || (_value$constructor = value.constructor) === null || _value$constructor === void 0 ? void 0 : _value$constructor.name) === "Object" ? value : { [key]: value };
272
+ return value?.constructor?.name === "Object" ? value : { [key]: value };
270
273
  }
271
274
  syncOriginal() {
272
275
  this.original = this.getAttributes();
@@ -376,9 +379,9 @@ const HasAttributes = (Model) => {
376
379
  return null;
377
380
  }
378
381
  case "collection": try {
379
- return collect$1(JSON.parse(value));
382
+ return collect(JSON.parse(value));
380
383
  } catch {
381
- return collect$1([]);
384
+ return collect([]);
382
385
  }
383
386
  case "date": return this.asDate(value);
384
387
  case "datetime":
@@ -391,8 +394,8 @@ const HasAttributes = (Model) => {
391
394
  attributesToData() {
392
395
  let attributes = { ...this.attributes };
393
396
  for (const key in attributes) {
394
- if (this.hidden.includes(key)) attributes = omit(attributes, [key]);
395
- if (this.visible.length > 0 && this.visible.includes(key) === false) attributes = omit(attributes, [key]);
397
+ if (this.hidden.includes(key)) attributes = Arr.except(attributes, [key]);
398
+ if (this.visible.length > 0 && this.visible.includes(key) === false) attributes = Arr.except(attributes, [key]);
396
399
  }
397
400
  for (const key of this.getDates()) {
398
401
  if (attributes[key] === void 0) continue;
@@ -447,7 +450,7 @@ const HasAttributes = (Model) => {
447
450
  }
448
451
  hasCast(key, types = []) {
449
452
  if (key in this.casts) {
450
- types = flat(types);
453
+ types = flatten(types);
451
454
  return types.length > 0 ? types.includes(this.getCastType(key)) : true;
452
455
  }
453
456
  return false;
@@ -554,7 +557,7 @@ var ModelNotFoundError = class extends BaseError {
554
557
  }
555
558
  setModel(model, ids = []) {
556
559
  this.model = model;
557
- this.ids = isArray(ids) ? ids : [ids];
560
+ this.ids = Array.isArray(ids) ? ids : [ids];
558
561
  this.message = `No query results for model [${model}]`;
559
562
  if (this.ids.length > 0) this.message += " " + this.ids.join(", ");
560
563
  else this.message += ".";
@@ -582,7 +585,7 @@ const HasRelations = (Instance) => {
582
585
  return this;
583
586
  }
584
587
  unsetRelation(relation) {
585
- this.relations = omit(this.relations, [relation]);
588
+ this.relations = Arr.except(this.relations, [relation]);
586
589
  return this;
587
590
  }
588
591
  relationLoaded(relation) {
@@ -615,9 +618,9 @@ const HasRelations = (Instance) => {
615
618
  else if (frame.includes("at ")) {
616
619
  const match = frame.match(/at\s+([^(]+)\s*\(/);
617
620
  functionName = match ? match[1].trim() : "anonymous";
618
- if (functionName === null || functionName === void 0 ? void 0 : functionName.includes(".")) functionName = functionName.split(".").pop();
621
+ if (functionName?.includes(".")) functionName = functionName.split(".").pop();
619
622
  }
620
- functionName = functionName === null || functionName === void 0 ? void 0 : functionName.replace(/^</, "").replace(/>$/, "").trim();
623
+ functionName = functionName?.replace(/^</, "").replace(/>$/, "").trim();
621
624
  return getRelationName(functionName || "anonymous");
622
625
  }
623
626
  joiningTable(related, instance = null) {
@@ -724,7 +727,7 @@ const HidesAttributes = (Model) => {
724
727
  makeVisible(...keys) {
725
728
  const visible = flattenDeep(keys);
726
729
  if (this.visible.length > 0) this.visible = [...this.visible, ...visible];
727
- this.hidden = diff(this.hidden, visible);
730
+ this.hidden = collect(this.hidden).diff(visible).all();
728
731
  return this;
729
732
  }
730
733
  makeHidden(key, ...keys) {
@@ -880,7 +883,7 @@ var Model = class Model extends BaseModel {
880
883
  return this;
881
884
  }
882
885
  toData() {
883
- return assign(this.attributesToData(), this.relationsToData());
886
+ return Obj.deepMerge(this.attributesToData(), this.relationsToData());
884
887
  }
885
888
  toJSON() {
886
889
  return this.toData();
@@ -951,7 +954,7 @@ var Pivot = class extends Model {
951
954
  };
952
955
  //#endregion
953
956
  //#region src/browser/collection.ts
954
- var Collection = class Collection extends Collection$1 {
957
+ var Collection = class extends Collection$1 {
955
958
  mapThen(callback) {
956
959
  return Promise.all(this.map(callback));
957
960
  }
@@ -970,20 +973,21 @@ var Collection = class Collection extends Collection$1 {
970
973
  diff(items) {
971
974
  const diff = new this.constructor();
972
975
  const dictionary = this.getDictionary(items);
973
- this.items.map((item) => {
976
+ this.all().map((item) => {
974
977
  if (dictionary[item.getKey()] == null) diff.add(item);
975
978
  });
976
979
  return diff;
977
980
  }
978
- except(keys) {
979
- const dictionary = omit(this.getDictionary(), keys);
981
+ except(...keys) {
982
+ const values = keys.length === 1 && Array.isArray(keys[0]) ? keys[0].map(String) : keys.map(String);
983
+ const dictionary = Arr.except(this.getDictionary(), values);
980
984
  return new this.constructor(Object.values(dictionary));
981
985
  }
982
986
  intersect(items) {
983
987
  const intersect = new this.constructor();
984
- if (isEmpty(items)) return intersect;
988
+ if (Arr.isEmpty(items)) return intersect;
985
989
  const dictionary = this.getDictionary(items);
986
- for (const item of this.items) if (dictionary[item.getKey()] != null) intersect.add(item);
990
+ for (const item of this.all()) if (dictionary[item.getKey()] != null) intersect.add(item);
987
991
  return intersect;
988
992
  }
989
993
  unique(key, _strict = false) {
@@ -992,14 +996,14 @@ var Collection = class Collection extends Collection$1 {
992
996
  }
993
997
  find(key, defaultValue = null) {
994
998
  if (key instanceof Model) key = key.getKey();
995
- if (isArray(key)) {
999
+ if (Array.isArray(key)) {
996
1000
  if (this.isEmpty()) return new this.constructor();
997
1001
  return this.whereIn(this.first().getKeyName(), key);
998
1002
  }
999
- collect(this.items).first((model) => {
1003
+ collect(this.all()).first((model) => {
1000
1004
  return model.getKey() == key;
1001
1005
  });
1002
- return this.items.filter((model) => {
1006
+ return this.all().filter((model) => {
1003
1007
  return model.getKey() == key;
1004
1008
  })[0] || defaultValue;
1005
1009
  }
@@ -1018,15 +1022,15 @@ var Collection = class Collection extends Collection$1 {
1018
1022
  item.append(attributes);
1019
1023
  });
1020
1024
  }
1021
- only(keys) {
1022
- if (keys === null) return new Collection(this.items);
1023
- const dictionary = pick(this.getDictionary(), keys);
1025
+ only(...keys) {
1026
+ const values = keys.length === 1 && Array.isArray(keys[0]) ? keys[0].map(String) : keys.map(String);
1027
+ const dictionary = Arr.select(this.getDictionary(), values);
1024
1028
  return new this.constructor(Object.values(dictionary));
1025
1029
  }
1026
1030
  getDictionary(items) {
1027
- items = !items ? this.items : items;
1031
+ const values = !items ? this.all() : items instanceof Collection$1 ? items.all() : items;
1028
1032
  const dictionary = {};
1029
- items.map((value) => {
1033
+ values.map((value) => {
1030
1034
  dictionary[value.getKey()] = value;
1031
1035
  });
1032
1036
  return dictionary;
@@ -1041,8 +1045,8 @@ var Collection = class Collection extends Collection$1 {
1041
1045
  return JSON.stringify(this.toData(), ...args);
1042
1046
  }
1043
1047
  [Symbol.iterator] = () => {
1044
- const items = this.items;
1045
- const length = this.items.length;
1048
+ const items = this.all();
1049
+ const length = items.length;
1046
1050
  let n = 0;
1047
1051
  return { next() {
1048
1052
  return n < length ? {
@@ -1165,7 +1169,7 @@ var pivot_default = Pivot;
1165
1169
  const make = (model, data, options = {}) => {
1166
1170
  const { paginated } = options;
1167
1171
  if (paginated) return new Paginator(data.data.map((item) => model.make(item)), data.total, data.per_page, data.current_page);
1168
- if (isArray(data)) return new Collection(data.map((item) => model.make(item)));
1172
+ if (Array.isArray(data)) return new Collection(data.map((item) => model.make(item)));
1169
1173
  return model.make(data);
1170
1174
  };
1171
1175
  const makeCollection = (model, data) => new Collection(data.map((item) => model.make(item)));