|
1 |
| -import { collect } from '@stacksjs/collections' |
2 |
| -import { loop } from '@stacksjs/utils' |
3 | 1 | import { PrismaClient } from '@prisma/client'
|
4 | 2 | import { ResultAsync } from '@stacksjs/error-handling'
|
5 |
| -import type { Result } from '@stacksjs/types' |
| 3 | +import type { FactoryOptions } from '@stacksjs/types' |
6 | 4 |
|
7 | 5 | const prisma = new PrismaClient()
|
8 | 6 |
|
9 |
| -class Factory { |
10 |
| - private factory: string |
11 |
| - private noOfItems = 5 |
12 |
| - private items: object = [] |
13 |
| - private columns: object = [] |
| 7 | +const factory = (options: FactoryOptions) => { |
| 8 | + return { |
| 9 | + make: async () => { |
| 10 | + if (options.count) |
| 11 | + |
| 12 | + return ResultAsync.fromPromise( |
| 13 | + await prisma[options.name].createMany({ |
| 14 | + data: options.items, |
| 15 | + skipDuplicates: true |
| 16 | + }), |
| 17 | + () => new Error('Failed to create the data'), |
| 18 | + ) |
| 19 | + }, |
| 20 | + |
| 21 | + create: async () => { |
| 22 | + if (options.count) |
| 23 | + |
| 24 | + return ResultAsync.fromPromise( |
| 25 | + await prisma[options.name].createMany({ |
| 26 | + data: options.items, |
| 27 | + skipDuplicates: false |
| 28 | + }), |
| 29 | + () => new Error('Failed to create the data'), |
| 30 | + ) |
| 31 | + }, |
14 | 32 |
|
15 |
| - constructor(factory: string) { |
16 |
| - this.factory = factory |
17 |
| - } |
18 |
| - |
19 |
| - define(params: any) { |
20 |
| - this.columns = params |
21 |
| - return this |
22 |
| - } |
23 |
| - |
24 |
| - count(count = 5) { |
25 |
| - this.noOfItems = count |
26 |
| - return this |
27 |
| - } |
28 |
| - |
29 |
| - async make(count = 0): Promise<Result<Object, Error>> { |
30 |
| - if (count) |
31 |
| - this.noOfItems = count |
32 |
| - |
33 |
| - this.initiateColumns() |
34 |
| - |
35 |
| - return ResultAsync.fromPromise( |
36 |
| - await prisma[this.factory].createMany( |
37 |
| - this.items, |
38 |
| - true, |
39 |
| - ), |
40 |
| - () => new Error('Failed to create the data'), |
41 |
| - ) |
42 |
| - } |
43 |
| - |
44 |
| - async create(count = 0): Promise<Result<Object, Error>> { |
45 |
| - if (count) |
46 |
| - this.noOfItems = count |
47 |
| - |
48 |
| - this.initiateColumns() |
49 |
| - |
50 |
| - return ResultAsync.fromPromise( |
51 |
| - await prisma[this.factory].createMany( |
52 |
| - this.items, |
53 |
| - false, |
54 |
| - ), |
55 |
| - () => new Error('Failed to create the data'), |
56 |
| - ) |
57 |
| - } |
58 |
| - |
59 |
| - initiateColumns() { |
60 |
| - const items = collect() |
61 |
| - |
62 |
| - loop(this.noOfItems, () => { |
63 |
| - items.push(this.columns) |
64 |
| - }) |
65 |
| - |
66 |
| - this.items = items |
67 | 33 | }
|
68 | 34 | }
|
69 | 35 |
|
70 |
| -export { Factory as factory, Factory } |
| 36 | +export { factory } |
0 commit comments