@h3ravel/arquebus 0.7.2 → 0.7.3

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
@@ -29,6 +29,12 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
29
29
  }) : target, mod));
30
30
 
31
31
  //#endregion
32
+ let node_fs_promises = require("node:fs/promises");
33
+ node_fs_promises = __toESM(node_fs_promises);
34
+ let path = require("path");
35
+ path = __toESM(path);
36
+ let node_path = require("node:path");
37
+ let node_url = require("node:url");
32
38
  let radashi = require("radashi");
33
39
  let dayjs_plugin_advancedFormat_js = require("dayjs/plugin/advancedFormat.js");
34
40
  dayjs_plugin_advancedFormat_js = __toESM(dayjs_plugin_advancedFormat_js);
@@ -38,19 +44,118 @@ let collect_js = require("collect.js");
38
44
  collect_js = __toESM(collect_js);
39
45
  let knex = require("knex");
40
46
  knex = __toESM(knex);
41
- let path = require("path");
42
- path = __toESM(path);
43
47
  let fs = require("fs");
44
48
  let __h3ravel_shared = require("@h3ravel/shared");
45
49
  let pluralize = require("pluralize");
46
50
  pluralize = __toESM(pluralize);
47
- let node_fs_promises = require("node:fs/promises");
48
- node_fs_promises = __toESM(node_fs_promises);
49
51
  let resolve_from = require("resolve-from");
50
52
  resolve_from = __toESM(resolve_from);
51
- let node_path = require("node:path");
52
- let node_url = require("node:url");
53
53
 
54
+ //#region src/seeders/seeder.ts
55
+ var Seeder = class {};
56
+ var seeder_default = Seeder;
57
+
58
+ //#endregion
59
+ //#region src/seeders/runner.ts
60
+ async function glob$1(folderPath) {
61
+ const { default: escalade } = await import("escalade");
62
+ const entries = [];
63
+ await escalade(folderPath, async (dir, names) => {
64
+ await Promise.all(names.map(async (name) => {
65
+ const p = path.default.join(dir, name);
66
+ try {
67
+ await (0, node_fs_promises.access)(p);
68
+ if (p.endsWith(".js") || p.endsWith(".ts")) entries.push(p);
69
+ } catch {}
70
+ }));
71
+ return "";
72
+ });
73
+ return entries;
74
+ }
75
+ var SeederRunner = class {
76
+ resolver;
77
+ connection;
78
+ paths = [];
79
+ constructor(resolver) {
80
+ this.resolver = resolver;
81
+ }
82
+ path(p) {
83
+ this.paths = Array.from(new Set([...this.paths, p]));
84
+ }
85
+ getPaths() {
86
+ return this.paths;
87
+ }
88
+ resolveConnection(connection) {
89
+ var _getInstance, _ref, _instance$connections;
90
+ const name = connection || this.connection || "default";
91
+ const instance = ((_getInstance = (_ref = this.resolver).getInstance) === null || _getInstance === void 0 ? void 0 : _getInstance.call(_ref)) ?? null;
92
+ if (!!!(instance === null || instance === void 0 || (_instance$connections = instance.connections) === null || _instance$connections === void 0 ? void 0 : _instance$connections[name])) this.resolver.autoLoad().catch(() => {
93
+ /** noop */
94
+ });
95
+ return this.resolver.fire(name);
96
+ }
97
+ setConnection(connection) {
98
+ this.connection = connection;
99
+ return this;
100
+ }
101
+ async getSeederFiles(paths) {
102
+ const files = [];
103
+ for (const p of paths) {
104
+ if (p.endsWith(".js") || p.endsWith(".ts")) {
105
+ files.push(p);
106
+ continue;
107
+ }
108
+ files.push(...await glob$1(p));
109
+ }
110
+ return files;
111
+ }
112
+ async resolvePath(filePath) {
113
+ try {
114
+ const mod = await import(filePath);
115
+ return new (mod.default ?? mod.Seeder)();
116
+ } catch {
117
+ return null;
118
+ }
119
+ }
120
+ async run(paths, connection) {
121
+ const files = await this.getSeederFiles(paths);
122
+ const conn = this.resolveConnection(connection);
123
+ for (const file of files) {
124
+ const seeder = await this.resolvePath(file);
125
+ if (seeder && typeof seeder.run === "function") await seeder.run(conn);
126
+ }
127
+ }
128
+ };
129
+ var runner_default = SeederRunner;
130
+
131
+ //#endregion
132
+ //#region src/seeders/seeder-creator.ts
133
+ var SeederCreator = class {
134
+ constructor(customStubPath) {
135
+ this.customStubPath = customStubPath;
136
+ }
137
+ async create(dir, name, type = "js") {
138
+ await (0, node_fs_promises.mkdir)(dir, { recursive: true });
139
+ let stub = await (0, node_fs_promises.readFile)(this.getStubPath(type), "utf-8");
140
+ stub = stub.replace(/{{ name }}/g, name);
141
+ const filePath = path.default.join(dir, `${name}.${type}`);
142
+ await (0, node_fs_promises.writeFile)(filePath, stub);
143
+ return filePath;
144
+ }
145
+ getStubPath(type) {
146
+ if (this.customStubPath) return path.default.join(this.customStubPath, `seeder-${type}.stub`);
147
+ const __dirname$1 = this.getDirname({});
148
+ return path.default.join(__dirname$1, "stubs", `seeder-${type}.stub`);
149
+ }
150
+ getDirname(meta) {
151
+ if (typeof __dirname !== "undefined") return __dirname;
152
+ if (meta && meta.url) return (0, node_path.dirname)((0, node_url.fileURLToPath)(meta.url));
153
+ throw new Error("Unable to determine dirname");
154
+ }
155
+ };
156
+ var seeder_creator_default = SeederCreator;
157
+
158
+ //#endregion
54
159
  //#region src/casts/attribute.ts
