CreateReadUpdateDelete : Defining a many:many relation
Create entities that have a many:many relation and connect them.
by SchizoDuckie
HTML
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://schizoduckie.github.io/CreateReadUpdateDelete.js/src/CRUD.js"></script>
<script src="https://schizoduckie.github.io/CreateReadUpdateDelete.js/src/CRUD.SqliteAdapter.js"></script>
<div class="well"><h1>CreateReadUpdateDelete : Defining a many:many relation</h1>
<p>Example many : many relationships</p>
<button id="wipe">Wipe database</button>
JavaScript
CRUD.DEBUG = true;
// checkout the console to see debug info by CRUD.SqliteAdapter.js
if(!'openDatabase' in window) {
alert("Your inferior browser does not support WebSQL. Try Chrome(ium), Safari, Opera, or any other Webkit based browser.");
}
// drop tables on click and reload page
document.getElementById('wipe').onclick = function() {
CRUD.executeQuery('drop table Series').then(
CRUD.executeQuery('drop table Actors')).then(
CRUD.executeQuery('drop table Actors_Roles')).then(
CRUD.executeQuery('drop table Roles')).then(
function() {
window.location.reload();
});
};
function Serie() {
CRUD.Entity.call(this);
}
function Role() {
CRUD.Entity.call(this);
}
function Actor() {
CRUD.Entity.call(this);
}
function Actor_Role() {
CRUD.Entity.call(this);
}
CRUD.define(Serie, {
table: 'Series',
primary: 'ID_Serie',
fields: ['ID_Serie', 'name', 'TVDB_ID'],
relations: {
'Role': CRUD.RELATION_FOREIGN
},
createStatement: 'CREATE TABLE Series (ID_Serie INTEGER PRIMARY KEY NOT NULL, name VARCHAR(250) DEFAULT(NULL), TVDB_ID INTEGER UNIQUE NOT NULL)',
});
CRUD.define(Role, {
table: 'Roles',
primary: 'ID_Role',
fields: ['ID_Role', 'name'],
relations: {
'Actor' : CRUD.RELATION_MANY
},
connectors: {
'Actor': 'Actor_Role'
},
createStatement: 'CREATE TABLE Roles (ID_Role INTEGER PRIMARY KEY NOT NULL, name VARCHAR(250) DEFAULT(NULL))'
});
CRUD.define(Actor, {
table: 'Actors',
primary: 'ID_Actor',
fields: ['ID_Actor', 'firstname', 'lastname', 'gender'],
relations: {
'Role' : CRUD.RELATION_MANY
},
connectors: {
'Role' : 'Actor_Role'
},
createStatement: 'CREATE TABLE Actors (ID_Actor INTEGER PRIMARY KEY NOT NULL, firstname VARCHAR(250) DEFAULT(NULL), lastname VARCHAR(250) DEFAULT(NULL), gender VARCHAR(1) DEFAULT(NULL))'
});
CRUD.define(Actor_Role, {
table: 'Actors_Roles',
primary: 'ID_Actor_Role',
fields:...