Skip to content
This repository has been archived by the owner on Jul 25, 2021. It is now read-only.

add ruby-style initializer to allow use of async (StoreAsync.new) #2

Merged
merged 2 commits into from May 12, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions __tests__/async.test.ts
@@ -1,7 +1,7 @@
import { complianceTestsAsync } from "@keeveestore/test-suite";
import { StoreAsync } from "../src/async";
import { StoreAsync } from "../src";

complianceTestsAsync(new StoreAsync<string, number>({ connection: "http://localhost:5984", database: "alice" }), {
complianceTestsAsync(() => StoreAsync.new<string, number>({ connection: "http://localhost:5984", database: "alice" }), {
one: 1,
two: 2,
// tslint:disable-next-line: object-literal-sort-keys
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -69,7 +69,7 @@
"nano": "^8.1.0"
},
"devDependencies": {
"@keeveestore/test-suite": "^0.1.0",
"@keeveestore/test-suite": "^0.1.3",
"@sindresorhus/tsconfig": "^0.3.0",
"@types/jest": "^24.0.12",
"@types/prettier": "^1.16.3",
Expand Down
160 changes: 0 additions & 160 deletions src/async.ts

This file was deleted.

162 changes: 161 additions & 1 deletion src/index.ts
@@ -1 +1,161 @@
export * from "./async";
import { IKeyValueStoreAsync } from "@keeveestore/keeveestore";
import nano from "nano";

export class StoreAsync<K, T> implements IKeyValueStoreAsync<K, T> {
public static async new<K, T>(opts: { connection: string; database: string }): Promise<StoreAsync<K, T>> {
const store = nano(opts.connection).db;

try {
await store.create(opts.database);
} catch (error) {} // tslint:disable-line: no-empty

return new StoreAsync<K, T>(store, opts.database);
}

private constructor(private readonly store, private readonly database: string) {}

public async all(): Promise<Array<[K, T]>> {
try {
const { rows } = await this.db.list();

return Promise.all(rows.map(async row => [row.id, await this.get(row.id)]));
} catch (error) {
return [];
}
}

public async keys(): Promise<K[]> {
try {
const { rows } = await this.db.list();

return rows.map(row => row.id);
} catch (error) {
return [];
}
}

public async values(): Promise<T[]> {
try {
const { rows } = await this.db.list();

return Promise.all(rows.map(async row => this.get(row.id)));
} catch (error) {
return [];
}
}

public async get(key: K): Promise<T | undefined> {
try {
const { value } = await this.db.get(key);

return value;
} catch (error) {
return undefined;
}
}

public async getMany(keys: K[]): Promise<Array<T | undefined>> {
return Promise.all([...keys].map(async (key: K) => this.get(key)));
}

public async pull(key: K): Promise<T | undefined> {
const item: T | undefined = await this.get(key);

await this.forget(key);

return item;
}

public async pullMany(keys: K[]): Promise<Array<T | undefined>> {
const items: Array<T | undefined> = await this.getMany(keys);

await this.forgetMany(keys);

return items;
}

public async put(key: K, value: T): Promise<boolean> {
try {
await this.db.insert({
_id: key,
_rev: (await this.db.get(key))._rev,
value,
});
} catch (error) {
await this.db.insert({ _id: key, value });
}

return this.has(key);
}

public async putMany(values: Array<[K, T]>): Promise<boolean[]> {
return Promise.all(values.map(async (value: [K, T]) => this.put(value[0], value[1])));
}

public async has(key: K): Promise<boolean> {
try {
return (await this.get(key)) !== undefined;
} catch (error) {
return false;
}
}

public async hasMany(keys: K[]): Promise<boolean[]> {
return Promise.all([...keys].map(async (key: K) => this.has(key)));
}

public async missing(key: K): Promise<boolean> {
return !(await this.has(key));
}

public async missingMany(keys: K[]): Promise<boolean[]> {
return Promise.all([...keys].map(async (key: K) => this.missing(key)));
}

public async forget(key: K): Promise<boolean> {
if (await this.missing(key)) {
return false;
}

try {
const doc = await this.db.get(key);

await this.db.destroy(doc._id, doc._rev);
// tslint:disable-next-line: no-empty
} catch (error) {}

return this.missing(key);
}

public async forgetMany(keys: K[]): Promise<boolean[]> {
return Promise.all([...keys].map((key: K) => this.forget(key)));
}

public async flush(): Promise<boolean> {
await this.forgetMany(await this.keys());

return this.isEmpty();
}

public async count(): Promise<number> {
try {
const { doc_count } = await this.db.info();

return doc_count;
} catch (error) {
return 0;
}
}

public async isEmpty(): Promise<boolean> {
return (await this.count()) === 0;
}

public async isNotEmpty(): Promise<boolean> {
return !(await this.isEmpty());
}

private get db() {
return this.store.use(this.database);
}
}
8 changes: 4 additions & 4 deletions yarn.lock
Expand Up @@ -290,10 +290,10 @@
resolved "https://registry.yarnpkg.com/@keeveestore/keeveestore/-/keeveestore-0.1.0.tgz#0a066bcfda279c36ee22a919872b02bd4500c560"
integrity sha512-DcCfHe7lrLBW8y4IesIeDnNKASZkxCY8mvIuCnUarzRzJuTqo/Z+7u/qWe7T43s0KauP6s/TsX0MqIZCYw1OwA==

"@keeveestore/test-suite@^0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@keeveestore/test-suite/-/test-suite-0.1.0.tgz#bdf3056204149b4a9e88918307ce7b1d9680b209"
integrity sha512-5WCEdDALcdVUApsH3HlCQLBTHRbBQ8NJ4dl9Mj9cHjczyW7VOZREXQNOoZjnW69OMvBVB63fHlu2edPaj2NghA==
"@keeveestore/test-suite@^0.1.3":
version "0.1.3"
resolved "https://registry.yarnpkg.com/@keeveestore/test-suite/-/test-suite-0.1.3.tgz#24c744096d34b08d7caf2e9c46b7cb042e425856"
integrity sha512-b3NMINgKA6Za3/piNBjL9AwsRpf+hs28MYP6tkeJTF1tpuKXGDvAntLvPBJMAYkTaAz1bFZ3fucFqjsX+D2bvQ==
dependencies:
"@keeveestore/keeveestore" "^0.1.0"
jest-extended "^0.11.1"
Expand Down