1
- import { AirtableRecord , DeleteResponse , SelectOptions } from 'asyncairtable/lib/@types' ;
2
- import { Service , Inject , Token } from 'typedi' ;
1
+ import { Service } from 'typedi' ;
3
2
import { BaseModel } from '@thomascsd/stools-models' ;
4
- import { BaseService } from './BaseService' ;
5
-
6
- const AIRTABLE_APIKEY_TOKEN = 'stools_AIRTABLE_APIKEY_TOKEN' ;
7
-
8
- /**
9
- * Defines a token that get Airtable key
10
- */
11
- export const API_KEY_TOKEN = new Token < string > ( AIRTABLE_APIKEY_TOKEN ) ;
3
+ import { BaseService } from './BaseService.js' ;
4
+ import { AirtableCreateMapping , AirtableDeleteMapping , SelectOptions } from '../dtos/index.js' ;
12
5
13
6
/**
14
- * Defines service that access AirTable's data, and use DI with typedi.
15
- *
7
+ * Defines service that accesses AirTable's data, and uses DI with typedi.
8
+ *
16
9
* ```typescript
17
10
* import { Service, Container } from 'typedi';
18
11
* import { DataService, BaseModel, API_KEY_TOKEN } from '@thomascsd/stools';
19
-
12
+ *
20
13
* Container.set(API_KEY_TOKEN, process.env.<your api key>);
21
-
14
+ *
22
15
* const BASE_ID = '<your base id>';
23
-
16
+ *
24
17
* export class Contact extends BaseModel {
25
18
* name: string;
26
19
* email: string;
@@ -29,105 +22,109 @@ export const API_KEY_TOKEN = new Token<string>(AIRTABLE_APIKEY_TOKEN);
29
22
*
30
23
* @Service ()
31
24
* export class ContactService {
32
- * constructor(private db: DataService) {}
25
+ * constructor(private db: DataService) {}
33
26
*
34
- * async getContacts(): Promise<Contact[]> {
35
- * return await this.db.getDatas<Contact>(BASE_ID, '<your table name of AirTable>');
36
- * }
27
+ * async getContacts(): Promise<Contact[]> {
28
+ * return await this.db.getDatas<Contact>(BASE_ID, '<your table name of AirTable>');
29
+ * }
37
30
*
38
- * async saveContact(contact: Contact) {
39
- * return await this.db.saveData<Contact>(BASE_ID, '<your table name of AirTable>', contact);
40
- * }
31
+ * async saveContact(contact: Contact) {
32
+ * return await this.db.saveData<Contact>(BASE_ID, '<your table name of AirTable>', contact);
33
+ * }
41
34
*
42
- * async updateContact(contact: Contact) {
43
- * return await this.db.updateData<Contact>(BASE_ID, '<your table name of AirTable>', contact);
44
- * }
35
+ * async updateContact(contact: Contact) {
36
+ * return await this.db.updateData<Contact>(BASE_ID, '<your table name of AirTable>', contact);
37
+ * }
45
38
* }
46
39
* ```
47
- *
40
+ *
48
41
* @export
49
42
* @class DataService
50
43
* @extends {BaseService }
51
44
*/
52
45
@Service ( )
53
46
export class DataService extends BaseService {
54
- constructor ( @Inject ( API_KEY_TOKEN ) public apiKey : string ) {
55
- super ( apiKey ) ;
56
-
57
- if ( ! this . apiKey ) {
58
- this . apiKey = process . env . AIRTABLE_API ?? '' ;
59
- }
47
+ constructor ( ) {
48
+ super ( ) ;
60
49
}
61
50
62
51
/**
63
- * Get Datas from AirTable
52
+ * Get data from AirTable
64
53
*
65
54
* @template T
66
- * @param {string } baseId
67
- * @param {string } tableName
68
- * @param {SelectOptions } [options]
69
- * @return {* } {Promise<T[]>}
55
+ * @param {string } token - API token
56
+ * @param {string } baseId - Base ID of AirTable
57
+ * @param {string } tableName - Table name in AirTable
58
+ * @param {SelectOptions } [options] - Options for selecting data
59
+ * @return {Promise<T[]> } - Promise resolving to an array of data
70
60
* @memberof DataService
71
61
*/
72
62
async getDatas < T extends BaseModel > (
63
+ token : string ,
73
64
baseId : string ,
74
65
tableName : string ,
75
66
options ?: SelectOptions
76
67
) : Promise < T [ ] > {
77
- return await super . get < T > ( baseId , tableName , options ) ;
68
+ return await super . get < T > ( token , baseId , tableName , options ) ;
78
69
}
79
70
80
71
/**
81
- * Insert data to Airtable
72
+ * Insert data into AirTable
82
73
*
83
74
* @template T
84
- * @param {string } baseId
85
- * @param {string } tableName
86
- * @param {T } model
87
- * @return {* } {Promise<AirtableRecord>}
75
+ * @param {string } token - API token
76
+ * @param {string } baseId - Base ID of AirTable
77
+ * @param {string } tableName - Table name in AirTable
78
+ * @param {T } model - Data model to insert
79
+ * @return {Promise<AirtableCreateMapping> } - Promise resolving to the created record mapping
88
80
* @memberof DataService
89
81
*/
90
82
async saveData < T extends BaseModel > (
83
+ token : string ,
91
84
baseId : string ,
92
85
tableName : string ,
93
86
model : T
94
- ) : Promise < AirtableRecord > {
95
- return await super . save < T > ( baseId , tableName , model ) ;
87
+ ) : Promise < AirtableCreateMapping > {
88
+ return await super . save < T > ( token , baseId , tableName , model ) ;
96
89
}
97
90
98
91
/**
99
- * Update data to AirTable
92
+ * Update data in AirTable
100
93
*
101
94
* @template T
102
- * @param {string } baseId
103
- * @param {string } tableName
104
- * @param {T } model
105
- * @return {* } {Promise<AirtableRecord>}
95
+ * @param {string } token - API token
96
+ * @param {string } baseId - Base ID of AirTable
97
+ * @param {string } tableName - Table name in AirTable
98
+ * @param {T } model - Data model to update
99
+ * @return {Promise<AirtableCreateMapping> } - Promise resolving to the updated record mapping
106
100
* @memberof DataService
107
101
*/
108
102
async updateData < T extends BaseModel > (
103
+ token : string ,
109
104
baseId : string ,
110
105
tableName : string ,
111
106
model : T
112
- ) : Promise < AirtableRecord > {
113
- return await super . update < T > ( baseId , tableName , model ) ;
107
+ ) : Promise < AirtableCreateMapping > {
108
+ return await super . update < T > ( token , baseId , tableName , model ) ;
114
109
}
115
110
116
111
/**
117
112
* Delete data from AirTable
118
113
*
119
114
* @template T
120
- * @param {string } baseId
121
- * @param {string } tableName
122
- * @param {T } model
123
- * @return {* } {Promise<DeleteResponse>}
115
+ * @param {string } token - API token
116
+ * @param {string } baseId - Base ID of AirTable
117
+ * @param {string } tableName - Table name in AirTable
118
+ * @param {T } model - Data model to delete
119
+ * @return {Promise<AirtableDeleteMapping> } - Promise resolving to the deleted record mapping
124
120
* @memberof DataService
125
121
*/
126
122
async deleteData < T extends BaseModel > (
123
+ token : string ,
127
124
baseId : string ,
128
125
tableName : string ,
129
126
model : T
130
- ) : Promise < DeleteResponse > {
131
- return await super . delete < T > ( baseId , tableName , model ) ;
127
+ ) : Promise < AirtableDeleteMapping > {
128
+ return await super . delete < T > ( token , baseId , tableName , model ) ;
132
129
}
133
130
}
0 commit comments