Here’s the data model.
The classes are defined like this
import wixData from ‘wix-data’ ;
let _chapters =
let _lessons =
class Group {
constructor ( key , arr ) {
this . key = key ;
this . array = arr ;
}
}
function groupBy ( lessonsArr , property ) {
var reduced = lessonsArr . reduce (( arr , obj ) => {
const key = obj[property ];
if (! arr[key ]) {
arr[key ] = ;
}
// Add object to list for given key’s value
arr[key ]. push ( obj );
return arr ;
}, {});
**const** groups = [];
**for(const** item **in** reduced ){
groups . push ( **new** Group ( item , reduced[item ]))
}
**return** groups ;
}
const uuid = require ( ‘uuid’ );
export class Course {
constructor ( course ) {
if (! course ) { debugger ; }
let id = uuid . v4 (). toString ();
this . _id = id ;
this . slug = course.slug ;
const groups = groupBy ( course.Lessons , “chapterTitle” );
_chapters=_chapters . concat ( groups . map ( group => new Chapter ( this , group )));
}
static fromArray ( courses ) {
**const** result = [];
**for** ( **const** c **of** courses ) {
**if** ( c.slug && c . Lessons ) {
result . push ( **new** Course ( c ));
}
}
**return** result ;
}
}
export class Chapter {
constructor ( course , group ) {
if (! group || ! group.array [ 0 ]) { debugger ; }
let id = uuid . v4 (). toString ();
this . _id = id ;
this [ “Course-2” ] = course ;
const first = group.array [ 0 ];
this . chapterTitle = first.chapterTitle ;
_lessons=_lessons . concat ( group.array . map ( lesson => new Lesson ( this , lesson )));
}
}
export class Lesson {
constructor ( chapter , lesson ) {
if (! lesson ) { debugger ; }
let id = uuid . v4 (). toString ();
this . _id = id ;
this . chapter = chapter ;
this . lessonTitle = lesson.lessonTitle ;
this . lessonUrl = lesson.lessonUrl ;
}
}
And it’s imported with this:
async function importData () {
try {
await wixData . truncate ( “Course” );
await wixData . truncate ( “Chapter” );
await wixData . truncate ( “Lesson” );
**const** array = **set** ; // is parsed JSON
**const** courses = Course . fromArray ( array );
**return** [
**await** wixData . bulkInsert ( "Course" , courses ),
**await** wixData . bulkInsert ( "Chapter" , _chapters ),
**await** wixData . bulkInsert ( "Lesson" , _lessons )
]
} **catch** ( er ) {
**debugger**
console . error ( er )
}
**finally** {
**debugger**
}
}
I don’t get any errors, but I also don’t get any references. If I change the global arrays to being fields of Course and Chapter, then bulk import reports the stack goes too deep which I’m assuming is the circular reference caused by double linking the related objects.