@chevre/domain
Version:
Chevre Domain Library for Node.js
101 lines (90 loc) • 3.56 kB
text/typescript
// tslint:disable:no-console
import * as mongoose from 'mongoose';
import { chevre } from '../../../lib/index';
// const project = { id: String(process.env.PROJECT_ID) };
// const excludedProject = { id: String(process.env.EXCLUDED_PROJECT_ID) };
// tslint:disable-next-line:max-func-body-length
async function main() {
await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
const identityProviderRepo = await chevre.repository.IdentityProvider.createInstance(mongoose.connection);
const projectRepo = await chevre.repository.Project.createInstance(mongoose.connection);
const cursor = projectRepo.getCursor(
{
// _id: { $eq: 'cinerino' }
},
{
_id: 1,
settings: 1,
typeOf: 1
}
);
console.log('docs found');
let i = 0;
let updateCount = 0;
await cursor.eachAsync(async (doc) => {
i += 1;
const project: Pick<chevre.factory.project.IProject, 'id' | 'typeOf' | 'settings'> = doc.toObject();
console.log(
'alreadyMigrated?', project.id, i);
const tokenIssuers: string[] | undefined = (<any>project.settings).tokenIssuers;
let alreadyMigrated = false;
if (Array.isArray(tokenIssuers)) {
let existingProvidersCount = 0;
for (const tokenIssuer of tokenIssuers) {
const existingProviders = await identityProviderRepo.projectFields(
{
limit: 1,
page: 1,
project: { id: { $eq: project.id } },
identifier: { $eq: tokenIssuer }
},
['identifier']
);
existingProvidersCount += existingProviders.length;
}
if (existingProvidersCount === tokenIssuers.length) {
alreadyMigrated = true;
}
} else {
alreadyMigrated = true;
}
if (alreadyMigrated) {
console.log(
'already migrated.', project.id, i);
} else {
if (Array.isArray(tokenIssuers)) {
const newProviders = tokenIssuers.map((tokenIssuer) => {
return {
identifier: tokenIssuer,
project: {
id: project.id,
typeOf: <chevre.factory.organizationType.Project>chevre.factory.organizationType.Project
},
typeOf: <chevre.factory.organizationType.Organization>chevre.factory.organizationType.Organization,
verified: true
};
});
console.log(
'updating project...',
project.id, i, newProviders.length, 'newProviders');
for (const newProvider of newProviders) {
console.log(
'updating project...',
project.id, i, newProvider);
// await identityProviderRepo.save({
// attributes: newProvider
// });
}
updateCount += 1;
console.log(
'updated.',
project.id, i);
}
}
});
console.log(i, 'docs checked');
console.log(updateCount, 'docs updated');
}
main()
.then()
.catch(console.error);