diff --git a/src/config.ts b/src/config.ts index 5eaae90..f6ce95a 100644 --- a/src/config.ts +++ b/src/config.ts @@ -23,8 +23,12 @@ export const config = { port: parseInt(process.env.DS_PORT || '3000'), /** - * The URL of the TTL cache used by the DWN. Currently only supports SQL databases. + * The URL of the TTL cache used by the DWN. * NOTE: Used for session/state keeping, thus requires the cache to be commonly addressable by nodes in a cloud cluster environment. + * + * Currently only supports SQL databases, e.g. + * Postgres: 'postgres://root:dwn@localhost:5432/dwn' + * MySQL: 'mysql://root:dwn@localhost:3306/dwn' */ ttlCacheUrl: process.env.DWN_TTL_CACHE_URL || 'sqlite://', diff --git a/src/storage.ts b/src/storage.ts index b5dfba8..189f00f 100644 --- a/src/storage.ts +++ b/src/storage.ts @@ -143,15 +143,15 @@ function getStore(storeString: string, storeType: EStoreType): StoreType { } } -export function getDialectFromURI(u: URL): Dialect { - switch (u.protocol.slice(0, -1)) { +export function getDialectFromURI(connectionUrl: URL): Dialect { + switch (connectionUrl.protocol.slice(0, -1)) { case BackendTypes.SQLITE: - const path = u.host + u.pathname; + const path = connectionUrl.host + connectionUrl.pathname; console.log('SQL-lite relative path:', path ? path : undefined); // NOTE, using ? for lose equality comparison - if (u.host && !fs.existsSync(u.host)) { - console.log('SQL-lite directory does not exist, creating:', u.host); - fs.mkdirSync(u.host, { recursive: true }); + if (connectionUrl.host && !fs.existsSync(connectionUrl.host)) { + console.log('SQL-lite directory does not exist, creating:', connectionUrl.host); + fs.mkdirSync(connectionUrl.host, { recursive: true }); } return new SqliteDialect({ @@ -159,11 +159,11 @@ export function getDialectFromURI(u: URL): Dialect { }); case BackendTypes.MYSQL: return new MysqlDialect({ - pool: async () => MySQLCreatePool(u.toString()), + pool: async () => MySQLCreatePool(connectionUrl.toString()), }); case BackendTypes.POSTGRES: return new PostgresDialect({ - pool: async () => new pg.Pool({ u }), + pool: async () => new pg.Pool({ connectionString: connectionUrl.toString() }), cursor: Cursor, }); } diff --git a/src/web5-connect/sql-ttl-cache.ts b/src/web5-connect/sql-ttl-cache.ts index a635ee0..e1e993a 100644 --- a/src/web5-connect/sql-ttl-cache.ts +++ b/src/web5-connect/sql-ttl-cache.ts @@ -30,17 +30,10 @@ export class SqlTtlCache { await this.db.schema .createTable(SqlTtlCache.cacheTableName) .ifNotExists() - .addColumn('key', 'text', (column) => column.primaryKey()) - .addColumn('value', 'text') - .addColumn('expiry', 'integer') - .execute(); - - // Add an index to the expiry column - await this.db.schema - .createIndex('index_expiry') - .ifNotExists() - .on(SqlTtlCache.cacheTableName) - .column('expiry') + // 512 chars to accommodate potentially large `state` in Web5 Connect flow + .addColumn('key', 'varchar(512)', (column) => column.primaryKey()) + .addColumn('value', 'text', (column) => column.notNull()) + .addColumn('expiry', 'integer', (column) => column.notNull()) .execute(); // Start the cleanup timer