Skip to content

Commit 897e2aa

Browse files
committed
Merge branch 'support-bun-lock'
2 parents d5a8d4b + de99ea0 commit 897e2aa

File tree

9 files changed

+181
-8
lines changed

9 files changed

+181
-8
lines changed

bun/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

bun/bun.lock

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"lockfileVersion": 1,
3+
"workspaces": {
4+
"": {
5+
"name": "bun",
6+
"dependencies": {
7+
"@prisma/client": "5.1.1",
8+
},
9+
"devDependencies": {
10+
"prisma": "5.1.1",
11+
},
12+
},
13+
},
14+
"packages": {
15+
"@prisma/client": ["@prisma/[email protected]", "", { "dependencies": { "@prisma/engines-version": "5.1.1-1.6a3747c37ff169c90047725a05a6ef02e32ac97e" }, "peerDependencies": { "prisma": "*" }, "optionalPeers": ["prisma"] }, "sha512-fxcCeK5pMQGcgCqCrWsi+I2rpIbk0rAhdrN+ke7f34tIrgPwA68ensrpin+9+fZvuV2OtzHmuipwduSY6HswdA=="],
16+
17+
"@prisma/engines": ["@prisma/[email protected]", "", {}, "sha512-NV/4nVNWFZSJCCIA3HIFJbbDKO/NARc9ej0tX5S9k2EVbkrFJC4Xt9b0u4rNZWL4V+F5LAjvta8vzEUw0rw+HA=="],
18+
19+
"@prisma/engines-version": ["@prisma/[email protected]", "", {}, "sha512-owZqbY/wucbr65bXJ/ljrHPgQU5xXTSkmcE/JcbqE1kusuAXV/TLN3/exmz21SZ5rJ7WDkyk70J2G/n68iogbQ=="],
20+
21+
"prisma": ["[email protected]", "", { "dependencies": { "@prisma/engines": "5.1.1" }, "bin": { "prisma": "build/index.js" } }, "sha512-WJFG/U7sMmcc6TjJTTifTfpI6Wjoh55xl4AzopVwAdyK68L9/ogNo8QQ2cxuUjJf/Wa82z/uhyh3wMzvRIBphg=="],
22+
}
23+
}

bun/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "bun",
3+
"type": "module",
4+
"dependencies": {
5+
"@prisma/client": "5.1.1"
6+
},
7+
"devDependencies": {
8+
"prisma": "5.1.1"
9+
}
10+
}

bun/prisma/schema.prisma

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This is your Prisma schema file,
2+
// learn more about it in the docs: https://pris.ly/d/prisma-schema
3+
4+
generator client {
5+
provider = "prisma-client-js"
6+
}
7+
8+
datasource db {
9+
provider = "postgresql"
10+
url = env("DATABASE_URL")
11+
}
12+
13+
model User {
14+
id Int @id @default(autoincrement())
15+
email String @unique
16+
name String?
17+
}

