todos-contra-el-fuego-web/imports/startup/server/fixtures.js
vjrj 0ff9848cd4 meteor3: re-enable fixtures and sitemaps with async/Meteor-3 implementations
fixtures: drop @cleverbeagle/seeder (sync Mongo, unmaintained) for a small
idempotent async seeder (same dev accounts). sitemaps: drop dead
gadicohen:sitemaps for a WebApp.connectHandlers /sitemap.xml with the same
static page list (the per-fire section was already disabled). Verified on
dev boot: 6 users ensured, sitemap serving; REST smoke byte-identical.
2026-07-14 07:06:07 +02:00

71 lines
2 KiB
JavaScript

/* eslint-disable no-await-in-loop */
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import Documents from '../../api/Documents/Documents';
/*
* Dev/staging fixtures. Replaces @cleverbeagle/seeder (sync Mongo, no Meteor 3
* support) with a small async seeder: idempotent (find-before-create), only
* runs outside production.
*/
const USERS = [
{
email: 'admin@admin.com',
password: 'password',
profile: { name: { first: 'Andy', last: 'Warhol' } },
roles: ['admin']
},
...[
['Frida', 'Kahlo'],
['Joaquín', 'Sorolla'],
['Maruja', 'Mallo'],
['Remedios', 'Varo'],
['Pablo', 'Picasso']
].map(([first, last], i) => ({
email: `user+${i + 1}@test.com`,
password: 'password',
profile: { name: { first, last } },
roles: ['user']
}))
];
const DOCUMENTS_PER_USER = 5;
async function ensureUser({ email, password, profile, roles }) {
const existing = await Meteor.users.findOneAsync({ 'emails.address': email });
const userId = existing
? existing._id
: await Accounts.createUserAsync({ email, password, profile });
if (!existing && roles) {
await Meteor.users.updateAsync(userId, { $set: { roles } });
}
return userId;
}
async function ensureDocuments(userId) {
const count = await Documents.find({ owner: userId }).countAsync();
if (count > 0) return;
for (let i = 0; i < DOCUMENTS_PER_USER; i += 1) {
await Documents.insertAsync({
owner: userId,
title: `Document #${i + 1}`,
body: `This is the body of document #${i + 1}`
});
}
}
Meteor.startup(async () => {
// dev always; staging opts in with TCEF_SEED=1 (the old seeder's
// 'staging' environment had no reliable detection either).
if (!Meteor.isDevelopment && process.env.TCEF_SEED !== '1') return;
try {
for (const user of USERS) {
const userId = await ensureUser(user);
await ensureDocuments(userId);
}
console.log(`fixtures: ${USERS.length} dev users ensured`);
} catch (e) {
console.warn(`fixtures: seeding failed: ${e}`);
}
});