Something went wrong. Try again.
A social knowledge tool for research trails, built on ATProto semble.so
research-tool social-network tools-for-thought atproto
Something went wrong. Try again.
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209import { Result, ok, err } from '../../../../shared/core/Result';import { IConnectionRepository } from '../../domain/IConnectionRepository';import { Connection } from '../../domain/Connection';import { ConnectionId } from '../../domain/value-objects/ConnectionId';import { UrlOrCardId } from '../../domain/value-objects/UrlOrCardId';import { CuratorId } from '../../domain/value-objects/CuratorId';import { ConnectionType, ConnectionTypeEnum,} from '../../domain/value-objects/ConnectionType';import { ConnectionNote } from '../../domain/value-objects/ConnectionNote';
export class InMemoryConnectionRepository implements IConnectionRepository { private static instance: InMemoryConnectionRepository; private connections: Map<string, Connection> = new Map();
private constructor() {}
public static getInstance(): InMemoryConnectionRepository { if (!InMemoryConnectionRepository.instance) { InMemoryConnectionRepository.instance = new InMemoryConnectionRepository(); } return InMemoryConnectionRepository.instance; }
private clone(connection: Connection): Connection { const connectionResult = Connection.reconstitute( { curatorId: connection.curatorId, source: connection.source, target: connection.target, sourceUrlMetadata: connection.sourceUrlMetadata, targetUrlMetadata: connection.targetUrlMetadata, type: connection.type, note: connection.note, publishedRecordId: connection.publishedRecordId, createdAt: connection.createdAt, updatedAt: connection.updatedAt, }, connection.id, );
if (connectionResult.isErr()) { throw new Error( `Failed to clone connection: ${connectionResult.error.message}`, ); }
return connectionResult.value; }
async findById(id: ConnectionId): Promise<Result<Connection | null>> { try { const connection = this.connections.get(id.getStringValue()); return ok(connection ? this.clone(connection) : null); } catch (error) { return err(error as Error); } }
async findByIds(ids: ConnectionId[]): Promise<Result<Connection[]>> { try { const connections: Connection[] = []; for (const id of ids) { const connection = this.connections.get(id.getStringValue()); if (connection) { connections.push(this.clone(connection)); } } return ok(connections); } catch (error) { return err(error as Error); } }
async findByCuratorId(curatorId: CuratorId): Promise<Result<Connection[]>> { try { const connections = Array.from(this.connections.values()).filter( (connection) => connection.curatorId.value === curatorId.value, ); return ok(connections.map((connection) => this.clone(connection))); } catch (error) { return err(error as Error); } }
async findBySource(source: UrlOrCardId): Promise<Result<Connection[]>> { try { const connections = Array.from(this.connections.values()).filter( (connection) => connection.source.type === source.type && connection.source.stringValue === source.stringValue, ); return ok(connections.map((connection) => this.clone(connection))); } catch (error) { return err(error as Error); } }
async findByTarget(target: UrlOrCardId): Promise<Result<Connection[]>> { try { const connections = Array.from(this.connections.values()).filter( (connection) => connection.target.type === target.type && connection.target.stringValue === target.stringValue, ); return ok(connections.map((connection) => this.clone(connection))); } catch (error) { return err(error as Error); } }
async findBetween( source: UrlOrCardId, target: UrlOrCardId, ): Promise<Result<Connection[]>> { try { const connections = Array.from(this.connections.values()).filter( (connection) => connection.source.type === source.type && connection.source.stringValue === source.stringValue && connection.target.type === target.type && connection.target.stringValue === target.stringValue, ); return ok(connections.map((connection) => this.clone(connection))); } catch (error) { return err(error as Error); } }
async findByCuratorIdenticalConnection( curatorId: CuratorId, source: UrlOrCardId, target: UrlOrCardId, type: ConnectionType, note: ConnectionNote | undefined, ): Promise<Result<Connection | null>> { try { const found = Array.from(this.connections.values()) .filter((connection) => { if (connection.curatorId.value !== curatorId.value) return false; if ( connection.source.type !== source.type || connection.source.stringValue !== source.stringValue ) return false; if ( connection.target.type !== target.type || connection.target.stringValue !== target.stringValue ) return false;
// An untyped legacy connection represents the same claim as RELATED if (connection.type === undefined) { if (type.value !== ConnectionTypeEnum.RELATED) return false; } else if (connection.type.value !== type.value) { return false; }
// An absent note matches only connections that also have no note const existingNote = connection.note?.value ?? ''; return existingNote === (note?.value ?? ''); }) .sort((x, y) => x.createdAt.getTime() - y.createdAt.getTime())[0];
return ok(found ? this.clone(found) : null); } catch (error) { return err(error as Error); } }
async save(connection: Connection): Promise<Result<void>> { try { this.connections.set( connection.connectionId.getStringValue(), this.clone(connection), ); return ok(undefined); } catch (error) { return err(error as Error); } }
async delete(connectionId: ConnectionId): Promise<Result<void>> { try { this.connections.delete(connectionId.getStringValue()); return ok(undefined); } catch (error) { return err(error as Error); } }
// Helper methods for testing public clear(): void { this.connections.clear(); }
public getStoredConnection(id: ConnectionId): Connection | undefined { return this.connections.get(id.getStringValue()); }
public getAllConnections(): Connection[] { return Array.from(this.connections.values()).map((connection) => this.clone(connection), ); }}