flake.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
buildInputs = [
3131
nixpkgs.nodejs-18_x
3232
nixpkgs.pnpm
33+
nixpkgs.bun
3334
nixpkgs.stdenv.cc.cc.lib
3435
prisma.package
3536
nixpkgs.nixfmt-rfc-style

prisma.nix

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ rec {
122122
};
123123
shellHook = nixpkgs.lib.concatStringsSep "\n" (exportCommands package);
124124
};
125+
# example:
126+
# a.b123c.d.e12345
127+
# => e12345
128+
afterLastDot = text: nixpkgs.lib.lists.last (nixpkgs.lib.strings.splitString "." text);
125129
fromPnpmLock =
126130
path:
127131
let
@@ -187,4 +191,69 @@ rec {
187191
commit = nixpkgs.lib.lists.last (nixpkgs.lib.strings.splitString "." version);
188192
in
189193
fromCommit commit;
194+
fromBunLock =
195+
path:
196+
let
197+
# HACK: nix doesn't support JSONC parsing, so currently doing
198+
# 1. remove whitespace and newline
199+
# 2. replace ",}" with "}"
200+
# 3. replace ",]" with "]"
201+
# to support JSON with trailing comma.
202+
# Keep in mind that this removes all whitespaces / tab / newline in the key / value
203+
# and doesn't support comments.
204+
fromJSONWithTrailingComma =
205+
jsonc:
206+
builtins.fromJSON (
207+
builtins.replaceStrings
208+
[
209+
",}"
210+
",]"
211+
]
212+
[
213+
"}"
214+
"]"
215+
]
216+
(
217+
builtins.replaceStrings
218+
[
219+
" "
220+
"\t"
221+
"\n"
222+
]
223+
[
224+
""
225+
""
226+
""
227+
]
228+
jsonc
229+
)
230+
);
231+
bunLockParsers = {
232+
# example:
233+
# nu> open bun.lock | from json | get packages.@prisma/engines-version.0
234+
235+
"0" = bunLockParsers."1";
236+
"1" =
237+
lock:
238+
afterLastDot (
239+
builtins.elemAt (lock."packages"."@prisma/engines-version" or (throw ''
240+
nix-prisma-utils: lockfile parsing error: package @prisma/engines-version not found.
241+
please make sure that you have @prisma/client installed.
242+
'')
243+
) 0
244+
);
245+
};
246+
lockfile = fromJSONWithTrailingComma (
247+
assert builtins.typeOf path == "path";
248+
builtins.readFile path
249+
);
250+
lockfileVersion = builtins.toString lockfile."lockfileVersion";
251+
parse =
252+
bunLockParsers.${lockfileVersion} or (throw ''
253+
nix-prisma-utils: Unsupported lockfile version: ${lockfileVersion}
254+
nix-prisma-utils currently supports bun.lock version of 0 and 1.
255+
'');
256+
commit = parse lockfile;
257+
in
258+
fromCommit commit;
190259
}

readme.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,34 @@ With nix-prisma-utils it's the other way around. You can simply install prisma t
7676
7777
```
7878

79+
### Using `bun`
80+
81+
```nix
82+
{
83+
inputs.pkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
84+
inputs.prisma-utils.url = "github:VanCoding/nix-prisma-utils";
85+
86+
outputs =
87+
{ pkgs, prisma-utils, ... }:
88+
let
89+
nixpkgs = import pkgs { system = "x86_64-linux"; };
90+
prisma =
91+
(prisma-utils.lib.prisma-factory {
92+
inherit nixpkgs;
93+
prisma-fmt-hash = "sha256-4zsJv0PW8FkGfiiv/9g0y5xWNjmRWD8Q2l2blSSBY3s="; # just copy these hashes for now, and then change them when nix complains about the mismatch
94+
query-engine-hash = "sha256-6ILWB6ZmK4ac6SgAtqCkZKHbQANmcqpWO92U8CfkFzw=";
95+
libquery-engine-hash = "sha256-n9IimBruqpDJStlEbCJ8nsk8L9dDW95ug+gz9DHS1Lc=";
96+
schema-engine-hash = "sha256-j38xSXOBwAjIdIpbSTkFJijby6OGWCoAx+xZyms/34Q=";
97+
}).fromBunLock
98+
./bun.lock; # <--- path to our bun.lock file that contains the version of prisma-engines.
99+
# NOTE: does not work with bun.lockb!
100+
in
101+
{
102+
devShells.x86_64-linux.default = nixpkgs.mkShell { shellHook = prisma.shellHook; };
103+
};
104+
}
105+
```
106+
79107
## Testing
80108

81109
`nix run .#test-all`

tests.nix

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,42 @@ let
5353
./node_modules/.bin/prisma generate
5454
'';
5555
};
56+
test-bun =
57+
let
58+
prisma =
59+
(prisma-factory ({ inherit nixpkgs; } // hashesBySystem.${nixpkgs.system})).fromBunLock
60+
./bun/bun.lock;
61+
in
62+
writeShellApplication {
63+
name = "test-bun";
64+
runtimeInputs = [ nixpkgs.bun ];
65+
text = ''
66+
echo "testing bun"
67+
${prisma.shellHook}
68+
cd bun
69+
bun install
70+
bunx prisma generate
71+
'';
72+
};
5673
test-all = writeShellApplication {
5774
name = "test";
5875
runtimeInputs = [
59-
test-pnpm
6076
test-npm
77+
test-pnpm
78+
test-bun
6179
];
6280
text = ''
6381
test-npm
6482
test-pnpm
83+
test-bun
6584
'';
6685
};
6786
in
6887
{
69-
inherit test-npm test-pnpm test-all;
88+
inherit
89+
test-npm
90+
test-pnpm
91+
test-bun
92+
test-all
93+
;
7094
}

0 commit comments

Comments
 (0)