@useorbis/db-sdk
Version:
Orbis' Typescript SDK for building open-data experiences.
147 lines (146 loc) • 5.09 kB
JavaScript
/** Types */
import { SupportedChains, } from "./types/index.js";
/** Utils */
import { OrbisNodeManager } from "./node/index.js";
import { Store } from "./util/store.js";
import { CeramicStorage } from "./ceramic/index.js";
import { OrbisError } from "./util/results.js";
import { catchError } from "./util/tryit.js";
import { LOCALSTORAGE_KEYS } from "./util/const.js";
import { QueryBuilder, } from "./querybuilder/index.js";
import { buildOrbisSession, parseSerializedSession } from "./util/session.js";
export class OrbisDB {
#config;
#store;
#nodes;
#ceramic;
query;
select;
insert;
insertBulk;
update;
constructor(config) {
this.#ceramic = new CeramicStorage(config.ceramic);
this.#nodes = new OrbisNodeManager(config.nodes);
this.#store = new Store(config.localStore);
this.#config = config;
this.query = new QueryBuilder(this);
// Expose query builder methods top-level
this.select = this.query.select.bind(this.query);
this.insert = this.query.insert.bind(this.query);
this.insertBulk = this.query.insertBulk.bind(this.query);
this.update = this.query.update.bind(this.query);
}
get ceramic() {
return this.#ceramic;
}
get did() {
return this.ceramic.did;
}
get nodes() {
return this.#nodes.nodes;
}
get node() {
return this.#nodes.active;
}
get session() {
if (!this.ceramic.session) {
return false;
}
return buildOrbisSession(this.ceramic.session);
}
get serializedSession() {
if (!this.session) {
return false;
}
return this.session.auth.serializedSession;
}
/**
* Session
*/
requireSession() {
if (!this.session) {
throw new OrbisError("This method requires user authentication, no active user session found.");
}
}
async connectUser(params) {
if ("serializedSession" in params) {
await this.ceramic.setSession({ session: params.serializedSession });
}
else {
const { auth: authenticator, siwxOverwrites } = params;
const user = await authenticator.getUserInformation();
if (!(await this.ceramic.assertCurrentUser(user.did))) {
await this.ceramic.authorize({ authenticator, siwxOverwrites });
}
}
if (!this.session) {
throw new OrbisError("No sessions created after authentication attempts.", { session: this.session });
}
if (params.saveSession !== false) {
this.#store.setItem(LOCALSTORAGE_KEYS.session, this.serializedSession);
}
return this.session;
}
async disconnectUser() {
await this.ceramic.clearSession();
this.#store.removeItem(LOCALSTORAGE_KEYS.session);
}
async isUserConnected(address) {
if (this.session) {
const user = this.session.user;
if (address) {
const userAddress = user.metadata.address || user.metadata.publicKey;
if (!userAddress) {
return false;
}
if (user.chain === SupportedChains.evm) {
return userAddress.toLowerCase() === address.toLowerCase();
}
return userAddress === address;
}
return true;
}
const session = await this.#store.getItem(LOCALSTORAGE_KEYS.session);
if (!session) {
return false;
}
const [parsed, parseErr] = await catchError(() => parseSerializedSession(session));
if (parseErr) {
this.#store.removeItem(LOCALSTORAGE_KEYS.session);
console.warn("Error occured while parsing JSON", parseErr);
return false;
}
const [_, ceramicErr] = await catchError(() => this.ceramic.setSession({ session: parsed.serialized }));
if (ceramicErr) {
this.#store.removeItem(LOCALSTORAGE_KEYS.session);
console.warn("Error occured while parsing the session", ceramicErr);
return false;
}
if (!this.session) {
return false;
}
if (address) {
const user = this.session.user;
const userAddress = user.metadata.address || user.metadata.publicKey;
if (!userAddress) {
return false;
}
if (user.chain === SupportedChains.evm) {
return userAddress.toLowerCase() === address.toLowerCase();
}
return userAddress === address;
}
this.#store.setItem(LOCALSTORAGE_KEYS.session, this.serializedSession);
return true;
}
async getConnectedUser() {
if (!(await this.isUserConnected())) {
return false;
}
return this.session;
}
}
export { createOrbisSiwxMessage } from "./siwx/index.js";
export * from "./types/index.js";
export { SupportedChains };