@kontenbase/sdk
    TypeScript icon, indicating that this package has built-in type declarations

    0.8.10 • Public • Published

    Kontenbase SDK

    This is the Official Node JS and Browser client/library for Kontenbase API. Visit https://kontenbase.com. More information about the product and see documentation at http://docs.kontenbase.com for more technical details.

    API Documentation

    Please check Kontenbase API Reference.

    Installation

    Node.js

    To install kontenbase in a node project:

    npm install --save @kontenbase/sdk

    Usage

    Configure package with your account's API key obtained from your Kontenbase Dashboard.

    const { KontenbaseClient } = require('@kontenbase/sdk');
    
    const kontenbase = new KontenbaseClient({
      apiKey: '*******************',
    });

    Authentication

    Use kontenbase auth services for manage your user.

    Register

    const { user, token, error } = await kontenbase.auth.register({
      firstName: 'Ega',
      lastName: 'Radiegtya',
      email: 'user@gmail.com',
      password: 'password',
    });

    Login

    const { user, token, error } = await kontenbase.auth.login({
      email: 'user@gmail.com',
      password: 'password',
    });

    User

    const { user, error } = await kontenbase.auth.user();
    // Get user with filter:
    // lookup
    const { user, error } = await kontenbase
      .auth
      .user({ filterKey: filterValue, ... });

    Update

    const { user, error } = await kontenbase.auth.update({ firstName: 'Ega' });

    Logout

    const { user, token, error } = await kontenbase.auth.logout();

    Database

    Create

    const { data, error } = await kontenbase.service('posts').create({
      name: 'Post 1',
      notes: 'Hello kontenbase',
    });

    Get

    const { data, error } = await kontenbase
      .service('posts')
      .getById('605a251d7b8678bf6811k3b1');
    // Get record with filters:
    // select
    // lookup
    const { data, error } = await kontenbase
      .service('posts')
      .getById('605a251d7b8678bf6811k3b1', { filterKey: filterValue, ... });

    Update

    const { data, error } = await kontenbase
      .service('posts')
      .updateById('605a251d7b8678bf6811k3b1', {
        notes: 'Hello world',
      });

    Delete

    const { data, error } = await kontenbase
      .service('posts')
      .deleteById('605a251d7b8678bf6811k3b1');

    Link

    const { data, error } = await kontenbase
      .service('posts')
      .link('605a251d7b8678bf6811k3b1', {
        categories: '61d26e8e2adb12b85c33029c',
      });

    Unlink

    const { data, error } = await kontenbase
      .service('posts')
      .unlink('605a251d7b8678bf6811k3b1', {
        categories: '61d26e8e2adb12b85c33029c',
      });

    Find

    const { data, error } = await kontenbase.service('posts').find();
    // sort
    // 1 = ascending
    // -1 = descending
    const { data, error } = await kontenbase
      .service('posts')
      .find({ sort: { name: 1 } });
    // skip
    const { data, error } = await kontenbase.service('posts').find({ skip: 10 });
    // limit
    const { data, error } = await kontenbase.service('posts').find({ limit: 10 });
    // select
    const { data, error } = await kontenbase
      .service('posts')
      .find({ select: ['name'] });
    // lookup into multiple fields of link to record
    const { data, error } = await kontenbase
      .service('posts')
      .find({ lookup: ['categories'] });
    // lookup into all fields of link to record
    const { data, error } = await kontenbase.service('posts').find({ lookup: '*' });
    // lookup but only show the ids
    const { data, error } = await kontenbase
      .service('posts')
      .find({ lookup: { _id: '*' } });
    // where
    const { data, error } = await kontenbase
      .service('posts')
      .find({ where: { name: 'Ega' } });
    // not equal
    const { data, error } = await kontenbase
      .service('posts')
      .find({ where: { name: { $ne: 'Ega' } } });
    // contains
    const { data, error } = await kontenbase
      .service('posts')
      .find({ where: { name: { $contains: 'Ega' } } });
    // not contains
    const { data, error } = await kontenbase
      .service('posts')
      .find({ where: { name: { $notContains: 'Ega' } } });
    // include
    const { data, error } = await kontenbase
      .service('posts')
      .find({ where: { name: { $in: ['Ega'] } } });
    // not include
    const { data, error } = await kontenbase
      .service('posts')
      .find({ where: { name: { $nin: ['Ega'] } } });
    // less then
    const { data, error } = await kontenbase
      .service('posts')
      .find({ where: { total: { $lt: 10 } } });
    // less then equal
    const { data, error } = await kontenbase
      .service('posts')
      .find({ where: { total: { $lte: 10 } } });
    // greater then
    const { data, error } = await kontenbase
      .service('posts')
      .find({ where: { total: { $gt: 10 } } });
    // greater then equal
    const { data, error } = await kontenbase
      .service('posts')
      .find({ where: { total: { $gte: 10 } } });

    Count

    // count all records
    const { data, error } = await kontenbase.service('posts').count();
    // count with filters
    const { data, error } = await kontenbase
      .service('posts')
      .count({ filterKey: filterValue, ... });

    Storage

    Upload

    // from client
    const file = event.target.files[0];
    const { data, error } = await kontenbase.storage.upload(file);
    // from server
    const file = req.files[0];
    const { data, error } = await kontenbase.storage.upload(
      file.buffer,
      file.originalname,
    );

    Realtime

    Event

    • *
    • CREATE_RECORD
    • UPDATE_RECORD
    • DELETE_RECORD

    Subscribe

    kontenbase.realtime.subscribe('posts', { event: '*' }, (message) => {
      if (message.error) {
        console.log(message.error);
        return;
      }
    
      console.log(message.event, message.payload);
    });

    Unsubscribe

    const key = await kontenbase.realtime.subscribe(
      'posts',
      { event: '*' },
      (message) => {
        if (message.error) {
          console.log(message.error);
          return;
        }
    
        console.log(message.event, message.payload);
      },
    );
    
    kontenbase.realtime.unsubscribe(key);

    CDN

    You can now use plain <script>s to import kontenbase from CDNs, like:

    <script src="https://cdn.jsdelivr.net/npm/@kontenbase/sdk"></script>

    Then you can use it from a global kontenbase variable:

    <script>
      const { createClient } = kontenbase;
      const client = createClient({
        apiKey: '*******************',
      });
    
      client
        .service('posts')
        .find()
        .then((res) => {
          if (res.error) {
            console.log(res.error);
            return;
          }
    
          console.log(res);
        });
    </script>

    Keywords

    none

    Install

    npm i @kontenbase/sdk

    DownloadsWeekly Downloads

    0

    Version

    0.8.10

    License

    MIT

    Unpacked Size

    831 kB

    Total Files

    176

    Last publish

    Collaborators

    • kontenbase