@develit-io/backend-sdk 5.33.1 → 5.34.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/dist/index.d.mts CHANGED
@@ -55,7 +55,7 @@ declare const bankAccountPostgres: {
55
55
 
56
56
  declare const ENVIRONMENT: string[];
57
57
 
58
- type Environment = string | 'dev' | 'test' | 'staging' | 'production';
58
+ type Environment = string | 'localhost' | 'dev' | 'test' | 'staging' | 'production';
59
59
 
60
60
  type Project = 'creditio' | 'paycorp-fiat-processor' | 'dbu-mdm' | 'moneio' | 'dbu-txs' | 'lrf-website';
61
61
 
@@ -307,14 +307,16 @@ declare function createAuditLogWriter<TAuditAction = string>(table: AuditLogTabl
307
307
 
308
308
  declare function durableObjectNamespaceIdFromName(uniqueKey: string, name: string): string;
309
309
  declare const getD1DatabaseIdFromWrangler: () => string | undefined;
310
- declare const getD1Credentials: () => {
310
+ declare const getD1Credentials: ({ instanceName, }: {
311
+ instanceName: string;
312
+ }) => Promise<{
311
313
  driver?: undefined;
312
314
  dbCredentials?: undefined;
313
315
  } | {
314
316
  driver: string;
315
317
  dbCredentials: {
316
318
  accountId: string | undefined;
317
- databaseId: string;
319
+ databaseId: string | undefined;
318
320
  token: string | undefined;
319
321
  url?: undefined;
320
322
  };
@@ -326,32 +328,90 @@ declare const getD1Credentials: () => {
326
328
  token?: undefined;
327
329
  };
328
330
  driver?: undefined;
329
- };
330
- declare const getDrizzleD1Config: () => {
331
- driver?: undefined;
332
- dbCredentials?: undefined;
333
- schema: string;
334
- out: string;
335
- dialect: "sqlite";
336
- } | {
337
- driver: string;
338
- dbCredentials: {
339
- accountId: string | undefined;
340
- databaseId: string;
341
- token: string | undefined;
342
- url?: undefined;
343
- };
344
- schema: string;
345
- out: string;
346
- dialect: "sqlite";
347
- } | {
348
- dbCredentials: {
349
- url: string | undefined;
350
- accountId?: undefined;
351
- databaseId?: undefined;
352
- token?: undefined;
353
- };
354
- driver?: undefined;
331
+ }>;
332
+ declare const getDrizzleD1Config: ({ instanceName, }: {
333
+ instanceName: string;
334
+ }) => {
335
+ then<TResult1 = {
336
+ driver?: undefined;
337
+ dbCredentials?: undefined;
338
+ } | {
339
+ driver: string;
340
+ dbCredentials: {
341
+ accountId: string | undefined;
342
+ databaseId: string | undefined;
343
+ token: string | undefined;
344
+ url?: undefined;
345
+ };
346
+ } | {
347
+ dbCredentials: {
348
+ url: string | undefined;
349
+ accountId?: undefined;
350
+ databaseId?: undefined;
351
+ token?: undefined;
352
+ };
353
+ driver?: undefined;
354
+ }, TResult2 = never>(onfulfilled?: ((value: {
355
+ driver?: undefined;
356
+ dbCredentials?: undefined;
357
+ } | {
358
+ driver: string;
359
+ dbCredentials: {
360
+ accountId: string | undefined;
361
+ databaseId: string | undefined;
362
+ token: string | undefined;
363
+ url?: undefined;
364
+ };
365
+ } | {
366
+ dbCredentials: {
367
+ url: string | undefined;
368
+ accountId?: undefined;
369
+ databaseId?: undefined;
370
+ token?: undefined;
371
+ };
372
+ driver?: undefined;
373
+ }) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): Promise<TResult1 | TResult2>;
374
+ catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined): Promise<{
375
+ driver?: undefined;
376
+ dbCredentials?: undefined;
377
+ } | {
378
+ driver: string;
379
+ dbCredentials: {
380
+ accountId: string | undefined;
381
+ databaseId: string | undefined;
382
+ token: string | undefined;
383
+ url?: undefined;
384
+ };
385
+ } | {
386
+ dbCredentials: {
387
+ url: string | undefined;
388
+ accountId?: undefined;
389
+ databaseId?: undefined;
390
+ token?: undefined;
391
+ };
392
+ driver?: undefined;
393
+ } | TResult>;
394
+ finally(onfinally?: (() => void) | null | undefined): Promise<{
395
+ driver?: undefined;
396
+ dbCredentials?: undefined;
397
+ } | {
398
+ driver: string;
399
+ dbCredentials: {
400
+ accountId: string | undefined;
401
+ databaseId: string | undefined;
402
+ token: string | undefined;
403
+ url?: undefined;
404
+ };
405
+ } | {
406
+ dbCredentials: {
407
+ url: string | undefined;
408
+ accountId?: undefined;
409
+ databaseId?: undefined;
410
+ token?: undefined;
411
+ };
412
+ driver?: undefined;
413
+ }>;
414
+ [Symbol.toStringTag]: string;
355
415
  schema: string;
356
416
  out: string;
357
417
  dialect: "sqlite";
package/dist/index.d.ts CHANGED
@@ -55,7 +55,7 @@ declare const bankAccountPostgres: {
55
55
 
56
56
  declare const ENVIRONMENT: string[];
57
57
 
58
- type Environment = string | 'dev' | 'test' | 'staging' | 'production';
58
+ type Environment = string | 'localhost' | 'dev' | 'test' | 'staging' | 'production';
59
59
 
60
60
  type Project = 'creditio' | 'paycorp-fiat-processor' | 'dbu-mdm' | 'moneio' | 'dbu-txs' | 'lrf-website';
61
61
 
@@ -307,14 +307,16 @@ declare function createAuditLogWriter<TAuditAction = string>(table: AuditLogTabl
307
307
 
308
308
  declare function durableObjectNamespaceIdFromName(uniqueKey: string, name: string): string;
309
309
  declare const getD1DatabaseIdFromWrangler: () => string | undefined;
310
- declare const getD1Credentials: () => {
310
+ declare const getD1Credentials: ({ instanceName, }: {
311
+ instanceName: string;
312
+ }) => Promise<{
311
313
  driver?: undefined;
312
314
  dbCredentials?: undefined;
313
315
  } | {
314
316
  driver: string;
315
317
  dbCredentials: {
316
318
  accountId: string | undefined;
317
- databaseId: string;
319
+ databaseId: string | undefined;
318
320
  token: string | undefined;
319
321
  url?: undefined;
320
322
  };
@@ -326,32 +328,90 @@ declare const getD1Credentials: () => {
326
328
  token?: undefined;
327
329
  };
328
330
  driver?: undefined;
329
- };
330
- declare const getDrizzleD1Config: () => {
331
- driver?: undefined;
332
- dbCredentials?: undefined;
333
- schema: string;
334
- out: string;
335
- dialect: "sqlite";
336
- } | {
337
- driver: string;
338
- dbCredentials: {
339
- accountId: string | undefined;
340
- databaseId: string;
341
- token: string | undefined;
342
- url?: undefined;
343
- };
344
- schema: string;
345
- out: string;
346
- dialect: "sqlite";
347
- } | {
348
- dbCredentials: {
349
- url: string | undefined;
350
- accountId?: undefined;
351
- databaseId?: undefined;
352
- token?: undefined;
353
- };
354
- driver?: undefined;
331
+ }>;
332
+ declare const getDrizzleD1Config: ({ instanceName, }: {
333
+ instanceName: string;
334
+ }) => {
335
+ then<TResult1 = {
336
+ driver?: undefined;
337
+ dbCredentials?: undefined;
338
+ } | {
339
+ driver: string;
340
+ dbCredentials: {
341
+ accountId: string | undefined;
342
+ databaseId: string | undefined;
343
+ token: string | undefined;
344
+ url?: undefined;
345
+ };
346
+ } | {
347
+ dbCredentials: {
348
+ url: string | undefined;
349
+ accountId?: undefined;
350
+ databaseId?: undefined;
351
+ token?: undefined;
352
+ };
353
+ driver?: undefined;
354
+ }, TResult2 = never>(onfulfilled?: ((value: {
355
+ driver?: undefined;
356
+ dbCredentials?: undefined;
357
+ } | {
358
+ driver: string;
359
+ dbCredentials: {
360
+ accountId: string | undefined;
361
+ databaseId: string | undefined;
362
+ token: string | undefined;
363
+ url?: undefined;
364
+ };
365
+ } | {
366
+ dbCredentials: {
367
+ url: string | undefined;
368
+ accountId?: undefined;
369
+ databaseId?: undefined;
370
+ token?: undefined;
371
+ };
372
+ driver?: undefined;
373
+ }) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): Promise<TResult1 | TResult2>;
374
+ catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined): Promise<{
375
+ driver?: undefined;
376
+ dbCredentials?: undefined;
377
+ } | {
378
+ driver: string;
379
+ dbCredentials: {
380
+ accountId: string | undefined;
381
+ databaseId: string | undefined;
382
+ token: string | undefined;
383
+ url?: undefined;
384
+ };
385
+ } | {
386
+ dbCredentials: {
387
+ url: string | undefined;
388
+ accountId?: undefined;
389
+ databaseId?: undefined;
390
+ token?: undefined;
391
+ };
392
+ driver?: undefined;
393
+ } | TResult>;
394
+ finally(onfinally?: (() => void) | null | undefined): Promise<{
395
+ driver?: undefined;
396
+ dbCredentials?: undefined;
397
+ } | {
398
+ driver: string;
399
+ dbCredentials: {
400
+ accountId: string | undefined;
401
+ databaseId: string | undefined;
402
+ token: string | undefined;
403
+ url?: undefined;
404
+ };
405
+ } | {
406
+ dbCredentials: {
407
+ url: string | undefined;
408
+ accountId?: undefined;
409
+ databaseId?: undefined;
410
+ token?: undefined;
411
+ };
412
+ driver?: undefined;
413
+ }>;
414
+ [Symbol.toStringTag]: string;
355
415
  schema: string;
356
416
  out: string;
357
417
  dialect: "sqlite";
package/dist/index.mjs CHANGED
@@ -6,6 +6,7 @@ import { z as z$1 } from 'zod';
6
6
  import * as z from 'zod/v4/core';
7
7
  import 'http-status-codes';
8
8
  import { createError } from 'h3';
9
+ import Cloudflare from 'cloudflare';
9
10
  import fs from 'node:fs';
10
11
  import crypto$1 from 'node:crypto';
11
12
  import path from 'node:path';
@@ -344,6 +345,37 @@ function durableObjectNamespaceIdFromName(uniqueKey, name) {
344
345
  const hmac = crypto$1.createHmac("sha256", key).update(nameHmac).digest().subarray(0, 16);
345
346
  return Buffer.concat([nameHmac, hmac]).toString("hex");
346
347
  }
348
+ const getD1DatabaseId = async ({
349
+ instanceName,
350
+ environment
351
+ }) => {
352
+ const remoteD1s = [];
353
+ const cloudflare = new Cloudflare({
354
+ apiToken: process.env.CLOUDFLARE_API_TOKEN
355
+ });
356
+ let currentPage = await cloudflare.d1.database.list({
357
+ account_id: process.env.CLOUDFLARE_ACCOUNT_ID
358
+ });
359
+ remoteD1s.push(...currentPage.getPaginatedItems());
360
+ while (currentPage.hasNextPage()) {
361
+ const pageInfo = currentPage.nextPageInfo();
362
+ let nextPageParams;
363
+ if ("params" in pageInfo) {
364
+ nextPageParams = pageInfo.params;
365
+ } else {
366
+ nextPageParams = Object.fromEntries(pageInfo.url.searchParams);
367
+ }
368
+ currentPage = await cloudflare.d1.database.list({
369
+ account_id: process.env.CLOUDFLARE_ACCOUNT_ID,
370
+ ...nextPageParams
371
+ });
372
+ remoteD1s.push(...currentPage.getPaginatedItems());
373
+ }
374
+ const instancesByName = remoteD1s.filter(
375
+ (d1) => d1.name?.includes(instanceName)
376
+ );
377
+ return instancesByName.filter((name) => name.name?.includes(environment)).at(0)?.uuid;
378
+ };
347
379
  const getD1DatabaseIdFromWrangler = () => {
348
380
  try {
349
381
  const wranglerPath = path.resolve("./wrangler.jsonc");
@@ -400,34 +432,40 @@ const getLocalD1 = (databaseId) => {
400
432
  }
401
433
  }
402
434
  };
403
- const getD1Credentials = () => {
435
+ const getD1Credentials = async ({
436
+ instanceName
437
+ }) => {
404
438
  const environment = process.env.ENVIRONMENT;
405
439
  if (environment === "localhost") {
406
440
  return {};
407
441
  }
408
- const databaseId = getD1DatabaseIdFromWrangler() ?? "";
409
442
  if (isRemoteEnvironment()) {
410
443
  return {
411
444
  driver: "d1-http",
412
445
  dbCredentials: {
413
446
  accountId: process.env.CLOUDFLARE_ACCOUNT_ID,
414
- databaseId,
447
+ databaseId: await getD1DatabaseId({
448
+ environment,
449
+ instanceName
450
+ }),
415
451
  token: process.env.CLOUDFLARE_API_TOKEN
416
452
  }
417
453
  };
418
454
  } else {
419
455
  return {
420
456
  dbCredentials: {
421
- url: getLocalD1(databaseId)
457
+ url: getLocalD1(getD1DatabaseIdFromWrangler() || "")
422
458
  }
423
459
  };
424
460
  }
425
461
  };
426
- const getDrizzleD1Config = () => ({
462
+ const getDrizzleD1Config = ({
463
+ instanceName
464
+ }) => ({
427
465
  schema: "./src/database/schema/",
428
466
  out: "./src/database/migrations/",
429
467
  dialect: "sqlite",
430
- ...getD1Credentials()
468
+ ...getD1Credentials({ instanceName })
431
469
  });
432
470
 
433
471
  class DatabaseTransaction {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@develit-io/backend-sdk",
3
- "version": "5.33.1",
3
+ "version": "5.34.0",
4
4
  "description": "Develit Backend SDK",
5
5
  "author": "Develit.io",
6
6
  "license": "ISC",
@@ -38,6 +38,7 @@
38
38
  },
39
39
  "peerDependencies": {
40
40
  "@develit-io/general-codes": "^1.14.0",
41
- "zod": "^4.1.8"
41
+ "cloudflare": "^5.2.0",
42
+ "zod": "^4.1.12"
42
43
  }
43
44
  }