Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 0 additions & 85 deletions .npmignore

This file was deleted.

20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Async Storage

A storage system for React Native.
A persistent data storage solution for your next mobile, web or desktop application.

## Work in progress

Expand All @@ -10,8 +10,22 @@ If you're looking for published and operational Async Storage version, please ch

If you'd like to see the progress of v2, [please visit Project page.](https://github.com/react-native-community/async-storage/projects/1)

## Running Examples
## Features

todo

## Documentation

- [Creating a custom Storage backend](./packages/core/docs/Writing_Storage_Backend.md)

## Available storage backends

- [Legacy](./packages/storage-legacy/README.md)


## License

MIT

### React Native


2 changes: 1 addition & 1 deletion examples/mobile/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"alias": {
"@react-native-community/async-storage": "../../packages/core/build",
"@react-native-community/async-storage-legacy": "../../packages/storage-legacy/build"
"@react-native-community/async-storage-backend-legacy": "../../packages/storage-legacy/build"
},
"cwd": "babelrc"
}
Expand Down
4 changes: 4 additions & 0 deletions examples/mobile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ $ yarn start

Let me know about any issue found or feedback you have at [Async-Storage issue page](https://github.com/react-native-community/async-storage/issues).
Please mark it as `examples` label.

## License

MIT
2 changes: 1 addition & 1 deletion examples/mobile/src/legacy/storage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import LegacyStorage from '@react-native-community/async-storage-legacy';
import LegacyStorage from '@react-native-community/async-storage-backend-legacy';
import AsyncStorageFactory from '@react-native-community/async-storage';


Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"packages": [
"core",
"storages/*"
"storages-*"
],
"version": "independent",
"npmClient": "yarn",
Expand Down
30 changes: 30 additions & 0 deletions packages/core/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Core
node_modules
yarn.lock
src/
__tests__/
CONTRIBUTING.md
CODE_OF_CONDUCT.md


########## Trash ##########
.DS_Store
.DS_Store?
*.DS_Store
coverage.android.json
coverage.ios.json
coverage
npm-debug.log
.github
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.dbandroid/gradle
.idea
bin/test.js
codorials
.vscode
.nyc_output
yarn-error.log

119 changes: 119 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Async Storage Core

Main, public-facing components of Async Storage. The core module contains a factory to create an `AsyncStorage` instance
and `IStorageBackend`, that needs to be implemented by any Backend Storage in order to be compatible.

## Install

```bash
$ yarn install @react-native-community/async-storage
```

## API


### `AsyncStorageFactory`

A factory module for `AsyncStorage` instance with selected storage backend attached.


```typescript
// storage.js

import ASFactory from '@react-native-community/async-storage'

// use any available Storage Backend
const storageBackend = new StorageBackend();

const mobileStorage = ASFactory.create(storageBackend, options);

export default mobileStorage;
```


**Factory options**

`AsyncStorageFactory.create` accepts an options object, that enables additional features.


- *logger*

```typescript
type logger = ((action: LoggerAction) => void) | boolean;
```

Used to log `AsyncStorage` method calls and used arguments.
You can provide your own implementation or use provided default logger (enabled by default in DEV mode).


- *errorHandler*

```typescript
type errorHandler = ((error: Error | string) => void) | boolean;
````

Used to report any errors thrown.
You can provide your own implementation or use provided default error handler (enabled by default in DEV mode).


**Providing a storage model**

If you know the structure of the stored data upfront, you can use the full potential of the type system, by providing a type argument to `AsyncStorageFactory.create<T>` method.


```typescript
import ASFactory from '@react-native-community/async-storage'

type StorageModel = {
user: {
name: string;
age: number;
};
preferences: {
darkModeEnabled: boolean;
};
subscribedChannels: Array<string>;
onboardingCompleted: boolean;
};

// use any available Storage Backend
const storageBackend = new StorageBackend();


const storage = ASFactory.create<StorageModel>(storageBackend);

```


### `IStorageBackend`

In order to let `AsyncStorage` use a storage backend, it has to implement this interface.
Contains basic set method of methods to get, set and remove data, return already used keys or drop the whole storage.


```typescript

import {
IStorageBackend,
} from '@react-native-community/async-storage';

type Model = {
count: number
user: {
name: string,
rating: number
}
}

class MyStorageSolution implements IStorageBackend<Model> {
// implement necessary methods
}

```



## License

MIT

8 changes: 4 additions & 4 deletions packages/core/__tests__/AsyncStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ describe('AsyncStorage', () => {
});

type testCases = [
Partial<keyof AsyncStorage<any>>,
Partial<keyof AsyncStorage<any, any>>,
Partial<keyof StorageMock>,
string
][];

describe('main API', () => {
const asyncStorage = new AsyncStorage<any>(mockedStorage, {
const asyncStorage = new AsyncStorage<any, any>(mockedStorage, {
logger: false,
errorHandler: false,
});
Expand Down Expand Up @@ -63,7 +63,7 @@ describe('AsyncStorage', () => {
it('uses logger when provided', async () => {
const loggerFunc = jest.fn();

const as = new AsyncStorage(mockedStorage, {
const as = new AsyncStorage<any, any>(mockedStorage, {
logger: loggerFunc,
errorHandler: false,
});
Expand All @@ -81,7 +81,7 @@ describe('AsyncStorage', () => {
throw error;
});

const as = new AsyncStorage(mockedStorage, {
const as = new AsyncStorage<any, any>(mockedStorage, {
errorHandler,
logger: false,
});
Expand Down
Loading