-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
60 lines (49 loc) · 1.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const { PrismaClient } = require('@prisma/client')
const { v4: uuidv4 } = require('uuid')
const prisma = new PrismaClient({
datasources: {
db: {
url: process.env.DATABASE_URL + "&application_name=$ docs_simplecrud_node-prisma",
},
},
})
const main = async () => {
const customerIds = Array(10).fill().map(() => ({ id: uuidv4() }))
const accountValues = Array(10).fill().map((_, index) => ({
id: uuidv4(),
customer_id: customerIds[index].id,
balance: Math.floor(Math.random() * 1000)
}))
const insertCustomerRows = await prisma.customer.createMany({
data: customerIds
})
console.log('Customer rows inserted.', insertCustomerRows)
const insertAccountRows = await prisma.account.createMany({
data: accountValues
})
console.log('Account rows inserted.', insertAccountRows)
console.log('Initial Account row values:\n', await prisma.account.findMany())
const updateRows = await prisma.account.updateMany({
where: {
balance: {
gt: 100
}
},
data: {
balance: {
decrement: 5
}
}
})
console.log('Account rows updated.', updateRows)
console.log('Updated Account row values:\n', await prisma.account.findMany())
const deleteAllRows = await prisma.customer.deleteMany()
console.log('All Customer rows deleted.', deleteAllRows)
}
main()
.catch((e) => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})