-
-
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.
Merge branch 'dev' into IN-857-user-management
- Loading branch information
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.