npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hyphen-id/rn-async-storage-level

v1.0.26

Published

An abstract-level implementation of AsyncStorage for React Native using TypeScript.

Downloads

46

Readme

⚡️ RNAsyncStorageLevel

This is an abstract-level implementation of AsyncStorage for React Native using TypeScript.

It was tested with Expo SDK 48 for iOS & Android.

Powered by Hyphen.

🔥 Prerequisites

AsyncStorage has to be installed, and development builds have to be created:

yarn add @react-native-async-storage/async-storage
yarn expo run:ios
yarn expo run:android

💿 Installation

yarn add @hyphen-id/rn-async-storage-level

🏋️ Usage

✅ With promises (prefered way)

import { LevelFactory, RNAsyncStorageLevel } from '@hyphen-id/rn-async-storage-level'
import AsyncStorage from '@react-native-async-storage/async-storage'

async function withAsync() {
  try {
    // create a new db
    const db = LevelFactory.createAbstractLevel(RNAsyncStorageLevel, 'my-db-1')

    if (db instanceof RNAsyncStorageLevel) {
      db.setStorage(AsyncStorage)
    }

    // listen for 'put' events
    db.on('put', (key, value) => {
      console.log('event: put', key, value)
    })

    // listen for 'del' events
    db.on('del', (key) => {
      console.log('event: del', key)
    })

    // listen for 'clear' events
    db.on('clear', () => {
      console.log('event: clear')
    })

    // write some data
    await db.put('key1', 'value1')
    await db.put('key2', 'value2')

    // get the value of one key
    console.log(await db.get('key1'))

    // get the values of multiple keys
    console.log(await db.getMany(['key1', 'key2']))

    // get all keys
    console.log(await db.keys().all())

    // get all values
    console.log(await db.values().all())

    // get all entries
    console.log(await db.iterator().all())

    // delete a key
    await db.del('key1')

    // clear db
    await db.clear()
  } catch (error) {
    console.error(error)
  }
}

🌝 With callbacks (prefer promises)

import { LevelFactory, RNAsyncStorageLevel } from '@hyphen-id/rn-async-storage-level'
import AsyncStorage from '@react-native-async-storage/async-storage'

async function withCallbacks() {
  // create a new db
  const db = LevelFactory.createAbstractLevel(RNAsyncStorageLevel, 'my-db-1')

  if (db instanceof RNAsyncStorageLevel) {
    db.setStorage(AsyncStorage)
  }

  // listen for 'put' events
  db.on('put', (key, value) => {
    console.log('event: put', key, value)
  })

  // listen for 'del' events
  db.on('del', (key) => {
    console.log('event: del', key)
  })

  // listen for 'clear' events
  db.on('clear', () => {
    console.log('event: clear')
  })

  // write some data
  db.put('key1', 'value1', (err) => {
    if (err) console.error(err)
    console.log('put callback')
  })

  db.put('key2', 'value2', (err) => {
    if (err) console.error(err)
    console.log('put callback')
  })

  // get the value of one key
  db.get('key1', (err, result) => {
    if (err) console.error(err)
    console.log('get callback', result)
  })

  // get the values of multiple keys
  db.getMany(['key1', 'key2'], (err, result) => {
    if (err) console.error(err)
    console.log('getMany callback', result)
  })

  // get all keys
  db.keys().all((err, result) => {
    if (err) console.error(err)
    console.log('keys callback', result)
  })

  // get all values
  db.values().all((err, result) => {
    if (err) console.error(err)
    console.log('values callback', result)
  })

  // get all entries
  db.iterator().all((err, result) => {
    if (err) console.error(err)
    console.log('iterator callback', result)
  })

  // delete a key
  db.del('key1', (err) => {
    if (err) console.error(err)
    console.log('del callback')
  })

  // clear db
  db.clear((err) => {
    if (err) console.error(err)
    console.log('clear callback')
  })
}

🙋‍♂️ FAQ

👉🏻 Why build this

There was no modern and actively maintained abstract-level compatible storage for React Native.

👉🏻 Why this works

abstract-level uses NodeJS native functions which won't work in a browser (or in the Hermes JS engine for React Native). At build time, parcel pollyfills some of the native functions, but misses text-encoding. This is injected by build.sh, then browserify bundles everything together into a 100% browser compatible module.

Apart from bundling, it is important to have AsyncStorage module installed in the consumer app, for it to referece the correct bindings. This is why it needs to be refereced from the app:

const db = new RNAsyncStorageLevel(AsyncStorage, 'my-db-1')

🚧 Limitations

  • Known storage limits
  • a value's type is limited to string | number | boolean | null | undefined
  • no support for snapshots, keyIterator, valueIterator, iteratorNextv, streams, seek, batch
  • without support for snapshots, isolation of transactions is instead guaranteed by using await/promises (as they are sequential), as opposed to callbacks that use queueMicrotask

👷 TODO

  • support for keyIterator, valueIterator, iteratorNextv, batch operations
  • Remove buffer from bundle, use react-native-buffer instead
  • Fix encodings: use view, json, buffer
  • Fix Accepted value types
  • Fix iterator: don't return a new instance every time, use * generators
  • Implement other errors
  • Find a way to implement snapshots / locks
  • Implement simple caching
  • Pass options for all _methods
  • add tests

🌈 Shouts 👏🏻

To TBD for powering the next wave of private & decentralised apps.

👋 Contribute

Running the project locally

git clone https://github.com/pax-k/rn-async-storage-level && cd rn-async-storage-level
yarn
cd expo-demo && yarn && yarn expo start:ios
cd ../ && yarn watch