66import type {
77 DatabaseDocumentListOptions ,
88 DatabaseService ,
9- } from "@storybooker/core" ;
9+ DatabaseServiceOptions ,
10+ StoryBookerDatabaseDocument ,
11+ } from "@storybooker/core/types" ;
1012
1113export class AzureDataTablesDatabaseService implements DatabaseService {
1214 #connectionString: string ;
@@ -18,9 +20,11 @@ export class AzureDataTablesDatabaseService implements DatabaseService {
1820 TableServiceClient . fromConnectionString ( connectionString ) ;
1921 }
2022
21- listCollections : DatabaseService [ "listCollections" ] = async ( ) => {
23+ listCollections : DatabaseService [ "listCollections" ] = async ( options ) => {
2224 const collections : string [ ] = [ ] ;
23- for await ( const table of this . #serviceClient. listTables ( ) ) {
25+ for await ( const table of this . #serviceClient. listTables ( {
26+ abortSignal : options . abortSignal ,
27+ } ) ) {
2428 if ( table . name ) {
2529 collections . push ( table . name ) ;
2630 }
@@ -29,38 +33,52 @@ export class AzureDataTablesDatabaseService implements DatabaseService {
2933 return collections ;
3034 } ;
3135
32- createCollection : DatabaseService [ "createCollection" ] = async ( name ) => {
33- await this . #serviceClient. createTable ( name ) ;
36+ createCollection : DatabaseService [ "createCollection" ] = async (
37+ collectionId ,
38+ options ,
39+ ) => {
40+ await this . #serviceClient. createTable ( collectionId , {
41+ abortSignal : options . abortSignal ,
42+ } ) ;
3443 return ;
3544 } ;
3645
37- deleteCollection : DatabaseService [ "deleteCollection" ] = async ( name ) => {
38- await this . #serviceClient. deleteTable ( name ) ;
46+ deleteCollection : DatabaseService [ "deleteCollection" ] = async (
47+ collectionId ,
48+ options ,
49+ ) => {
50+ await this . #serviceClient. deleteTable ( collectionId , {
51+ abortSignal : options . abortSignal ,
52+ } ) ;
3953 return ;
4054 } ;
4155
42- listDocuments = async < Item extends { id : string } > (
43- tableName : string ,
44- options ?: DatabaseDocumentListOptions < Item > ,
45- ) : Promise < Item [ ] > => {
46- const { filter, limit, select, sort } = options || { } ;
56+ listDocuments : DatabaseService [ "listDocuments" ] = async <
57+ Document extends StoryBookerDatabaseDocument ,
58+ > (
59+ collectionId : string ,
60+ listOptions : DatabaseDocumentListOptions < Document > ,
61+ options : DatabaseServiceOptions ,
62+ ) : Promise < Document [ ] > => {
63+ const { filter, limit, select, sort } = listOptions || { } ;
4764 const tableClient = TableClient . fromConnectionString (
4865 this . #connectionString,
49- tableName ,
66+ collectionId ,
5067 ) ;
5168 const pageIterator = tableClient
5269 . listEntities ( {
70+ abortSignal : options . abortSignal ,
5371 queryOptions : {
5472 filter : typeof filter === "string" ? filter : undefined ,
5573 select,
5674 } ,
5775 } )
5876 . byPage ( { maxPageSize : limit } ) ;
5977
60- const items : Item [ ] = [ ] ;
78+ const items : Document [ ] = [ ] ;
6179 for await ( const page of pageIterator ) {
6280 for ( const entity of page ) {
63- const item = this . #entityToItem< Item > ( entity ) ;
81+ const item = this . #entityToItem< Document > ( entity ) ;
6482 if ( filter && typeof filter === "function" ) {
6583 if ( filter ( item ) ) {
6684 items . push ( item ) ;
@@ -80,65 +98,76 @@ export class AzureDataTablesDatabaseService implements DatabaseService {
8098 return items ;
8199 } ;
82100
83- getDocument = async < Item extends { id : string } > (
84- tableName : string ,
85- id : string ,
86- partitionKey = id ,
87- ) : Promise < Item > => {
101+ getDocument : DatabaseService [ "getDocument" ] = async <
102+ Document extends StoryBookerDatabaseDocument ,
103+ > (
104+ collectionId : string ,
105+ documentId : string ,
106+ options : DatabaseServiceOptions ,
107+ ) : Promise < Document > => {
88108 const tableClient = TableClient . fromConnectionString (
89109 this . #connectionString,
90- tableName ,
110+ collectionId ,
91111 ) ;
92- const entity = await tableClient . getEntity ( partitionKey , id ) ;
112+ const entity = await tableClient . getEntity ( collectionId , documentId , {
113+ abortSignal : options . abortSignal ,
114+ } ) ;
93115
94- return this . #entityToItem< Item > ( entity ) ;
116+ return this . #entityToItem< Document > ( entity ) ;
95117 } ;
96118
97- createDocument = async < Item extends { id : string } > (
98- tableName : string ,
99- item : Item ,
119+ createDocument : DatabaseService [ "createDocument" ] = async (
120+ collectionId ,
121+ documentData ,
122+ options ,
100123 ) : Promise < void > => {
101124 const tableClient = TableClient . fromConnectionString (
102125 this . #connectionString,
103- tableName ,
126+ collectionId ,
127+ ) ;
128+ await tableClient . createEntity (
129+ {
130+ ...documentData ,
131+ partitionKey : collectionId ,
132+ rowKey : documentData . id ,
133+ } ,
134+ { abortSignal : options . abortSignal } ,
104135 ) ;
105- await tableClient . createEntity ( {
106- ...item ,
107- partitionKey : item . id ,
108- rowKey : item . id ,
109- } ) ;
110136
111137 return ;
112138 } ;
113139
114- deleteDocument = async (
115- tableName : string ,
116- id : string ,
117- partitionKey = id ,
140+ deleteDocument : DatabaseService [ "deleteDocument" ] = async (
141+ collectionId ,
142+ documentId ,
143+ options ,
118144 ) : Promise < void > => {
119145 const tableClient = TableClient . fromConnectionString (
120146 this . #connectionString,
121- tableName ,
147+ collectionId ,
122148 ) ;
123- await tableClient . deleteEntity ( partitionKey , id ) ;
149+ await tableClient . deleteEntity ( collectionId , documentId , {
150+ abortSignal : options . abortSignal ,
151+ } ) ;
124152
125153 return ;
126154 } ;
127155
128156 // oxlint-disable-next-line max-params
129- updateDocument = async < Item extends { id : string } > (
130- tableName : string ,
131- id : string ,
132- item : Partial < Omit < Item , "id" > > ,
133- partitionKey = id ,
157+ updateDocument : DatabaseService [ "updateDocument" ] = async (
158+ collectionId ,
159+ documentId ,
160+ documentData ,
161+ options ,
134162 ) : Promise < void > => {
135163 const tableClient = TableClient . fromConnectionString (
136164 this . #connectionString,
137- tableName ,
165+ collectionId ,
138166 ) ;
139167 await tableClient . updateEntity (
140- { ...item , partitionKey, rowKey : id } ,
168+ { ...documentData , partitionKey : collectionId , rowKey : documentId } ,
141169 "Merge" ,
170+ { abortSignal : options . abortSignal } ,
142171 ) ;
143172
144173 return ;
0 commit comments