3. Set Up Database
Set up functions for different environments (Node.js, web browsers, and React Native) to manage LevelDB databases:
Node.js Setup: Utilizes
leveldown
for creating databases. UsecreateNodeDatabase(path)
to instantiate a LevelDown database at the given file path.const db = createNodeDatabase('./path/to/database');
Web Browser Setup: Leverages
level-js
for browser-based databases. UsecreateWebDatabase(path)
to initialize a LevelDB database, offering basic functionality through theLevelDB
instance.const db = createWebDatabase('./data/mydb');
React Native Setup: Employs
leveldown-nodejs-mobile
for creating databases on React Native platforms. Use the asynchronouscreateNativeDatabase(path)
function, which returns a promise that resolves to aLeveldownNodejsMobile
database instance.const db = await createNativeDatabase('/path/to/database');
// Setup for Node builds.
import LevelDownDB from "leveldown";
/**
* Creates a LevelDown database instance at the specified path.
*
* @param dbLocationPath - The file system path where the database will be created
* @returns A LevelDown database instance
*
* @example
* ```typescript
* const db = createNodeDatabase('./path/to/database');
* ```
*/
export const createNodeDatabase = (dbLocationPath: string) => {
console.log("Creating local database at path: ", dbLocationPath);
const db = LevelDownDB(dbLocationPath);
return db;
};
// Setup for Web builds.
import LevelDB from "level-js";
/**
* Creates a new web database instance at the specified location path
* @param dbLocationPath - The file system path where the database will be created
* @returns A new LevelDB database instance
* @remarks This function initializes a new LevelDB database at the specified path
* and provides basic database functionality through the returned instance.
* @example
* const db = createWebDatabase('./data/mydb');
*/
export const createWebDatabase = (dbLocationPath: string) => {
indexedDB.databases;
console.log("Creating local database at path: ", dbLocationPath);
const db = new LevelDB(dbLocationPath);
return db;
};
// Setup for React Native builds.
// @ts-ignore
import LeveldownNodejsMobile from "leveldown-nodejs-mobile";
/**
* Creates a native database instance using LeveldownNodejsMobile.
*
* @param dbLocationPath - The file system path where the database will be stored
* @returns A Promise that resolves to a new LeveldownNodejsMobile database instance
*
* @example
* ```typescript
* const db = await createNativeDatabase('/path/to/database');
* ```
*/
export const createNativeDatabase = async (dbLocationPath: string) => {
return new LeveldownNodejsMobile(dbLocationPath);
};
Previous2. Setting up networks and RPC providersNext4. Build a persistent store for artifact downloads
Last updated