@h3ravel/arquebus 0.5.0 → 0.6.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.
Files changed (45) hide show
  1. package/README.md +20 -1
  2. package/bin/index.cjs +197 -145
  3. package/bin/index.js +178 -95
  4. package/bin/seeders-8GJzfIIN.js +3 -0
  5. package/bin/seeders-ByeSoCAQ.cjs +131 -0
  6. package/bin/seeders-CltigymO.js +79 -0
  7. package/bin/seeders-_xJ6VGVS.cjs +3 -0
  8. package/dist/browser/index.cjs +9 -9
  9. package/dist/browser/index.d.cts +8 -8
  10. package/dist/browser/index.d.ts +8 -8
  11. package/dist/browser/index.js +9 -9
  12. package/dist/index.cjs +252 -121
  13. package/dist/index.d.cts +41 -9
  14. package/dist/index.d.ts +41 -9
  15. package/dist/index.js +245 -119
  16. package/dist/inspector/index.cjs +105 -33
  17. package/dist/inspector/index.js +102 -33
  18. package/dist/migrations/index.cjs +144 -126
  19. package/dist/migrations/index.d.cts +11 -11
  20. package/dist/migrations/index.d.ts +11 -11
  21. package/dist/migrations/index.js +148 -130
  22. package/dist/migrations/stubs/migration-js.stub +1 -1
  23. package/dist/migrations/stubs/migration-ts.stub +1 -1
  24. package/dist/migrations/stubs/migration.create-js.stub +5 -5
  25. package/dist/migrations/stubs/migration.create-ts.stub +5 -5
  26. package/dist/migrations/stubs/migration.update-js.stub +2 -2
  27. package/dist/migrations/stubs/migration.update-ts.stub +3 -3
  28. package/dist/seeders/index.cjs +141 -0
  29. package/dist/seeders/index.d.cts +4766 -0
  30. package/dist/seeders/index.d.ts +4766 -0
  31. package/dist/seeders/index.js +118 -0
  32. package/dist/seeders/index.ts +3 -0
  33. package/dist/seeders/runner.ts +101 -0
  34. package/dist/seeders/seeder-creator.ts +42 -0
  35. package/dist/seeders/seeder.ts +10 -0
  36. package/dist/stubs/seeder-js.stub +13 -0
  37. package/dist/stubs/seeder-ts.stub +9 -0
  38. package/package.json +14 -3
  39. package/types/builder.ts +153 -80
  40. package/types/container.ts +79 -66
  41. package/types/generics.ts +75 -37
  42. package/types/modeling.ts +213 -158
  43. package/types/query-builder.ts +221 -191
  44. package/types/query-methods.ts +145 -109
  45. package/types/utils.ts +64 -56
package/dist/index.d.ts CHANGED
@@ -98,7 +98,7 @@ declare class Builder<M extends Model$1 = Model$1, R = IModel | ICollection<M>>
98
98
  enforceOrderBy(this: any): void;
99
99
  clone(this: any): any;
100
100
  forPage(this: any, page: number, perPage?: number): any;
101
- insert(...args: Parameters<typeof (void 0).query.insert>): Promise<any>;
101
+ insert(...args: Parameters<typeof this.query.insert>): Promise<any>;
102
102
  update<T extends TGeneric>(values: T): Promise<any>;
103
103
  increment(column: string, amount?: number, extra?: {}): Promise<any>;
104
104
  decrement(column: string, amount?: number, extra?: {}): Promise<any>;
@@ -437,7 +437,7 @@ declare class QueryBuilder<M extends Model$1 = Model$1, R = M[] | M> extends Inf
437
437
  chunk(count: number, callback: TFunction): Promise<boolean>;
438
438
  paginate<F extends IPaginatorParams>(this: any, page: number | undefined, perPage: number | undefined, _pageName: string, _page: number): Promise<IPaginator<M, F>>;
439
439
  forPage(this: any, page?: number, perPage?: number): any;
440
- toSQL(...args: Parameters<typeof (void 0).connector.toSQL>): Knex.Sql;
440
+ toSQL(...args: any[]): Knex.Sql;
441
441
  count(column: string): Promise<number>;
