@fileverse/api 0.0.3 → 0.0.5
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 +280 -88
- package/dist/cli/index.js.map +1 -1
- package/dist/commands/index.js +1198 -607
- package/dist/commands/index.js.map +1 -1
- package/dist/index.js +3101 -2361
- package/dist/index.js.map +1 -1
- package/dist/worker.js +2736 -2144
- package/dist/worker.js.map +1 -1
- package/package.json +3 -1
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,39 +528,195 @@ var init_infra = __esm({
|
|
|
522
528
|
}
|
|
523
529
|
});
|
|
524
530
|
|
|
525
|
-
// src/infra/database/
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
531
|
+
// src/infra/database/adapters/sql-compat.ts
|
|
532
|
+
function sqliteToPostgres(sql) {
|
|
533
|
+
let result = "";
|
|
534
|
+
let paramIndex = 0;
|
|
535
|
+
let inString = false;
|
|
536
|
+
for (let i = 0; i < sql.length; i++) {
|
|
537
|
+
const ch = sql[i];
|
|
538
|
+
if (ch === "'") {
|
|
539
|
+
if (inString && i + 1 < sql.length && sql[i + 1] === "'") {
|
|
540
|
+
result += "''";
|
|
541
|
+
i++;
|
|
542
|
+
continue;
|
|
543
|
+
}
|
|
544
|
+
inString = !inString;
|
|
545
|
+
result += ch;
|
|
546
|
+
} else if (ch === "?" && !inString) {
|
|
547
|
+
paramIndex++;
|
|
548
|
+
result += `$${paramIndex}`;
|
|
549
|
+
} else {
|
|
550
|
+
result += ch;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
return result;
|
|
554
|
+
}
|
|
555
|
+
var init_sql_compat = __esm({
|
|
556
|
+
"src/infra/database/adapters/sql-compat.ts"() {
|
|
530
557
|
"use strict";
|
|
531
558
|
init_esm_shims();
|
|
532
|
-
|
|
559
|
+
}
|
|
560
|
+
});
|
|
561
|
+
|
|
562
|
+
// src/infra/database/adapters/postgres-adapter.ts
|
|
563
|
+
var postgres_adapter_exports = {};
|
|
564
|
+
__export(postgres_adapter_exports, {
|
|
565
|
+
PostgresAdapter: () => PostgresAdapter
|
|
566
|
+
});
|
|
567
|
+
async function getPg() {
|
|
568
|
+
if (!pgModule) {
|
|
569
|
+
pgModule = await import("pg");
|
|
570
|
+
}
|
|
571
|
+
return pgModule;
|
|
572
|
+
}
|
|
573
|
+
var pgModule, PostgresAdapter;
|
|
574
|
+
var init_postgres_adapter = __esm({
|
|
575
|
+
"src/infra/database/adapters/postgres-adapter.ts"() {
|
|
576
|
+
"use strict";
|
|
577
|
+
init_esm_shims();
|
|
578
|
+
init_sql_compat();
|
|
533
579
|
init_infra();
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
580
|
+
pgModule = null;
|
|
581
|
+
PostgresAdapter = class {
|
|
582
|
+
pool = null;
|
|
583
|
+
connectionUrl;
|
|
584
|
+
connected = false;
|
|
585
|
+
dialect = "postgres";
|
|
586
|
+
constructor(connectionUrl) {
|
|
587
|
+
this.connectionUrl = connectionUrl;
|
|
538
588
|
}
|
|
539
|
-
|
|
540
|
-
if (!
|
|
541
|
-
|
|
589
|
+
async getPool() {
|
|
590
|
+
if (!this.pool) {
|
|
591
|
+
const pg = await getPg();
|
|
592
|
+
this.pool = new pg.default.Pool({
|
|
593
|
+
connectionString: this.connectionUrl,
|
|
594
|
+
max: 10,
|
|
595
|
+
ssl: this.connectionUrl.includes("sslmode=require") || this.connectionUrl.includes("amazonaws.com") || this.connectionUrl.includes("heroku") ? { rejectUnauthorized: false } : void 0
|
|
596
|
+
});
|
|
597
|
+
const client = await this.pool.connect();
|
|
598
|
+
client.release();
|
|
599
|
+
this.connected = true;
|
|
600
|
+
logger.info("PostgreSQL database connected");
|
|
542
601
|
}
|
|
543
|
-
return
|
|
602
|
+
return this.pool;
|
|
603
|
+
}
|
|
604
|
+
async select(sql, params = []) {
|
|
605
|
+
const pool = await this.getPool();
|
|
606
|
+
const pgSql = sqliteToPostgres(sql);
|
|
607
|
+
const result = await pool.query(pgSql, params);
|
|
608
|
+
return result.rows;
|
|
609
|
+
}
|
|
610
|
+
async selectOne(sql, params = []) {
|
|
611
|
+
const pool = await this.getPool();
|
|
612
|
+
const pgSql = sqliteToPostgres(sql);
|
|
613
|
+
const result = await pool.query(pgSql, params);
|
|
614
|
+
return result.rows[0] ?? void 0;
|
|
615
|
+
}
|
|
616
|
+
async execute(sql, params = []) {
|
|
617
|
+
const pool = await this.getPool();
|
|
618
|
+
const pgSql = sqliteToPostgres(sql);
|
|
619
|
+
const result = await pool.query(pgSql, params);
|
|
620
|
+
return {
|
|
621
|
+
changes: result.rowCount ?? 0,
|
|
622
|
+
lastInsertRowid: 0
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
async transaction(callback) {
|
|
626
|
+
const pool = await this.getPool();
|
|
627
|
+
const client = await pool.connect();
|
|
628
|
+
try {
|
|
629
|
+
await client.query("BEGIN");
|
|
630
|
+
const result = await callback();
|
|
631
|
+
await client.query("COMMIT");
|
|
632
|
+
return result;
|
|
633
|
+
} catch (error) {
|
|
634
|
+
await client.query("ROLLBACK");
|
|
635
|
+
throw error;
|
|
636
|
+
} finally {
|
|
637
|
+
client.release();
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
async exec(sql) {
|
|
641
|
+
const pool = await this.getPool();
|
|
642
|
+
await pool.query(sql);
|
|
643
|
+
}
|
|
644
|
+
async close() {
|
|
645
|
+
if (this.pool) {
|
|
646
|
+
await this.pool.end();
|
|
647
|
+
this.pool = null;
|
|
648
|
+
this.connected = false;
|
|
649
|
+
logger.info("Database connection closed");
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
isConnected() {
|
|
653
|
+
return this.connected;
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
});
|
|
658
|
+
|
|
659
|
+
// src/infra/database/adapters/sqlite-adapter.ts
|
|
660
|
+
var sqlite_adapter_exports = {};
|
|
661
|
+
__export(sqlite_adapter_exports, {
|
|
662
|
+
SqliteAdapter: () => SqliteAdapter
|
|
663
|
+
});
|
|
664
|
+
import Database from "better-sqlite3";
|
|
665
|
+
var SqliteAdapter;
|
|
666
|
+
var init_sqlite_adapter = __esm({
|
|
667
|
+
"src/infra/database/adapters/sqlite-adapter.ts"() {
|
|
668
|
+
"use strict";
|
|
669
|
+
init_esm_shims();
|
|
670
|
+
init_infra();
|
|
671
|
+
SqliteAdapter = class {
|
|
672
|
+
constructor(dbPath) {
|
|
673
|
+
this.dbPath = dbPath;
|
|
544
674
|
}
|
|
545
|
-
|
|
675
|
+
db = null;
|
|
676
|
+
dialect = "sqlite";
|
|
677
|
+
getDb() {
|
|
546
678
|
if (!this.db) {
|
|
547
|
-
|
|
548
|
-
this.db = new Database(dbPath, {
|
|
549
|
-
verbose: config.NODE_ENV === "development" ? (msg) => logger.debug(String(msg)) : void 0
|
|
550
|
-
});
|
|
679
|
+
this.db = new Database(this.dbPath);
|
|
551
680
|
this.db.pragma("journal_mode = WAL");
|
|
552
681
|
this.db.pragma("foreign_keys = ON");
|
|
553
682
|
this.db.prepare("SELECT 1").get();
|
|
554
|
-
logger.info(`SQLite database connected: ${dbPath}`);
|
|
683
|
+
logger.info(`SQLite database connected: ${this.dbPath}`);
|
|
555
684
|
}
|
|
556
685
|
return this.db;
|
|
557
686
|
}
|
|
687
|
+
async select(sql, params = []) {
|
|
688
|
+
const stmt = this.getDb().prepare(sql);
|
|
689
|
+
return stmt.all(params);
|
|
690
|
+
}
|
|
691
|
+
async selectOne(sql, params = []) {
|
|
692
|
+
const stmt = this.getDb().prepare(sql);
|
|
693
|
+
return stmt.get(params);
|
|
694
|
+
}
|
|
695
|
+
async execute(sql, params = []) {
|
|
696
|
+
const stmt = this.getDb().prepare(sql);
|
|
697
|
+
const result = stmt.run(params);
|
|
698
|
+
return {
|
|
699
|
+
changes: result.changes,
|
|
700
|
+
lastInsertRowid: result.lastInsertRowid
|
|
701
|
+
};
|
|
702
|
+
}
|
|
703
|
+
async transaction(callback) {
|
|
704
|
+
const db = this.getDb();
|
|
705
|
+
const savepointName = `sp_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
706
|
+
db.exec(`SAVEPOINT ${savepointName}`);
|
|
707
|
+
try {
|
|
708
|
+
const result = await callback();
|
|
709
|
+
db.exec(`RELEASE ${savepointName}`);
|
|
710
|
+
return result;
|
|
711
|
+
} catch (error) {
|
|
712
|
+
db.exec(`ROLLBACK TO ${savepointName}`);
|
|
713
|
+
db.exec(`RELEASE ${savepointName}`);
|
|
714
|
+
throw error;
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
async exec(sql) {
|
|
718
|
+
this.getDb().exec(sql);
|
|
719
|
+
}
|
|
558
720
|
async close() {
|
|
559
721
|
if (this.db) {
|
|
560
722
|
this.db.close();
|
|
@@ -566,7 +728,48 @@ var init_connection = __esm({
|
|
|
566
728
|
return this.db !== null && this.db.open;
|
|
567
729
|
}
|
|
568
730
|
};
|
|
569
|
-
|
|
731
|
+
}
|
|
732
|
+
});
|
|
733
|
+
|
|
734
|
+
// src/infra/database/connection.ts
|
|
735
|
+
import path5 from "path";
|
|
736
|
+
import fs3 from "fs";
|
|
737
|
+
async function initializeAdapter() {
|
|
738
|
+
if (adapter) return adapter;
|
|
739
|
+
const databaseUrl = process.env.DATABASE_URL;
|
|
740
|
+
const dbPath = process.env.DB_PATH;
|
|
741
|
+
if (databaseUrl) {
|
|
742
|
+
const { PostgresAdapter: PostgresAdapter2 } = await Promise.resolve().then(() => (init_postgres_adapter(), postgres_adapter_exports));
|
|
743
|
+
adapter = new PostgresAdapter2(databaseUrl);
|
|
744
|
+
logger.info("Using PostgreSQL adapter");
|
|
745
|
+
} else if (dbPath) {
|
|
746
|
+
const dbDir = path5.dirname(dbPath.trim());
|
|
747
|
+
if (!fs3.existsSync(dbDir)) {
|
|
748
|
+
fs3.mkdirSync(dbDir, { recursive: true });
|
|
749
|
+
}
|
|
750
|
+
const { SqliteAdapter: SqliteAdapter2 } = await Promise.resolve().then(() => (init_sqlite_adapter(), sqlite_adapter_exports));
|
|
751
|
+
adapter = new SqliteAdapter2(dbPath);
|
|
752
|
+
logger.info("Using SQLite adapter");
|
|
753
|
+
} else {
|
|
754
|
+
throw new Error(
|
|
755
|
+
"No database configured. Set DATABASE_URL (PostgreSQL) or DB_PATH (SQLite)."
|
|
756
|
+
);
|
|
757
|
+
}
|
|
758
|
+
return adapter;
|
|
759
|
+
}
|
|
760
|
+
async function getAdapter() {
|
|
761
|
+
if (!adapter) {
|
|
762
|
+
return initializeAdapter();
|
|
763
|
+
}
|
|
764
|
+
return adapter;
|
|
765
|
+
}
|
|
766
|
+
var adapter;
|
|
767
|
+
var init_connection = __esm({
|
|
768
|
+
"src/infra/database/connection.ts"() {
|
|
769
|
+
"use strict";
|
|
770
|
+
init_esm_shims();
|
|
771
|
+
init_infra();
|
|
772
|
+
adapter = null;
|
|
570
773
|
}
|
|
571
774
|
});
|
|
572
775
|
|
|
@@ -581,9 +784,6 @@ var init_constants3 = __esm({
|
|
|
581
784
|
});
|
|
582
785
|
|
|
583
786
|
// src/infra/database/query-builder.ts
|
|
584
|
-
function getDb() {
|
|
585
|
-
return databaseConnectionManager.getConnection();
|
|
586
|
-
}
|
|
587
787
|
var QueryBuilder;
|
|
588
788
|
var init_query_builder = __esm({
|
|
589
789
|
"src/infra/database/query-builder.ts"() {
|
|
@@ -592,24 +792,21 @@ var init_query_builder = __esm({
|
|
|
592
792
|
init_connection();
|
|
593
793
|
init_constants3();
|
|
594
794
|
QueryBuilder = class {
|
|
595
|
-
static select(sql, params = []) {
|
|
596
|
-
const
|
|
597
|
-
return
|
|
795
|
+
static async select(sql, params = []) {
|
|
796
|
+
const adapter2 = await getAdapter();
|
|
797
|
+
return adapter2.select(sql, params);
|
|
598
798
|
}
|
|
599
|
-
static selectOne(sql, params = []) {
|
|
600
|
-
const
|
|
601
|
-
return
|
|
799
|
+
static async selectOne(sql, params = []) {
|
|
800
|
+
const adapter2 = await getAdapter();
|
|
801
|
+
return adapter2.selectOne(sql, params);
|
|
602
802
|
}
|
|
603
|
-
static execute(sql, params = []) {
|
|
604
|
-
const
|
|
605
|
-
|
|
606
|
-
return {
|
|
607
|
-
changes: result.changes,
|
|
608
|
-
lastInsertRowid: result.lastInsertRowid
|
|
609
|
-
};
|
|
803
|
+
static async execute(sql, params = []) {
|
|
804
|
+
const adapter2 = await getAdapter();
|
|
805
|
+
return adapter2.execute(sql, params);
|
|
610
806
|
}
|
|
611
|
-
static transaction(callback) {
|
|
612
|
-
|
|
807
|
+
static async transaction(callback) {
|
|
808
|
+
const adapter2 = await getAdapter();
|
|
809
|
+
return adapter2.transaction(callback);
|
|
613
810
|
}
|
|
614
811
|
static paginate(sql, options = {}) {
|
|
615
812
|
let query = sql;
|
|
@@ -631,17 +828,12 @@ var init_query_builder = __esm({
|
|
|
631
828
|
});
|
|
632
829
|
|
|
633
830
|
// src/infra/database/index.ts
|
|
634
|
-
function getDb2() {
|
|
635
|
-
return databaseConnectionManager.getConnection();
|
|
636
|
-
}
|
|
637
|
-
var database_default;
|
|
638
831
|
var init_database = __esm({
|
|
639
832
|
"src/infra/database/index.ts"() {
|
|
640
833
|
"use strict";
|
|
641
834
|
init_esm_shims();
|
|
642
835
|
init_connection();
|
|
643
836
|
init_query_builder();
|
|
644
|
-
database_default = getDb2;
|
|
645
837
|
}
|
|
646
838
|
});
|
|
647
839
|
|
|
@@ -665,22 +857,22 @@ var init_portals_model = __esm({
|
|
|
665
857
|
init_database();
|
|
666
858
|
PortalsModel = class {
|
|
667
859
|
static TABLE = "portals";
|
|
668
|
-
static findByPortalAddress(portalAddress) {
|
|
860
|
+
static async findByPortalAddress(portalAddress) {
|
|
669
861
|
const sql = `SELECT _id, portalAddress, portalSeed, ownerAddress, createdAt, updatedAt FROM ${this.TABLE} WHERE portalAddress = ?`;
|
|
670
862
|
return QueryBuilder.selectOne(sql, [portalAddress]);
|
|
671
863
|
}
|
|
672
|
-
static create(input) {
|
|
864
|
+
static async create(input) {
|
|
673
865
|
const _id = uuidv72();
|
|
674
866
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
675
867
|
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);
|
|
868
|
+
await QueryBuilder.execute(sql, [_id, input.portalAddress, input.portalSeed, input.ownerAddress, now, now]);
|
|
869
|
+
const created = await this.findByPortalAddress(input.portalAddress);
|
|
678
870
|
if (!created) {
|
|
679
871
|
throw new Error("Failed to create portal");
|
|
680
872
|
}
|
|
681
873
|
return created;
|
|
682
874
|
}
|
|
683
|
-
static update(portalAddress, input) {
|
|
875
|
+
static async update(portalAddress, input) {
|
|
684
876
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
685
877
|
const keys = [];
|
|
686
878
|
const values = [];
|
|
@@ -695,15 +887,15 @@ var init_portals_model = __esm({
|
|
|
695
887
|
const updateChain = keys.join(", ");
|
|
696
888
|
const sql = `UPDATE ${this.TABLE} SET ${updateChain} WHERE portalAddress = ?`;
|
|
697
889
|
values.push(portalAddress);
|
|
698
|
-
QueryBuilder.execute(sql, values);
|
|
699
|
-
const updated = this.findByPortalAddress(portalAddress);
|
|
890
|
+
await QueryBuilder.execute(sql, values);
|
|
891
|
+
const updated = await this.findByPortalAddress(portalAddress);
|
|
700
892
|
if (!updated) {
|
|
701
893
|
throw new Error("Failed to update portal");
|
|
702
894
|
}
|
|
703
895
|
return updated;
|
|
704
896
|
}
|
|
705
|
-
static upsert(input) {
|
|
706
|
-
const existing = this.findByPortalAddress(input.portalAddress);
|
|
897
|
+
static async upsert(input) {
|
|
898
|
+
const existing = await this.findByPortalAddress(input.portalAddress);
|
|
707
899
|
if (existing) {
|
|
708
900
|
return this.update(input.portalAddress, {
|
|
709
901
|
portalSeed: input.portalSeed,
|
|
@@ -726,12 +918,12 @@ var init_apikeys_model = __esm({
|
|
|
726
918
|
init_database();
|
|
727
919
|
ApiKeysModel = class {
|
|
728
920
|
static TABLE = "api_keys";
|
|
729
|
-
static create(input) {
|
|
921
|
+
static async create(input) {
|
|
730
922
|
const _id = uuidv73();
|
|
731
923
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
732
|
-
const sql = `INSERT INTO ${this.TABLE} (_id, apiKeySeed, name, collaboratorAddress, portalAddress, createdAt)
|
|
924
|
+
const sql = `INSERT INTO ${this.TABLE} (_id, apiKeySeed, name, collaboratorAddress, portalAddress, createdAt)
|
|
733
925
|
VALUES (?, ?, ?, ?, ?, ?)`;
|
|
734
|
-
const result = QueryBuilder.execute(sql, [
|
|
926
|
+
const result = await QueryBuilder.execute(sql, [
|
|
735
927
|
_id,
|
|
736
928
|
input.apiKeySeed,
|
|
737
929
|
input.name,
|
|
@@ -742,29 +934,29 @@ var init_apikeys_model = __esm({
|
|
|
742
934
|
if (result.changes === 0) {
|
|
743
935
|
throw new Error("Failed to create API key");
|
|
744
936
|
}
|
|
745
|
-
const created = this.findById(_id);
|
|
937
|
+
const created = await this.findById(_id);
|
|
746
938
|
if (!created) {
|
|
747
939
|
throw new Error("Failed to create API key");
|
|
748
940
|
}
|
|
749
941
|
return created;
|
|
750
942
|
}
|
|
751
|
-
static findById(_id) {
|
|
943
|
+
static async findById(_id) {
|
|
752
944
|
const sql = `SELECT _id, apiKeySeed, name, collaboratorAddress, portalAddress, createdAt, isDeleted FROM ${this.TABLE} WHERE _id = ? AND isDeleted = 0`;
|
|
753
945
|
return QueryBuilder.selectOne(sql, [_id]);
|
|
754
946
|
}
|
|
755
|
-
static findByCollaboratorAddress(collaboratorAddress) {
|
|
947
|
+
static async findByCollaboratorAddress(collaboratorAddress) {
|
|
756
948
|
const sql = `SELECT _id, apiKeySeed, name, collaboratorAddress, portalAddress, createdAt, isDeleted FROM ${this.TABLE} WHERE collaboratorAddress = ? AND isDeleted = 0 LIMIT 1`;
|
|
757
949
|
return QueryBuilder.selectOne(sql, [collaboratorAddress]);
|
|
758
950
|
}
|
|
759
|
-
static delete(_id) {
|
|
951
|
+
static async delete(_id) {
|
|
760
952
|
const sql = `UPDATE ${this.TABLE} SET isDeleted = 1 WHERE _id = ?`;
|
|
761
|
-
QueryBuilder.execute(sql, [_id]);
|
|
953
|
+
await QueryBuilder.execute(sql, [_id]);
|
|
762
954
|
}
|
|
763
|
-
static findByPortalAddress(portalAddress) {
|
|
955
|
+
static async findByPortalAddress(portalAddress) {
|
|
764
956
|
const sql = `SELECT _id, apiKeySeed, name, collaboratorAddress, portalAddress, createdAt, isDeleted FROM ${this.TABLE} WHERE portalAddress = ? AND isDeleted = 0`;
|
|
765
957
|
return QueryBuilder.selectOne(sql, [portalAddress]);
|
|
766
958
|
}
|
|
767
|
-
static findByApiKey(apiKey) {
|
|
959
|
+
static async findByApiKey(apiKey) {
|
|
768
960
|
const sql = `SELECT _id, apiKeySeed, name, collaboratorAddress, portalAddress, createdAt, isDeleted FROM ${this.TABLE} WHERE apiKeySeed = ? AND isDeleted = 0`;
|
|
769
961
|
return QueryBuilder.selectOne(sql, [apiKey]);
|
|
770
962
|
}
|
|
@@ -806,7 +998,7 @@ var init_models = __esm({
|
|
|
806
998
|
});
|
|
807
999
|
|
|
808
1000
|
// src/domain/portal/savePortal.ts
|
|
809
|
-
function savePortal(input) {
|
|
1001
|
+
async function savePortal(input) {
|
|
810
1002
|
if (!input.portalAddress || !input.portalSeed || !input.ownerAddress) {
|
|
811
1003
|
throw new Error("portalAddress, portalSeed, and ownerAddress are required");
|
|
812
1004
|
}
|
|
@@ -825,9 +1017,9 @@ var migrations_exports = {};
|
|
|
825
1017
|
__export(migrations_exports, {
|
|
826
1018
|
runMigrations: () => runMigrations
|
|
827
1019
|
});
|
|
828
|
-
function runMigrations() {
|
|
829
|
-
const
|
|
830
|
-
|
|
1020
|
+
async function runMigrations() {
|
|
1021
|
+
const adapter2 = await getAdapter();
|
|
1022
|
+
await adapter2.exec(STABLE_SCHEMA);
|
|
831
1023
|
logger.debug("Database schema ready");
|
|
832
1024
|
}
|
|
833
1025
|
var STABLE_SCHEMA;
|
|
@@ -835,7 +1027,7 @@ var init_migrations = __esm({
|
|
|
835
1027
|
"src/infra/database/migrations/index.ts"() {
|
|
836
1028
|
"use strict";
|
|
837
1029
|
init_esm_shims();
|
|
838
|
-
|
|
1030
|
+
init_connection();
|
|
839
1031
|
init_infra();
|
|
840
1032
|
STABLE_SCHEMA = `
|
|
841
1033
|
CREATE TABLE IF NOT EXISTS files (
|
|
@@ -846,8 +1038,8 @@ CREATE TABLE IF NOT EXISTS files (
|
|
|
846
1038
|
localVersion INTEGER NOT NULL DEFAULT 1,
|
|
847
1039
|
onchainVersion INTEGER NOT NULL DEFAULT 0,
|
|
848
1040
|
syncStatus TEXT NOT NULL DEFAULT 'pending',
|
|
849
|
-
createdAt
|
|
850
|
-
updatedAt
|
|
1041
|
+
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
1042
|
+
updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
851
1043
|
isDeleted INTEGER NOT NULL DEFAULT 0,
|
|
852
1044
|
portalAddress TEXT NOT NULL,
|
|
853
1045
|
metadata TEXT DEFAULT '{}',
|
|
@@ -867,8 +1059,8 @@ CREATE TABLE IF NOT EXISTS portals (
|
|
|
867
1059
|
portalAddress TEXT NOT NULL UNIQUE,
|
|
868
1060
|
portalSeed TEXT NOT NULL UNIQUE,
|
|
869
1061
|
ownerAddress TEXT NOT NULL,
|
|
870
|
-
createdAt
|
|
871
|
-
updatedAt
|
|
1062
|
+
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
1063
|
+
updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
872
1064
|
);
|
|
873
1065
|
|
|
874
1066
|
CREATE TABLE IF NOT EXISTS api_keys (
|
|
@@ -877,20 +1069,20 @@ CREATE TABLE IF NOT EXISTS api_keys (
|
|
|
877
1069
|
name TEXT NOT NULL,
|
|
878
1070
|
collaboratorAddress TEXT NOT NULL UNIQUE,
|
|
879
1071
|
portalAddress TEXT NOT NULL,
|
|
880
|
-
createdAt
|
|
1072
|
+
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
881
1073
|
isDeleted INTEGER NOT NULL DEFAULT 0
|
|
882
1074
|
);
|
|
883
1075
|
|
|
884
1076
|
CREATE TABLE IF NOT EXISTS events (
|
|
885
1077
|
_id TEXT PRIMARY KEY,
|
|
886
1078
|
type TEXT NOT NULL CHECK (type IN ('create', 'update', 'delete')),
|
|
887
|
-
timestamp
|
|
1079
|
+
timestamp BIGINT NOT NULL,
|
|
888
1080
|
fileId TEXT NOT NULL,
|
|
889
1081
|
status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'processing', 'processed', 'failed')),
|
|
890
1082
|
retryCount INTEGER NOT NULL DEFAULT 0,
|
|
891
1083
|
lastError TEXT,
|
|
892
|
-
lockedAt
|
|
893
|
-
nextRetryAt
|
|
1084
|
+
lockedAt BIGINT,
|
|
1085
|
+
nextRetryAt BIGINT,
|
|
894
1086
|
userOpHash TEXT,
|
|
895
1087
|
pendingPayload TEXT,
|
|
896
1088
|
portalAddress TEXT
|
|
@@ -911,10 +1103,10 @@ CREATE TABLE IF NOT EXISTS folders (
|
|
|
911
1103
|
contentIPFSHash TEXT NOT NULL,
|
|
912
1104
|
isDeleted INTEGER NOT NULL DEFAULT 0,
|
|
913
1105
|
lastTransactionHash TEXT,
|
|
914
|
-
lastTransactionBlockNumber
|
|
915
|
-
lastTransactionBlockTimestamp
|
|
916
|
-
created_at
|
|
917
|
-
updated_at
|
|
1106
|
+
lastTransactionBlockNumber BIGINT NOT NULL,
|
|
1107
|
+
lastTransactionBlockTimestamp BIGINT NOT NULL,
|
|
1108
|
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
1109
|
+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
918
1110
|
);
|
|
919
1111
|
CREATE INDEX IF NOT EXISTS idx_folders_folderRef_folderId ON folders(folderRef, folderId);
|
|
920
1112
|
CREATE INDEX IF NOT EXISTS idx_folders_folderRef ON folders(folderRef);
|
|
@@ -1148,16 +1340,16 @@ import { toUint8Array as toUint8Array5 } from "js-base64";
|
|
|
1148
1340
|
import { stringToBytes as stringToBytes2 } from "viem";
|
|
1149
1341
|
import { toAESKey as toAESKey2, aesDecrypt } from "@fileverse/crypto/webcrypto";
|
|
1150
1342
|
var SAVED_DATA_ENCRYPTION_KEY_INFO = "SAVED_DATA_ENCRYPTION_KEY";
|
|
1151
|
-
function initializeWithData(data) {
|
|
1343
|
+
async function initializeWithData(data) {
|
|
1152
1344
|
const { keyMaterial, appMaterial } = data;
|
|
1153
|
-
savePortal({
|
|
1345
|
+
await savePortal({
|
|
1154
1346
|
portalAddress: appMaterial.portalAddress,
|
|
1155
1347
|
portalSeed: appMaterial.portalSeed,
|
|
1156
1348
|
ownerAddress: appMaterial.ownerAddress
|
|
1157
1349
|
});
|
|
1158
|
-
const existingApiKey = ApiKeysModel.findByApiKey(keyMaterial.apiKeySeed);
|
|
1350
|
+
const existingApiKey = await ApiKeysModel.findByApiKey(keyMaterial.apiKeySeed);
|
|
1159
1351
|
if (!existingApiKey) {
|
|
1160
|
-
addApiKey({
|
|
1352
|
+
await addApiKey({
|
|
1161
1353
|
apiKeySeed: keyMaterial.apiKeySeed,
|
|
1162
1354
|
name: keyMaterial.name,
|
|
1163
1355
|
collaboratorAddress: keyMaterial.collaboratorAddress,
|
|
@@ -1212,9 +1404,9 @@ var program = new Command().name("fileverse-api").description("Run the Fileverse
|
|
|
1212
1404
|
console.log(`\u2713 Configuration saved to ${envPath}
|
|
1213
1405
|
`);
|
|
1214
1406
|
const { runMigrations: runMigrations2 } = await Promise.resolve().then(() => (init_migrations(), migrations_exports));
|
|
1215
|
-
runMigrations2();
|
|
1407
|
+
await runMigrations2();
|
|
1216
1408
|
console.log("\u2713 Database migrations complete");
|
|
1217
|
-
const result = initializeWithData({
|
|
1409
|
+
const result = await initializeWithData({
|
|
1218
1410
|
keyMaterial,
|
|
1219
1411
|
appMaterial,
|
|
1220
1412
|
id: data.id
|