Skip to content

Commit 7ed2ec0

Browse files
committed
refactor(model): re-implement seed to use new API
1 parent 06b5ac4 commit 7ed2ec0

3 files changed

Lines changed: 17 additions & 165 deletions

File tree

examples/app.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict';
22

33
/* dependencies */
4-
const path = require('path');
5-
const async = require('async');
64
const { include } = require('@lykmapipo/include');
75
const { connect } = require('@lykmapipo/mongoose-common');
86
const { get, start, mount } = require('@lykmapipo/express-common');

lib/feature.model.js

Lines changed: 17 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222
*/
2323

2424
/* dependencies */
25-
const path = require('path');
2625
const _ = require('lodash');
27-
const { parallel, waterfall } = require('async');
2826
const { getString } = require('@lykmapipo/env');
29-
const { Schema, model, SCHEMA_OPTIONS } = require('@lykmapipo/mongoose-common');
27+
const { Schema, model, SCHEMA_OPTIONS, copyInstance } = require('@lykmapipo/mongoose-common');
3028
const { Point, Geometry, centroidOf } = require('mongoose-geojson-schemas');
3129
const {
3230
CONTINENT_NAMES,
@@ -50,7 +48,6 @@ const FEATURE_COLLECTION_NAME = getString(
5048
'FEATURE_COLLECTION_NAME',
5149
'features'
5250
);
53-
const FEATURE_SEED = getString('FEATURE_SEED', 'features');
5451

5552
/**
5653
* @name FeatureSchema
@@ -444,106 +441,31 @@ FeatureSchema.statics.CONTINENTS = CONTINENT_NAMES;
444441
FeatureSchema.statics.DEFAULT_COUNTRY_NAME = DEFAULT_COUNTRY_NAME;
445442
FeatureSchema.statics.COUNTRIES = COUNTRY_NAMES;
446443

444+
447445
/**
448-
* @name upsert
449-
* @function upsert
450-
* @description create or update existing feature
451-
* @param {Object} feature valid feature details
452-
* @param {Function} done callback to invoke on success or error
446+
* @name prepareSeedCriteria
447+
* @function prepareSeedCriteria
448+
* @description prepare feature seeding upsert criteria
449+
* @param {Object} seed plain object feature seed
450+
* @return {Object} criteria used to upsert feature
453451
*
454452
* @author lally elias <lallyelias87@gmail.com>
455-
* @since 1.0.0
453+
* @since 1.5.0
456454
* @version 0.1.0
457455
* @public
458456
*/
459-
FeatureSchema.statics.upsert = function upsert(feature, done) {
460-
//normalize arguments
461-
let _feature = _.isFunction(feature.toObject) ? feature.toObject() : feature;
462-
463-
//refs
464-
const Feature = this;
465-
466-
// prepare upsert
467-
waterfall(
468-
[
469-
function findExistingFeature(next) {
470-
// prepare criteria by _id or fields
471-
let criteria = _.merge({}, _feature);
472-
/* jshint ignore:start */
473-
criteria = criteria._id
474-
? _.pick(criteria, '_id')
475-
: _.pick(criteria, 'nature', 'family', 'type', 'name');
476-
/* jshint ignore:end */
477-
Feature.findOne(criteria, next);
478-
},
479-
480-
function upsertFeature(found, next) {
481-
// instantiate if not found
482-
if (!found) {
483-
found = new Feature(_feature);
484-
}
485-
486-
// prepare updates
487-
_feature = _.merge({}, _feature, found.toObject());
488-
489-
// do upsert
490-
found.updatedAt = new Date();
491-
found.put(_feature, next);
492-
},
493-
],
494-
done
457+
FeatureSchema.statics.prepareSeedCriteria = seed => {
458+
// prepare feature upsert criteria by _id or fields
459+
let criteria = copyInstance(seed);
460+
criteria = (
461+
criteria._id ?
462+
_.pick(criteria, '_id') :
463+
_.pick(criteria, 'nature', 'family', 'type', 'name')
495464
);
465+
// return feature upsert criteria
466+
return criteria;
496467
};
497468

498-
/**
499-
* @name seed
500-
* @function seed
501-
* @description seed features into database, on duplicate existing wins
502-
* on merging.
503-
* @param {Feature[]} [features] set of feature(s) to seed
504-
* @param {Function} done callback to invoke on success or error
505-
*
506-
* @author lally elias <lallyelias87@gmail.com>
507-
* @since 1.0.0
508-
* @version 0.1.0
509-
* @public
510-
*/
511-
FeatureSchema.statics.seed = function seed(seeds, done) {
512-
// normalize arguments
513-
let _seeds = _.isFunction(seeds) ? [] : [].concat(seeds);
514-
const _done = _.isFunction(seeds) ? seeds : done;
515-
516-
// refs
517-
const Feature = this;
518-
519-
// init features collection
520-
let features = [];
521-
522-
// try load seeds from environment
523-
const BASE_PATH = getString('BASE_PATH', process.cwd());
524-
const SEEDS_PATH = getString('SEEDS_PATH', path.join(BASE_PATH, 'seeds'));
525-
const SEED_PATH = path.join(SEEDS_PATH, FEATURE_SEED);
526-
try {
527-
const seed = require(SEED_PATH);
528-
_seeds = [].concat(_seeds).concat(seed);
529-
} catch (e) {
530-
/* ignore */
531-
}
532-
533-
// collect unique feature from seeds
534-
_seeds = _.compact(_seeds);
535-
_seeds = _.uniqWith(_seeds, _.isEqual);
536-
537-
// upsert features
538-
features = _.map([].concat(_seeds), function(feature) {
539-
return function upsertFeatures(next) {
540-
Feature.upsert(feature, next);
541-
};
542-
});
543-
544-
// seed features
545-
parallel(features, _done);
546-
};
547469

548470
/*
549471
*------------------------------------------------------------------------------

test/integration/feature.upsert.spec.js

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)