@@ -9,6 +9,19 @@ import path from "path";
9
9
10
10
const require = Module . createRequire ( import . meta. url ) ;
11
11
12
+ const HAS_COLLECTION_ATTRIBUTE = ( collectionId , attributeKey ) => {
13
+ const collection = schema . collections . find (
14
+ ( item ) => item . $id === collectionId
15
+ ) ;
16
+ if ( ! collection ) {
17
+ return false ;
18
+ }
19
+ const attribute = collection . attributes . find (
20
+ ( item ) => item . key === attributeKey
21
+ ) ;
22
+ return ! ! attribute ;
23
+ } ;
24
+
12
25
if ( ! existsSync ( ".env" ) ) {
13
26
console . log ( "Missing .env file. Please use .env.example as a template" ) ;
14
27
process . exit ( - 1 ) ;
@@ -45,6 +58,27 @@ if (process.env.APPWRITE_SELF_SIGNED) {
45
58
46
59
const databases = new sdk . Databases ( client ) ;
47
60
61
+ const tryRead = async ( databaseId , collectionId , documentId ) => {
62
+ try {
63
+ return await databases . getDocument ( databaseId , collectionId , documentId ) ;
64
+ } catch {
65
+ return null ;
66
+ }
67
+ } ;
68
+
69
+ const writeTransform = ( data , collectionId ) => {
70
+ const userEntries = Object . entries ( data )
71
+ . filter ( ( [ key ] ) => ! key . startsWith ( "$" ) )
72
+ . filter ( ( [ key ] ) => {
73
+ const hasAttribute = HAS_COLLECTION_ATTRIBUTE ( collectionId , key ) ;
74
+ if ( ! hasAttribute ) {
75
+ console . log ( `SKIP ATTRIBUTE key=${ key } collection=${ collectionId } ` ) ;
76
+ }
77
+ return hasAttribute ;
78
+ } )
79
+ return Object . fromEntries ( userEntries ) ;
80
+ } ;
81
+
48
82
const sleep = ( timeout = 1_000 ) =>
49
83
new Promise ( ( res ) => {
50
84
setTimeout ( ( ) => {
@@ -59,18 +93,18 @@ await fs.mkdir("backup/databases", { recursive: true });
59
93
console . log ( `Found ${ total } databases!` ) ;
60
94
for ( const database of databasesList ) {
61
95
const collections = entries . map ( FIND_COLLECTION ) ;
62
- console . log ( `Found ${ total } collections id ${ database . $id } !` ) ;
96
+ console . log ( `Found ${ collections . length } collections id ${ database . $id } !` ) ;
63
97
for ( const collection of collections ) {
64
98
const databaseFiles = await glob (
65
- `backup/databases/*/${ collection } /*.json` ,
99
+ `backup/databases/*/${ collection . $id } /*.json` ,
66
100
{
67
101
withFileTypes : true ,
68
102
stat : true ,
69
103
} ,
70
104
) ;
71
105
databaseFiles . sort ( ( a , b ) => a . mtime - b . mtime ) ;
72
106
console . log (
73
- `Found ${ databaseFiles . length } collection ${ collection } documents` ,
107
+ `Found ${ databaseFiles . length } collection ${ collection . $id } documents` ,
74
108
) ;
75
109
for ( const fileRef of databaseFiles ) {
76
110
const file = fileRef . fullpathPosix ( ) ;
0 commit comments