docpouch-client
v0.8.17
Published
A small class to more easily access the docPouch API
Readme
docpouch-client
A TypeScript client library for interacting with the docPouch database API.
Description
docpouch-client provides a simple and intuitive interface to access the docPouch API, allowing you to manage users, documents, data structures, and document types. It also supports real-time synchronization via WebSockets.
Installation
npm install docpouch-clientUsage
Initializing the Client
import docPouchClient from 'docpouch-client';
// Initialize the client with the server URL
const client = new docPouchClient('https://your-docpouch-server.com', 80);
// Initialize with a callback for real-time updates
const client = new docPouchClient('https://your-docpouch-server.com', 80,
(event, data) => {
console.log(`Received event: ${event}`, data);
}
);Authentication
// Login to get an authentication token
const loginResponse = await client.login({
name: 'username',
password: 'password'
});
if (loginResponse) {
console.log('Login successful!');
console.log(`Token: ${loginResponse.token}`);
} else {
console.log('Login failed');
}
// Set an existing token
client.setToken('your-auth-token');User Management
// List all users
const users = await client.listUsers();
// Create a new user
const newUser = await client.createUser({
name: 'newuser',
password: 'password',
department: 'IT',
group: 'Developers',
isAdmin: false
});
// Update a user
await client.updateUser('user-id', {
department: 'Engineering',
group: 'Frontend'
});
// Remove a user
await client.removeUser('user-id');Document Management
// Create a new document
const newDocument = await client.createDocument({
_id: 'document-id',
owner: 'user-id',
title: 'Mission Statement',
type: 17,
subType: 11,
content: '{"msg": "We strive for a world that..."}',
shareWithGroup: false,
shareWithDepartment: false
});
// Update a document
await client.updateDocument('document-id', {
_id: 'document-id',
owner: 'user-id',
title: 'Updated Mission Statement',
type: 17,
subType: 11,
content: '{"msg": "Our updated mission..."}',
shareWithGroup: false,
shareWithDepartment: false
});
// Delete a document
await client.removeDocument('document-id');
// List all documents
const allDocuments = await client.listDocuments();
// Fetch documents by query
const documents = await client.fetchDocuments({type: 17, subType: 11});Data Structure Management
// Create a new data structure
const newStructure = await client.createStructure({
name: "Data resulting from Vision-Mission-Value-Canvas",
fields: [
{
name: "Mission value statement",
type: "string"
},
// Add more fields as needed
]
});
// Update a data structure
await client.updateStructure('structure-id', {
_id: 'structure-id',
name: 'Updated Structure Title',
description: 'Updated structure description',
fields: [
{name: 'New Field Name', type: 'number'}
]
});
// Delete a data structure
await client.removeStructure('structure-id');
// List all data structures
const structures = await client.getStructures();Document Type Management
// List all document types
const docTypes = await client.getTypes();
// Create a new document type
const newDocType = await client.createType({
name: "HR Wages",
description: "HR Wage information Document listing wages per personnel ID",
type: 14,
subType: 2,
defaultStructureID: 'structure-id'
});
// Update a document type
await client.updateType({
_id: 'doc-type-id',
name: 'Updated HR Wages',
type: 14,
subType: 2
});
// Delete a document type
await client.removeType('doc-type-id');API Reference
Client Class
Constructor
new docPouchClient(host: string, port?: number, callback?: (event: I_EventString, data: I_WsMessage) => void)
Methods
setRealTimeSync(newRealTimeSync: boolean): voidlogin(credentials: I_UserLogin): Promise<I_LoginResponse | null>listUsers(): Promise<I_UserEntry[]>updateUser(userID: string, userData: I_UserUpdate): Promise<void>createUser(userData: I_UserCreation): Promise<I_UserDisplay>removeUser(userID: string): Promise<void>createDocument(document: I_DocumentEntry): Promise<I_DocumentEntry>listDocuments(): Promise<I_DocumentEntry[]>fetchDocuments(queryObject: I_DocumentQuery): Promise<I_DocumentEntry[]>updateDocument(documentID: string, documentData: I_DocumentEntry): Promise<void>removeDocument(documentID: string): Promise<void>createStructure(structure: I_StructureCreation): Promise<I_DataStructure>getStructures(): Promise<I_DataStructure[]>updateStructure(structureID: string, structureData: I_DataStructure): Promise<void>removeStructure(structureID: string): Promise<void>createType(type: I_DocumentType): Promise<I_DocumentType>removeType(typeID: string): Promise<void>getTypes(): Promise<I_DocumentType[]>updateType(updatedType: I_DocumentType): Promise<void>setToken(token: string | null): voidgetVersion(): stringdebugSocketConnection(): void
Types
I_UserEntry
{
_id: string;
name: string;
password: string;
email?: string;
department: string;
group: string;
isAdmin: boolean;
}I_UserLogin
{
name: string;
password: string;
}I_UserDisplay
{
_id: string;
username: string;
department: string;
group: string;
email?: string;
}I_UserCreation
{
name: string;
password: string;
email?: string;
department: string;
group: string;
isAdmin: boolean;
}I_UserUpdate
{
_id?: string;
name?: string;
password?: string;
email?: string;
department?: string;
group?: string;
isAdmin?: boolean;
}I_LoginResponse
{
token: string;
isAdmin: boolean;
userName: string;
}I_DocumentEntry
{
_id: string;
owner: string;
title: string;
description?: string;
type: number;
subType: number;
content: any;
shareWithGroup: boolean;
shareWithDepartment: boolean;
}I_DocumentCreation
{
title: string;
description?: string;
type: number;
subType: number;
content: any;
shareWithGroup: boolean;
shareWithDepartment: boolean;
}I_DocumentCreationOwned
{
owner: string;
title: string;
description?: string;
type: number;
subType: number;
content: any;
shareWithGroup: boolean;
shareWithDepartment: boolean;
}I_DocumentUpdate
{
_id?: string;
owner?: string;
title?: string;
type?: number;
subType?: number;
shareWithGroup?: boolean;
shareWithDepartment?: boolean;
content?: any;
description?: string;
}I_DocumentQuery
{
_id?: string;
owner?: string;
title?: string;
type?: number;
subType?: number;
shareWithGroup?: boolean;
shareWithDepartment?: boolean;
}I_DataStructure
{
_id?: string;
name: string;
description: string;
fields: I_StructureField[];
}I_StructureCreation
{
name: string;
description?: string;
fields: I_StructureField[];
}I_StructureUpdate
{
_id?: string;
name?: string;
description?: string;
fields?: I_StructureField[];
}I_StructureField
{
name: string;
type: string;
items?: string;
}I_StructureEntry
{
_id?: string;
name: string;
description: string;
fields: I_StructureField[];
}I_DocumentType
{
_id?: string;
name: string;
description?: string;
type: number;
subType: number;
defaultStructureID?: string;
}I_EventString
'heartbeatPong' | 'heartbeatPing' | 'newDocument' | 'newStructure' |
'newUser' | 'newType' | 'removedID' | 'changedDocument' |
'changedStructure' | 'changedUser' | 'changedType' | 'removedUser' |
'removedStructure' | 'removedDocument' | 'removedType'I_WsMessage
{
newDocument?: I_DocumentEntry;
newStructure?: I_StructureEntry;
newUser?: I_UserEntry;
removedID?: string;
changedDocument?: I_DocumentUpdate;
changedStructure?: I_StructureUpdate;
changedUser?: I_UserUpdate;
confirmSubscription?: boolean;
confirmUnsubscription?: boolean;
heartbeatPing?: number;
heartbeatPong?: number;
newType?: I_DocumentType;
}