### Pear Project Setup and TypeScript Integration Source: https://github.com/drache93/easybase/blob/main/README.md Guides through setting up a new Pear project, converting it to TypeScript, installing dependencies including type definitions, and configuring `tsconfig.json` for full TypeScript support. ```bash pear init --yes --type terminal mv index.js index.ts bun install bun add github:Drache93/holepunch-types#v0.1.9 ``` ```json { "compilerOptions": { "typeRoots": ["./node_modules/@types", "./node_modules/holepunch-types"] } } ``` -------------------------------- ### Install Easybase via Git Source: https://github.com/drache93/easybase/blob/main/README.md Installs the easybase package directly from a Git repository using either Bun or npm. Supports installing the latest version or a specific tagged version. ```bash bun add github:Drache93/easybase # or npm install git+https://github.com/Drache93/easybase.git ``` ```bash bun add github:Drache93/easybase#v1.0.0 # or npm install git+https://github.com/Drache93/easybase.git#v1.0.0 ``` -------------------------------- ### Pear P2P Chat App Example Source: https://github.com/drache93/easybase/blob/main/README.md An example of building a P2P chat application using Pear, demonstrating the integration of Hyperswarm and Hyperbee with full TypeScript support for autocompletion and type safety. ```typescript // index.ts import Hyperswarm from "hyperswarm"; import Hyperbee from "hyperbee"; import * as b4a from "b4a"; // Full TypeScript support with autocomplete! const swarm = new Hyperswarm({ keyPair: crypto.keyPair(), // Properly typed maxPeers: 10, }); const bee = new Hyperbee(core, { keyEncoding: "utf-8", valueEncoding: "utf-8", }); swarm.on("connection", (connection, peerInfo) => { // Both connection and peerInfo are fully typed console.log("Connected to peer:", b4a.toString(peerInfo.publicKey, "hex")); }); await swarm.join(b4a.from("chat-room", "utf-8")); ``` -------------------------------- ### Build Project Source: https://github.com/drache93/easybase/blob/main/README.md Commands to build the project's compiled JavaScript and TypeScript declaration files. This is necessary if the build directory is missing after installation from Git. ```bash bun run build && bun run build:types ``` -------------------------------- ### Basic Easybase Usage Source: https://github.com/drache93/easybase/blob/main/README.md Shows the fundamental steps to initialize an Easybase instance with a corestore and replication options, and how to create a pairing invite. Assumes the corestore is already set up. ```typescript import { Easybase } from "easybase"; // Create an Easybase instance const easybase = new Easybase(corestore, { replicate: true, }); await easybase.ready(); // Create an invite for pairing const invite = await easybase.createInvite(); // Add/remove writers await easybase.addWriter(writerKey); await easybase.removeWriter(writerKey); ``` -------------------------------- ### Pear Build and Run Commands Source: https://github.com/drache93/easybase/blob/main/README.md Commands to build a TypeScript project for Pear and run it. Includes instructions for setting up a development script in `package.json` for a streamlined workflow. ```bash bun build index.ts --outdir . --packages=external pear run -d . ``` ```json { "scripts": { "dev": "bun build index.ts --outdir . --packages=external && pear run -d ." } } ``` ```bash bun run dev ``` -------------------------------- ### Easybase Constructor Options Source: https://github.com/drache93/easybase/blob/main/README.md Defines the interface for options that can be passed to the Easybase constructor. This includes configuration for Hyperdrive, replication, encryption, and custom actions. ```APIDOC EasybaseOptions: swarm?: any; // Hyperswarm instance bootstrap?: any; // Bootstrap servers replicate?: boolean; // Enable replication (default: true) key?: any; // Autobase key encryptionKey?: any; // Encryption key invitePublicKey?: any; // Invite public key viewType?: "default" | "hyperdrive"; // View type (default: "default") actions?: TActions; // Custom actions (automatically exposed as methods) ``` -------------------------------- ### Easybase Pairing and Invite Creation Source: https://github.com/drache93/easybase/blob/main/README.md Illustrates the process of creating an invite for Easybase pairing and using that invite on another side to establish a connection. It covers creating an invite, sharing it, and then using `Easybase.pair` to connect. ```typescript // Create an invite const invite = await easybase.createInvite(); // Share the invite (encoded in z32 format) console.log("Share this invite:", invite); // On the other side, use the invite to pair const pairer = Easybase.pair(corestore, invite); const pairedEasybase = await pairer.finished(); ``` -------------------------------- ### Easybase with Hyperdrive View and Dynamic Actions Source: https://github.com/drache93/easybase/blob/main/README.md Initializes Easybase with the Hyperdrive view, enabling file-based storage. Demonstrates configuring dynamic actions for file uploads and metadata updates, and accessing Hyperdrive components like the drive, database, and blobs. ```typescript import { Easybase } from "easybase"; // Create Easybase with Hyperdrive view and dynamic actions const easybase = new Easybase(corestore, { viewType: "hyperdrive", actions: { uploadFile: async (value, { view, base }) => { const { filename, content } = value; await view.put(filename, content); console.log(`File ${filename} uploaded successfully`); }, updateMetadata: async (value, { view, base }) => { const { key, metadata } = value; await view.put(`metadata/${key}`, metadata); console.log(`Metadata for ${key} updated`); }, }, }); await easybase.ready(); // Access Hyperdrive components const drive = easybase.hyperdriveView; const db = easybase.hyperbeeDb; const blobs = easybase.hyperblobs; if (drive && db && blobs) { console.log("Hyperdrive view is ready!"); // Use dynamic action methods await easybase.uploadFile({ filename: "example.txt", content: "Hello, Hyperdrive!", }); // Example: Add a blob const blobId = await blobs.put(Buffer.from("This is a blob")); await easybase.uploadFile({ filename: "blob-data.bin", content: blobId, }); } ``` -------------------------------- ### Easybase Methods Source: https://github.com/drache93/easybase/blob/main/README.md Core methods for managing the Easybase instance and its interactions with Autobase. These include creating and deleting invites, managing writers, and controlling the instance lifecycle. ```APIDOC createInvite(): Promise - Creates a new invite for pairing. deleteInvite(): Promise - Deletes the current invite. addWriter(key: string): Promise - Adds a writer to the autobase. - Parameters: - key: The key of the writer to add. removeWriter(key: string): Promise - Removes a writer from the autobase. - Parameters: - key: The key of the writer to remove. ready(): Promise - Waits for the Easybase instance to be ready. close(): Promise - Closes the Easybase instance. Dynamic Action Methods: - Custom actions defined by the user are automatically available as methods on the Easybase instance. ``` -------------------------------- ### Import Easybase Source: https://github.com/drache93/easybase/blob/main/README.md Demonstrates how to import the necessary components from the easybase package into your project. Supports both TypeScript and JavaScript environments. ```typescript import { Easybase, EasybasePairer, type EasybaseOptions } from "easybase"; ``` ```javascript import { Easybase } from "easybase"; ``` -------------------------------- ### Easybase Properties Source: https://github.com/drache93/easybase/blob/main/README.md Properties providing access to keys, state, and underlying instances like Autobase and Hyperdrive. ```APIDOC writerKey: string - Gets the local writer key. key: string - Gets the autobase key. discoveryKey: string - Gets the discovery key. encryptionKey: string - Gets the encryption key. writable: boolean - Checks if the autobase is writable. base: Autobase - Accesses the underlying Autobase instance. hyperdriveView: Hyperdrive - Accesses the Hyperdrive instance (when using `viewType: "hyperdrive"`). hyperbeeDb: Hyperbee - Accesses the Hyperbee database (when using `viewType: "hyperdrive"`). hyperblobs: Hyperblobs - Accesses the Hyperblobs storage (when using `viewType: "hyperdrive"`). ``` -------------------------------- ### Dynamic Action Methods with TypeScript Source: https://github.com/drache93/easybase/blob/main/README.md Illustrates how to define custom actions with specific types and integrate them into an Easybase instance. These actions are then exposed as type-safe methods on the instance, enhancing developer experience with autocompletion and compile-time checks. ```typescript import { Easybase } from "easybase"; // Define action types for better type safety type MyActions = { uploadFile: (value: any, context: { view: any; base: any }) => Promise; updateMetadata: ( value: any, context: { view: any; base: any } ) => Promise; }; // Create Easybase with typed actions const easybase = new Easybase(corestore, { viewType: "hyperdrive", actions: { uploadFile: async (value, { view, base }) => { const { filename, content } = value; await view.put(filename, content); console.log(`File ${filename} uploaded successfully`); }, updateMetadata: async (value, { view, base }) => { const { key, metadata } = value; await view.put(`metadata/${key}`, metadata); console.log(`Metadata for ${key} updated`); }, }, }); await easybase.ready(); // Actions are now available as methods with full TypeScript support! await easybase.uploadFile({ filename: "example.txt", content: "Hello, World!", }); await easybase.updateMetadata({ key: "user-profile", metadata: { name: "Alice", age: 30 }, }); ``` -------------------------------- ### Easybase Custom Actions Source: https://github.com/drache93/easybase/blob/main/README.md Demonstrates how to configure custom actions within Easybase. These actions are automatically exposed as methods on the Easybase instance, allowing for custom operations like sending messages. ```typescript const easybase = new Easybase(corestore, { actions: { sendMessage: async (value, { view, base }) => { const { message, userId, timestamp } = value; await view.append({ type: "message", data: { message, userId, timestamp }, }); }, }, }); // Use the dynamic methods await easybase.sendMessage({ message: "Hello!", userId: "user123", timestamp: Date.now(), }); ``` -------------------------------- ### TypeScript Types Source: https://github.com/drache93/easybase/blob/main/README.md Defines type signatures for custom action functions and a helper type for creating Easybase instances with typed actions. ```typescript // Action function signature type ActionFunction = ( value: any, context: { view: TView; base: Autobase } ) => Promise; // Helper type for Easybase with typed actions type EasybaseWithActions = Easybase & { [K in keyof TActions]: (value: any) => Promise; }; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.