-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Closed
Labels
RepositoryIssues related to @loopback/repository packageIssues related to @loopback/repository packagefeature
Description
Hi,
Just wanted to share a script I wrote to migrate an existing mongodb database where all collections are lowercase. It's just too tedious to enter each field and collections!
Not sure where I should publish this so here goes. This matches style used in lb4 model creation. Each collection is seperated by '=============new collection================'. Please let me know if there was a better place to post code.
John
Run from mongo shell and copy all code between {}
{
//show dbs;
use dbname;
//show collections;
let collections = db.getCollectionNames();
for (var colpos in collections) {
let colname = collections[colpos];
let casecolname = collections[colpos].replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
var schema = db[colname].findOne();
var rec = '';
var recs = [];
var part1 = '@property({';
var part2 = " type: 'string',";
var part3 = ' id: true,';
var part4 = ' })';
var partreq = ' required: false,';
var partid = 'id?: string;';
recs.push(`import { Entity, model, property } from '@loopback/repository';`);
recs.push('');
recs.push(' // @model() '+colname);
recs.push('');
recs.push('@model({');
recs.push(' settings: {');
recs.push(' mongodb: {');
recs.push( ` collection: '${colname}', `);
recs.push(' }');
recs.push(' },');
recs.push('})');
recs.push('export class ' + casecolname + ' extends Entity {');
recs.push(part1);
recs.push(part2);
recs.push(part3);
recs.push(part4);
recs.push(partid);
recs.push('');
for (var key in schema) {
if (key === '_id' || key === 'createdAt' || key === 'updatedAt') { } else {
if (typeof schema[key] === 'string') {
recs.push(part1);
recs.push("type: 'string',");
recs.push(partreq);
recs.push(part4);
recs.push(key + ": string;");
recs.push('');
}
if (typeof schema[key] === 'number') {
recs.push(part1);
recs.push("type: 'number',");
recs.push(partreq);
recs.push(part4);
recs.push(key + ": number;");
recs.push('');
}
if (typeof schema[key] === 'date') {
recs.push(part1);
recs.push("type: 'date',");
recs.push(partreq);
recs.push(part4);
recs.push(key + ": date;");
recs.push('');
}
if (typeof schema[key] === 'boolean') {
recs.push(part1);
recs.push("type: 'boolean',");
recs.push(partreq);
recs.push(' default: false,');
recs.push(part4);
recs.push(key + "?: boolean;");
recs.push('');
}
}
}
recs.push( ' constructor(data?: Partial<' + casecolname + '>) {');
recs.push(' super(data);');
recs.push(' }');
recs.push('}');
for (var ln in recs) {
print(recs[ln]);
}
print('=============new collection================')
}
}
PushTheLimit
Metadata
Metadata
Assignees
Labels
RepositoryIssues related to @loopback/repository packageIssues related to @loopback/repository packagefeature