https://github.com/itsdillon/prepare-array-for-prisma
A utility that will take regular arrays from forms and adapt them to work with prisma. It also works with nested arrays.
https://github.com/itsdillon/prepare-array-for-prisma
Last synced: over 1 year ago
JSON representation
A utility that will take regular arrays from forms and adapt them to work with prisma. It also works with nested arrays.
- Host: GitHub
- URL: https://github.com/itsdillon/prepare-array-for-prisma
- Owner: itsdillon
- License: mit
- Created: 2021-07-10T23:11:14.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-08-11T17:58:04.000Z (almost 5 years ago)
- Last Synced: 2025-04-04T20:59:27.553Z (over 1 year ago)
- Language: TypeScript
- Size: 147 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Prepare Array For Prisma
A utility that will take regular arrays from forms and adapt them to work with prisma. It also works with nested arrays.
## Get Started
`yarn add @creatorsneverdie/prepare-array-for-prisma`
`import {prepareArrayField} from "@creatorsneverdie/prepare-array-for-prisma"`
## How To
`prepareArrayField()` takes 4 parameters:
1. Form values as `any[]`
2. Initial database data as `any[]`
3. Mapper as `((item: any, initial?: any) => any)`
4. Options as `{ removedItemsMethod: 'disconnect' | 'delete'}`
You use the function like this:
```ts
const value = [ {name: 1}, {id: 2, name: 3} ]
const initial = [ {id: 2, name: 2}, {id: 3} ]
const mappedArray = prepareArrayField(value, initial, (item) => ({
...item,
}))
```
You can also set options object for handling removing elements. You can choose to either delete the element from the database completely, or disconnect. By default the option is set to `disconnect`:
```ts
const mappedArray = prepareArrayField(value, initial, (item) => ({
...item,
}), {removedItemsMethod: "delete" })
```
Then inside a prisma operation:
```ts
const updateUser = await prisma.project.update({
where: {
id: 1
},
data: {
title: 'test project update',
users: mappedArray
}
})
```