-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base.ts
73 lines (66 loc) · 1.85 KB
/
Base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { Client, Record, RecordFields, UpdateParams } from './Client'
import { invariant } from './lib'
import { Query, QueryParams } from './query'
import { Table } from './Table'
const _client = Symbol('_client') // tslint:disable-line:variable-name
const _baseId = Symbol('_baseId') // tslint:disable-line:variable-name
/**
* Airtable Base
*/
export class Base {
private [_client]: Client
private [_baseId]: string
constructor(client: Client, baseId: string) {
invariant(client, '"client" is required')
invariant(client instanceof Client, '"client" must be a Client instance')
invariant(baseId, '"baseId" is required')
this[_client] = client
this[_baseId] = baseId
}
/**
* Create a new Table reference
*/
public table(tableName: string): Table {
return new Table(this, tableName)
}
/**
* Create a query
*/
public query(tableName: string, params: QueryParams): Query {
return this[_client].query(this[_baseId], tableName, params)
}
/**
* Fetch a Record
*/
public async get(
tableName: string,
recordId: string
): Promise<Record | null> {
return this[_client].get(this[_baseId], tableName, recordId)
}
/**
* Create new Record
*/
public async create(
tableName: string,
fields: RecordFields
): Promise<Record> {
return this[_client].create(this[_baseId], tableName, fields)
}
/**
* Update existing Record
*/
public async update(
tableName: string,
record: Record,
options: UpdateParams
): Promise<Record> {
return this[_client].update(this[_baseId], tableName, record, options)
}
/**
* Delete a Record
*/
public async delete(tableName: string, recordId: string): Promise<undefined> {
return this[_client].delete(this[_baseId], tableName, recordId)
}
}