Hello I need help with next-on-pages, I

Hello I need help with next-on-pages, I have a remote D1 database, and added it locally too. I did some migration with drizzle and everything worked fine and the data persisted locally to a sqlite file (say X). But when I follow the steps in next-on-pages/next-dev to use bindings inside next.js app, it does not use the file X, instead, it creates new file Y which has no migrations. Any solutions to his behavior? I tried to see if there is a way to make the next.js local binding use file X but I found nothing.
7 Replies
yeehow
yeehow5mo ago
Was happening to me. I set databaseName in the binding to be the database ID instead of the name and it worked
Amr Yasser
Amr Yasser5mo ago
@yeehow like this?
setupDevBindings({
bindings: {
DB: {
type: "d1",
databaseName: "34137a76-b347-42f0-9786-8b71fbeaddeb",
},
},
});
setupDevBindings({
bindings: {
DB: {
type: "d1",
databaseName: "34137a76-b347-42f0-9786-8b71fbeaddeb",
},
},
});
yeehow
yeehow5mo ago
Yeah, whatever you have for database_id in your wrangler.toml
Amr Yasser
Amr Yasser5mo ago
First of all thank you! It worked but by setting databaseName to the binding in the wrangler.toml I found this due to the way they generate those strings below
import crypto from "node:crypto";

function durableObjectNamespaceIdFromName(uniqueKey, name) {
const key = crypto.createHash("sha256").update(uniqueKey).digest();
const nameHmac = crypto
.createHmac("sha256", key)
.update(name)
.digest()
.subarray(0, 16);
const hmac = crypto
.createHmac("sha256", key)
.update(nameHmac)
.digest()
.subarray(0, 16);
return Buffer.concat([nameHmac, hmac]).toString("hex");
}

console.log(
durableObjectNamespaceIdFromName("miniflare-D1DatabaseObject", "DB")
);
import crypto from "node:crypto";

function durableObjectNamespaceIdFromName(uniqueKey, name) {
const key = crypto.createHash("sha256").update(uniqueKey).digest();
const nameHmac = crypto
.createHmac("sha256", key)
.update(name)
.digest()
.subarray(0, 16);
const hmac = crypto
.createHmac("sha256", key)
.update(nameHmac)
.digest()
.subarray(0, 16);
return Buffer.concat([nameHmac, hmac]).toString("hex");
}

console.log(
durableObjectNamespaceIdFromName("miniflare-D1DatabaseObject", "DB")
);
This will output the exact same .sqlite file name
yeehow
yeehow5mo ago
So you did databaseName: "DB" ?
Amr Yasser
Amr Yasser5mo ago
yes
setupDevBindings({
bindings: {
DB: {
type: "d1",
databaseName: "DB",
},
},
});
setupDevBindings({
bindings: {
DB: {
type: "d1",
databaseName: "DB",
},
},
});
yeehow
yeehow5mo ago
Oh cool! That is much easier