recherche
This commit is contained in:
@ -8,13 +8,42 @@ migrate(
|
||||
(app) => {
|
||||
const usersId = app.findCollectionByNameOrId("users").id;
|
||||
|
||||
function loadOrCreate(name, factory) {
|
||||
/** Retrouve une collection même si findCollectionByNameOrId échoue (casse, cache, image Docker). */
|
||||
function findExistingCollection(name) {
|
||||
try {
|
||||
return app.findCollectionByNameOrId(name);
|
||||
} catch {
|
||||
} catch (_) {}
|
||||
try {
|
||||
const all = app.findAllCollections();
|
||||
const want = String(name).toLowerCase();
|
||||
for (let i = 0; i < all.length; i++) {
|
||||
const c = all[i];
|
||||
if (c && c.name && String(c.name).toLowerCase() === want) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
} catch (_) {}
|
||||
return null;
|
||||
}
|
||||
|
||||
function loadOrCreate(name, factory) {
|
||||
const existing = findExistingCollection(name);
|
||||
if (existing != null) {
|
||||
return existing;
|
||||
}
|
||||
try {
|
||||
const col = factory();
|
||||
app.save(col);
|
||||
return col;
|
||||
} catch (err) {
|
||||
const msg = String(err && err.value ? err.value : err && err.message ? err.message : err);
|
||||
if (msg.includes("unique") || msg.includes("Unique")) {
|
||||
const again = findExistingCollection(name);
|
||||
if (again != null) {
|
||||
return again;
|
||||
}
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user