442
442
  min(column: string): Promise<number>;
443
443
  max(column: string): Promise<number>;
@@ -445,9 +445,9 @@ declare class QueryBuilder<M extends Model$1 = Model$1, R = M[] | M> extends Inf
445
445
  avg(column: string): Promise<number>;
446
446
  clone(): IQueryBuilder<M, R>;
447
447
  delete(): Promise<number | boolean>;
448
- insert(...args: Parameters<typeof (void 0).connector.insert>): Promise<unknown>;
449
- update(...args: Parameters<typeof (void 0).connector.update>): Promise<number>;
450
- destroy(...args: Parameters<typeof (void 0).connector.destroy>): Promise<number>;
448
+ insert(...args: Parameters<typeof this.connector.insert>): Promise<unknown>;
449
+ update(...args: Parameters<typeof this.connector.update>): Promise<number>;
450
+ destroy(...args: Parameters<typeof this.connector.destroy>): Promise<number>;
451
451
  get _statements(): IStatement[] & any[];
452
452
  get _single(): any;
453
453
  get from(): Knex.Table<any, any> & Knex.Table<any, any[]>;
@@ -473,7 +473,7 @@ declare class Relation$1 {
473
473
  getEager(): any;
474
474
  get(columns?: string | string[]): Promise<any>;
475
475
  first(columns?: string[]): Promise<any>;
476
- paginate(...args: any[]): Promise<any>;
476
+ paginate(...args: unknown[]): Promise<any>;
477
477
  count(...args: any[]): Promise<any>;
478
478
  toSql(): string;
479
479
  addConstraints(): void;
@@ -1044,7 +1044,7 @@ declare class arquebus<M extends Model$1 = Model$1> {
1044
1044
  static getConnectorFactory(): typeof Knex$1;
1045
1045
  static addConnection(config: TConfig | TBaseConfig, name?: string): void;
1046
1046
  static beginTransaction(connection?: null): Promise<Knex$1.Knex.Transaction<any, any[]>>;
1047
- static transaction(callback: TFunction, connection?: null): Promise<Knex$1.Knex.Transaction<any, any[]>> | undefined;
1047
+ static transaction<A, R>(callback: TFunction<A[], R>, connection?: null): Promise<Knex$1.Knex.Transaction<any, any[]>> | undefined;
1048
1048
  static table(name: string, connection?: null): IQueryBuilder<Model$1, Model$1 | Model$1[]>;
1049
1049
  static schema(connection?: null): SchemaBuilder;
1050
1050
  static destroyAll(): Promise<void>;
@@ -4629,7 +4629,7 @@ type ReturnTypeOfMethod<T, K extends keyof T> = T[K] extends ((...args: any[]) =
4629
4629
  type SnakeToCamelCase<S extends string> = S extends `${infer T}_${infer U}` ? `${T}${Capitalize<SnakeToCamelCase<U>>}` : S;
4630
4630
  type CamelToSnakeCase<S extends string> = S extends `${infer T}${infer U}` ? U extends Uncapitalize<U> ? `${Uncapitalize<T>}${CamelToSnakeCase<U>}` : `${Uncapitalize<T>}_${CamelToSnakeCase<U>}` : S;
4631
4631
  type FunctionPropertyNames<T> = { [K in keyof T]: T[K] extends ((...args: any[]) => any) ? K : never }[keyof T];
4632
- type RelationNames<T> = FunctionPropertyNames<T> extends infer R ? R extends `relation${infer P}` ? P extends ('sToData' | 'loaded') ? never : CamelToSnakeCase<P> : never : never;
4632
+ type RelationNames<T> = FunctionPropertyNames<T> extends infer R ? R extends `relation${infer P}` ? P extends 'sToData' | 'loaded' ? never : CamelToSnakeCase<P> : never : never;
4633
4633
  type MixinConstructor<T = TGeneric> = new (...args: any[]) => T;
4634
4634
  //#endregion
4635
4635
  //#region src/browser/collection.d.ts
@@ -4961,6 +4961,38 @@ declare class Migrate {
4961
4961
  }>;
4962
4962
  }
4963
4963
  //#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
4964
4996
  //#region src/soft-deletes.d.ts
4965
4997
  declare const softDeletes: <TBase extends MixinConstructor>(Model: TBase) => {
4966
4998
  new (...args: any[]): {
@@ -5094,4 +5126,4 @@ declare const make: <M extends Model$1 | typeof Model$1>(model: M, data: TGeneri
5094
5126
  declare const makeCollection: <M extends Model$1 | typeof Model$1>(model: M, data: TGeneric) => Collection<Model$1 | Model$2>;
5095
5127
  declare const makePaginator: <M extends Model$1 | typeof Model$1>(model: M, data: TGeneric, _: any) => Paginator<Model$1, IPaginatorParams>;
5096
5128
  //#endregion
5097
- export { Attribute, Builder, CastsAttributes, Collection, HasUniqueIds, InvalidArgumentError, Migrate, Migration, Model$1 as Model, ModelNotFoundError, Paginator, Pivot, QueryBuilder, RelationNotFoundError, Scope, softDeletes as SoftDeletes, arquebus, compose, defineConfig, flatten, flattenDeep, getAttrMethod, getAttrName, getGetterMethod, getRelationMethod, getRelationName, getScopeMethod, getScopeName, getSetterMethod, kebabCase, make, makeCollection, makePaginator, now, snakeCase, tap };
5129
+ 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 };
package/dist/index.js CHANGED
@@ -6,12 +6,14 @@ import collect$1, { Collection, collect } from "collect.js";
6
6
  import Knex$1 from "knex";
7
7
  import path from "path";
8
8
  import { existsSync } from "fs";
9
- import pluralize from "pluralize";
10
- import { Logger, TaskManager } from "@h3ravel/shared";
11
- import fs from "node:fs/promises";
12
9
  import { access } from "fs/promises";
13
10
  import escalade from "escalade/sync";
14
11
  import resolveFrom from "resolve-from";
12
+ import pluralize from "pluralize";
13
+ import { Logger, TaskManager } from "@h3ravel/shared";
14
+ import fs, { access as access$1, mkdir, readFile, writeFile } from "node:fs/promises";
15
+ import { dirname } from "node:path";
16
+ import { fileURLToPath } from "node:url";
15
17
 
16
18
  //#region rolldown:runtime
17
19
  var __defProp = Object.defineProperty;
@@ -97,7 +99,7 @@ var InvalidArgumentError = class extends BaseError {};
97
99
  var mixin_exports = /* @__PURE__ */ __export({ compose: () => compose$1 });
98
100
  /**
99
101
  * Compose function that merges multiple classes and mixins
100
- *
102
+ *
101
103
  * @example
102
104
  * const SomePlugin = <TBase extends new (...args: any[]) => TGeneric> (Base: TBase) => {
103
105
  * return class extends Base {
@@ -134,10 +136,10 @@ var mixin_exports = /* @__PURE__ */ __export({ compose: () => compose$1 });
134
136
  * console.log(user.pluginMethod('w')) // "plugin"
135
137
  * console.log(user.pluginMethod()) // "plugin"
136
138
  * console.log(user.relationPosts()) // "hasMany Posts"
137
- *
138
- * @param Base
139
- * @param mixins
140
- * @returns
139
+ *
140
+ * @param Base
141
+ * @param mixins
142
+ * @returns
141
143
  */
142
144
  function compose$1(Base, ...mixins) {
143
145
  /**
@@ -198,10 +200,10 @@ const getAttrName = (attrMethod) => {
198
200
  };
199
201
  /**
200
202
  * Tap into a model a collection instance
201
- *
202
- * @param instance
203
- * @param callback
204
- * @returns
203
+ *
204
+ * @param instance
205
+ * @param callback
206
+ * @returns
205
207
  */
206
208
  const tap = (instance, callback) => {
207
209
  const result = callback(instance);
@@ -1656,6 +1658,59 @@ var QueryBuilder = class QueryBuilder extends Inference$2 {
1656
1658
  };
1657
1659
  var query_builder_default = QueryBuilder;
1658
1660
 
1661
+ //#endregion
1662
+ //#region src/cli/utils.ts
1663
+ const join = path.join;
1664
+ var Utils = class {
1665
+ /**
1666
+ * Wraps text with chalk
1667
+ *
1668
+ * @param txt
1669
+ * @param color
1670
+ * @returns
1671
+ */
1672
+ static textFormat(txt, color) {
1673
+ return String(txt).split(":").map((e, i, a) => i == 0 && a.length > 1 ? color(" " + e + ": ") : e).join("");
1674
+ }
1675
+ static findModulePkg(moduleId, cwd) {
1676
+ const parts = moduleId.replace(/\\/g, "/").split("/");
1677
+ let packageName = "";
1678
+ if (parts.length > 0 && parts[0][0] === "@") packageName += parts.shift() + "/";
1679
+ packageName += parts.shift();
1680
+ const packageJson = path.join(packageName, "package.json");
1681
+ const resolved = resolveFrom.silent(cwd ?? process.cwd(), packageJson);
1682
+ if (!resolved) return;
1683
+ return path.join(path.dirname(resolved), parts.join("/"));
1684
+ }
1685
+ static async getMigrationPaths(cwd, migrator, defaultPath, path$1) {
1686
+ if (path$1) return [join(cwd, path$1)];
1687
+ return [...migrator.getPaths(), join(cwd, defaultPath)];
1688
+ }
1689
+ /**
1690
+ * Check if file exists
1691
+ *
1692
+ * @param path
1693
+ * @returns
1694
+ */
1695
+ static async fileExists(path$1) {
1696
+ try {
1697
+ await access(path$1);
1698
+ return true;
1699
+ } catch {
1700
+ return false;
1701
+ }
1702
+ }
1703
+ static findUpConfig(cwd, name, extensions) {
1704
+ return escalade(cwd, (_dir, names) => {
1705
+ for (const ext of extensions) {
1706
+ const filename = `${name}.${ext}`;
1707
+ if (names.includes(filename)) return filename;
1708
+ }
1709
+ return false;
1710
+ });
1711
+ }
1712
+ };
1713
+
1659
1714
  //#endregion
1660
1715
  //#region src/arquebus.ts
1661
1716
  var arquebus = class arquebus {
@@ -1677,22 +1732,22 @@ var arquebus = class arquebus {
1677
1732
  return this.instance;
1678
1733
  }
1679
1734
  /**
1680
- * Initialize a new database connection
1681
- *
1682
- * @returns
1735
+ * Initialize a new database connection
1736
+ *
1737
+ * @returns
1683
1738
  */
1684
1739
  static fire(connection = null) {
1685
1740
  return this.getInstance().getConnection(connection);
1686
1741
  }
1687
1742
  /**
1688
- * Initialize a new database connection
1689
- *
1743
+ * Initialize a new database connection
1744
+ *
1690
1745
  * This is an alias of `arquebus.fire()` and will be removed in the future
1691
- *
1746
+ *
1692
1747
  * @deprecated since version 0.3.0
1693
1748
  * @alias fire
1694
- *
1695
- * @returns
1749
+ *
1750
+ * @returns
1696
1751
  */
1697
1752
  static connection(connection = null) {
1698
1753
  return this.fire(connection);
@@ -1729,11 +1784,12 @@ var arquebus = class arquebus {
1729
1784
  }
1730
1785
  getConnection(name = null) {
1731
1786
  name = name || "default";
1732
- if (this.manager[name] === void 0) {
1733
- const queryBuilder = new query_builder_default(this.connections[name], arquebus.getConnectorFactory());
1734
- this.manager[name] = queryBuilder;
1787
+ const resolvedName = this.connections[name] ? name : "default";
1788
+ if (this.manager[resolvedName] === void 0) {
1789
+ const queryBuilder = new query_builder_default(this.connections[resolvedName], arquebus.getConnectorFactory());
1790
+ this.manager[resolvedName] = queryBuilder;
1735
1791
  }
1736
- return this.manager[name];
1792
+ return this.manager[resolvedName];
1737
1793
  }
1738
1794
  addConnection(config, name = "default") {
1739
1795
  this.connections[name] = {
@@ -1750,13 +1806,13 @@ var arquebus = class arquebus {
1750
1806
  }
1751
1807
  /**
1752
1808
  * Autoload the config file
1753
- *
1754
- * @param addConnection
1809
+ *
1810
+ * @param addConnection
1755
1811
  * @default true
1756
- * If set to `false` we will no attempt add the connection, we
1812
+ * If set to `false` we will no attempt add the connection, we
1757
1813
  * will just go ahead and return the config
1758
- *
1759
- * @returns
1814
+ *
1815
+ * @returns
1760
1816
  */
1761
1817
  static async autoLoad(addConnection = true) {
1762
1818
  let config;
@@ -1773,6 +1829,23 @@ var arquebus = class arquebus {
1773
1829
  if (addConnection) instance.addConnection(config, config.client);
1774
1830
  return config;
1775
1831
  } else throw new Error("arquebus.config.ts found in production without build step");
1832
+ const candidateDirs = [
1833
+ process.cwd(),
1834
+ path.join(process.cwd(), "test", "cli"),
1835
+ path.join(process.cwd(), "test")
1836
+ ];
1837
+ for (const dir of candidateDirs) {
1838
+ const found = Utils.findUpConfig(dir, "arquebus.config", [
1839
+ "js",
1840
+ "ts",
1841
+ "cjs"
1842
+ ]);
1843
+ if (found) if (!found.endsWith(".ts") || process.env.NODE_ENV !== "production") {
1844
+ config = (await import(found)).default;
1845
+ if (addConnection) instance.addConnection(config, config.client);
1846
+ return config;
1847
+ } else throw new Error("arquebus.config.ts found in production without build step");
1848
+ }
1776
1849
  return {};
1777
1850
  }
1778
1851
  beginTransaction(connection = null) {
@@ -4957,7 +5030,7 @@ var SchemaInspector = class {
4957
5030
 
4958
5031
  //#endregion
4959
5032
  //#region src/migrations/migrator.ts
4960
- async function glob(folderPath) {
5033
+ async function glob$1(folderPath) {
4961
5034
  const files = await fs.readdir(folderPath);
4962
5035
  const allFiles = [];
4963
5036
  for (const file of files) {
@@ -4965,7 +5038,7 @@ async function glob(folderPath) {
4965
5038
  const stats = await fs.stat(filePath);
4966
5039
  if (stats.isFile()) allFiles.push(filePath);
4967
5040
  else if (stats.isDirectory()) {
4968
- const subFiles = await glob(filePath);
5041
+ const subFiles = await glob$1(filePath);
4969
5042
  allFiles.push(...subFiles);
4970
5043
  }
4971
5044
  }
@@ -5059,9 +5132,9 @@ var Migrator = class {
5059
5132
  }
5060
5133
  /**
5061
5134
  * Drop all tables and re-run all migrations
5062
- *
5063
- * @param paths
5064
- * @param options
5135
+ *
5136
+ * @param paths
5137
+ * @param options
5065
5138
  */
5066
5139
  async fresh(paths, options) {
5067
5140
  /** Initialise connections */
@@ -5108,7 +5181,7 @@ var Migrator = class {
5108
5181
  files.push(p);
5109
5182
  continue;
5110
5183
  }
5111
- files.push(...await glob(p));
5184
+ files.push(...await glob$1(p));
5112
5185
  }
5113
5186
  return files.filter(Boolean).reduce((result, file) => {
5114
5187
  result[this.getMigrationName(file)] = file;
@@ -5153,59 +5226,6 @@ var Migrator = class {
5153
5226
  };
5154
5227
  var migrator_default = Migrator;
5155
5228
 
5156
- //#endregion
5157
- //#region src/cli/utils.ts
5158
- const join = path.join;
5159
- var Utils = class {
5160
- /**
5161
- * Wraps text with chalk
5162
- *
5163
- * @param txt
5164
- * @param color
5165
- * @returns
5166
- */
5167
- static textFormat(txt, color) {
5168
- return String(txt).split(":").map((e, i, a) => i == 0 && a.length > 1 ? color(" " + e + ": ") : e).join("");
5169
- }
5170
- static findModulePkg(moduleId, cwd) {
5171
- const parts = moduleId.replace(/\\/g, "/").split("/");
5172
- let packageName = "";
5173
- if (parts.length > 0 && parts[0][0] === "@") packageName += parts.shift() + "/";
5174
- packageName += parts.shift();
5175
- const packageJson = path.join(packageName, "package.json");
5176
- const resolved = resolveFrom.silent(cwd ?? process.cwd(), packageJson);
5177
- if (!resolved) return;
5178
- return path.join(path.dirname(resolved), parts.join("/"));
5179
- }
5180
- static async getMigrationPaths(cwd, migrator, defaultPath, path$1) {
5181
- if (path$1) return [join(cwd, path$1)];
5182
- return [...migrator.getPaths(), join(cwd, defaultPath)];
5183
- }
5184
- /**
5185
- * Check if file exists
5186
- *
5187
- * @param path
5188
- * @returns
5189
- */
5190
- static async fileExists(path$1) {
5191
- try {
5192
- await access(path$1);
5193
- return true;
5194
- } catch {
5195
- return false;
5196
- }
5197
- }
5198
- static findUpConfig(cwd, name, extensions) {
5199
- return escalade(cwd, (_dir, names) => {
5200
- for (const ext of extensions) {
5201
- const filename = `${name}.${ext}`;
5202
- if (names.includes(filename)) return filename;
5203
- }
5204
- return false;
5205
- });
5206
- }
5207
- };
5208
-
5209
5229
  //#endregion
5210
5230
  //#region src/migrate.ts
5211
5231
  var Migrate = class {
@@ -5217,10 +5237,10 @@ var Migrate = class {
5217
5237
  }
5218
5238
  /**
5219
5239
  * Runs all pending migrations
5220
- *
5221
- * @param config
5222
- * @param options
5223
- * @param destroyAll
5240
+ *
5241
+ * @param config
5242
+ * @param options
5243
+ * @param destroyAll
5224
5244
  */
5225
5245
  async run(config, options = {}, destroyAll = false) {
5226
5246
  const { arquebus: arquebus$1, migrator } = await this.setupConnection(config);
@@ -5234,10 +5254,10 @@ var Migrate = class {
5234
5254
  }
5235
5255
  /**
5236
5256
  * Rollback the last migration
5237
- *
5238
- * @param config
5239
- * @param options
5240
- * @param destroyAll
5257
+ *
5258
+ * @param config
5259
+ * @param options
5260
+ * @param destroyAll
5241
5261
  */
5242
5262
  async rollback(config, options = {}, destroyAll = false) {
5243
5263
  const { arquebus: arquebus$1, migrator } = await this.setupConnection(config);
@@ -5251,10 +5271,10 @@ var Migrate = class {
5251
5271
  }
5252
5272
  /**
5253
5273
  * Rollback all database migrations
5254
- *
5255
- * @param config
5256
- * @param options
5257
- * @param destroyAll
5274
+ *
5275
+ * @param config
5276
+ * @param options
5277
+ * @param destroyAll
5258
5278
  */
5259
5279
  async reset(config, options = {}, destroyAll = false) {
5260
5280
  const { arquebus: arquebus$1, migrator } = await this.setupConnection(config);
@@ -5269,10 +5289,10 @@ var Migrate = class {
5269
5289
  }
5270
5290
  /**
5271
5291
  * Reset and re-run all migrations
5272
- *
5273
- * @param config
5274
- * @param options
5275
- * @param destroyAll
5292
+ *
5293
+ * @param config
5294
+ * @param options
5295
+ * @param destroyAll
5276
5296
  */
5277
5297
  async refresh(config, options = {}, destroyAll = false) {
5278
5298
  await this.reset(config, Object.assign({}, options, { quiet: true }), false);
@@ -5281,10 +5301,10 @@ var Migrate = class {
5281
5301
  }
5282
5302
  /**
5283
5303
  * Drop all tables and re-run all migrations
5284
- *
5285
- * @param config
5286
- * @param options
5287
- * @param destroyAll
5304
+ *
5305
+ * @param config
5306
+ * @param options
5307
+ * @param destroyAll
5288
5308
  */
5289
5309
  async fresh(config, options = {}, destroyAll = false) {
5290
5310
  const { arquebus: arquebus$1, migrator } = await this.setupConnection(config);
@@ -5294,8 +5314,8 @@ var Migrate = class {
5294
5314
  }
5295
5315
  /**
5296
5316
  * Prepares the database for migration
5297
- *
5298
- * @param migrator
5317
+ *
5318
+ * @param migrator
5299
5319
  */
5300
5320
  async prepareDatabase(migrator) {
5301
5321
  if (!await migrator.repositoryExists()) {
@@ -5307,11 +5327,11 @@ var Migrate = class {
5307
5327
  }
5308
5328
  /**
5309
5329
  * Check the status of available migrations
5310
- *
5311
- * @param config
5312
- * @param options
5313
- * @param destroyAll
5314
- * @returns
5330
+ *
5331
+ * @param config
5332
+ * @param options
5333
+ * @param destroyAll
5334
+ * @returns
5315
5335
  */
5316
5336
  async status(config, options = {}, destroyAll = false) {
5317
5337
  const { arquebus: arquebus$1, migrator } = await this.setupConnection(config);
@@ -5338,9 +5358,9 @@ var Migrate = class {
5338
5358
  }
5339
5359
  /**
5340
5360
  * Setup the database connection
5341
- *
5342
- * @param config
5343
- * @returns
5361
+ *
5362
+ * @param config
5363
+ * @returns
5344
5364
  */
5345
5365
  async setupConnection(config) {
5346
5366
  var _config$migrations;
@@ -5372,6 +5392,112 @@ var Migration = class extends Inference {
5372
5392
  };
5373
5393
  var migration_default = Migration;
5374
5394
 
5395
+ //#endregion
5396
+ //#region src/seeders/seeder.ts
5397
+ var Seeder = class {};
5398
+ var seeder_default = Seeder;
5399
+
5400
+ //#endregion
5401
+ //#region src/seeders/runner.ts
5402
+ async function glob(folderPath) {
5403
+ const { default: escalade$1 } = await import("escalade");
5404
+ const entries = [];
5405
+ await escalade$1(folderPath, async (dir, names) => {
5406
+ await Promise.all(names.map(async (name) => {
5407
+ const p = path.join(dir, name);
5408
+ try {
5409
+ await access$1(p);
5410
+ if (p.endsWith(".js") || p.endsWith(".ts")) entries.push(p);
5411
+ } catch {}
5412
+ }));
5413
+ return "";
5414
+ });
5415
+ return entries;
5416
+ }
5417
+ var SeederRunner = class {
5418
+ resolver;
5419
+ connection;
5420
+ paths = [];
5421
+ constructor(resolver) {
5422
+ this.resolver = resolver;
5423
+ }
5424
+ path(p) {
5425
+ this.paths = Array.from(new Set([...this.paths, p]));
5426
+ }
5427
+ getPaths() {
5428
+ return this.paths;
5429
+ }
5430
+ resolveConnection(connection) {
5431
+ var _getInstance, _ref, _instance$connections;
5432
+ const name = connection || this.connection || "default";
5433
+ const instance = ((_getInstance = (_ref = this.resolver).getInstance) === null || _getInstance === void 0 ? void 0 : _getInstance.call(_ref)) ?? null;
5434
+ if (!!!(instance === null || instance === void 0 || (_instance$connections = instance.connections) === null || _instance$connections === void 0 ? void 0 : _instance$connections[name])) this.resolver.autoLoad().catch(() => {
5435
+ /** noop */
5436
+ });
5437
+ return this.resolver.fire(name);
5438
+ }
5439
+ setConnection(connection) {
5440
+ this.connection = connection;
5441
+ return this;
5442
+ }
5443
+ async getSeederFiles(paths) {
5444
+ const files = [];
5445
+ for (const p of paths) {
5446
+ if (p.endsWith(".js") || p.endsWith(".ts")) {
5447
+ files.push(p);
5448
+ continue;
5449
+ }
5450
+ files.push(...await glob(p));
5451
+ }
5452
+ return files;
5453
+ }
5454
+ async resolvePath(filePath) {
5455
+ try {
5456
+ const mod = await import(filePath);
5457
+ return new (mod.default ?? mod.Seeder)();
5458
+ } catch {
5459
+ return null;
5460
+ }
5461
+ }
5462
+ async run(paths, connection) {
5463
+ const files = await this.getSeederFiles(paths);
5464
+ const conn = this.resolveConnection(connection);
5465
+ for (const file of files) {
5466
+ const seeder = await this.resolvePath(file);
5467
+ if (seeder && typeof seeder.run === "function") await seeder.run(conn);
5468
+ }
5469
+ }
5470
+ };
5471
+ var runner_default = SeederRunner;
5472
+
5473
+ //#endregion
5474
+ //#region src/seeders/seeder-creator.ts
5475
+ var SeederCreator = class {
5476
+ constructor(customStubPath) {
5477
+ this.customStubPath = customStubPath;
5478
+ }
5479
+ async create(dir, name, type = "js") {
5480
+ await mkdir(dir, { recursive: true });
5481
+ const stubPath = this.getStubPath(type);
5482
+ let stub = await readFile(stubPath, "utf-8");
5483
+ stub = stub.replace(/{{ name }}/g, name);
5484
+ const filePath = path.join(dir, `${name}.${type}`);
5485
+ await writeFile(filePath, stub);
5486
+ return filePath;
5487
+ }
5488
+ getStubPath(type) {
5489
+ if (this.customStubPath) return path.join(this.customStubPath, `seeder-${type}.stub`);
5490
+ const __dirname$1 = this.getDirname(import.meta);
5491
+ return path.join(__dirname$1, "stubs", `seeder-${type}.stub`);
5492
+ }
5493
+ getDirname(meta) {
5494
+ if (typeof __dirname !== "undefined") return __dirname;
5495
+ if (meta && meta.url) return dirname(fileURLToPath(meta.url));
5496
+ throw new Error("Unable to determine dirname");
5497
+ }
5498
+ };
5499
+ var seeder_creator_default = SeederCreator;
5500
+
5375
5501
  //#endregion
5376
5502
  //#region src/pivot.ts
5377
5503
  var pivot_default = Pivot;
@@ -5550,4 +5676,4 @@ const makeCollection = (model, data) => new collection_default(data.map((item) =
5550
5676
  const makePaginator = (model, data, _) => new paginator_default(data.data.map((item) => model.make(item)), data.total, data.per_page, data.current_page);
5551
5677
 
5552
5678
  //#endregion
5553
- export { attribute_default as Attribute, builder_default as Builder, casts_attributes_default as CastsAttributes, collection_default as Collection, has_unique_ids_default as HasUniqueIds, InvalidArgumentError, Migrate, migration_default as Migration, model_default as Model, ModelNotFoundError, paginator_default as Paginator, pivot_default as Pivot, query_builder_default as QueryBuilder, RelationNotFoundError, scope_default as Scope, soft_deletes_default as SoftDeletes, arquebus_default as arquebus, compose, defineConfig, flatten, flattenDeep, getAttrMethod, getAttrName, getGetterMethod, getRelationMethod, getRelationName, getScopeMethod, getScopeName, getSetterMethod, kebabCase, make, makeCollection, makePaginator, now, snakeCase, tap };
5679
+ export { attribute_default as Attribute, builder_default as Builder, casts_attributes_default as CastsAttributes, collection_default as Collection, has_unique_ids_default as HasUniqueIds, InvalidArgumentError, Migrate, migration_default as Migration, model_default as Model, ModelNotFoundError, paginator_default as Paginator, pivot_default as Pivot, query_builder_default as QueryBuilder, RelationNotFoundError, scope_default as Scope, seeder_default as Seeder, seeder_creator_default as SeederCreator, runner_default as SeederRunner, soft_deletes_default as SoftDeletes, arquebus_default as arquebus, compose, defineConfig, flatten, flattenDeep, getAttrMethod, getAttrName, getGetterMethod, getRelationMethod, getRelationName, getScopeMethod, getScopeName, getSetterMethod, kebabCase, make, makeCollection, makePaginator, now, snakeCase, tap };