@@ -24,7 +24,6 @@ const GET_COLLECTION = (id) => {
24
24
} ;
25
25
26
26
const DATABASE_ID = '64c4de8e7b30179809ef' ;
27
- const COLLECTION_ID = '64d7502a5ce9f881b680' ;
28
27
29
28
const client = new sdk . Client ( ) ;
30
29
const databases = new sdk . Databases ( client ) ;
@@ -37,37 +36,56 @@ if (process.env.APPWRITE_SELF_SIGNED) {
37
36
client . setSelfSigned ( ) ;
38
37
}
39
38
40
- const hasCollection = async ( ) => {
41
- try {
42
- return await databases . getCollection ( DATABASE_ID , COLLECTION_ID )
43
- } catch {
44
- return null
45
- }
46
- }
47
-
48
39
const sleep = ( timeout = 1_000 ) => new Promise ( ( res ) => {
49
40
setTimeout ( ( ) => {
50
41
res ( ) ;
51
42
} , timeout ) ;
52
43
} ) ;
53
44
54
- const { attributes, name } = GET_COLLECTION ( COLLECTION_ID ) ;
55
45
56
- {
57
- if ( ! ( await hasCollection ( ) ) ) {
58
- console . log ( `Creating collection id=${ COLLECTION_ID } name=${ name } ` ) ;
59
- await databases . createCollection ( DATABASE_ID , COLLECTION_ID , name ) ;
46
+ for ( const collectionId of schema . collections . map ( ( { $id } ) => $id ) ) {
47
+
48
+ const { attributes, name } = GET_COLLECTION ( collectionId ) ;
49
+
50
+ const hasCollection = async ( ) => {
51
+ try {
52
+ return await databases . getCollection ( DATABASE_ID , collectionId )
53
+ } catch {
54
+ return null
55
+ }
56
+ } ;
57
+
58
+ const hasAttribute = async ( key ) => {
59
+ try {
60
+ return await databases . getAttribute ( DATABASE_ID , collectionId , key )
61
+ } catch {
62
+ return null
63
+ }
64
+ } ;
65
+
66
+ if ( await hasCollection ( ) ) {
67
+ console . log ( `Found collection id=${ collectionId } name=${ name } ` ) ;
68
+ } else {
69
+ console . log ( `Creating collection id=${ collectionId } name=${ name } ` ) ;
70
+ await databases . createCollection ( DATABASE_ID , collectionId , name ) ;
60
71
}
72
+
61
73
for ( const { key, type, required, array, size } of attributes ) {
62
- console . log ( `creating ${ key } : type=${ type } array=${ array } size=${ size } ` )
74
+ if ( await hasAttribute ( key ) ) {
75
+ console . log ( `skip ${ key } : type=${ type } array=${ array } size=${ size } ` )
76
+ await sleep ( 1_500 ) ;
77
+ continue ;
78
+ } else {
79
+ console . log ( `creating ${ key } : type=${ type } array=${ array } size=${ size } ` )
80
+ }
63
81
if ( type === "string" ) {
64
- await databases . createStringAttribute ( DATABASE_ID , COLLECTION_ID , key , size , required , undefined , array ) ;
82
+ await databases . createStringAttribute ( DATABASE_ID , collectionId , key , size , required , undefined , array ) ;
65
83
}
66
84
if ( type === "boolean" ) {
67
- await databases . createBooleanAttribute ( DATABASE_ID , COLLECTION_ID , key , required , undefined , array ) ;
85
+ await databases . createBooleanAttribute ( DATABASE_ID , collectionId , key , required , undefined , array ) ;
68
86
}
69
87
if ( type === "integer" ) {
70
- await databases . createIntegerAttribute ( DATABASE_ID , COLLECTION_ID , key , required , undefined , undefined , undefined , array ) ;
88
+ await databases . createIntegerAttribute ( DATABASE_ID , collectionId , key , required , undefined , undefined , undefined , array ) ;
71
89
}
72
90
await sleep ( 1_500 ) ;
73
91
}
0 commit comments