1.1.3 • Published 3 years ago

canhazdb-client v1.1.3

Weekly downloads
11
License
AGPL-3.0
Repository
github
Last release
3 years ago

canhazdb-client

GitHub code size in bytes GitHub package.json version GitHub js-semistandard-style

A client to simplify making rest api calls by using database like functions.

Getting Started

You should create a server before trying to use the client.

Client

Connecting

const client = require('canhazdb-client');

const tls = {
  key: fs.readFileSync('./certs/localhost.privkey.pem'),
  cert: fs.readFileSync('./certs/localhost.cert.pem'),
  ca: [ fs.readFileSync('./certs/ca.cert.pem') ],
  requestCert: true /* this denys any cert not signed with our ca above */
};
const client = createClient('https://localhost:8063', { tls });

Making requests

const document = await client.post('tests', { a: 1 });
const changed = await client.put('tests', { id: document.id }, { query: { b: 2 } });
const changedDocument = await client.getOne('tests', { query: { id: document.id } });

Using events

// Capture an event based on regex
// client.on('.*:/tests/.*', ...)
// client.on('.*:/tests/uuid-uuid-uuid-uuid', ...)
// client.on('POST:/tests', ...)
// client.on('DELETE:/tests/.*', ...)
// client.on('(PUT|PATCH):/tests/uuid-uuid-uuid-uuid', ...)

client.on('POST:/tests/.*', (path, collectionId, resourceId, pattern) => {
  console.log(path) // === 'POST:/tests/uuid-uuid-uuid-uuid'
  console.log(collectionId) // === 'tests'
  console.log(resourceId) // === 'uuid-uuid-uuid-uuid'
  console.log(pattern) // === 'POST:/tests/.*'
})

console.log( {
  document, /* { a: 1 } */
  changed, /* { changes: 1 } */
  changedDocument, /* { b: 2 } */
})

Examples

client.get('tests', { 
  query: {
    id: 'example-uuid-paramater'
  }
});
client.count('tests', {
  query: {
    firstName: 'Joe'
  }
});
client.get('tests', {
  query: {
    firstName: 'Joe'
  },
  limit: 10,
  order: 'desc(firstName)'
});
client.post('tests', {
  firstName: 'Joe'
});
client.put('tests', {
  firstName: 'Joe'
});
client.put('tests', {
    firstName: 'Zoe',
    location: 'GB',
    timezone: 'GMT'
}, {
  query: {
    location: 'GB'
  }
});
client.patch('tests', {
    timezone: 'GMT'
}, {
  query: {
    location: 'GB'
  }
});
client.patch('tests', {
    timezone: 'GMT'
}, {
  query: {
    location: 'GB'
  }
});
client.delete('tests', {
  query: {
    id: 'example-uuid-paramater'
  }
});
client.delete('tests', {
  query: {
    location: 'GB'
  }
});
const lockId = await client.lock(['users']);
const lockId = await client.lock(['users']);
const newDocument = await client.post('users', {
  name: 'mark'
}, {
  lockId,
  lockStrategy: 'wait' // optional: can be 'fail' or 'wait'. default is 'wait'.
});
await client.unlock(lockId);

License

This project is licensed under the terms of the AGPL-3.0 license.

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago