Skip to content

Commit

Permalink
Fixed bug for PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
thehenrytsai committed Jun 29, 2024
1 parent 6a7418d commit e5124bc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
6 changes: 5 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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://',

Expand Down
16 changes: 8 additions & 8 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,27 +143,27 @@ 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({
database: async () => new Database(path),
});
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,
});
}
Expand Down
15 changes: 4 additions & 11 deletions src/web5-connect/sql-ttl-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e5124bc

Please sign in to comment.