almeira-web/wait_and_seed.js

48 lines
1.6 KiB
JavaScript

const { Client, Databases } = require('./node_modules/node-appwrite');
const client = new Client()
.setEndpoint('https://appwrite.almeira.com.co/v1')
.setProject('69b84df5003bd8957c5c')
.setKey('standard_cce3cdb866ea6433888d40cedd24dc45987372308fdc3dd0697de08648c0e5c7e04b02c3bcee271403fd656b00a5820f04c08b51631284a6e8a3582d8aed2aa33234f3c29cc777e9b162eb54810b789251b0d006a9a3b081ebc2da53b9baa94614b124b3508ad849fd9a4187cc6094f506d4b57f409b69d9562a74df594f3ec3');
const dbs = new Databases(client);
const DB_ID = 'sneakers_multistore_db';
async function waitForAvailable() {
const collections = ['cities', 'payment_methods', 'suppliers', 'store_settings', 'orders', 'products', 'supplier_posts', 'monitored_groups'];
for (let attempt = 1; attempt <= 30; attempt++) {
let allAvailable = true;
let pendingCount = 0;
for (const col of collections) {
try {
const res = await dbs.listAttributes(DB_ID, col);
const processing = res.attributes.filter(a => a.status === 'processing');
if (processing.length > 0) {
allAvailable = false;
pendingCount += processing.length;
}
} catch (e) {
allAvailable = false;
}
}
if (allAvailable) {
console.log('✅ All attributes are AVAILABLE now!');
return true;
}
console.log(`[Attempt ${attempt}/30] Waiting... ${pendingCount} attributes still processing...`);
await new Promise(r => setTimeout(r, 2000));
}
return false;
}
waitForAvailable().then((ok) => {
if (ok) {
require('./seed_appwrite_initial.js');
}
}).catch(console.error);