55
160
  var Attribute = class Attribute {
56
161
  get;
@@ -4974,7 +5079,7 @@ var SchemaInspector = class {
4974
5079
 
4975
5080
  //#endregion
4976
5081
  //#region src/migrations/migrator.ts
4977
- async function glob$1(folderPath) {
5082
+ async function glob(folderPath) {
4978
5083
  const files = await node_fs_promises.default.readdir(folderPath);
4979
5084
  const allFiles = [];
4980
5085
  for (const file of files) {
@@ -4982,7 +5087,7 @@ async function glob$1(folderPath) {
4982
5087
  const stats = await node_fs_promises.default.stat(filePath);
4983
5088
  if (stats.isFile()) allFiles.push(filePath);
4984
5089
  else if (stats.isDirectory()) {
4985
- const subFiles = await glob$1(filePath);
5090
+ const subFiles = await glob(filePath);
4986
5091
  allFiles.push(...subFiles);
4987
5092
  }
4988
5093
  }
@@ -5125,7 +5230,7 @@ var Migrator = class {
5125
5230
  files.push(p);
5126
5231
  continue;
5127
5232
  }
5128
- files.push(...await glob$1(p));
5233
+ files.push(...await glob(p));
5129
5234
  }
5130
5235
  return files.filter(Boolean).reduce((result, file) => {
5131
5236
  result[this.getMigrationName(file)] = file;
@@ -5352,111 +5457,6 @@ var Migration = class extends Inference {
5352
5457
  };
5353
5458
  var migration_default = Migration;
5354
5459
 
5355
- //#endregion
5356
- //#region src/seeders/seeder.ts
5357
- var Seeder = class {};
5358
- var seeder_default = Seeder;
5359
-
5360
- //#endregion
5361
- //#region src/seeders/runner.ts
5362
- async function glob(folderPath) {
5363
- const { default: escalade } = await import("escalade");
5364
- const entries = [];
5365
- await escalade(folderPath, async (dir, names) => {
5366
- await Promise.all(names.map(async (name) => {
5367
- const p = path.default.join(dir, name);
5368
- try {
5369
- await (0, node_fs_promises.access)(p);
5370
- if (p.endsWith(".js") || p.endsWith(".ts")) entries.push(p);
5371
- } catch {}
5372
- }));
5373
- return "";
5374
- });
5375
- return entries;
5376
- }
5377
- var SeederRunner = class {
5378
- resolver;
5379
- connection;
5380
- paths = [];
5381
- constructor(resolver) {
5382
- this.resolver = resolver;
5383
- }
5384
- path(p) {
5385
- this.paths = Array.from(new Set([...this.paths, p]));
5386
- }
5387
- getPaths() {
5388
- return this.paths;
5389
- }
5390
- resolveConnection(connection) {
5391
- var _getInstance, _ref, _instance$connections;
5392
- const name = connection || this.connection || "default";
5393
- const instance = ((_getInstance = (_ref = this.resolver).getInstance) === null || _getInstance === void 0 ? void 0 : _getInstance.call(_ref)) ?? null;
5394
- if (!!!(instance === null || instance === void 0 || (_instance$connections = instance.connections) === null || _instance$connections === void 0 ? void 0 : _instance$connections[name])) this.resolver.autoLoad().catch(() => {
5395
- /** noop */
5396
- });
5397
- return this.resolver.fire(name);
5398
- }
5399
- setConnection(connection) {
5400
- this.connection = connection;
5401
- return this;
5402
- }
5403
- async getSeederFiles(paths) {
5404
- const files = [];
5405
- for (const p of paths) {
5406
- if (p.endsWith(".js") || p.endsWith(".ts")) {
5407
- files.push(p);
5408
- continue;
5409
- }
5410
- files.push(...await glob(p));
5411
- }
5412
- return files;
5413
- }
5414
- async resolvePath(filePath) {
5415
- try {
5416
- const mod = await import(filePath);
5417
- return new (mod.default ?? mod.Seeder)();
5418
- } catch {
5419
- return null;
5420
- }
5421
- }
5422
- async run(paths, connection) {
5423
- const files = await this.getSeederFiles(paths);
5424
- const conn = this.resolveConnection(connection);
5425
- for (const file of files) {
5426
- const seeder = await this.resolvePath(file);
5427
- if (seeder && typeof seeder.run === "function") await seeder.run(conn);
5428
- }
5429
- }
5430
- };
5431
- var runner_default = SeederRunner;
5432
-
5433
- //#endregion
5434
- //#region src/seeders/seeder-creator.ts
5435
- var SeederCreator = class {
5436
- constructor(customStubPath) {
5437
- this.customStubPath = customStubPath;
5438
- }
5439
- async create(dir, name, type = "js") {
5440
- await (0, node_fs_promises.mkdir)(dir, { recursive: true });
5441
- let stub = await (0, node_fs_promises.readFile)(this.getStubPath(type), "utf-8");
5442
- stub = stub.replace(/{{ name }}/g, name);
5443
- const filePath = path.default.join(dir, `${name}.${type}`);
5444
- await (0, node_fs_promises.writeFile)(filePath, stub);
5445
- return filePath;
5446
- }
5447
- getStubPath(type) {
5448
- if (this.customStubPath) return path.default.join(this.customStubPath, `seeder-${type}.stub`);
5449
- const __dirname$1 = this.getDirname({});
5450
- return path.default.join(__dirname$1, "stubs", `seeder-${type}.stub`);
5451
- }
5452
- getDirname(meta) {
5453
- if (typeof __dirname !== "undefined") return __dirname;
5454
- if (meta && meta.url) return (0, node_path.dirname)((0, node_url.fileURLToPath)(meta.url));
5455
- throw new Error("Unable to determine dirname");
5456
- }
5457
- };
5458
- var seeder_creator_default = SeederCreator;
5459
-
5460
5460
  //#endregion
5461
5461
  //#region src/pivot.ts
5462
5462
  var pivot_default = Pivot;
@@ -5628,11 +5628,11 @@ var soft_deletes_default = softDeletes;
5628
5628
  const make = (model, data, options = {}) => {
5629
5629
  const { paginated } = options;
5630
5630
  if (paginated) return new paginator_default(data.data.map((item) => model.make(item)), data.total, data.per_page, data.current_page);
5631
- if (Array.isArray(data)) return new collection_default(data.map((item) => model.make(item)));
5631
+ if ((0, radashi.isArray)(data)) return new collection_default(data.map((item) => model.make(item)));
5632
5632
  return model.make(data);
5633
5633
  };
5634
5634
  const makeCollection = (model, data) => new collection_default(data.map((item) => model.make(item)));
5635
- const makePaginator = (model, data, _) => new paginator_default(data.data.map((item) => model.make(item)), data.total, data.per_page, data.current_page);
5635
+ const makePaginator = (model, data) => new paginator_default(data.data.map((item) => model.make(item)), data.total, data.per_page, data.current_page);
5636
5636
 
5637
5637
  //#endregion
5638
5638
  exports.Attribute = attribute_default;
package/dist/index.d.ts CHANGED
@@ -667,6 +667,7 @@ interface IModel {
667
667
  setAppends(appends: string[]): this;
668
668
  append(key: string | string[]): this;
669
669
  getRelation<T$1 extends Model$1>(relation: string): T$1 | ICollection<T$1> | null | undefined;
670
+ getRelation<T$1 extends Model$1, IsCollection extends boolean = false>(relation: string): IsCollection extends true ? ICollection<T$1> | undefined : T$1 | null | undefined;
670
671
  setRelation<T$1 extends Model$1>(relation: string, value: T$1 | ICollection<T$1> | null): this;
671
672
  unsetRelation(relation: string): this;
672
673
  relationLoaded(relation: string): boolean;
@@ -1939,7 +1940,10 @@ declare const BaseModel$1: (new (...args: any[]) => any) & {
1939
1940
  uniqueIds(): never[];
1940
1941
  setUniqueIds(): void;
1941
1942
  });
1942
- getRelation: <T$1 extends Model$1>(relation: string) => T$1 | ICollection<T$1> | null | undefined;
1943
+ getRelation: {
1944
+ <T$1 extends Model$1>(relation: string): T$1 | ICollection<T$1> | null | undefined;
1945
+ <T$1 extends Model$1, IsCollection extends boolean = false>(relation: string): IsCollection extends true ? ICollection<T$1> | undefined : T$1 | null | undefined;
1946
+ };
1943
1947
  setRelation: <T$1 extends Model$1>(relation: string, value: T$1 | ICollection<T$1> | null) => (new () => IModel) & IModel & {
1944
1948
  [x: string]: any;
1945
1949
  timestamps: boolean;
@@ -4647,16 +4651,16 @@ declare class Collection$2<I$1 extends Model$2> extends Collection$1<I$1> {
4647
4651
  append(attributes: any): this;
4648
4652
  only(keys: any[]): any;
4649
4653
  getDictionary(items?: ICollection<any> | any[]): TGeneric;
4650
- toData(): (I$1 | {
4654
+ toData(): ({
4651
4655
  [x: string]: any;
4652
4656
  [x: number]: any;
4653
4657
  [x: symbol]: any;
4654
- })[];
4655
- toJSON(): (I$1 | {
4658
+ } | I$1)[];
4659
+ toJSON(): ({
4656
4660
  [x: string]: any;
4657
4661
  [x: number]: any;
4658
4662
  [x: symbol]: any;
4659
- })[];
4663
+ } | I$1)[];
4660
4664
  toJson(...args: any[]): string;
4661
4665
  [Symbol.iterator]: () => Iterator<I$1>;
4662
4666
  }
@@ -4734,6 +4738,38 @@ declare class Pivot$1 extends Model$2 {
4734
4738
  hasTimestampAttributes(this: any, attributes?: TGeneric | null): boolean;
4735
4739
  }
4736
4740
  //#endregion
4741
+ //#region src/seeders/seeder.d.ts
4742
+ declare abstract class Seeder {
4743
+ /**
4744
+ * Run the database seeds
4745
+ */
4746
+ abstract run(connection: QueryBuilder): Promise<void>;
4747
+ }
4748
+ //#endregion
4749
+ //#region src/seeders/runner.d.ts
4750
+ declare class SeederRunner {
4751
+ resolver: typeof arquebus;
4752
+ connection: TBaseConfig['client'];
4753
+ paths: string[];
4754
+ constructor(resolver: typeof arquebus);
4755
+ path(p: string): void;
4756
+ getPaths(): string[];
4757
+ resolveConnection(connection?: TBaseConfig['client']): QueryBuilder;
4758
+ setConnection(connection: TBaseConfig['client']): this;
4759
+ getSeederFiles(paths: string[]): Promise<string[]>;
4760
+ resolvePath(filePath: string): Promise<Seeder | null>;
4761
+ run(paths: string[], connection?: TBaseConfig['client']): Promise<void>;
4762
+ }
4763
+ //#endregion
4764
+ //#region src/seeders/seeder-creator.d.ts
4765
+ declare class SeederCreator {
4766
+ private customStubPath?;
4767
+ constructor(customStubPath?: string | undefined);
4768
+ create(dir: string, name: string, type?: 'js' | 'ts'): Promise<string>;
4769
+ getStubPath(type: 'js' | 'ts'): string;
4770
+ getDirname(meta: ImportMeta | null): string;
4771
+ }
4772
+ //#endregion
4737
4773
  //#region src/casts/attribute.d.ts
4738
4774
  interface IAttribute {
4739
4775
  get?: null | (<A extends TGeneric>(value: string, attr: A) => string);
@@ -4961,38 +4997,6 @@ declare class Migrate {
4961
4997
  }>;
4962
4998
  }
4963
4999
  //#endregion
4964
- //#region src/seeders/seeder.d.ts
4965
- declare abstract class Seeder {
4966
- /**
4967
- * Run the database seeds
4968
- */
4969
- abstract run(connection: QueryBuilder): Promise<void>;
4970
- }
4971
- //#endregion
4972
- //#region src/seeders/runner.d.ts
4973
- declare class SeederRunner {
4974
- resolver: typeof arquebus;
4975
- connection: TBaseConfig['client'];
4976
- paths: string[];
4977
- constructor(resolver: typeof arquebus);
4978
- path(p: string): void;
4979
- getPaths(): string[];
4980
- resolveConnection(connection?: TBaseConfig['client']): QueryBuilder;
4981
- setConnection(connection: TBaseConfig['client']): this;
4982
- getSeederFiles(paths: string[]): Promise<string[]>;
4983
- resolvePath(filePath: string): Promise<Seeder | null>;
4984
- run(paths: string[], connection?: TBaseConfig['client']): Promise<void>;
4985
- }
4986
- //#endregion
4987
- //#region src/seeders/seeder-creator.d.ts
4988
- declare class SeederCreator {
4989
- private customStubPath?;
4990
- constructor(customStubPath?: string | undefined);
4991
- create(dir: string, name: string, type?: 'js' | 'ts'): Promise<string>;
4992
- getStubPath(type: 'js' | 'ts'): string;
4993
- getDirname(meta: ImportMeta | null): string;
4994
- }
4995
- //#endregion
4996
5000
  //#region src/soft-deletes.d.ts
4997
5001
  declare const softDeletes: <TBase extends MixinConstructor>(Model: TBase) => {
4998
5002
  new (...args: any[]): {
@@ -5120,10 +5124,15 @@ declare const snakeCase: (str: string) => string;
5120
5124
  declare const defineConfig: (config: TConfig) => XGeneric<TConfig>;
5121
5125
  //#endregion
5122
5126
  //#region src/index.d.ts
5123
- declare const make: <M extends Model$1 | typeof Model$1>(model: M, data: TGeneric, options?: {
5124
- paginated: IPaginatorParams | boolean;
5125
- }) => any;
5126
- declare const makeCollection: <M extends Model$1 | typeof Model$1>(model: M, data: TGeneric) => Collection<Model$1 | Model$2>;
5127
- declare const makePaginator: <M extends Model$1 | typeof Model$1>(model: M, data: TGeneric, _: any) => Paginator<Model$1, IPaginatorParams>;
5127
+ interface IMake {
5128
+ <T$1 extends Model$1>(model: T$1, data: TGeneric): T$1;
5129
+ <T$1 extends Model$1>(model: T$1, data: Array<TGeneric>): Collection<T$1>;
5130
+ <T$1 extends Model$1>(model: T$1, data: TGeneric, options: {
5131
+ paginated?: IPaginatorParams;
5132
+ }): Paginator<T$1>;
5133
+ }
5134
+ declare const make: IMake;
5135
+ declare const makeCollection: <T$1 extends Model$1>(model: T$1, data: TGeneric) => Collection<Model$1 | Model$2>;
5136
+ declare const makePaginator: <T$1 extends Model$1>(model: T$1, data: TGeneric) => Paginator<T$1, IPaginatorParams>;
5128
5137
  //#endregion
5129
5138
  export { Attribute, Builder, CastsAttributes, Collection, HasUniqueIds, InvalidArgumentError, Migrate, Migration, Model$1 as Model, ModelNotFoundError, Paginator, Pivot, QueryBuilder, RelationNotFoundError, Scope, Seeder, SeederCreator, SeederRunner, softDeletes as SoftDeletes, arquebus, compose, defineConfig, flatten, flattenDeep, getAttrMethod, getAttrName, getGetterMethod, getRelationMethod, getRelationName, getScopeMethod, getScopeName, getSetterMethod, kebabCase, make, makeCollection, makePaginator, now, snakeCase, tap };