From 3864f4daf81a4571562bc434a1844c5f272bd93a Mon Sep 17 00:00:00 2001 From: Zoe Snow Date: Sat, 29 Jul 2017 08:15:10 -0600 Subject: [PATCH] Refactoring models to use a single column for primaryKey --- eanaEltuMigration/dictionary.js | 135 ++++++++++-------------- models/dictionaryBlock.js | 3 +- models/dictionaryBuild.js | 2 +- models/dictionaryBuildData.js | 4 +- models/dictionaryTemplate.js | 3 +- models/entryLayout.js | 3 +- models/entryLayoutTemplates.js | 6 +- models/entryTemplate.js | 3 +- models/entryType.js | 8 +- models/entryTypeEntries.js | 24 ----- models/entryTypeLayouts.js | 6 +- models/entryTypeTemplates.js | 25 ----- models/grapheme.js | 5 +- models/graphemePhonemeCorrespondence.js | 2 - models/lemma.js | 3 +- models/lemmaClassType.js | 3 +- models/lemmaClassTypeAssociation.js | 2 - models/lemmaDefinition.js | 4 - models/linkedLemma.js | 4 - models/localizedEntryLayout.js | 4 - models/localizedMetadata.js | 4 - models/morpheme.js | 3 +- models/morphemeAffixType.js | 3 +- models/morphemeDefinition.js | 4 - models/phoneme.js | 6 +- models/source.js | 10 +- routes/apiV1.js | 2 + routes/v1/crud.js | 34 ++++++ 28 files changed, 117 insertions(+), 198 deletions(-) delete mode 100644 models/entryTypeEntries.js delete mode 100644 models/entryTypeTemplates.js create mode 100644 routes/v1/crud.js diff --git a/eanaEltuMigration/dictionary.js b/eanaEltuMigration/dictionary.js index 299848a..cd4cab8 100644 --- a/eanaEltuMigration/dictionary.js +++ b/eanaEltuMigration/dictionary.js @@ -112,9 +112,11 @@ Dictionary.prototype.exportDictionaryBuilds = function () { ]; const builds = []; const buildData = []; + let i = 1; for(const type of Object.keys(self.eanaEltu.dictOrder)){ builds.push({ - id: type, + id: i, + name: type, description: getDictionaryBuildDescription(type), LanguageIsoCode: "nav" }); @@ -135,7 +137,7 @@ Dictionary.prototype.exportDictionaryBuilds = function () { } buildData.push({ - DictionaryBuildId: type, + DictionaryBuildId: i, DictionaryBlockId: getDictionaryBlockId(data), TemplateId: data.template, position: data.pos, @@ -144,8 +146,9 @@ Dictionary.prototype.exportDictionaryBuilds = function () { LanguageIsoCode: "nav" }); } + i++; } - + return models.DictionaryBuild.bulkCreate(builds).then(function(){ "use strict"; return models.DictionaryBlock.bulkCreate(blocks).then(function(){ @@ -588,7 +591,7 @@ Dictionary.prototype.exportLemmaClassTypes = function () { for (const classType of Object.keys(self.lemmaClassTypes)) { const newClassType = { LanguageIsoCode: "nav", - id: classType, + classType: classType, name: classType, abbreviation: classType, description: classType @@ -600,7 +603,7 @@ Dictionary.prototype.exportLemmaClassTypes = function () { //console.log(classType, lc); classTypes.push({ LanguageIsoCode: lc, - id: classType, + classType: classType, name: lcClassType, abbreviation: lcClassType, description: lcClassType @@ -621,8 +624,9 @@ Dictionary.prototype.exportLemmaClassTypes = function () { return models.LemmaClassType.bulkCreate(classTypes).then(function(){ "use strict"; return models.LemmaClassType.findAll({where: {LanguageIsoCode: "nav"}}).then(function(lemmaClassTypes){ + self.lemmaClassTypes = {}; for(let i = 0; i < lemmaClassTypes.length; i ++){ - self.lemmaClassTypes[lemmaClassTypes[i].id] = lemmaClassTypes[i]; + self.lemmaClassTypes[lemmaClassTypes[i].classType] = lemmaClassTypes[i].get({plain: true}); } }) }); @@ -675,7 +679,8 @@ function findLinkedLemma(self, lemma, linkedLemma, position){ referencedObject.DerivedMorphemeId = self.morphemeLookup[id].id; referencedObject.type = "morpheme"; } else { - console.log(linkedLemma.id, id); + // TODO: Finish matching up irregular entries + //console.log(linkedLemma.id, id); referencedObject.type = "unknown"; } } @@ -849,135 +854,135 @@ Dictionary.prototype.exportMorphemes = function() { Dictionary.prototype.exportGraphemes = function(){ "use strict"; const graphemes = [{ - id: "'", + grapheme: "'", sortOrder: 1, LanguageIsoCode: "nav" },{ - id: "a", + grapheme: "a", sortOrder: 2, LanguageIsoCode: "nav" },{ - id: "aw", + grapheme: "aw", sortOrder: 3, LanguageIsoCode: "nav" },{ - id: "ay", + grapheme: "ay", sortOrder: 4, LanguageIsoCode: "nav" },{ - id: "ä", + grapheme: "ä", sortOrder: 5, LanguageIsoCode: "nav" },{ - id: "e", + grapheme: "e", sortOrder: 6, LanguageIsoCode: "nav" },{ - id: "ew", + grapheme: "ew", sortOrder: 7, LanguageIsoCode: "nav" },{ - id: "ey", + grapheme: "ey", sortOrder: 8, LanguageIsoCode: "nav" },{ - id: "f", + grapheme: "f", sortOrder: 9, LanguageIsoCode: "nav" },{ - id: "h", + grapheme: "h", sortOrder: 10, LanguageIsoCode: "nav" },{ - id: "i", + grapheme: "i", sortOrder: 11, LanguageIsoCode: "nav" },{ - id: "ì", + grapheme: "ì", sortOrder: 12, LanguageIsoCode: "nav" },{ - id: "k", + grapheme: "k", sortOrder: 13, LanguageIsoCode: "nav" },{ - id: "kx", + grapheme: "kx", sortOrder: 14, LanguageIsoCode: "nav" },{ - id: "l", + grapheme: "l", sortOrder: 15, LanguageIsoCode: "nav" },{ - id: "ll", + grapheme: "ll", sortOrder: 16, LanguageIsoCode: "nav" },{ - id: "m", + grapheme: "m", sortOrder: 17, LanguageIsoCode: "nav" },{ - id: "n", + grapheme: "n", sortOrder: 18, LanguageIsoCode: "nav" },{ - id: "ng", + grapheme: "ng", sortOrder: 19, LanguageIsoCode: "nav" },{ - id: "o", + grapheme: "o", sortOrder: 20, LanguageIsoCode: "nav" },{ - id: "p", + grapheme: "p", sortOrder: 21, LanguageIsoCode: "nav" },{ - id: "px", + grapheme: "px", sortOrder: 22, LanguageIsoCode: "nav" },{ - id: "r", + grapheme: "r", sortOrder: 23, LanguageIsoCode: "nav" },{ - id: "rr", + grapheme: "rr", sortOrder: 24, LanguageIsoCode: "nav" },{ - id: "s", + grapheme: "s", sortOrder: 25, LanguageIsoCode: "nav" },{ - id: "t", + grapheme: "t", sortOrder: 26, LanguageIsoCode: "nav" },{ - id: "tx", + grapheme: "tx", sortOrder: 27, LanguageIsoCode: "nav" },{ - id: "ts", + grapheme: "ts", sortOrder: 28, LanguageIsoCode: "nav" },{ - id: "u", + grapheme: "u", sortOrder: 29, LanguageIsoCode: "nav" },{ - id: "v", + grapheme: "v", sortOrder: 30, LanguageIsoCode: "nav" },{ - id: "w", + grapheme: "w", sortOrder: 31, LanguageIsoCode: "nav" },{ - id: "y", + grapheme: "y", sortOrder: 32, LanguageIsoCode: "nav" },{ - id: "z", + grapheme: "z", sortOrder: 33, LanguageIsoCode: "nav" }]; @@ -988,167 +993,134 @@ Dictionary.prototype.exportGraphemes = function(){ Dictionary.prototype.exportPhonemes = function(){ "use strict"; const phonemes = [{ - id: "'", ipa: "ʔ", sortOrder: 1, LanguageIsoCode: "nav" },{ - id: "a", ipa: "a", sortOrder: 2, LanguageIsoCode: "nav" },{ - id: "aw", ipa: "aw", sortOrder: 3, LanguageIsoCode: "nav" },{ - id: "ay", ipa: "aj", sortOrder: 4, LanguageIsoCode: "nav" },{ - id: "ä", ipa: "æ", sortOrder: 5, LanguageIsoCode: "nav" },{ - id: "e", ipa: "ɛ", sortOrder: 6, LanguageIsoCode: "nav" },{ - id: "ew", ipa: "ɛw", sortOrder: 7, LanguageIsoCode: "nav" },{ - id: "ey", ipa: "ɛj", sortOrder: 8, LanguageIsoCode: "nav" },{ - id: "f", ipa: "f", sortOrder: 9, LanguageIsoCode: "nav" },{ - id: "h", ipa: "h", sortOrder: 10, LanguageIsoCode: "nav" },{ - id: "i", ipa: "i", sortOrder: 11, LanguageIsoCode: "nav" },{ - id: "ì", ipa: "ɪ", sortOrder: 12, LanguageIsoCode: "nav" },{ - id: "k", ipa: "k", sortOrder: 13, LanguageIsoCode: "nav" },{ - id: "kx", ipa: "kʼ", sortOrder: 14, LanguageIsoCode: "nav" },{ - id: "l", ipa: "l", sortOrder: 15, LanguageIsoCode: "nav" },{ - id: "ll", ipa: "ḷ", sortOrder: 16, LanguageIsoCode: "nav" },{ - id: "m", ipa: "m", sortOrder: 17, LanguageIsoCode: "nav" },{ - id: "n", ipa: "n", sortOrder: 18, LanguageIsoCode: "nav" },{ - id: "ng", ipa: "ŋ", sortOrder: 19, LanguageIsoCode: "nav" },{ - id: "o", ipa: "o", sortOrder: 20, LanguageIsoCode: "nav" },{ - id: "p", ipa: "p", sortOrder: 21, LanguageIsoCode: "nav" },{ - id: "px", ipa: "pʼ", sortOrder: 22, LanguageIsoCode: "nav" },{ - id: "r", ipa: "ɾ", sortOrder: 23, LanguageIsoCode: "nav" },{ - id: "rr", ipa: "ṛ", sortOrder: 24, LanguageIsoCode: "nav" },{ - id: "s", ipa: "s", sortOrder: 25, LanguageIsoCode: "nav" },{ - id: "t", ipa: "t", sortOrder: 26, LanguageIsoCode: "nav" },{ - id: "tx", ipa: "tʼ", sortOrder: 27, LanguageIsoCode: "nav" },{ - id: "ts", ipa: "t͡s", sortOrder: 28, LanguageIsoCode: "nav" },{ - id: "u", ipa: "u", sortOrder: 29, LanguageIsoCode: "nav" },{ - id: "v", ipa: "v", sortOrder: 30, LanguageIsoCode: "nav" },{ - id: "w", ipa: "w", sortOrder: 31, LanguageIsoCode: "nav" },{ - id: "y", ipa: "j", sortOrder: 32, LanguageIsoCode: "nav" },{ - id: "z", ipa: "z", sortOrder: 33, LanguageIsoCode: "nav" @@ -1212,7 +1184,7 @@ Dictionary.prototype.export = function (callback) { thirdLayerPromises.push(self.exportMorphemes()); Promise.all(thirdLayerPromises).then(function(){ - self.exportLemmas().then(function () { + self.exportLemmas().then(callback);/*function () { models.EntryType.findAll().then(function(entryTypes) { const promises = []; entryTypes.forEach(function(entryType){ @@ -1225,7 +1197,7 @@ Dictionary.prototype.export = function (callback) { }); Promise.all(promises).then(callback); }); - }); + });*/ }); }); }); @@ -1729,10 +1701,11 @@ function buildDictionaryTemplates(self) { function processLemmaClass(self, classType){ "use strict"; - const id = classType.trim(); - if(self.lemmaClassTypes[id] === undefined){ - self.lemmaClassTypes[id] = { - id: id, + const name = classType.trim(); + if(self.lemmaClassTypes[name] === undefined){ + self.lemmaClassTypes[name] = { + id: name, + name: name, localizations: {} }; } @@ -1740,9 +1713,9 @@ function processLemmaClass(self, classType){ function processLocalizedLemmaClass(self, classType, localizedClassType, lc){ "use strict"; - const id = classType.trim(); + const name = classType.trim(); if(localizedClassType !== null && localizedClassType !== undefined){ - self.lemmaClassTypes[id].localizations[lc] = localizedClassType.trim(); + self.lemmaClassTypes[name].localizations[lc] = localizedClassType.trim(); } } diff --git a/models/dictionaryBlock.js b/models/dictionaryBlock.js index 956e5d3..0b53d72 100644 --- a/models/dictionaryBlock.js +++ b/models/dictionaryBlock.js @@ -9,8 +9,7 @@ module.exports = function (sequelize, DataTypes) { // associations can be defined here DictionaryBlock.belongsTo(models.Language, { foreignKey: { - allowNull: false, - primaryKey: true + allowNull: false }, constraints: true, onDelete: 'cascade' diff --git a/models/dictionaryBuild.js b/models/dictionaryBuild.js index 8d8b4c1..8ef602b 100644 --- a/models/dictionaryBuild.js +++ b/models/dictionaryBuild.js @@ -1,7 +1,7 @@ 'use strict'; module.exports = function (sequelize, DataTypes) { const DictionaryBuild = sequelize.define('DictionaryBuild', { - id: { type: DataTypes.STRING, primaryKey: true }, + name: { type: DataTypes.STRING, allowNull: false }, description: { type: DataTypes.STRING } }); diff --git a/models/dictionaryBuildData.js b/models/dictionaryBuildData.js index cf7a302..bfc7b3f 100644 --- a/models/dictionaryBuildData.js +++ b/models/dictionaryBuildData.js @@ -1,7 +1,7 @@ 'use strict'; module.exports = function (sequelize, DataTypes) { const DictionaryBuildData = sequelize.define('DictionaryBuildData', { - position: { type: DataTypes.INTEGER, primaryKey: true }, + position: { type: DataTypes.INTEGER, allowNull: false }, type: { type: DataTypes.STRING}, data: { type: DataTypes.TEXT, allowNull: true } }); @@ -10,7 +10,6 @@ module.exports = function (sequelize, DataTypes) { // associations can be defined here DictionaryBuildData.belongsTo(models.DictionaryBuild, { foreignKey: { - primaryKey: true, allowNull: false }, constraints: true, @@ -19,7 +18,6 @@ module.exports = function (sequelize, DataTypes) { DictionaryBuildData.belongsTo(models.Language, { foreignKey: { - primaryKey: true, allowNull: false }, constraints: true, diff --git a/models/dictionaryTemplate.js b/models/dictionaryTemplate.js index 52fb3e2..07a9110 100644 --- a/models/dictionaryTemplate.js +++ b/models/dictionaryTemplate.js @@ -11,8 +11,7 @@ module.exports = function (sequelize, DataTypes) { // associations can be defined here DictionaryTemplate.belongsTo(models.Language, { foreignKey: { - allowNull: false, - primaryKey: true + allowNull: false }, constraints: true, onDelete: 'cascade' diff --git a/models/entryLayout.js b/models/entryLayout.js index be3da9a..f98bb4a 100644 --- a/models/entryLayout.js +++ b/models/entryLayout.js @@ -21,8 +21,7 @@ module.exports = function (sequelize, DataTypes) { through: { model: models.EntryLayoutTemplates, unique: false - }, - primaryKey: true + } }); }; diff --git a/models/entryLayoutTemplates.js b/models/entryLayoutTemplates.js index eac3adc..be040e4 100644 --- a/models/entryLayoutTemplates.js +++ b/models/entryLayoutTemplates.js @@ -1,8 +1,9 @@ 'use strict'; module.exports = function (sequelize, DataTypes) { const EntryLayoutTemplates = sequelize.define('EntryLayoutTemplates', { - position: { type: DataTypes.INTEGER, primaryKey: true, defaultValue: 0 }, - field: { type: DataTypes.STRING, primaryKey: true } + id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, + position: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0 }, + field: { type: DataTypes.STRING, allowNull: false } }); EntryLayoutTemplates.associate = function (models) { @@ -13,7 +14,6 @@ module.exports = function (sequelize, DataTypes) { EntryLayoutTemplates.belongsTo(models.EntryLayout, { foreignKey: { - primaryKey: true, allowNull: false }, constraints: true, diff --git a/models/entryTemplate.js b/models/entryTemplate.js index b8bb771..08871f3 100644 --- a/models/entryTemplate.js +++ b/models/entryTemplate.js @@ -12,8 +12,7 @@ module.exports = function (sequelize, DataTypes) { through: { model: models.EntryLayoutTemplates, unique: false - }, - primaryKey: true + } }); }; diff --git a/models/entryType.js b/models/entryType.js index 505c3ac..925044c 100644 --- a/models/entryType.js +++ b/models/entryType.js @@ -18,12 +18,8 @@ module.exports = function (sequelize, DataTypes) { onDelete: 'CASCADE' }); - EntryType.belongsToMany(models.Lemma, { - through: { - model: models.EntryTypeEntries, - unique: false - }, - primaryKey: true + EntryType.hasMany(models.Lemma, { + onDelete: 'CASCADE' }); EntryType.belongsToMany(models.Metadata, { diff --git a/models/entryTypeEntries.js b/models/entryTypeEntries.js deleted file mode 100644 index 30c7bd0..0000000 --- a/models/entryTypeEntries.js +++ /dev/null @@ -1,24 +0,0 @@ -'use strict'; -module.exports = function (sequelize, DataTypes) { - const EntryTypeEntries = sequelize.define('EntryTypeEntries', { - position: { type: DataTypes.INTEGER, primaryKey: true, defaultValue: 0 } - }); - - EntryTypeEntries.associate = function (models) { - // associations can be defined here - EntryTypeEntries.belongsTo(models.EntryType, { - foreignKey: { - primaryKey: true, - allowNull: false - }, - constraints: true, - onDelete: 'CASCADE' - }); - - EntryTypeEntries.belongsTo(models.Lemma, { - - }); - }; - - return EntryTypeEntries; -}; diff --git a/models/entryTypeLayouts.js b/models/entryTypeLayouts.js index 90ed3b8..e4d93c3 100644 --- a/models/entryTypeLayouts.js +++ b/models/entryTypeLayouts.js @@ -1,16 +1,13 @@ 'use strict'; module.exports = function (sequelize, DataTypes) { const EntryTypeLayout = sequelize.define('EntryTypeLayout', { - order: { type: DataTypes.INTEGER, primaryKey: true } + order: { type: DataTypes.INTEGER, allowNull: false } }); - EntryTypeLayout.removeAttribute('id'); - EntryTypeLayout.associate = function (models) { // associations can be defined here EntryTypeLayout.belongsTo(models.EntryType, { foreignKey: { - primaryKey: true, allowNull: false }, constraints: true, @@ -19,7 +16,6 @@ module.exports = function (sequelize, DataTypes) { EntryTypeLayout.belongsTo(models.EntryLayout, { foreignKey: { - primaryKey: true, allowNull: false }, constraints: true, diff --git a/models/entryTypeTemplates.js b/models/entryTypeTemplates.js deleted file mode 100644 index 8c4262d..0000000 --- a/models/entryTypeTemplates.js +++ /dev/null @@ -1,25 +0,0 @@ -'use strict'; -module.exports = function (sequelize, DataTypes) { - const EntryTypeTemplates = sequelize.define('EntryTypeTemplates', { - position: { type: DataTypes.INTEGER, primaryKey: true, defaultValue: 0 }, - field: { type: DataTypes.STRING, primaryKey: true } - }); - - EntryTypeTemplates.associate = function (models) { - // associations can be defined here - EntryTypeTemplates.belongsTo(models.EntryTemplate, { - - }); - - EntryTypeTemplates.belongsTo(models.EntryType, { - foreignKey: { - primaryKey: true, - allowNull: false - }, - constraints: true, - onDelete: 'CASCADE' - }); - }; - - return EntryTypeTemplates; -}; diff --git a/models/grapheme.js b/models/grapheme.js index c0206c1..49bc4a0 100644 --- a/models/grapheme.js +++ b/models/grapheme.js @@ -1,7 +1,7 @@ 'use strict'; module.exports = function (sequelize, DataTypes) { const Grapheme = sequelize.define('Grapheme', { - id: { type: DataTypes.STRING, primaryKey: true }, + grapheme: { type: DataTypes.STRING, allowNull: false }, sortOrder: { type: DataTypes.INTEGER, allowNull: false } }); @@ -9,8 +9,7 @@ module.exports = function (sequelize, DataTypes) { // associations can be defined here Grapheme.belongsTo(models.Language, { foreignKey: { - allowNull: false, - primaryKey: true + allowNull: false }, constraints: true, onDelete: 'cascade' diff --git a/models/graphemePhonemeCorrespondence.js b/models/graphemePhonemeCorrespondence.js index 4acf677..b415395 100644 --- a/models/graphemePhonemeCorrespondence.js +++ b/models/graphemePhonemeCorrespondence.js @@ -10,7 +10,6 @@ module.exports = function (sequelize, DataTypes) { // associations can be defined here GraphemePhonemeCorrespondence.belongsTo(models.Grapheme, { foreignKey: { - primaryKey: true, allowNull: false }, constraints: true, @@ -19,7 +18,6 @@ module.exports = function (sequelize, DataTypes) { GraphemePhonemeCorrespondence.belongsTo(models.Phoneme, { foreignKey: { - primaryKey: true, allowNull: false }, constraints: true, diff --git a/models/lemma.js b/models/lemma.js index d62a7c8..24d9fb0 100644 --- a/models/lemma.js +++ b/models/lemma.js @@ -16,8 +16,7 @@ module.exports = function (sequelize, DataTypes) { // associations can be defined here Lemma.belongsTo(models.Language, { foreignKey: { - allowNull: false, - primaryKey: true + allowNull: false }, constraints: true, onDelete: 'cascade' diff --git a/models/lemmaClassType.js b/models/lemmaClassType.js index 20d10b6..2f7480d 100644 --- a/models/lemmaClassType.js +++ b/models/lemmaClassType.js @@ -1,7 +1,7 @@ 'use strict'; module.exports = function (sequelize, DataTypes) { const LemmaClassType = sequelize.define('LemmaClassType', { - id: { type: DataTypes.STRING, allowNull: false, primaryKey: true }, + classType: { type: DataTypes.STRING, allowNull: false }, name: { type: DataTypes.STRING, allowNull: false }, abbreviation: { type: DataTypes.STRING, allowNull: false }, description: { type: DataTypes.STRING, allowNull: true } @@ -11,7 +11,6 @@ module.exports = function (sequelize, DataTypes) { // associations can be defined here LemmaClassType.belongsTo(models.Language, { foreignKey: { - primaryKey: true, allowNull: false }, constraints: true, diff --git a/models/lemmaClassTypeAssociation.js b/models/lemmaClassTypeAssociation.js index 26aa43e..23e8788 100644 --- a/models/lemmaClassTypeAssociation.js +++ b/models/lemmaClassTypeAssociation.js @@ -10,7 +10,6 @@ module.exports = function (sequelize, DataTypes) { // associations can be defined here LemmaClassTypeAssociation.belongsTo(models.Lemma, { foreignKey: { - primaryKey: true, allowNull: false }, constraints: true, @@ -19,7 +18,6 @@ module.exports = function (sequelize, DataTypes) { LemmaClassTypeAssociation.belongsTo(models.LemmaClassType, { foreignKey: { - primaryKey: true, allowNull: false }, constraints: true, diff --git a/models/lemmaDefinition.js b/models/lemmaDefinition.js index 5898a9f..d4b33e6 100644 --- a/models/lemmaDefinition.js +++ b/models/lemmaDefinition.js @@ -10,13 +10,10 @@ module.exports = function (sequelize, DataTypes) { odd: DataTypes.TEXT }); - LemmaDefinition.removeAttribute('id'); - LemmaDefinition.associate = function (models) { // associations can be defined here LemmaDefinition.belongsTo(models.Lemma, { foreignKey: { - primaryKey: true, allowNull: false }, constraints: true, @@ -24,7 +21,6 @@ module.exports = function (sequelize, DataTypes) { }); LemmaDefinition.belongsTo(models.Language, { foreignKey: { - primaryKey: true, allowNull: false }, constraints: true, diff --git a/models/linkedLemma.js b/models/linkedLemma.js index a735e56..ce8f972 100644 --- a/models/linkedLemma.js +++ b/models/linkedLemma.js @@ -5,13 +5,10 @@ module.exports = function (sequelize, DataTypes) { note: { type: DataTypes.STRING, allowNull: true } }); - LinkedLemma.removeAttribute('id'); - LinkedLemma.associate = function (models) { // associations can be defined here LinkedLemma.belongsTo(models.Lemma, { foreignKey: { - primaryKey: true, allowNull: false }, constraints: true, @@ -20,7 +17,6 @@ module.exports = function (sequelize, DataTypes) { LinkedLemma.belongsTo(models.Lemma, { as: "ReferencesLemma", foreignKey: { - primaryKey: true, allowNull: false }, constraints: true, diff --git a/models/localizedEntryLayout.js b/models/localizedEntryLayout.js index 1808b6f..fa0a917 100644 --- a/models/localizedEntryLayout.js +++ b/models/localizedEntryLayout.js @@ -6,13 +6,10 @@ module.exports = function (sequelize, DataTypes) { changeable: { type: DataTypes.STRING, allowNull: false, defaultValue: "" } }); - LocalizedEntryLayout.removeAttribute('id'); - LocalizedEntryLayout.associate = function (models) { // associations can be defined here LocalizedEntryLayout.belongsTo(models.EntryType, { foreignKey: { - primaryKey: true, allowNull: false }, constraints: true, @@ -20,7 +17,6 @@ module.exports = function (sequelize, DataTypes) { }); LocalizedEntryLayout.belongsTo(models.Language, { foreignKey: { - primaryKey: true, allowNull: false }, constraints: true, diff --git a/models/localizedMetadata.js b/models/localizedMetadata.js index a0e4625..8bc0ad2 100644 --- a/models/localizedMetadata.js +++ b/models/localizedMetadata.js @@ -5,13 +5,10 @@ module.exports = function (sequelize, DataTypes) { value: { type: DataTypes.TEXT, allowNull: false } }); - LocalizedMetadata.removeAttribute('id'); - LocalizedMetadata.associate = function (models) { // associations can be defined here LocalizedMetadata.belongsTo(models.Metadata, { foreignKey: { - primaryKey: true, allowNull: false }, constraints: true, @@ -19,7 +16,6 @@ module.exports = function (sequelize, DataTypes) { }); LocalizedMetadata.belongsTo(models.Language, { foreignKey: { - primaryKey: true, allowNull: false }, constraints: true, diff --git a/models/morpheme.js b/models/morpheme.js index b964c62..0101ffd 100644 --- a/models/morpheme.js +++ b/models/morpheme.js @@ -15,8 +15,7 @@ module.exports = function (sequelize, DataTypes) { // associations can be defined here Morpheme.belongsTo(models.Language, { foreignKey: { - allowNull: false, - primaryKey: true + allowNull: false }, constraints: true, onDelete: 'cascade' diff --git a/models/morphemeAffixType.js b/models/morphemeAffixType.js index 8031668..e93ccdd 100644 --- a/models/morphemeAffixType.js +++ b/models/morphemeAffixType.js @@ -9,8 +9,7 @@ module.exports = function (sequelize, DataTypes) { // associations can be defined here MorphemeAffixType.belongsTo(models.Language, { foreignKey: { - allowNull: false, - primaryKey: true + allowNull: false }, constraints: true, onDelete: 'cascade' diff --git a/models/morphemeDefinition.js b/models/morphemeDefinition.js index 8236e8f..3379e38 100644 --- a/models/morphemeDefinition.js +++ b/models/morphemeDefinition.js @@ -10,13 +10,10 @@ module.exports = function (sequelize, DataTypes) { odd: DataTypes.TEXT }); - MorphemeDefinition.removeAttribute('id'); - MorphemeDefinition.associate = function (models) { // associations can be defined here MorphemeDefinition.belongsTo(models.Morpheme, { foreignKey: { - primaryKey: true, allowNull: false }, constraints: true, @@ -24,7 +21,6 @@ module.exports = function (sequelize, DataTypes) { }); MorphemeDefinition.belongsTo(models.Language, { foreignKey: { - primaryKey: true, allowNull: false }, constraints: true, diff --git a/models/phoneme.js b/models/phoneme.js index 1f989f0..553eb1e 100644 --- a/models/phoneme.js +++ b/models/phoneme.js @@ -1,16 +1,14 @@ 'use strict'; module.exports = function (sequelize, DataTypes) { const Phoneme = sequelize.define('Phoneme', { - id: { type: DataTypes.STRING, primaryKey: true }, - ipa: { type: DataTypes.STRING }, + ipa: { type: DataTypes.STRING, allowNull: false } }); Phoneme.associate = function (models) { // associations can be defined here Phoneme.belongsTo(models.Language, { foreignKey: { - allowNull: false, - primaryKey: true + allowNull: false }, constraints: true, onDelete: 'cascade' diff --git a/models/source.js b/models/source.js index 750ec57..afb5419 100644 --- a/models/source.js +++ b/models/source.js @@ -1,16 +1,20 @@ 'use strict'; module.exports = function (sequelize, DataTypes) { const Source = sequelize.define('Source', { - name: { type: DataTypes.STRING, unique: true }, + name: { type: DataTypes.STRING }, description: DataTypes.STRING + }, { + indexes: [{ + unique: true, + fields: ["name", "LanguageIsoCode"] + }] }); Source.associate = function (models) { // associations can be defined here Source.belongsTo(models.Language, { foreignKey: { - allowNull: false, - primaryKey: true + allowNull: false }, constraints: true, onDelete: 'cascade' diff --git a/routes/apiV1.js b/routes/apiV1.js index 9eafa9e..cf0a888 100644 --- a/routes/apiV1.js +++ b/routes/apiV1.js @@ -2,6 +2,8 @@ const express = require('express'); const router = express.Router(); +router.use('/crud', require('./v1/crud')); + router.use('/dictionary/blocks', require('./v1/dictionary/blocks')); router.use('/dictionary/builds', require('./v1/dictionary/builds')); router.use('/dictionary/buildData', require('./v1/dictionary/buildData')); diff --git a/routes/v1/crud.js b/routes/v1/crud.js new file mode 100644 index 0000000..85b9a7e --- /dev/null +++ b/routes/v1/crud.js @@ -0,0 +1,34 @@ +const express = require('express'); +const router = express.Router(); +const models = require('../../models/index'); + +/* GET listing. */ +router.get('/:model', function(req, res, next) { + const model = models[req.params.model]; + if(model === undefined){ + res.status(400).send(`Model [${req.params.model}] does not exist`); + return; + } + + model.findAll().then(function (data) { + "use strict"; + res.send(data); + }); + +}); + +router.get('/:model/:id', function(req, res, next) { + const model = models[req.params.model]; + if(model === undefined){ + res.status(400).send(`Model [${req.params.model}] does not exist`); + return; + } + + model.findAll().then(function (data) { + "use strict"; + res.send(data); + }); + +}); + +module.exports = router;