@@ -18,7 +18,7 @@ import { convertObjectByAttributeMappings } from "appwrite-utils-helpers";
1818import { z } from "zod" ;
1919import { checkForCollection } from "../collections/methods.js" ;
2020import { ID , Users , type Databases } from "node-appwrite" ;
21- import { logger , LegacyAdapter , MessageFormatter } from "appwrite-utils-helpers" ;
21+ import { logger , AdapterFactory , type DatabaseAdapter , MessageFormatter } from "appwrite-utils-helpers" ;
2222import { findOrCreateOperation , updateOperation } from "../shared/migrationHelpers.js" ;
2323import { AuthUserCreateSchema } from "../schemas/authUser.js" ;
2424import { UsersController } from "../users/methods.js" ;
@@ -67,6 +67,15 @@ export class DataLoader {
6767 private userIdSet = new Set < string > ( ) ;
6868 userExistsMap = new Map < string , boolean > ( ) ;
6969 private shouldWriteFile = false ;
70+ private _adapter : DatabaseAdapter | null = null ;
71+
72+ private async getAdapter ( ) : Promise < DatabaseAdapter > {
73+ if ( ! this . _adapter ) {
74+ const { adapter } = await AdapterFactory . createFromConfig ( this . config ) ;
75+ this . _adapter = adapter ;
76+ }
77+ return this . _adapter ;
78+ }
7079
7180 // Constructor to initialize the DataLoader with necessary configurations
7281 constructor (
@@ -279,7 +288,7 @@ export class DataLoader {
279288 ) ;
280289 }
281290
282- async setupMaps ( dbId : string ) {
291+ async setupMaps ( dbId : string , specificCollections ?: string [ ] ) {
283292 // Initialize the users collection in the import map
284293 this . importMap . set ( this . getCollectionKey ( "users" ) , {
285294 data : [ ] ,
@@ -294,6 +303,11 @@ export class DataLoader {
294303 for ( let index = 0 ; index < this . config . collections . length ; index ++ ) {
295304 const collectionConfig = this . config . collections [ index ] ;
296305 let collection = CollectionCreateSchema . parse ( collectionConfig ) ;
306+ // Skip collections not in the specific list if one was provided
307+ if ( specificCollections && specificCollections . length > 0 &&
308+ ! specificCollections . includes ( collection . name ) ) {
309+ continue ;
310+ }
297311 // Check if the collection exists in the database
298312 const collectionExists = await checkForCollection (
299313 this . database ,
@@ -311,19 +325,25 @@ export class DataLoader {
311325 collectionConfig . $id = collectionExists . $id ;
312326 collection . $id = collectionExists . $id ;
313327 this . config . collections [ index ] = collectionConfig ;
314- // Find or create an import operation for the collection
315- const adapter = new LegacyAdapter ( this . database . client ) ;
316- const collectionImportOperation = await findOrCreateOperation (
317- adapter ,
318- dbId ,
319- "importData" ,
320- collection . $id !
321- ) ;
322- // Store the operation ID in the map
323- this . collectionImportOperations . set (
324- this . getCollectionKey ( collection . name ) ,
325- collectionImportOperation . $id
326- ) ;
328+ // Find or create an import operation for the collection (non-fatal)
329+ try {
330+ const adapter = await this . getAdapter ( ) ;
331+ const collectionImportOperation = await findOrCreateOperation (
332+ adapter ,
333+ dbId ,
334+ "importData" ,
335+ collection . $id !
336+ ) ;
337+ this . collectionImportOperations . set (
338+ this . getCollectionKey ( collection . name ) ,
339+ collectionImportOperation . $id
340+ ) ;
341+ } catch ( error ) {
342+ MessageFormatter . warning (
343+ `Operations tracking unavailable for ${ collection . name } , import will proceed without it` ,
344+ { prefix : "Import" }
345+ ) ;
346+ }
327347 // Initialize the collection in the import map
328348 this . importMap . set ( this . getCollectionKey ( collection . name ) , {
329349 collection : collection ,
@@ -373,17 +393,22 @@ export class DataLoader {
373393 }
374394
375395 // Main method to start the data loading process for a given database ID
376- async start ( dbId : string ) {
396+ async start ( dbId : string , specificCollections ?: string [ ] ) {
377397 MessageFormatter . divider ( ) ;
378398 MessageFormatter . info ( `Starting data setup for database: ${ dbId } ` , { prefix : "Data" } ) ;
379399 MessageFormatter . divider ( ) ;
380- await this . setupMaps ( dbId ) ;
381- const allUsers = await this . getAllUsers ( ) ;
382- MessageFormatter . info (
383- `Fetched ${ allUsers . length } users, waiting a few seconds to let the program catch up...` ,
384- { prefix : "Data" }
385- ) ;
386- await new Promise ( ( resolve ) => setTimeout ( resolve , 5000 ) ) ;
400+ // Only fetch users if we're importing users or no specific collections specified
401+ const needsUsers = ! specificCollections || specificCollections . length === 0 ||
402+ specificCollections . some ( c =>
403+ this . getCollectionKey ( c ) === this . getCollectionKey ( this . config . usersCollectionName )
404+ ) ;
405+ if ( needsUsers ) {
406+ const allUsers = await this . getAllUsers ( ) ;
407+ MessageFormatter . info (
408+ `Fetched ${ allUsers . length } users` ,
409+ { prefix : "Data" }
410+ ) ;
411+ }
387412 // Iterate over the configured databases to find the matching one
388413 for ( const db of this . config . databases ) {
389414 if ( db . $id !== dbId ) {
@@ -395,6 +420,11 @@ export class DataLoader {
395420 // Iterate over the configured collections to process each
396421 for ( const collectionConfig of this . config . collections ) {
397422 const collection = collectionConfig ;
423+ // Skip collections not in the specific list
424+ if ( specificCollections && specificCollections . length > 0 &&
425+ ! specificCollections . includes ( collection . name ) ) {
426+ continue ;
427+ }
398428 // Determine if this is the users collection
399429 let isUsersCollection =
400430 this . getCollectionKey ( this . config . usersCollectionName ) ===
@@ -954,7 +984,7 @@ export class DataLoader {
954984 this . oldIdToNewIdPerCollectionMap
955985 . set ( this . getCollectionKey ( collection . name ) , oldIdToNewIdMap )
956986 . get ( this . getCollectionKey ( collection . name ) ) ;
957- const adapter = new LegacyAdapter ( this . database . client ) ;
987+ const adapter = await this . getAdapter ( ) ;
958988 if ( ! operationId ) {
959989 const collectionImportOperation = await findOrCreateOperation (
960990 adapter ,
@@ -971,7 +1001,7 @@ export class DataLoader {
9711001 }
9721002 if ( operationId ) {
9731003 await updateOperation ( adapter , db . $id , operationId , {
974- status : "ready " ,
1004+ status : "in_progress " ,
9751005 total : rawData . length ,
9761006 } ) ;
9771007 }
@@ -1185,7 +1215,7 @@ export class DataLoader {
11851215 let operationId = this . collectionImportOperations . get (
11861216 this . getCollectionKey ( collection . name )
11871217 ) ;
1188- const adapter = new LegacyAdapter ( this . database . client ) ;
1218+ const adapter = await this . getAdapter ( ) ;
11891219 if ( ! operationId ) {
11901220 const collectionImportOperation = await findOrCreateOperation (
11911221 adapter ,
@@ -1202,7 +1232,7 @@ export class DataLoader {
12021232 }
12031233 if ( operationId ) {
12041234 await updateOperation ( adapter , db . $id , operationId , {
1205- status : "ready " ,
1235+ status : "in_progress " ,
12061236 total : rawData . length ,
12071237 } ) ;
12081238 }
0 commit comments