0.1.11 • Published 6 years ago

couchrelay v0.1.11

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

CouchRelay

CouchDB Object Document Model 🛋️

  • Support GraphQL Relay style cursor connection
  • Built-in DataLoader

Build Status

Model

Definition

A model corresponds to a database in CouchDB.

import { Model, field } from 'couchrelay';

class Post extends Model {
  @field() title: string;
  @field() content: string;
  @field() authorId: string;
}

Model.init() method ensures its corresponding database exists in CouchDB server by creating a new db if it does not exists.

await Post.init();

Then we get post database.

CouchRelay generates database name from Model's class name, like: Post -> post, MyUser -> my-user. You can also specify its database name by giving static db property to model class.

class Post extends Model {
  static db = 'my-post-db';

  @field() title: string;
  @field() content: string;
  @field() authorId: string;
}

Basic CRUD

const post = new Post();

post.title = 'My first post';
post.content = 'Boom!';
post.authorId = 'hanpama';

await post.$save();
console.log(post._id); // prints generated id
console.log(post._rev); // 1-......

Or you can do equivalent task like below:

const post = await Post.insert({
  title: 'My first post',
  content: 'Boom!',
  authorId: 'hanpama',
});

console.log(post._id);

You can update the document by modifying the object and saving it.

// Update
post.title = 'I am changing post title';
await post.$save();
console.log(post._rev); // 2-......

You can also delete a document by calling $delete();

await post.$delete();

Index

class Post extends Model {
  @field() title: string;
  @field() content: string;
  @field() category: string;
  @field() createdAt: number;
}
const postByCreatedAt = new Index(Post, {
  ddoc: 'post-by-created-at',
  name: 'index',
  index: {
    fields: ['createdAt', '_id'],
  },
} ,{
  maxCount: 10,
});
await postByCreatedAt.init()

Quering with index

const res = await postByCreatedAt.query({ selectors: [{ title: 'Something' }] });

Quering with index connection

You can execute query with GraphQL relay connection arguments using getConnection(). Its returning object has format of relay connection.

const connection = await postByCreatedAt.getConnection().exec({ first: 5 });

console.log(connection.pageInfo);
console.log(connection.edges);
console.log(connection.edges[0].cursor);
console.log(connection.edges[0].node);

View

class Post extends Model {
  @field() title: string;
  @field() content: string;
  @field() category: string;
  @field() createdAt: number;
}
const postByCreatedAt = new View(Post, 'post-by-created-at', {
  limit: 10,
  map: `
    function(doc) {
      emit([doc.createdAt, doc._id], null);
    }
  `
});
await postByCreatedAt.init();

Querying with view

You can query values and documents with view instance.

await postByCreatedAt.getDoc(key);
await postByCreatedAt.getManyDocs(keys);

// it is not useful here because all the values are null
await postByCreatedAt.getValue(key);
await postByCreatedAt.getManyValues(keys)

Quering with view connection

const connection = await postByCreatedAt.getDocConnection().exec({ last: 5 });
console.log(connection.pageInfo);
console.log(connection.edges);
console.log(connection.edges[0].cursor);
console.log(connection.edges[0].node);

allDocs view

A Model class has allDocs view by default.

const post = await Post.allDocs().getDoc('00003bbd26096c0bb063eb0797d71a7');

You can query allDocs() view in the same way where you query on any other views.

More Examples

https://github.com/hanpama/couchrelay/tree/master/test

0.1.11

6 years ago

0.1.10

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago