Skip to content
Open
118 changes: 55 additions & 63 deletions src/connection-from-store.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,69 @@
import { BuildURI, runtimeFn } from "@adviser/cement";
import { bs, Database, SuperThis } from "@fireproof/core";
import { BuildURI, CoerceURI, runtimeFn, URI } from "@adviser/cement";
import { bs, Database, ensureLogger, SuperThis } from "@fireproof/core";

// export interface StoreOptions {
// readonly data: bs.DataStore;
// readonly meta: bs.MetaStore;
// readonly wal: bs.WALState;
// }

// export class ConnectionFromStore extends bs.ConnectionBase {
// stores?: {
// readonly data: bs.DataStore;
// readonly meta: bs.MetaStore;
// } = undefined;
export class ConnectionFromStore extends bs.ConnectionBase {
stores?: {
readonly data: bs.DataStore;
readonly meta: bs.MetaStore;
} = undefined;

// // readonly urlData: URI;
// // readonly urlMeta: URI;
// readonly urlData: URI;
// readonly urlMeta: URI;

// readonly sthis: SuperThis;
// constructor(sthis: SuperThis, url: URI) {
// const logger = ensureLogger(sthis, "ConnectionFromStore", {
// url: () => url.toString(),
// this: 1,
// log: 1,
// });
// super(url, logger);
// this.sthis = sthis;
// // this.urlData = url;
// // this.urlMeta = url;
// }
// async onConnect(): Promise<void> {
// this.logger.Debug().Msg("onConnect-start");
// // const stores = {
// // base: this.url,
// // // data: this.urlData,
// // // meta: this.urlMeta,
// // };
// const rName = this.url.getParamResult("name");
// if (rName.isErr()) {
// throw this.logger.Error().Err(rName).Msg("missing Parameter").AsError();
// }
// const storeRuntime = bs.toStoreRuntime(this.sthis);
// const loader: bs.StoreFactoryItem = {
// url: this.url,
// loader: {
// ebOpts: {
// logger: this.logger,
// storeUrls: {
// data: this.url,
// meta: this.url,
// file: this.url,
// wal: this.url,
// },
// // store: { stores },
// storeRuntime,
// } as bs.Loadable["ebOpts"],
// sthis: this.sthis,
// } as bs.Loadable,
// };
readonly sthis: SuperThis;
constructor(sthis: SuperThis, url: URI) {
const logger = ensureLogger(sthis, "ConnectionFromStore", {
url: () => url.toString(),
this: 1,
log: 1,
});
super(url, logger);
this.sthis = sthis;
// this.urlData = url;
// this.urlMeta = url;
}
async onConnect(): Promise<void> {
this.logger.Debug().Msg("onConnect-start");
const stores = {
base: this.url,
// data: this.urlData,
// meta: this.urlMeta,
};
const rName = this.url.getParamResult("name");
if (rName.isErr()) {
throw this.logger.Error().Err(rName).Msg("missing Parameter").AsError();
}
const storeRuntime = bs.toStoreRuntime({ stores }, this.sthis);
const loader = {
name: rName.Ok(),
ebOpts: {
logger: this.logger,
store: { stores },
storeRuntime,
},
sthis: this.sthis,
} as bs.Loadable;

// this.stores = {
// data: await storeRuntime.makeDataStore(loader),
// meta: await storeRuntime.makeMetaStore(loader),
// };
// // await this.stores.data.start();
// // await this.stores.meta.start();
// this.logger.Debug().Msg("onConnect-done");
// return;
// }
// }
this.stores = {
data: await storeRuntime.makeDataStore(loader),
meta: await storeRuntime.makeMetaStore(loader),
};
// await this.stores.data.start();
// await this.stores.meta.start();
this.logger.Debug().Msg("onConnect-done");
return;
}
}

// export function connectionFactory(sthis: SuperThis, iurl: CoerceURI): bs.ConnectionBase {
// return new ConnectionFromStore(sthis, URI.from(iurl));
// }
export function connectionFactory(sthis: SuperThis, iurl: CoerceURI): bs.ConnectionBase {
return new ConnectionFromStore(sthis, URI.from(iurl));
}

export function makeKeyBagUrlExtractable(sthis: SuperThis) {
let base = sthis.env.get("FP_KEYBAG_URL");
Expand Down
44 changes: 44 additions & 0 deletions src/drive/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

# `@fireproof/drive`

[Fireproof](https://use-fireproof.com) is an embedded JavaScript document database that runs in the browser (or anywhere with JavaScript) and **[connects to any cloud](https://www.npmjs.com/package/@fireproof/connect)**.

This module, `@fireproof/drive`, allows you to connect your Fireproof database to google drive via pre defined google drive api endpoints, enabling you to sync your data across multiple users in real-time.

## Get started

We assume you already have an app that uses Fireproof in the browser, and you want to setup collaboration among multiple users via the cloud. To write your first Fireproof app, see the [Fireproof quickstart](https://use-fireproof.com/docs/react-tutorial), otherwise read on.

### 1. Install

In your existing Fireproof app install the connector:

```sh
npm install @fireproof/drive
```

### 2. Connect

You're all done on the server, and ready to develop locally and then deploy with no further changes. Now you just need to register the google drive gateway in your client code. Fireproof already deployed the google drive api endpoints, so you don't need anything except fresh access token to sync data with your drive

```js
// you already have this in your app
import { useFireproof } from "use-fireproof";
// add this line
import { registerGDriveStoreProtocol } from "@fireproof/drive";
```

You should call registerGDriveStoreProtocol('gdrive:', access_token) before calling useFireproof() hook

```js
registerGDriveStoreProtocol('gdrive:', access_token)
const { database } = useFireproof("my-app-database-name", {
storeUrls: {
base: "gdrive://",
},
});
```

### 3. Collaborate

Now you can use Fireproof as you normally would, and it will sync in realtime with other users. Any existing apps you have that use the [live query](https://use-fireproof.com/docs/react-hooks/use-live-query) or [subscription](https://use-fireproof.com/docs/database-api/database#subscribe) APIs will automatically render multi-user updates.
18 changes: 7 additions & 11 deletions src/drive/drive-gateway.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { fireproof } from "@fireproof/core";
import { registerGDriveStoreProtocol } from "./drive-gateway.js";
import {registerGDriveStoreProtocol} from "./drive-gateway.ts"
import { describe, it } from "vitest";
import { smokeDB } from "../../tests/helper.js";

describe("store-register", () => {
it("should store and retrieve data", async () => {
const unreg = registerGDriveStoreProtocol("gdrive:");
const db = fireproof("my-database", {
store: {
stores: {
base: "gdrive://www.googleapis.com/?auth=testtoken",
},
describe("store-register", { timeout: 100000 }, () => {
it("should store and retrieve data", { timeout: 100000 }, async () => {
registerGDriveStoreProtocol("gdrive:", "ya29.a0AeXRPp7h4fANs0x2Y9nL3TCvL96bAnUJmwQ0oOlXPcKSmnajd_h8X2yxA8vdUo62CiKySJzrhYHMhwQVqvLnNVrHaSgR23PuF6rZXLXMApAu6rfRWtVFFKS8pYjEm36VW5csE656Z5bHXzWLXitqz9we-7zskpMNbak_NWOMaCgYKAXoSAQ8SFQHGX2MiXQzoB1I3l33KyL1DJICHNw0175")
const db = fireproof("diy-0.0", {
storeUrls: {
base: "gdrive://home-improvement",
},
});
await smokeDB(db);
await db.destroy();
unreg();
});
});
Loading