Skip to content

Commit

Permalink
Merge pull request #52 from dilsher1/main
Browse files Browse the repository at this point in the history
Fix autoincrement to support BigInt
  • Loading branch information
stijnvanderlaan authored Apr 19, 2024
2 parents e58116b + 3f9c044 commit 6d3dbd9
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/defaults/autoincrement.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
import { Prisma } from "@prisma/client";
import { PrismaMockData } from "..";

let autoincrement_cache: { [key: string]: number } = {};
let autoincrement_cache: { [key: string]: number | bigint } = {};

export default function autoincrement<P>(
prop: string,
field: Prisma.DMMF.Field,
data: PrismaMockData<P> = {}
): Number {
): Number | BigInt {
const key = `${prop}_${field.name}`;
let m = autoincrement_cache?.[key];
if (m === undefined) {
m = 0;
data[prop].forEach((item: { [x: string]: number }) => {
m = Math.max(m, item[field.name]);
});
if (field.type === 'BigInt') {
if (m === undefined) {
m = 0n;
data[prop].forEach((item: { [x: string]: bigint }) => {
m = (m as bigint) > item[field.name] ? m : item[field.name];
});
}
m = (m as bigint) + 1n;
autoincrement_cache[key] = m;
} else {
if (m === undefined) {
m = 0;
data[prop].forEach((item: { [x: string]: number }) => {
m = Math.max(m as number, item[field.name]);
});
}
m = (m as number) + 1;
autoincrement_cache[key] = m;
}
m += 1;
autoincrement_cache[key] = m;

return m;
}

Expand Down

0 comments on commit 6d3dbd9

Please sign in to comment.