create-db 1.0.3-pr46-feat-add-sslmode-17243005030.0 → 1.0.3-pr47-DC-4759-set-region-to-be-near-user-17250359916.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +71 -2
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -13,6 +13,74 @@ const CREATE_DB_WORKER_URL =
|
|
|
13
13
|
const CLAIM_DB_WORKER_URL =
|
|
14
14
|
process.env.CLAIM_DB_WORKER_URL || "https://create-db.prisma.io";
|
|
15
15
|
|
|
16
|
+
async function detectUserLocation() {
|
|
17
|
+
try {
|
|
18
|
+
const response = await fetch("https://ipapi.co/json/", {
|
|
19
|
+
method: "GET",
|
|
20
|
+
headers: {
|
|
21
|
+
"User-Agent": "create-db-cli/1.0",
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
if (!response.ok) {
|
|
26
|
+
throw new Error(`Failed to fetch location data: ${response.status}`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const data = await response.json();
|
|
30
|
+
return {
|
|
31
|
+
country: data.country_code,
|
|
32
|
+
continent: data.continent_code,
|
|
33
|
+
city: data.city,
|
|
34
|
+
region: data.region,
|
|
35
|
+
latitude: data.latitude,
|
|
36
|
+
longitude: data.longitude,
|
|
37
|
+
};
|
|
38
|
+
} catch (error) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Region coordinates (latitude, longitude)
|
|
44
|
+
const REGION_COORDINATES = {
|
|
45
|
+
"ap-southeast-1": { lat: 1.3521, lng: 103.8198 }, // Singapore
|
|
46
|
+
"ap-northeast-1": { lat: 35.6762, lng: 139.6503 }, // Tokyo
|
|
47
|
+
"eu-central-1": { lat: 50.1109, lng: 8.6821 }, // Frankfurt
|
|
48
|
+
"eu-west-3": { lat: 48.8566, lng: 2.3522 }, // Paris
|
|
49
|
+
"us-east-1": { lat: 38.9072, lng: -77.0369 }, // N. Virginia
|
|
50
|
+
"us-west-1": { lat: 37.7749, lng: -122.4194 }, // N. California
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
function getRegionClosestToLocation(userLocation) {
|
|
54
|
+
if (!userLocation) return null;
|
|
55
|
+
|
|
56
|
+
const userLat = parseFloat(userLocation.latitude);
|
|
57
|
+
const userLng = parseFloat(userLocation.longitude);
|
|
58
|
+
|
|
59
|
+
let closestRegion = null;
|
|
60
|
+
let minDistance = Infinity;
|
|
61
|
+
|
|
62
|
+
for (const [region, coordinates] of Object.entries(REGION_COORDINATES)) {
|
|
63
|
+
// Simple distance calculation using Haversine formula
|
|
64
|
+
const latDiff = ((userLat - coordinates.lat) * Math.PI) / 180;
|
|
65
|
+
const lngDiff = ((userLng - coordinates.lng) * Math.PI) / 180;
|
|
66
|
+
const a =
|
|
67
|
+
Math.sin(latDiff / 2) * Math.sin(latDiff / 2) +
|
|
68
|
+
Math.cos((userLat * Math.PI) / 180) *
|
|
69
|
+
Math.cos((coordinates.lat * Math.PI) / 180) *
|
|
70
|
+
Math.sin(lngDiff / 2) *
|
|
71
|
+
Math.sin(lngDiff / 2);
|
|
72
|
+
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
73
|
+
const distance = 6371 * c; // Earth radius in km
|
|
74
|
+
|
|
75
|
+
if (distance < minDistance) {
|
|
76
|
+
minDistance = distance;
|
|
77
|
+
closestRegion = region;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return closestRegion;
|
|
82
|
+
}
|
|
83
|
+
|
|
16
84
|
async function listRegions() {
|
|
17
85
|
try {
|
|
18
86
|
const regions = await getRegions();
|
|
@@ -363,7 +431,7 @@ async function createDatabase(name, region, returnJson = false) {
|
|
|
363
431
|
const directDbName = directConnDetails?.database || "postgres";
|
|
364
432
|
const directConn =
|
|
365
433
|
directConnDetails && directHost
|
|
366
|
-
? `postgresql://${directUser}:${directPass}@${directHost}${directPort}/${directDbName}
|
|
434
|
+
? `postgresql://${directUser}:${directPass}@${directHost}${directPort}/${directDbName}`
|
|
367
435
|
: null;
|
|
368
436
|
|
|
369
437
|
const claimUrl = `${CLAIM_DB_WORKER_URL}?projectID=${projectId}&utm_source=${CLI_NAME}&utm_medium=cli`;
|
|
@@ -483,7 +551,8 @@ async function main() {
|
|
|
483
551
|
}
|
|
484
552
|
|
|
485
553
|
let name = new Date().toISOString();
|
|
486
|
-
let
|
|
554
|
+
let userLocation = await detectUserLocation();
|
|
555
|
+
let region = getRegionClosestToLocation(userLocation) || "us-east-1";
|
|
487
556
|
let chooseRegionPrompt = false;
|
|
488
557
|
|
|
489
558
|
if (flags.help) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-db",
|
|
3
|
-
"version": "1.0.3-
|
|
3
|
+
"version": "1.0.3-pr47-DC-4759-set-region-to-be-near-user-17250359916.0",
|
|
4
4
|
"description": "Instantly create a temporary Prisma Postgres database with one command, then claim and persist it in your Prisma Data Platform project when ready.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": "",
|