Atek Database API
atek.cloud/adb-api
npm install @atek-cloud/adb-api
import adb from '@atek-cloud/adb-api'
import cats from 'example-cats-table'
const db = adb.db('mydb')
await cats(db).create({id: 'kit', name: 'Kit'})
await cats(db).list()
await cats(db).get('kit')
await cats(db).put('kit', {id: 'kit', name: 'Kitty'})
await cats(db).delete('kit')
See The Atek DB Guide to learn how to use ADB.
Default export
Name | Type |
---|
.api | AdbApi & AtekRpcClient |
.db | (dbId : string | DbSettings , opts? : DbSettings ) => AdbDatabase |
Use the .db()
method to create AdbDatabase
instances. You may pass a Hypercore 64-character hex-string key or an arbitrary string (which will be a local alias).
import adb from '@atek-cloud/adb-api'
const db = adb.db('mydb')
const db2 = adb.db('97396e81e407e5ae7a64b375cc54c1fc1a0d417a5a72e2169b5377506e1e3163')
The .api
is the RPC interface which will be used by .db()
.
Functions
createClient
▸ createClient(): AdbApi
& AtekRpcClient
Returns
AdbApi
& AtekRpcClient
Use this method to create additional instances of the RPC interface, if needed.
import { createClient } from '@atek-cloud/adb-api'
const adbApi = createClient()
createServer
▸ createServer(handlers
): AtekRpcServer
Parameters
Returns
AtekRpcServer
Use this method to create an ADB server API. You'll only need this if you're creating an alternative implementation to the main ADB.
defineTable
▸ defineTable<T
>(tableId
, desc
): (db
: AdbDatabase
) => any
Type parameters
Parameters
Name | Type |
---|
tableId | string |
desc | TableSettings |
Returns
fn
▸ (db
): AdbTable<T>
Parameters
Returns
AdbDatabase
Creates a table API which can be called on databases:
import adb, { defineTable } from '@atek-cloud/adb-api`
// provide a typescript interface for the records
interface CatRecord {
id: string
name: string
createdAt: string
}
// define the cats table
const catsTable = defineTable<CatRecord>({
id: 'example.com/cats',
schema: {
type: 'object',
required: ['id', 'name']
properties: {
id: {type: 'string'},
name: {type: 'string'},
createdAt: {type: 'string', format: 'date-time'}
}
},
templates: {
table: {
title: 'Cats',
description: 'A table for tracking my cats'
},
record: {
key: '{{/id}}',
title: 'Kitty: {{/name}}'
}
}
})
// use the cats table
const db = adb.db('mydb')
await cats(db).create({id: 'kit', name: 'Kit'})
await cats(db).list() // => {records: [{key: 'kit', value: {id: 'kit', name: 'Kit', createdAt: '2021-09-07T01:06:07.487Z'}}]}
await cats(db).get('kit') // => {key: 'kit', value: {id: 'kit', name: 'Kit', createdAt: '2021-09-07T01:06:07.487Z'}}
await cats(db).put('kit', {id: 'kit', name: 'Kitty'})
await cats(db).delete('kit')
Class: AdbDatabase
- Properties
- Methods
- describe
- define
- list
- get
- create
- put
- delete
- diff
- getBlob
- putBlob
- delBlob
Constructor
• new AdbDatabase(api
, dbId
, opts?
)
Parameters
Name | Type |
---|
api | AdbApi |
dbId | string |
opts? | DbSettings |
Properties
isReady
• isReady: undefined
| Promise
<any
>
api
• api: AdbApi
dbId
• dbId: string
Methods
describe
▸ describe(): Promise
<DbDescription
>
desc
Get metadata and information about the database.
Returns
Promise
<DbDescription
>
define
▸ define(tableId
, desc
): Promise
<TableDescription
>
desc
Register a table's schema and metadata.
Parameters
Name | Type |
---|
tableId | string |
desc | TableSettings |
Returns
Promise
<TableDescription
>
list
▸ list(tableId
, opts?
): Promise
<Object
>
desc
List records in a table.
Parameters
Name | Type |
---|
tableId | string |
opts? | ListOpts |
Returns
Promise
<Object
>
get
▸ get(tableId
, key
): Promise
<Record
<object
>>
desc
Get a record in a table.
Parameters
Name | Type |
---|
tableId | string |
key | string |
Returns
Promise
<Record
<object
>>
create
▸ create(tableId
, value
, blobs?
): Promise
<Record
<object
>>
desc
Add a record to a table.
Parameters
Name | Type |
---|
tableId | string |
value | object |
blobs? | BlobMap |
Returns
Promise
<Record
<object
>>
put
▸ put(tableId
, key
, value
): Promise
<Record
<object
>>
desc
Write a record to a table.
Parameters
Name | Type |
---|
tableId | string |
key | string |
value | object |
Returns
Promise
<Record
<object
>>
delete
▸ delete(tableId
, key
): Promise
<void
>
desc
Delete a record from a table.
Parameters
Name | Type |
---|
tableId | string |
key | string |
Returns
Promise
<void
>
diff
▸ diff(opts
): Promise
<Diff
[]>
desc
Enumerate the differences between two versions of the database.
Parameters
Name | Type |
---|
opts | Object |
opts.left | number |
opts.right? | number |
opts.tableIds? | string [] |
Returns
Promise
<Diff
[]>
getBlob
▸ getBlob(tableId
, key
, blobName
): Promise
<Blob
>
desc
Get a blob of a record.
Parameters
Name | Type |
---|
tableId | string |
key | string |
blobName | string |
Returns
Promise
<Blob
>
putBlob
▸ putBlob(tableId
, key
, blobName
, blobValue
): Promise
<void
>
desc
Write a blob of a record.
Parameters
Name | Type |
---|
tableId | string |
key | string |
blobName | string |
blobValue | BlobDesc |
Returns
Promise
<void
>
delBlob
▸ delBlob(tableId
, key
, blobName
): Promise
<void
>
desc
Delete a blob of a record.
Parameters
Name | Type |
---|
tableId | string |
key | string |
blobName | string |
Returns
Promise
<void
>
Class: AdbTable<T>
- Properties
- isReady
- db
- tableId
- tableDesc
- Methods
- list
- get
- create
- put
- delete
- diff
- getBlob
- putBlob
- delBlob
Constructor
• new AdbTable<T
>(db
, tableId
, tableDesc
)
Type parameters
Parameters
Name | Type |
---|
db | AdbDatabase |
tableId | string |
tableDesc | TableSettings |
Properties
isReady
• isReady: Promise
<any
>
db
• db: AdbDatabase
tableId
• tableId: string
tableDesc
• tableDesc: TableSettings
Methods
list
▸ list(opts?
): Promise
<Object
>
desc
List records in the table.
Parameters
Returns
Promise
<Object
>
get
▸ get(key
): Promise
<Record
<T
>>
desc
Get a record in the table.
Parameters
Returns
Promise
<Record
<T
>>
create
▸ create(value
, blobs?
): Promise
<Record
<T
>>
desc
Add a record to the table.
Parameters
Name | Type |
---|
value | T |
blobs? | BlobMap |
Returns
Promise
<Record
<T
>>
put
▸ put(key
, value
): Promise
<Record
<T
>>
desc
Write a record to the table.
Parameters
Returns
Promise
<Record
<T
>>
delete
▸ delete(key
): Promise
<void
>
desc
Delete a record from the table.
Parameters
Returns
Promise
<void
>
diff
▸ diff(opts
): Promise
<Diff
[]>
desc
Enumerate the differences between two versions of the database.
Parameters
Name | Type |
---|
opts | Object |
opts.left | number |
opts.right? | number |
Returns
Promise
<Diff
[]>
getBlob
▸ getBlob(key
, blobName
): Promise
<Blob
>
desc
Get a blob of a record.
Parameters
Name | Type |
---|
key | string |
blobName | string |
Returns
Promise
<Blob
>
putBlob
▸ putBlob(key
, blobName
, blobValue
): Promise
<void
>
desc
Write a blob of a record.
Parameters
Name | Type |
---|
key | string |
blobName | string |
blobValue | BlobDesc |
Returns
Promise
<void
>
delBlob
▸ delBlob(key
, blobName
): Promise
<void
>
desc
Delete a blob of a record.
Parameters
Name | Type |
---|
key | string |
blobName | string |
Returns
Promise
<void
>