25 lines
1.6 KiB
JavaScript
25 lines
1.6 KiB
JavaScript
// Add named feature indexes only. No .env loading and no index deletions.
|
|
const mongoose = require('mongoose');
|
|
const { PostSchema } = require('../dist/legacy/src/modules/posts/schemas/post.schema');
|
|
const { UserSchema } = require('../dist/legacy/src/modules/users/schemas/user.schema');
|
|
const { musicIndexes } = require('../dist/legacy/src/modules/music-commerce/music-commerce.store');
|
|
|
|
async function main() {
|
|
const specs = [
|
|
{ collection: 'posts', schema: PostSchema, allowed: (name) => name?.startsWith('feed_') || name === 'video_worker_claim_v1' },
|
|
{ collection: 'users', schema: UserSchema, allowed: (name) => name?.startsWith('talent_') },
|
|
].map(({ collection, schema, allowed }) => ({ collection,
|
|
indexes: schema.indexes().filter(([, options]) => allowed(options.name)).map(([key, options]) => ({ key, ...options })) }));
|
|
specs.push(...Object.entries(musicIndexes).map(([collection, indexes]) => ({ collection, indexes })));
|
|
if (process.argv.includes('--dry-run')) { console.log(JSON.stringify(specs, null, 2)); return; }
|
|
if (!process.env.MONGODB_URI) throw new Error('Set MONGODB_URI explicitly');
|
|
const connection = await mongoose.createConnection(process.env.MONGODB_URI, { autoIndex: false }).asPromise();
|
|
try {
|
|
for (const spec of specs) {
|
|
const indexes = await connection.collection(spec.collection).createIndexes(spec.indexes);
|
|
console.log(JSON.stringify({ collection: spec.collection, indexes }));
|
|
}
|
|
} finally { await connection.close(); }
|
|
}
|
|
main().catch(() => { console.error('Feature index synchronization failed; verify the build and database configuration'); process.exitCode = 1; });
|