Skip to content

Commit

Permalink
chore: trim white space throughout word documents
Browse files Browse the repository at this point in the history
  • Loading branch information
ijemmao committed Jun 5, 2023
1 parent c93d215 commit 55e734f
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 20 deletions.
2 changes: 1 addition & 1 deletion migrate-mongo-config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const config = {
mongodb: {
url: process.env.MONGO_URI || 'mongodb://localhost:27017',
url: process.env.MONGO_URI || 'mongodb://0.0.0.0:27017',
databaseName: process.env.DB_NAME || 'igbo_api',
options: {
useNewUrlParser: true,
Expand Down
103 changes: 103 additions & 0 deletions migrations/20230605005447-trim-headword-and-tenses-whitespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const trimPipeline = [
{
$project: {
word: {
$trim: {
input: '$word',
},
},
wordPronunciation: 1,
conceptualWord: 1,
definitions: 1,
dialects: 1,
tags: 1,
'tenses.infinitive': {
$trim: {
input: '$tenses.infinitive',
},
},
'tenses.imperative': {
$trim: {
input: '$tenses.imperative',
},
},
'tenses.simplePast': {
$trim: {
input: '$tenses.simplePast',
},
},
'tenses.presentPassive': {
$trim: {
input: '$tenses.presentPassive',
},
},
'tenses.simplePresent': {
$trim: {
input: '$tenses.simplePresent',
},
},
'tenses.presentContinuous': {
$trim: {
input: '$tenses.presentContinuous',
},
},
'tenses.future': {
$trim: {
input: '$tenses.future',
},
},
attributes: 1,
pronunciation: 1,
variations: 1,
frequency: 1,
relatedTerms: 1,
hypernyms: 1,
hyponyms: 1,
stems: 1,
__v: 1,
updatedAt: 1,
createdAt: 1,
},
},
];

const revertTrimPipeline = [
{
$project: {
word: 1,
wordPronunciation: 1,
conceptualWord: 1,
definitions: 1,
dialects: 1,
tags: 1,
tenses: 1,
attributes: 1,
pronunciation: 1,
variations: 1,
frequency: 1,
relatedTerms: 1,
hypernyms: 1,
hyponyms: 1,
stems: 1,
__v: 1,
updatedAt: 1,
createdAt: 1,
},
},
];

module.exports = {
async up(db) {
const collections = ['words', 'wordsuggestions'];
await Promise.all(collections.map((collection) => (
db.collection(collection).updateMany({}, trimPipeline)
)));
},

async down(db) {
const collections = ['words', 'wordsuggestions'];
await Promise.all(collections.map((collection) => (
db.collection(collection).updateMany({}, revertTrimPipeline)
)));
},
};
40 changes: 21 additions & 19 deletions src/models/Word.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,36 @@ const definitionSchema = new Schema({
default: WordClass.NNC.value,
enum: Object.values(WordClass).map(({ value }) => value),
},
label: { type: String, default: '', trim: true },
definitions: { type: [{ type: String }], default: [] },
nsibidi: { type: String, default: '' },
nsibidi: { type: String, default: '', index: true },
nsibidiCharacters: { type: [{ type: Types.ObjectId, ref: 'NsibidiCharacter' }], default: [] },
igboDefinitions: {
type: [{
igbo: String,
nsibidi: String,
nsibidiCharacters: { type: [{ type: Types.ObjectId, ref: 'NsibidiCharacter' }], default: [] },
}],
default: [],
},
}, { _id: true });
}, { _id: true, toObject: toObjectPlugin });

const dialectSchema = new Schema({
word: { type: String, required: true, index: true },
word: {
type: String,
required: true,
index: true,
trim: true,
},
variations: { type: [{ type: String }], default: [] },
dialects: { type: [{ type: String }], validate: (v) => every(v, (dialect) => Dialects[dialect].value), default: [] },
pronunciation: { type: String, default: '' },
}, { toObject: toObjectPlugin });

export const wordSchema = new Schema({
word: { type: String, required: true },
wordPronunciation: { type: String, default: '' },
conceptualWord: { type: String, default: '' },
word: { type: String, required: true, trim: true },
wordPronunciation: { type: String, default: '', trim: true },
conceptualWord: { type: String, default: '', trim: true },
definitions: [{
type: definitionSchema,
validate: (definitions) => (
Expand All @@ -52,19 +60,13 @@ export const wordSchema = new Schema({
v.every((tag) => Object.values(WordTags).map(({ value }) => value).includes(tag))
),
},
tenses: {
type: Object,
validate: (v) => {
const tenseValues = Object.values(Tenses);
Object.keys(v).every((key) => (
tenseValues.find(({ value: tenseValue }) => key === tenseValue)
));
},
required: false,
default: {},
},
attributes: Object.entries(WordAttributes)
.reduce((finalAttributes, [, { value }]) => ({
tenses: Object.values(Tenses)
.reduce((tenses, { value }) => ({
...tenses,
[value]: { type: String, default: '', trim: true },
}), {}),
attributes: Object.values(WordAttributes)
.reduce((finalAttributes, { value }) => ({
...finalAttributes,
[value]: { type: Boolean, default: false },
}), {}),
Expand Down

0 comments on commit 55e734f

Please sign in to comment.