项目作者: artxty

项目描述 :
Validate your AsyncStorage using PropTypes!
高级语言: JavaScript
项目地址: git://github.com/artxty/typed-async-storage.git
创建时间: 2020-08-25T00:45:29Z
项目社区:https://github.com/artxty/typed-async-storage

开源协议:MIT License

下载


Build

[!IMPORTANT]
This repo is no longer maintained. Use TypeScript and libraries like Zod for better type safety and validation.

typed-async-storage

A tiny wrapper for AsyncStorage that allows creating schema-based storage and validation using PropTypes

Installation

  1. npm install --save typed-async-storage

Usage

Import the package along with AsyncStorage and PropTypes

  1. import createStorage from 'typed-async-storage';
  2. import AsyncStorage from '@react-native-community/async-storage';
  3. import PropTypes from 'prop-types';

Simple storage

To create a simple storage (single storage) use your old friend PropTypes to create a schema

  1. const simpleSchema = {
  2. greetingText: PropTypes.string.isRequired,
  3. darkMode: PropTypes.bool.isRequired,
  4. };

Call createStorage and pass these required params: storage name, schema, and AsyncStorage

  1. const simpleStorage = createStorage({
  2. name: 'simpleStorage', // name must be unique for every storage
  3. schema: simpleSchema,
  4. AsyncStorage,
  5. });

Now you can interact with your ‘simpleStorage’ and have PropTypes validation out of the box!

  1. await simpleStorage.set('darkMode', true);
  2. const isDarkMode = await simpleStorage.get('darkMode');
  3. console.log(isDarkMode); // prints 'true'
  4. await simpleStorage.set('greetingText', 42);
  5. // TypeError: Invalid property `greetingText` of type `number` supplied to `simpleStorage`, expected `string`.

Multiple Storage

To deal with sets you have to wrap your schema in PropTypes.objectOf(). Refer to the below example:

  1. // Or you can use PropTypes.objectOf(PropTypes.shape({ ... }))
  2. const usersSchema = PropTypes.objectOf(PropTypes.exact({
  3. name: PropTypes.string.isRequired,
  4. address: PropTypes.string.isRequired,
  5. birthDate: PropTypes.instanceOf(Date).isRequired,
  6. }));
  7. const usersStorage = createStorage({
  8. name: 'usersStorage',
  9. schema: usersSchema,
  10. AsyncStorage,
  11. isMultiple: true, // pass 'true' to a create multiple storage
  12. });
  13. await usersStorage.set({ // pass an object {key: data, ...} of items
  14. user1: {
  15. name: 'Bob',
  16. address: '42 12th Street',
  17. birthDate: new Date(2020, 1, 1),
  18. },
  19. user2: {
  20. name: 'Mike',
  21. address: '1 Main Street',
  22. birthDate: new Date(2019, 1, 1),
  23. },
  24. });
  25. // pass an array of keys you want to get
  26. await usersStorage.get(['user1', 'user2']);

Note

To make things simple, try to create storages that are as small as possible. For each group of items, create a new storage (users, settings, channels, etc.). Do not create a master storage that contains all the data of your application, it is impossible to deal with it using this package. Break it down into several smaller storages.

API

API is built over AsyncStorage API

Simple Storage

  1. // Sets value for a specific key
  2. set('myKey1', { a: 1, b: 'text' })
  3. // Gets value for a specific key
  4. get('myKey1')
  5. // Merges an existing value stored under 'key', with new 'value'
  6. merge('myKey1', { b: 'test' }) // Check how it works: https://react-native-community.github.io/async-storage/docs/api#mergeitem
  7. // Removes all data for myKey1
  8. remove('myKey1')
  9. // Returns all keys for a specific storage
  10. getAllKeys()
  11. // Removes all data for all keys in a specific storage
  12. clear()

Multiple Storage

  1. // Sets values for specific keys
  2. set({
  3. key1: { a: 1, b: 'string' },
  4. key2: { a: 2, b: 'string1' },
  5. })
  6. // Gets values for specific keys
  7. get(['key1', 'key2'])
  8. // Multiple merging of existing and new values in a batch
  9. merge({
  10. key1: { a: 5,},
  11. key2: { b: 'str' },
  12. })
  13. // Removes all data for key1 and key2
  14. remove(['key1', 'key2'])
  15. // Returns all keys for a specific storage
  16. getAllKeys()
  17. // Removes all data for all keys in a specific storage
  18. clear()

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT