これは、JSON オブジェクトからのインポートを行う私の方法です。
関係に従って正しい順序でデータをインポートすることに注意する必要があります。たとえば、会社がまだ存在しないため、最初に従業員をインポートして会社を割り当てることはできません。したがって、ここでの正しい順序は、会社をインポートしてから従業員をインポートし、関連する会社をリンクすることです。
/**
* Example Data
*/
var hireDate = new Date('2012',7,13,1,0,0),
companies = [{name:'Company 1'},{name:'Company 2'},{name:'Company 3'}],
employees = [{firstName:'John',lastName:'Doe',company:'Company 1',hireDate:hireDate,salary:2000},
{firstName:'Frederic',lastName:'Smith',company:'Company 2',hireDate:hireDate,salary:2000},
{firstName:'John',lastName:'Doe',company:'Company 3',hireDate:hireDate,salary:3000},
{firstName:'Marc',lastName:'Petit',company:'Company 2',hireDate:hireDate,salary:5000},
{firstName:'Arthur',lastName:'Johns',company:'Company 3',hireDate:hireDate,salary:2500},
{firstName:'Mike',lastName:'Sina',company:'Company 3',hireDate:hireDate,salary:2900},
{firstName:'Jack',lastName:'Taylor',company:'Company 1',hireDate:hireDate,salary:7800},
{firstName:'Simon',lastName:'Portmann',company:'Company 2',hireDate:hireDate,salary:4700}],
newCompany,newEmployee;
companies.forEach(function(company){
newCompany = new ds.Company(company);
newCompany.save();
});
employees.forEach(function(employee){
newEmployee = new ds.Employee(employee);
newEmployee.company = ds.Company.find('name == :1',employee.company)
newEmployee.save();
});
ds.Employee.all()
属性名が dataClass のものと同じである場合は、作成時にオブジェクトを割り当てることができます。または、そのような特別なルールがある場合は、属性を操作できます。
employees.forEach(function(employee){
newEmployee = new ds.Employee();
// Example how to assign value to an attribute on the import
// newEmployee is the new employee created in the dataStore of Wakanda.
// employee is the employee which come from the JSON object.
// Maybe the imported data have different attribute names.
newEmployee.firstName = employee.name;
newEmployee.lastName = employee.lname;
newEmployee.birthDate = new Date(employee.datestamp);
newEmployee.company = ds.Company.find('name == :1',employee.company)
newEmployee.save();
});