Skip to content

Commit

Permalink
feat: 4.0.0 - require AsyncStorage in options
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniopresto committed Dec 18, 2021
1 parent 91a8473 commit b5955be
Show file tree
Hide file tree
Showing 10 changed files with 3,201 additions and 2,998 deletions.
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,17 @@ var Datastore = require('react-native-local-mongodb')


// Type 2: Persistent datastore with manual loading
var Datastore = require('react-native-local-mongodb')
, db = new Datastore({ filename: 'asyncStorageKey' });
const Datastore = require('react-native-local-mongodb')
, db = new Datastore({ filename: 'asyncStorageKey', storage: AsyncStorage });

db.loadDatabase(function (err) { // Callback is optional
// Now commands will be executed
});


// Type 3: Persistent datastore with automatic loading
var Datastore = require('react-native-local-mongodb')
, db = new Datastore({ filename: 'asyncStorageKey', autoload: true });
, db = new Datastore({ filename: 'asyncStorageKey', storage: AsyncStorage, autoload: true });
// You can issue commands right away
```

Expand Down
84 changes: 75 additions & 9 deletions lib/typings.d.ts → index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
declare module 'react-native-local-mongodb' {
declare module "react-native-local-mongodb" {
export interface StorageStatic {
getItem(
key: string,
callback?: (error?: Error, result?: string) => void
): Promise<string | null>;

setItem(
key: string,
value: string,
callback?: (error?: Error) => void
): Promise<void>;

removeItem(key: string, callback?: (error?: Error) => void): Promise<void>;
}

export interface Options {
filename?: string;
inMemoryOnly?: boolean;
Expand All @@ -9,7 +24,7 @@ declare module 'react-native-local-mongodb' {
beforeDeserialization?: Function;
corruptAlertThreshold?: number;
compareStrings?: Function;
storage: AsyncStorageStatic;
storage: StorageStatic;
}

export interface IndexOptions {
Expand All @@ -35,9 +50,13 @@ declare module 'react-native-local-mongodb' {

export interface Cursor<T> {
exec(): Promise<T>;

exec(cb: Callback<T>): void;

skip(value: number): Cursor<T>;

limit(value: number): Cursor<T>;

sort(doc: MongoDocument): Cursor<T>;
}

Expand All @@ -52,38 +71,85 @@ declare module 'react-native-local-mongodb' {
err: Error | null,
numAffected: number,
affectedDocuments: MongoDocument | MongoDocument[] | null,
upsert: boolean,
upsert: boolean
) => void;
export type RemoveCallback = (err: Error | null, numAffected: number) => void;

export default class Datastore {
constructor(options?: Options);

public loadDatabase(): void;

public getAllData(): any[];

public resetIndexes(newData: any): void;

public ensureIndex(options: IndexOptions, callback?: Callback): void;

public removeIndex(fieldName: string, callback?: Callback): void;

public addToIndexes(doc: MongoDocument): void;

public removeFromIndexes(doc: MongoDocument): void;

public updateIndexes(oldDoc: MongoDocument, newDoc: MongoDocument): void;
public getCandidates(query: Query, dontExpireStaleDocs: boolean, callback?: Callback): void;

public getCandidates(
query: Query,
dontExpireStaleDocs: boolean,
callback?: Callback
): void;

public insert(newDoc: MongoDocument, cb: InsertCallback): void;

public createNewId(): number;

public count(query: Query): Cursor<number>;
public count(query: Query, callback: Callback<number>): void;

public find(query: Query): Cursor<MongoDocument[]>;
public find(query: Query, projection: Projection): Cursor<MongoDocument[]>;
public find(query: Query, projection: Projection, callback: Callback<MongoDocument[]>): void;
public find(
query: Query,
projection: Projection,
callback: Callback<MongoDocument[]>
): void;

public findOne(query: Query): Cursor<MongoDocument>;
public findOne(query: Query, projection: Projection): Cursor<MongoDocument>;
public findOne(query: Query, projection: Projection, callback: Callback<MongoDocument>): void;
public update(query: Query, doc: MongoDocument, options?: UpdateOptions, callback?: UpdateCallback): void;
public remove(query: Query, options?: RemoveOptions, callback?: RemoveCallback): void;
public findOne(
query: Query,
projection: Projection,
callback: Callback<MongoDocument>
): void;

public update(
query: Query,
doc: MongoDocument,
options?: UpdateOptions,
callback?: UpdateCallback
): void;

public remove(
query: Query,
options?: RemoveOptions,
callback?: RemoveCallback
): void;

public loadDatabaseAsync(): Promise<void>;

public findAsync(query: Query): Promise<MongoDocument[]>;

public findOneAsync(query: Query): Promise<MongoDocument>;

public insertAsync(newDoc: MongoDocument): Promise<MongoDocument>;
public updateAsync(query: Query, doc: MongoDocument, options?: UpdateOptions): Promise<MongoDocument>;

public updateAsync(
query: Query,
doc: MongoDocument,
options?: UpdateOptions
): Promise<MongoDocument>;

public removeAsync(query: Query, options?: RemoveOptions): Promise<number>;
}
}
8 changes: 8 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
testRegex: '(spec|test)\\.tsx?$',
"moduleNameMapper": {
"react-native": "<rootDir>/config/jest/reactNativeMock.js"
},
};
Loading

0 comments on commit b5955be

Please sign in to comment.