-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<!--- Please provide a general summary of your changes in the title above --> # Pull Request type <!-- Please try to limit your pull request to one type; submit multiple pull requests if needed. --> Please check the type of change your PR introduces: - [x] Bugfix - [ ] Feature - [ ] Code style update (formatting, renaming) - [ ] Refactoring (no functional changes, no API changes) - [ ] Build-related changes - [ ] Documentation content changes - [ ] Other (please describe): ## What is the current behavior? <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. --> Issue Number: IN-955 ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - - - ## Does this introduce a breaking change? - [ ] Yes - [ ] No <!-- If this does introduce a breaking change, please describe the impact and migration path for existing applications below. --> ## Other information <!-- Any other information that is important to this PR, such as screenshots of how the component looks before and after the change. --> --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
327989c
commit e13330f
Showing
24 changed files
with
275 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Prisma } from '~db/client' | ||
import { generateId, idPrefix } from '~db/lib/idGen' | ||
|
||
const applicableModels = Object.keys(idPrefix) as (keyof typeof idPrefix)[] | ||
|
||
const isApplicableModel = (model: string | undefined): model is keyof typeof idPrefix => | ||
Boolean(model) && applicableModels.includes(model as keyof typeof idPrefix) | ||
|
||
export const idGeneratorExtension = Prisma.defineExtension({ | ||
name: 'Id Generator', | ||
query: { | ||
$allOperations({ args, model, operation, query }) { | ||
if (model) { | ||
model = model.charAt(0).toLowerCase() + model.slice(1) | ||
} | ||
if (isApplicableModel(model)) { | ||
switch (operation) { | ||
case 'create': { | ||
if (args.data) { | ||
args.data.id ??= generateId(model) | ||
} | ||
break | ||
} | ||
case 'createMany': { | ||
if (Array.isArray(args.data)) { | ||
args.data.forEach((item: Record<string, unknown>) => { | ||
item.id ??= generateId(model) | ||
}) | ||
} | ||
break | ||
} | ||
case 'upsert': { | ||
args.create.id ??= generateId(model) | ||
break | ||
} | ||
} | ||
} | ||
return query(args) | ||
}, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.