@fileverse/api 0.0.1 → 0.0.2

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/cli/index.js CHANGED
@@ -75,6 +75,9 @@ function getRuntimeConfig() {
75
75
  get DB_PATH() {
76
76
  return process.env.DB_PATH;
77
77
  },
78
+ get DATABASE_URL() {
79
+ return process.env.DATABASE_URL;
80
+ },
78
81
  get PORT() {
79
82
  return process.env.PORT || STATIC_CONFIG.DEFAULT_PORT;
80
83
  },
@@ -121,6 +124,9 @@ var init_config = __esm({
121
124
  get DB_PATH() {
122
125
  return process.env.DB_PATH;
123
126
  },
127
+ get DATABASE_URL() {
128
+ return process.env.DATABASE_URL;
129
+ },
124
130
  get PORT() {
125
131
  return process.env.PORT || STATIC_CONFIG.DEFAULT_PORT;
126
132
  },
@@ -382,11 +388,11 @@ var init_publish = __esm({
382
388
  });
383
389
 
384
390
  // src/domain/portal/saveApiKey.ts
385
- function addApiKey(input) {
391
+ async function addApiKey(input) {
386
392
  if (!input.apiKeySeed || !input.name || !input.collaboratorAddress || !input.portalAddress) {
387
393
  throw new Error("apiKeySeed, name, collaboratorAddress, and portalAddress are required");
388
394
  }
389
- const portal = PortalsModel.findByPortalAddress(input.portalAddress);
395
+ const portal = await PortalsModel.findByPortalAddress(input.portalAddress);
390
396
  if (!portal) {
391
397
  throw new Error(`Portal with address ${input.portalAddress} does not exist`);
392
398
  }
@@ -522,51 +528,251 @@ var init_infra = __esm({
522
528
  }
523
529
  });
524
530
 
525
- // src/infra/database/connection.ts
531
+ // src/infra/database/adapters/sqlite-adapter.ts
526
532
  import Database from "better-sqlite3";
527
- var DatabaseConnectionManager, databaseConnectionManager;
528
- var init_connection = __esm({
529
- "src/infra/database/connection.ts"() {
533
+ var SqliteAdapter;
534
+ var init_sqlite_adapter = __esm({
535
+ "src/infra/database/adapters/sqlite-adapter.ts"() {
530
536
  "use strict";
531
537
  init_esm_shims();
532
- init_config();
533
538
  init_infra();
534
- DatabaseConnectionManager = class _DatabaseConnectionManager {
535
- static instance;
536
- db = null;
537
- constructor() {
539
+ init_config();
540
+ SqliteAdapter = class {
541
+ db;
542
+ constructor(dbPath) {
543
+ this.db = new Database(dbPath, {
544
+ verbose: config.NODE_ENV === "development" ? (msg) => logger.debug(String(msg)) : void 0
545
+ });
546
+ this.db.pragma("journal_mode = WAL");
547
+ this.db.pragma("foreign_keys = ON");
548
+ this.db.prepare("SELECT 1").get();
549
+ logger.info(`SQLite database connected: ${dbPath}`);
550
+ }
551
+ async select(sql, params = []) {
552
+ const stmt = this.db.prepare(sql);
553
+ return stmt.all(params);
538
554
  }
539
- static getInstance() {
540
- if (!_DatabaseConnectionManager.instance) {
541
- _DatabaseConnectionManager.instance = new _DatabaseConnectionManager();
555
+ async selectOne(sql, params = []) {
556
+ const stmt = this.db.prepare(sql);
557
+ return stmt.get(params);
558
+ }
559
+ async execute(sql, params = []) {
560
+ const stmt = this.db.prepare(sql);
561
+ const result = stmt.run(params);
562
+ return {
563
+ changes: result.changes,
564
+ lastInsertRowid: result.lastInsertRowid
565
+ };
566
+ }
567
+ async transaction(callback) {
568
+ this.db.exec("SAVEPOINT txn");
569
+ try {
570
+ const result = await callback(this);
571
+ this.db.exec("RELEASE txn");
572
+ return result;
573
+ } catch (err) {
574
+ this.db.exec("ROLLBACK TO txn");
575
+ throw err;
542
576
  }
543
- return _DatabaseConnectionManager.instance;
544
577
  }
545
- getConnection() {
546
- if (!this.db) {
547
- const dbPath = config.DB_PATH;
548
- this.db = new Database(dbPath, {
549
- verbose: config.NODE_ENV === "development" ? (msg) => logger.debug(String(msg)) : void 0
550
- });
551
- this.db.pragma("journal_mode = WAL");
552
- this.db.pragma("foreign_keys = ON");
553
- this.db.prepare("SELECT 1").get();
554
- logger.info(`SQLite database connected: ${dbPath}`);
578
+ async exec(sql) {
579
+ this.db.exec(sql);
580
+ }
581
+ async close() {
582
+ this.db.close();
583
+ logger.info("Database connection closed");
584
+ }
585
+ };
586
+ }
587
+ });
588
+
589
+ // src/infra/database/adapters/pg-adapter.ts
590
+ import pg from "pg";
591
+ function translateParams(sql) {
592
+ let idx = 0;
593
+ return sql.replace(/\?/g, () => `$${++idx}`);
594
+ }
595
+ function mapRow(row) {
596
+ const mapped = {};
597
+ for (const [key, value] of Object.entries(row)) {
598
+ const mappedKey = COLUMN_NAME_MAP[key] ?? key;
599
+ mapped[mappedKey] = value instanceof Date ? value.toISOString() : value;
600
+ }
601
+ return mapped;
602
+ }
603
+ var Pool, COLUMN_NAME_MAP, PgClientAdapter, PgAdapter;
604
+ var init_pg_adapter = __esm({
605
+ "src/infra/database/adapters/pg-adapter.ts"() {
606
+ "use strict";
607
+ init_esm_shims();
608
+ init_infra();
609
+ ({ Pool } = pg);
610
+ COLUMN_NAME_MAP = {
611
+ ddocid: "ddocId",
612
+ localversion: "localVersion",
613
+ onchainversion: "onchainVersion",
614
+ syncstatus: "syncStatus",
615
+ isdeleted: "isDeleted",
616
+ onchainfileid: "onChainFileId",
617
+ portaladdress: "portalAddress",
618
+ createdat: "createdAt",
619
+ updatedat: "updatedAt",
620
+ linkkey: "linkKey",
621
+ linkkeynonce: "linkKeyNonce",
622
+ commentkey: "commentKey",
623
+ portalseed: "portalSeed",
624
+ owneraddress: "ownerAddress",
625
+ apikeyseed: "apiKeySeed",
626
+ collaboratoraddress: "collaboratorAddress",
627
+ fileid: "fileId",
628
+ retrycount: "retryCount",
629
+ lasterror: "lastError",
630
+ lockedat: "lockedAt",
631
+ nextretryat: "nextRetryAt",
632
+ userophash: "userOpHash",
633
+ pendingpayload: "pendingPayload",
634
+ folderid: "folderId",
635
+ folderref: "folderRef",
636
+ foldername: "folderName",
637
+ metadataipfshash: "metadataIPFSHash",
638
+ contentipfshash: "contentIPFSHash",
639
+ lasttransactionhash: "lastTransactionHash",
640
+ lasttransactionblocknumber: "lastTransactionBlockNumber",
641
+ lasttransactionblocktimestamp: "lastTransactionBlockTimestamp",
642
+ created_at: "created_at",
643
+ updated_at: "updated_at"
644
+ };
645
+ PgClientAdapter = class {
646
+ constructor(client) {
647
+ this.client = client;
648
+ }
649
+ async select(sql, params = []) {
650
+ const result = await this.client.query(translateParams(sql), params);
651
+ return result.rows.map((row) => mapRow(row));
652
+ }
653
+ async selectOne(sql, params = []) {
654
+ const result = await this.client.query(translateParams(sql), params);
655
+ return result.rows[0] ? mapRow(result.rows[0]) : void 0;
656
+ }
657
+ async execute(sql, params = []) {
658
+ const result = await this.client.query(translateParams(sql), params);
659
+ return { changes: result.rowCount ?? 0, lastInsertRowid: 0 };
660
+ }
661
+ async transaction(callback) {
662
+ await this.client.query("SAVEPOINT nested_txn");
663
+ try {
664
+ const result = await callback(this);
665
+ await this.client.query("RELEASE SAVEPOINT nested_txn");
666
+ return result;
667
+ } catch (err) {
668
+ await this.client.query("ROLLBACK TO SAVEPOINT nested_txn");
669
+ throw err;
555
670
  }
556
- return this.db;
671
+ }
672
+ async exec(sql) {
673
+ await this.client.query(sql);
557
674
  }
558
675
  async close() {
559
- if (this.db) {
560
- this.db.close();
561
- this.db = null;
562
- logger.info("Database connection closed");
676
+ }
677
+ };
678
+ PgAdapter = class {
679
+ pool;
680
+ constructor(connectionString) {
681
+ const url = new URL(connectionString);
682
+ const isLocal = url.hostname === "localhost" || url.hostname === "127.0.0.1" || url.hostname === "::1";
683
+ const sslDisabled = connectionString.includes("sslmode=disable");
684
+ const needsSsl = !isLocal && !sslDisabled;
685
+ this.pool = new Pool({
686
+ connectionString,
687
+ // pg requires password to be a string; local trust/peer auth uses empty string
688
+ password: url.password || "",
689
+ max: 20,
690
+ idleTimeoutMillis: 3e4,
691
+ ssl: needsSsl ? { rejectUnauthorized: false } : void 0
692
+ });
693
+ logger.info(`PostgreSQL pool created (ssl: ${needsSsl ? "on" : "off"})`);
694
+ }
695
+ async select(sql, params = []) {
696
+ const result = await this.pool.query(translateParams(sql), params);
697
+ return result.rows.map((row) => mapRow(row));
698
+ }
699
+ async selectOne(sql, params = []) {
700
+ const result = await this.pool.query(translateParams(sql), params);
701
+ return result.rows[0] ? mapRow(result.rows[0]) : void 0;
702
+ }
703
+ async execute(sql, params = []) {
704
+ const result = await this.pool.query(translateParams(sql), params);
705
+ return { changes: result.rowCount ?? 0, lastInsertRowid: 0 };
706
+ }
707
+ async transaction(callback) {
708
+ const client = await this.pool.connect();
709
+ try {
710
+ await client.query("BEGIN");
711
+ const clientAdapter = new PgClientAdapter(client);
712
+ const result = await callback(clientAdapter);
713
+ await client.query("COMMIT");
714
+ return result;
715
+ } catch (err) {
716
+ await client.query("ROLLBACK");
717
+ throw err;
718
+ } finally {
719
+ client.release();
563
720
  }
564
721
  }
565
- isConnected() {
566
- return this.db !== null && this.db.open;
722
+ async exec(sql) {
723
+ await this.pool.query(sql);
724
+ }
725
+ async close() {
726
+ await this.pool.end();
727
+ logger.info("PostgreSQL pool closed");
567
728
  }
568
729
  };
569
- databaseConnectionManager = DatabaseConnectionManager.getInstance();
730
+ }
731
+ });
732
+
733
+ // src/infra/database/connection.ts
734
+ var connection_exports = {};
735
+ __export(connection_exports, {
736
+ closeAdapter: () => closeAdapter,
737
+ getAdapter: () => getAdapter,
738
+ getAdapterSync: () => getAdapterSync
739
+ });
740
+ async function getAdapter() {
741
+ if (adapter) return adapter;
742
+ const databaseUrl = config.DATABASE_URL;
743
+ const dbPath = config.DB_PATH;
744
+ if (databaseUrl) {
745
+ adapter = new PgAdapter(databaseUrl);
746
+ } else if (dbPath) {
747
+ adapter = new SqliteAdapter(dbPath);
748
+ } else {
749
+ throw new Error("Either DATABASE_URL or DB_PATH must be set");
750
+ }
751
+ return adapter;
752
+ }
753
+ function getAdapterSync() {
754
+ if (!adapter) {
755
+ throw new Error(
756
+ "Database adapter not initialized. Call getAdapter() at startup first."
757
+ );
758
+ }
759
+ return adapter;
760
+ }
761
+ async function closeAdapter() {
762
+ if (adapter) {
763
+ await adapter.close();
764
+ adapter = null;
765
+ }
766
+ }
767
+ var adapter;
768
+ var init_connection = __esm({
769
+ "src/infra/database/connection.ts"() {
770
+ "use strict";
771
+ init_esm_shims();
772
+ init_sqlite_adapter();
773
+ init_pg_adapter();
774
+ init_config();
775
+ adapter = null;
570
776
  }
571
777
  });
572
778
 
@@ -581,9 +787,6 @@ var init_constants3 = __esm({
581
787
  });
582
788
 
583
789
  // src/infra/database/query-builder.ts
584
- function getDb() {
585
- return databaseConnectionManager.getConnection();
586
- }
587
790
  var QueryBuilder;
588
791
  var init_query_builder = __esm({
589
792
  "src/infra/database/query-builder.ts"() {
@@ -592,24 +795,17 @@ var init_query_builder = __esm({
592
795
  init_connection();
593
796
  init_constants3();
594
797
  QueryBuilder = class {
595
- static select(sql, params = []) {
596
- const stmt = getDb().prepare(sql);
597
- return stmt.all(params);
798
+ static async select(sql, params = []) {
799
+ return getAdapterSync().select(sql, params);
598
800
  }
599
- static selectOne(sql, params = []) {
600
- const stmt = getDb().prepare(sql);
601
- return stmt.get(params);
801
+ static async selectOne(sql, params = []) {
802
+ return getAdapterSync().selectOne(sql, params);
602
803
  }
603
- static execute(sql, params = []) {
604
- const stmt = getDb().prepare(sql);
605
- const result = stmt.run(params);
606
- return {
607
- changes: result.changes,
608
- lastInsertRowid: result.lastInsertRowid
609
- };
804
+ static async execute(sql, params = []) {
805
+ return getAdapterSync().execute(sql, params);
610
806
  }
611
- static transaction(callback) {
612
- return getDb().transaction(callback)();
807
+ static async transaction(callback) {
808
+ return getAdapterSync().transaction(callback);
613
809
  }
614
810
  static paginate(sql, options = {}) {
615
811
  let query = sql;
@@ -631,17 +827,12 @@ var init_query_builder = __esm({
631
827
  });
632
828
 
633
829
  // src/infra/database/index.ts
634
- function getDb2() {
635
- return databaseConnectionManager.getConnection();
636
- }
637
- var database_default;
638
830
  var init_database = __esm({
639
831
  "src/infra/database/index.ts"() {
640
832
  "use strict";
641
833
  init_esm_shims();
642
834
  init_connection();
643
835
  init_query_builder();
644
- database_default = getDb2;
645
836
  }
646
837
  });
647
838
 
@@ -665,22 +856,22 @@ var init_portals_model = __esm({
665
856
  init_database();
666
857
  PortalsModel = class {
667
858
  static TABLE = "portals";
668
- static findByPortalAddress(portalAddress) {
859
+ static async findByPortalAddress(portalAddress) {
669
860
  const sql = `SELECT _id, portalAddress, portalSeed, ownerAddress, createdAt, updatedAt FROM ${this.TABLE} WHERE portalAddress = ?`;
670
861
  return QueryBuilder.selectOne(sql, [portalAddress]);
671
862
  }
672
- static create(input) {
863
+ static async create(input) {
673
864
  const _id = uuidv72();
674
865
  const now = (/* @__PURE__ */ new Date()).toISOString();
675
866
  const sql = `INSERT INTO ${this.TABLE} (_id, portalAddress, portalSeed, ownerAddress, createdAt, updatedAt) VALUES (?, ?, ?, ?, ?, ?)`;
676
- QueryBuilder.execute(sql, [_id, input.portalAddress, input.portalSeed, input.ownerAddress, now, now]);
677
- const created = this.findByPortalAddress(input.portalAddress);
867
+ await QueryBuilder.execute(sql, [_id, input.portalAddress, input.portalSeed, input.ownerAddress, now, now]);
868
+ const created = await this.findByPortalAddress(input.portalAddress);
678
869
  if (!created) {
679
870
  throw new Error("Failed to create portal");
680
871
  }
681
872
  return created;
682
873
  }
683
- static update(portalAddress, input) {
874
+ static async update(portalAddress, input) {
684
875
  const now = (/* @__PURE__ */ new Date()).toISOString();
685
876
  const keys = [];
686
877
  const values = [];
@@ -695,15 +886,15 @@ var init_portals_model = __esm({
695
886
  const updateChain = keys.join(", ");
696
887
  const sql = `UPDATE ${this.TABLE} SET ${updateChain} WHERE portalAddress = ?`;
697
888
  values.push(portalAddress);
698
- QueryBuilder.execute(sql, values);
699
- const updated = this.findByPortalAddress(portalAddress);
889
+ await QueryBuilder.execute(sql, values);
890
+ const updated = await this.findByPortalAddress(portalAddress);
700
891
  if (!updated) {
701
892
  throw new Error("Failed to update portal");
702
893
  }
703
894
  return updated;
704
895
  }
705
- static upsert(input) {
706
- const existing = this.findByPortalAddress(input.portalAddress);
896
+ static async upsert(input) {
897
+ const existing = await this.findByPortalAddress(input.portalAddress);
707
898
  if (existing) {
708
899
  return this.update(input.portalAddress, {
709
900
  portalSeed: input.portalSeed,
@@ -726,12 +917,12 @@ var init_apikeys_model = __esm({
726
917
  init_database();
727
918
  ApiKeysModel = class {
728
919
  static TABLE = "api_keys";
729
- static create(input) {
920
+ static async create(input) {
730
921
  const _id = uuidv73();
731
922
  const now = (/* @__PURE__ */ new Date()).toISOString();
732
- const sql = `INSERT INTO ${this.TABLE} (_id, apiKeySeed, name, collaboratorAddress, portalAddress, createdAt)
923
+ const sql = `INSERT INTO ${this.TABLE} (_id, apiKeySeed, name, collaboratorAddress, portalAddress, createdAt)
733
924
  VALUES (?, ?, ?, ?, ?, ?)`;
734
- const result = QueryBuilder.execute(sql, [
925
+ const result = await QueryBuilder.execute(sql, [
735
926
  _id,
736
927
  input.apiKeySeed,
737
928
  input.name,
@@ -742,29 +933,29 @@ var init_apikeys_model = __esm({
742
933
  if (result.changes === 0) {
743
934
  throw new Error("Failed to create API key");
744
935
  }
745
- const created = this.findById(_id);
936
+ const created = await this.findById(_id);
746
937
  if (!created) {
747
938
  throw new Error("Failed to create API key");
748
939
  }
749
940
  return created;
750
941
  }
751
- static findById(_id) {
942
+ static async findById(_id) {
752
943
  const sql = `SELECT _id, apiKeySeed, name, collaboratorAddress, portalAddress, createdAt, isDeleted FROM ${this.TABLE} WHERE _id = ? AND isDeleted = 0`;
753
944
  return QueryBuilder.selectOne(sql, [_id]);
754
945
  }
755
- static findByCollaboratorAddress(collaboratorAddress) {
946
+ static async findByCollaboratorAddress(collaboratorAddress) {
756
947
  const sql = `SELECT _id, apiKeySeed, name, collaboratorAddress, portalAddress, createdAt, isDeleted FROM ${this.TABLE} WHERE collaboratorAddress = ? AND isDeleted = 0 LIMIT 1`;
757
948
  return QueryBuilder.selectOne(sql, [collaboratorAddress]);
758
949
  }
759
- static delete(_id) {
950
+ static async delete(_id) {
760
951
  const sql = `UPDATE ${this.TABLE} SET isDeleted = 1 WHERE _id = ?`;
761
- QueryBuilder.execute(sql, [_id]);
952
+ await QueryBuilder.execute(sql, [_id]);
762
953
  }
763
- static findByPortalAddress(portalAddress) {
954
+ static async findByPortalAddress(portalAddress) {
764
955
  const sql = `SELECT _id, apiKeySeed, name, collaboratorAddress, portalAddress, createdAt, isDeleted FROM ${this.TABLE} WHERE portalAddress = ? AND isDeleted = 0`;
765
956
  return QueryBuilder.selectOne(sql, [portalAddress]);
766
957
  }
767
- static findByApiKey(apiKey) {
958
+ static async findByApiKey(apiKey) {
768
959
  const sql = `SELECT _id, apiKeySeed, name, collaboratorAddress, portalAddress, createdAt, isDeleted FROM ${this.TABLE} WHERE apiKeySeed = ? AND isDeleted = 0`;
769
960
  return QueryBuilder.selectOne(sql, [apiKey]);
770
961
  }
@@ -806,7 +997,7 @@ var init_models = __esm({
806
997
  });
807
998
 
808
999
  // src/domain/portal/savePortal.ts
809
- function savePortal(input) {
1000
+ async function savePortal(input) {
810
1001
  if (!input.portalAddress || !input.portalSeed || !input.ownerAddress) {
811
1002
  throw new Error("portalAddress, portalSeed, and ownerAddress are required");
812
1003
  }
@@ -825,9 +1016,9 @@ var migrations_exports = {};
825
1016
  __export(migrations_exports, {
826
1017
  runMigrations: () => runMigrations
827
1018
  });
828
- function runMigrations() {
829
- const db = database_default();
830
- db.exec(STABLE_SCHEMA);
1019
+ async function runMigrations() {
1020
+ const adapter2 = getAdapterSync();
1021
+ await adapter2.exec(STABLE_SCHEMA);
831
1022
  logger.debug("Database schema ready");
832
1023
  }
833
1024
  var STABLE_SCHEMA;
@@ -835,7 +1026,7 @@ var init_migrations = __esm({
835
1026
  "src/infra/database/migrations/index.ts"() {
836
1027
  "use strict";
837
1028
  init_esm_shims();
838
- init_database();
1029
+ init_connection();
839
1030
  init_infra();
840
1031
  STABLE_SCHEMA = `
841
1032
  CREATE TABLE IF NOT EXISTS files (
@@ -846,8 +1037,8 @@ CREATE TABLE IF NOT EXISTS files (
846
1037
  localVersion INTEGER NOT NULL DEFAULT 1,
847
1038
  onchainVersion INTEGER NOT NULL DEFAULT 0,
848
1039
  syncStatus TEXT NOT NULL DEFAULT 'pending',
849
- createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
850
- updatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
1040
+ createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
1041
+ updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
851
1042
  isDeleted INTEGER NOT NULL DEFAULT 0,
852
1043
  portalAddress TEXT NOT NULL,
853
1044
  metadata TEXT DEFAULT '{}',
@@ -867,8 +1058,8 @@ CREATE TABLE IF NOT EXISTS portals (
867
1058
  portalAddress TEXT NOT NULL UNIQUE,
868
1059
  portalSeed TEXT NOT NULL UNIQUE,
869
1060
  ownerAddress TEXT NOT NULL,
870
- createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
871
- updatedAt DATETIME DEFAULT CURRENT_TIMESTAMP
1061
+ createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
1062
+ updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP
872
1063
  );
873
1064
 
874
1065
  CREATE TABLE IF NOT EXISTS api_keys (
@@ -877,7 +1068,7 @@ CREATE TABLE IF NOT EXISTS api_keys (
877
1068
  name TEXT NOT NULL,
878
1069
  collaboratorAddress TEXT NOT NULL UNIQUE,
879
1070
  portalAddress TEXT NOT NULL,
880
- createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
1071
+ createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
881
1072
  isDeleted INTEGER NOT NULL DEFAULT 0
882
1073
  );
883
1074
 
@@ -913,8 +1104,8 @@ CREATE TABLE IF NOT EXISTS folders (
913
1104
  lastTransactionHash TEXT,
914
1105
  lastTransactionBlockNumber INTEGER NOT NULL,
915
1106
  lastTransactionBlockTimestamp INTEGER NOT NULL,
916
- created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
917
- updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
1107
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
1108
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
918
1109
  );
919
1110
  CREATE INDEX IF NOT EXISTS idx_folders_folderRef_folderId ON folders(folderRef, folderId);
920
1111
  CREATE INDEX IF NOT EXISTS idx_folders_folderRef ON folders(folderRef);
@@ -1148,16 +1339,16 @@ import { toUint8Array as toUint8Array5 } from "js-base64";
1148
1339
  import { stringToBytes as stringToBytes2 } from "viem";
1149
1340
  import { toAESKey as toAESKey2, aesDecrypt } from "@fileverse/crypto/webcrypto";
1150
1341
  var SAVED_DATA_ENCRYPTION_KEY_INFO = "SAVED_DATA_ENCRYPTION_KEY";
1151
- function initializeWithData(data) {
1342
+ async function initializeWithData(data) {
1152
1343
  const { keyMaterial, appMaterial } = data;
1153
- savePortal({
1344
+ await savePortal({
1154
1345
  portalAddress: appMaterial.portalAddress,
1155
1346
  portalSeed: appMaterial.portalSeed,
1156
1347
  ownerAddress: appMaterial.ownerAddress
1157
1348
  });
1158
- const existingApiKey = ApiKeysModel.findByApiKey(keyMaterial.apiKeySeed);
1349
+ const existingApiKey = await ApiKeysModel.findByApiKey(keyMaterial.apiKeySeed);
1159
1350
  if (!existingApiKey) {
1160
- addApiKey({
1351
+ await addApiKey({
1161
1352
  apiKeySeed: keyMaterial.apiKeySeed,
1162
1353
  name: keyMaterial.name,
1163
1354
  collaboratorAddress: keyMaterial.collaboratorAddress,
@@ -1186,7 +1377,7 @@ var decryptSavedData = async (apiKey, encryptedData) => {
1186
1377
  };
1187
1378
 
1188
1379
  // src/cli/index.ts
1189
- var program = new Command().name("fileverse-api").description("Run the Fileverse API server").version("0.0.1").option("--apiKey <key>", "API key for authentication").option("--rpcUrl <url>", "RPC URL for blockchain connection").option("--port <port>", "Port to run the server on", "8001").option("--db <path>", "Database path").action(async (options) => {
1380
+ var program = new Command().name("fileverse-api").description("Run the Fileverse API server").version("0.0.2").option("--apiKey <key>", "API key for authentication").option("--rpcUrl <url>", "RPC URL for blockchain connection").option("--port <port>", "Port to run the server on", "8001").option("--db <path>", "Database path").action(async (options) => {
1190
1381
  try {
1191
1382
  console.log("Fileverse API - Starting initialization...\n");
1192
1383
  if (needsPrompting(options)) {
@@ -1211,10 +1402,12 @@ var program = new Command().name("fileverse-api").description("Run the Fileverse
1211
1402
  loadConfig();
1212
1403
  console.log(`\u2713 Configuration saved to ${envPath}
1213
1404
  `);
1405
+ const { getAdapter: getAdapter2 } = await Promise.resolve().then(() => (init_connection(), connection_exports));
1406
+ await getAdapter2();
1214
1407
  const { runMigrations: runMigrations2 } = await Promise.resolve().then(() => (init_migrations(), migrations_exports));
1215
- runMigrations2();
1408
+ await runMigrations2();
1216
1409
  console.log("\u2713 Database migrations complete");
1217
- const result = initializeWithData({
1410
+ const result = await initializeWithData({
1218
1411
  keyMaterial,
1219
1412
  appMaterial,
1220
1413
  id: data.id