Skip to content

Commit 1bcf516

Browse files
committed
chore: wip
1 parent 835eabe commit 1bcf516

File tree

2 files changed

+34
-61
lines changed

2 files changed

+34
-61
lines changed
Lines changed: 27 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,36 @@
1-
import { collect } from '@stacksjs/collections'
2-
import { loop } from '@stacksjs/utils'
31
import { PrismaClient } from '@prisma/client'
42
import { ResultAsync } from '@stacksjs/error-handling'
5-
import type { Result } from '@stacksjs/types'
3+
import type { FactoryOptions } from '@stacksjs/types'
64

75
const prisma = new PrismaClient()
86

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+
},
1432

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
6733
}
6834
}
6935

70-
export { Factory as factory, Factory }
36+
export { factory }

.stacks/core/types/src/database.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@ export type DatabaseClient = PrismaClient
55
export interface DatabaseOptions {
66
client: DatabaseClient
77
}
8+
9+
export interface FactoryOptions {
10+
name: string;
11+
count?: number;
12+
items: object;
13+
columns: object;
14+
}

0 commit comments

Comments
 (0)