delete related records
delete related records
by JayData
HTML
<script src="http://include.jaydata.org/1.2.7/jaydata.js"></script>
JavaScript
$data.Entity.extend('$mwDemo.TYPE1', {
ID: { type: 'int', key: true, computed: true},
FIELD_1: { type: 'int' },
FIELD_2: { type: 'int' },
FIELD_DATETIME: { type: Date }
});
$data.Entity.extend('$mwDemo.TYPE2', {
ID: { type: 'int', key: true, computed: true},
FIELD_1: { type: 'int' },
FIELD_2: { type: 'int' },
FIELD_3: { type: 'int' },
FIELD_4: { type: 'int' }
});
$data.EntityContext.extend('$mwDemo.Context', {
TABLE1: { type: $data.EntitySet, elementType: $mwDemo.TYPE1 },
TABLE2: { type: $data.EntitySet, elementType: $mwDemo.TYPE2 }
});
var ctx = new $mwDemo.Context({ name: 'local', databaseName: 'jayDataTest38' });
// clear the db so you can run this multiple time
// if you have lots of data then divide it into smaller batches
function init() {
// this is a workaround for a bug, won't be needed from the next release (1.2.7+)
if (!$data.StorageProviderLoader.isSupported('sqLite'))
ctx.storageProvider.fieldConverter.toDb['$data.Date'] = function (date) { return date ? "new Date(Date.parse('" + date.toISOString() + "'))" : null; };
var p1 = ctx.TABLE1.toArray();
var p2 = ctx.TABLE2.toArray();
return $.when(p1,p2)
.then(function(t1, t2) {
t1.forEach(function(o) {
ctx.TABLE1.remove(o);
});
t2.forEach(function(o) {
ctx.TABLE2.remove(o);
});
console.log('initialized');
return ctx.saveChanges();
});
}
function fill() {
ctx.TABLE1.add(new $mwDemo.TYPE1({FIELD_1: 1, FIELD_2: 1, FIELD_DATETIME: new Date() }));
ctx.TABLE2.add(new $mwDemo.TYPE2({FIELD_1: 1, FIELD_2: 1, FIELD_3: 1, FIELD_4: 1 }));
ctx.TABLE2.add(new $mwDemo.TYPE2({FIELD_1: 1, FIELD_2: 1, FIELD_3: 2, FIELD_4: 2 }));
ctx.TABLE2.add(new $mwDemo.TYPE2({FIELD_1: 1, FIELD_2: 1, FIELD_3: 3, FIELD_4: 3 }));
ctx.TABLE2.add(new $mwDemo.TYPE2({FIELD_1: 1, FIELD_2: 2, FIELD_3: 3, FIELD_4: 3 }));
console.log('filled');
return ctx.saveChanges();
}
function...