@h3ravel/arquebus 0.6.9 → 0.6.11

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.
@@ -1,3 +0,0 @@
1
- export { default as Seeder } from './seeder'
2
- export { default as SeederRunner } from './runner'
3
- export { default as SeederCreator } from './seeder-creator'
@@ -1,101 +0,0 @@
1
- import type QueryBuilder from 'src/query-builder'
2
- import type Seeder from './seeder'
3
- import type { TBaseConfig } from 'types/container'
4
- import { access } from 'node:fs/promises'
5
- import type { arquebus } from 'src'
6
- import path from 'path'
7
-
8
- async function glob (folderPath: string): Promise<string[]> {
9
- const { default: escalade } = await import('escalade')
10
- const entries: string[] = []
11
- const root = folderPath
12
- await escalade(root, async (dir, names) => {
13
- await Promise.all(
14
- names.map(async (name) => {
15
- const p = path.join(dir, name)
16
- try {
17
- await access(p)
18
- if (p.endsWith('.js') || p.endsWith('.ts')) entries.push(p)
19
- } catch {
20
- /** */
21
- }
22
- }),
23
- )
24
- return ''
25
- })
26
- return entries
27
- }
28
-
29
- export class SeederRunner {
30
- resolver: typeof arquebus
31
- connection!: TBaseConfig['client']
32
- paths: string[] = []
33
-
34
- constructor(resolver: typeof arquebus) {
35
- this.resolver = resolver
36
- }
37
-
38
- path (p: string): void {
39
- this.paths = Array.from(new Set([...this.paths, p]))
40
- }
41
-
42
- getPaths (): string[] {
43
- return this.paths
44
- }
45
-
46
- resolveConnection (connection?: TBaseConfig['client']): QueryBuilder {
47
- const name = connection || this.connection || 'default'
48
- // If the resolver has no connection manager entry, attempt to autoload config
49
- const instance: any = (this.resolver as any).getInstance?.() ?? null
50
- const hasConn = !!instance?.connections?.[name]
51
- if (!hasConn) {
52
- // Attempt autoload; do not throw if empty
53
- this.resolver
54
- .autoLoad()
55
- .catch(() => {
56
- /** noop */
57
- })
58
- }
59
- return this.resolver.fire(name)
60
- }
61
-
62
- setConnection (connection: TBaseConfig['client']): this {
63
- this.connection = connection
64
- return this
65
- }
66
-
67
- async getSeederFiles (paths: string[]): Promise<string[]> {
68
- const files: string[] = []
69
- for (const p of paths) {
70
- if (p.endsWith('.js') || p.endsWith('.ts')) {
71
- files.push(p)
72
- continue
73
- }
74
- files.push(...(await glob(p)))
75
- }
76
- return files
77
- }
78
-
79
- async resolvePath (filePath: string): Promise<Seeder | null> {
80
- try {
81
- const mod = await import(filePath)
82
- const instance = new (mod.default ?? mod.Seeder)()
83
- return instance as Seeder
84
- } catch {
85
- return null
86
- }
87
- }
88
-
89
- async run (paths: string[], connection?: TBaseConfig['client']): Promise<void> {
90
- const files = await this.getSeederFiles(paths)
91
- const conn = this.resolveConnection(connection)
92
- for (const file of files) {
93
- const seeder = await this.resolvePath(file)
94
- if (seeder && typeof seeder.run === 'function') {
95
- await seeder.run(conn)
96
- }
97
- }
98
- }
99
- }
100
-
101
- export default SeederRunner
@@ -1,42 +0,0 @@
1
- import { mkdir, readFile, writeFile } from 'node:fs/promises'
2
-
3
- import { dirname } from 'node:path'
4
- import { fileURLToPath } from 'node:url'
5
- import path from 'path'
6
-
7
- export class SeederCreator {
8
- constructor(private customStubPath?: string) { }
9
-
10
- async create (dir: string, name: string, type: 'js' | 'ts' = 'js') {
11
- await mkdir(dir, { recursive: true })
12
-
13
- const stubPath = this.getStubPath(type)
14
- let stub = await readFile(stubPath, 'utf-8')
15
- stub = stub.replace(/{{ name }}/g, name)
16
-
17
- const filePath = path.join(dir, `${name}.${type}`)
18
- await writeFile(filePath, stub)
19
-
20
- return filePath
21
- }
22
-
23
- getStubPath (type: 'js' | 'ts') {
24
- if (this.customStubPath) return path.join(this.customStubPath, `seeder-${type}.stub`)
25
- const __dirname = this.getDirname(import.meta as any)
26
- return path.join(__dirname, 'stubs', `seeder-${type}.stub`)
27
- }
28
-
29
- getDirname (meta: ImportMeta | null) {
30
- if (typeof __dirname !== 'undefined') {
31
- // CJS build
32
- return __dirname
33
- }
34
- if (meta && meta.url) {
35
- // ESM build
36
- return dirname(fileURLToPath(meta.url))
37
- }
38
- throw new Error('Unable to determine dirname')
39
- }
40
- }
41
-
42
- export default SeederCreator
@@ -1,10 +0,0 @@
1
- import type QueryBuilder from 'src/query-builder'
2
-
3
- export abstract class Seeder {
4
- /**
5
- * Run the database seeds
6
- */
7
- abstract run(connection: QueryBuilder): Promise<void>
8
- }
9
-
10
- export default Seeder
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes