Skip to content

Commit

Permalink
2.0.0 release: adds intellisense for selector indexes.
Browse files Browse the repository at this point in the history
  • Loading branch information
nozzlegear committed Apr 13, 2017
1 parent 20fcba4 commit 82b86e5
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 9 deletions.
61 changes: 55 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const GENERIC_LIST_VIEW = {
/**
* Configures a Davenport client and database by validating the CouchDB version, creating indexes and design documents, and then returning a client to interact with the database.
*/
export async function configureDatabase<T extends CouchDoc>(databaseUrl: string, configuration: DatabaseConfiguration<T>, options?: ClientOptions): Promise<Client<T>> {
export async function configureDatabase<DocType extends CouchDoc>(databaseUrl: string, configuration: DatabaseConfiguration<DocType>, options?: ClientOptions): Promise<Client<DocType>> {
const dbInfo = await Axios.get(databaseUrl);

if (!isOkay(dbInfo)) {
Expand Down Expand Up @@ -130,7 +130,7 @@ export async function configureDatabase<T extends CouchDoc>(databaseUrl: string,
}));
}

return new Client<T>(databaseUrl, configuration.name, options);
return new Client<DocType>(databaseUrl, configuration.name, options);
}

/**
Expand Down Expand Up @@ -232,7 +232,9 @@ export class Client<T extends CouchDoc> {
/**
* Counts all documents by the given selector. Warning: this uses more memory than a regular count, because it needs to pull in the _id field of all selected documents. For large queries, it's better to create a dedicated view and use the .view function.
*/
public async countBySelector(selector: Partial<T>): Promise<number> {
public async countBySelector(selector: DocSelector<T>): Promise<number>
public async countBySelector(selector: Partial<T>): Promise<number>
public async countBySelector(selector): Promise<number> {
const result = await this.find({
fields: ["_id"],
selector,
Expand Down Expand Up @@ -338,7 +340,7 @@ export class Client<T extends CouchDoc> {
/**
* Checks that a document that matches the field value exists.
*/
public async existsBySelector(value, field: keyof T): Promise<boolean> {
public async existsByFieldValue(value, field: keyof T): Promise<boolean> {
const findResult = await this.find({
fields: ["_id"],
limit: 1,
Expand All @@ -350,6 +352,19 @@ export class Client<T extends CouchDoc> {
return findResult.length > 0;
}

/**
* Checks that a document matching the selector exists.
*/
public async existsBySelector(selector: DocSelector<T>): Promise<boolean> {
const findResult = await this.find({
fields: ["_id"],
limit: 1,
selector: selector as any,
});

return findResult.length > 0;
}

/**
* Executes a view with the given designDocName and viewName.
*/
Expand Down Expand Up @@ -468,7 +483,7 @@ export interface FindOptions<T> {
limit?: number;
skip?: number;
use_index?: Object;
selector: Partial<T>;
selector: Partial<T> | DocSelector<T>;
}

export interface CouchDBView {
Expand Down Expand Up @@ -497,4 +512,38 @@ export interface ClientOptions {
* Whether the Davenport client should log warnings.
*/
warnings: boolean;
}
}

export interface PropSelector {
/**
* Property is equal to this value.
*/
$eq?: any;

/**
* Property is not equal to this value.
*/
$ne?: any;

/**
* Property is greater than this value.
*/
$gt?: any;

/**
* Property is greater than or equal to this value.
*/
$gte?: any;

/**
* Property is less than this value.
*/
$lt?: any;

/**
* Property is lesser than or equal to this value.
*/
$lte?: any;
}

export type DocSelector<T> = Partial<Record<keyof T, PropSelector>>;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "davenport",
"version": "1.2.0",
"version": "2.0.0",
"description": "A CouchDB client for simplifying common tasks like get, list, create, update and delete. Comes complete with full TypeScript definitions.",
"main": "bin/index.js",
"typings": "bin/index.d.ts",
Expand Down
46 changes: 44 additions & 2 deletions tests/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import inspect from "logspect";
import { Expect, AsyncTest, Timeout, TestFixture } from "alsatian";
import Client, { configureDatabase, CouchDoc, DesignDocConfiguration } from "../";
import Client, { configureDatabase, CouchDoc, DesignDocConfiguration, PropSelector } from "../";

const DB_URL = "http://localhost:5984";
const DB_NAME = "davenport_tests";
Expand Down Expand Up @@ -168,6 +168,29 @@ export class DavenportTestFixture {
Expect(count).toBe(3);
}

@AsyncTest("Davenport.count with selector indexes")
@Timeout(5000)
public async countWithSelectorIndexesTest() {
const client = new Client<TestObject>(DB_URL, DB_NAME);
const uuid = `a-unique-string-${Date.now()}`;

for (let i = 0; i < 3; i++) {
await client.post({
bar: i,
foo: i + 1,
hello: uuid
})
};

const count = await client.countBySelector({
hello: {
$eq: uuid
}
});

Expect(count).toBe(3);
}

@AsyncTest("Davenport.delete")
@Timeout(5000)
public async deleteTest() {
Expand Down Expand Up @@ -202,6 +225,21 @@ export class DavenportTestFixture {
Expect(exists).toBe(true);
}

@AsyncTest("Davenport.exists with field value")
@Timeout(5000)
public async existsWithFieldValueTest() {
const client = new Client<TestObject>(DB_URL, DB_NAME);
const uuid = `a-unique-string-${Date.now()}`;
const createResult = await client.post({
bar: 5,
foo: 4,
hello: uuid,
})
const exists = await client.existsByFieldValue(uuid, "hello");

Expect(exists).toBe(true);
}

@AsyncTest("Davenport.exists with selector")
@Timeout(5000)
public async existsWithSelectorTest() {
Expand All @@ -212,7 +250,11 @@ export class DavenportTestFixture {
foo: 4,
hello: uuid,
})
const exists = await client.existsBySelector(uuid, "hello");
const exists = await client.existsBySelector({
hello: {
$eq: uuid
}
});

Expect(exists).toBe(true);
}
Expand Down

0 comments on commit 82b86e5

Please sign in to comment.