-
-
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.
prisma client extensions -- may mess up prisma client types
- Loading branch information
Showing
6 changed files
with
138 additions
and
94 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,45 @@ | ||
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) { | ||
args.data.id = generateId(model) | ||
} | ||
break | ||
} | ||
case 'createMany': { | ||
if (Array.isArray(args.data)) { | ||
for (const item of args.data) { | ||
if (!item.id) { | ||
item.id = generateId(model) | ||
} | ||
} | ||
} | ||
break | ||
} | ||
case 'upsert': { | ||
if (args.create && !args.create.id) { | ||
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