cher-db
Advanced tools
Comparing version 0.4.2 to 0.5.0
@@ -8,4 +8,4 @@ | ||
const Bookshelf = require('bookshelf'); | ||
const dataModel = require('./models/dataModel'); | ||
const userModel = require('./models/userModel'); | ||
const socialContent = require('./models/socialContent'); | ||
const userModel = require('./models/user'); | ||
const modelManager = require('./modelManager'); | ||
@@ -31,3 +31,3 @@ const modelUtils = require('./models/utils'); | ||
(callback) => { | ||
dataModel(bookshelf, knex, 'social_data') | ||
socialContent(bookshelf, knex, 'social_content') | ||
.then((model) => { | ||
@@ -50,2 +50,3 @@ callback(null, model); | ||
results.modelUtils = modelUtils; | ||
results.destroy = bookshelf.knex.destroy; | ||
resolver.resolve(results); | ||
@@ -52,0 +53,0 @@ } |
@@ -13,7 +13,7 @@ | ||
const proto = { | ||
register( name, model ) { | ||
register(name, model) { | ||
models[ name ] = model; | ||
this.emit( 'model', name ); | ||
this.emit('model', name); | ||
}, | ||
get( name ) { | ||
get(name) { | ||
return models[ name ]; | ||
@@ -23,2 +23,10 @@ }, | ||
return models; | ||
}, | ||
create(type, opts) { | ||
const Type = this.get(type); | ||
if (Type) { | ||
return new Type(opts); | ||
} else { | ||
throw new Error(`Model type '${type}' does not exist`); | ||
} | ||
} | ||
@@ -28,6 +36,6 @@ }; | ||
module.exports = () => { | ||
if ( !instance ) { | ||
instance = utils.extend( EventEmitter, proto ); | ||
instance.on( 'model', ( modelName ) => { | ||
log.debug( `Registered new model: ${modelName}` ); | ||
if (!instance) { | ||
instance = utils.extend(EventEmitter, proto); | ||
instance.on('model', (modelName) => { | ||
log.debug(`Registered new model: ${modelName}`); | ||
}); | ||
@@ -34,0 +42,0 @@ } |
'use strict'; | ||
const P = require( 'bluebird' ); | ||
const tools = require( 'cher-tools' ); | ||
const modelManager = require( '../modelManager' )(); | ||
const P = require('bluebird'); | ||
const tools = require('cher-tools'); | ||
const modelManager = require('../modelManager')(); | ||
const log = tools.log(); | ||
const createUser = ( opts, isAdmin ) => { | ||
log.debug( 'Creating user %s', opts.username ); | ||
const UserModel = modelManager.get( 'UserModel' ); | ||
const createUser = (opts, isAdmin) => { | ||
log.debug(`Creating user ${opts.username}`); | ||
const UserModel = modelManager.get('User'); | ||
const resolver = P.pending(); | ||
@@ -16,3 +16,3 @@ const uname = opts.username; | ||
if ( uname && upass ) { | ||
if (uname && upass) { | ||
const userData = { | ||
@@ -23,14 +23,14 @@ username: uname, | ||
if ( isAdmin === true ) { | ||
userData.is_admin = true; | ||
if (isAdmin === true) { | ||
userData.is_admin = true; // jshint ignore: line | ||
} | ||
new UserModel( userData ) | ||
new UserModel(userData) | ||
.save() | ||
.then( ( u ) => { | ||
const username = u.get( 'username' ); | ||
log.debug( `Successfully created user ${username}` ); | ||
resolver.resolve( u ); | ||
}).catch( ( err ) => { | ||
resolver.reject( err ); | ||
.then((u) => { | ||
const username = u.get('username'); | ||
log.debug(`Successfully created user ${username}`); | ||
resolver.resolve(u); | ||
}).catch((err) => { | ||
resolver.reject(err); | ||
}); | ||
@@ -40,3 +40,3 @@ } else { | ||
'Username and Password are required for creating a user!' | ||
); | ||
); | ||
} | ||
@@ -46,22 +46,19 @@ return resolver.promise; | ||
exports.fetchUser = ( username ) => { | ||
const UserModel = modelManager.get( 'UserModel' ); | ||
exports.fetchUser = (username) => { | ||
const UserModel = modelManager.get('User'); | ||
const resolver = P.pending(); | ||
UserModel | ||
.where( { username: username } ) | ||
.where({ username: username }) | ||
.fetch() | ||
.then( ( user ) => { | ||
resolver.resolve( user ); | ||
}).catch( ( err ) => { | ||
resolver.reject( err ); | ||
}); | ||
.then((user) => resolver.resolve(user)) | ||
.catch((err) => resolver.reject(err)); | ||
return resolver.promise; | ||
}; | ||
exports.createAdmin = ( opts ) => { | ||
return createUser( opts, true ); | ||
exports.createAdmin = (opts) => { | ||
return createUser(opts, true); | ||
}; | ||
exports.createUser = ( opts ) => { | ||
return createUser( opts ); | ||
exports.createUser = (opts) => { | ||
return createUser(opts); | ||
}; |
{ | ||
"name": "cher-db", | ||
"version": "0.4.2", | ||
"version": "0.5.0", | ||
"description": "Data layer for Cher", | ||
@@ -23,3 +23,4 @@ "main": "index.js", | ||
"bookshelf": "^0.9.1", | ||
"cher-tools": "^0.4.3", | ||
"cher-tools": "^0.5.6", | ||
"co": "^4.6.0", | ||
"knex": "^0.9.0", | ||
@@ -30,2 +31,3 @@ "pg": "^4.4.3" | ||
"dotenv": "^1.2.0", | ||
"knex-cleaner": "^1.1.1", | ||
"tape": "^4.2.2" | ||
@@ -32,0 +34,0 @@ }, |
170
test/test.js
'use strict'; | ||
const path = require( 'path' ); | ||
const test = require( 'tape' ); | ||
const db = require( '../index' ); | ||
require('dotenv').config({ path: 'test/test.env' }); | ||
require('dotenv') | ||
.config( { path: path.resolve( __dirname, 'test.env' ) } ); | ||
const test = require('tape'); | ||
const co = require('co'); | ||
const setup = require('./utils/setup'); | ||
const P = require('bluebird'); | ||
const getConfig = () => { | ||
return { | ||
HOST: process.env.DB_HOST, | ||
PORT: process.env.DB_PORT, | ||
NAME: process.env.DB_NAME, | ||
USER: process.env.USER | ||
}; | ||
const pause = () => { | ||
const resolver = P.pending(); | ||
setTimeout(() => resolver.resolve(), 1000); | ||
return resolver.promise; | ||
}; | ||
test('it works', ( t ) => { | ||
test('fetching items works', (t) => { | ||
co(function*() { | ||
const db = yield setup.up(); | ||
const SocialContent = db.models().get('SocialContent'); | ||
let data1 = yield setup.createSocialContent(db.models()); | ||
yield setup.createSocialContent(db.models()); | ||
let data4 = yield setup.createSocialContent(db.models()); | ||
let data5 = yield setup.createSocialContent(db.models()); | ||
let tag = yield setup.createTag(db.models(), 'foo', 'bar'); | ||
data4 = yield data4.tagContentWith(tag); | ||
const thePast = new Date(); | ||
yield pause(); | ||
yield data5.set('fetched_at', new Date()).save(); | ||
const items = yield SocialContent.fetchItems({ | ||
exclude: [ data1.get('id') ], | ||
moderation: null, | ||
source: 'twitter', | ||
before: thePast | ||
}); | ||
items.forEach((item) => { | ||
if (item.get('id') === data1.get('id')) { | ||
t.fail('data1 should have been exculded from the fetch'); | ||
} | ||
if (item.get('id') === data4.get('id')) { | ||
const tag = item.related('tags').first(); | ||
t.equal(tag.get('key'), 'foo', 'tag has the correct key'); | ||
t.equal(tag.get('value'), 'bar', 'tag has the correct value'); | ||
} | ||
if (item.get('id') === data5.get('id')) { | ||
t.fail('data5 should not be here because it was fetched after the specified param'); | ||
} | ||
}); | ||
yield setup.down(db, true); | ||
t.end(); | ||
}) | ||
.catch((err) => t.fail(err)); | ||
}); | ||
test('tagging works', (t) => { | ||
co(function*() { | ||
const db = yield setup.up(); | ||
let data = yield setup.createSocialContent(db.models()); | ||
let tag = yield setup.createTag(db.models(), 'foo', 'bar'); | ||
let fetchedContent = yield data.tagContentWith(tag); | ||
let contentTags = fetchedContent.related('tags'); | ||
t.equal(contentTags.length, 1, 'content has only 1 tag'); | ||
const tagInstance = contentTags.first(); | ||
t.equal( | ||
tagInstance.get('id'), | ||
tag.get('id'), | ||
'the associated tag and the original tag are the same' | ||
); | ||
fetchedContent = yield fetchedContent | ||
.removeTag(tagInstance); | ||
contentTags = fetchedContent.related('tags'); | ||
t.equal(contentTags.length, 0, 'content has no tags'); | ||
yield setup.down(db, true); | ||
t.end(); | ||
}) | ||
.catch((err) => t.fail(err)); | ||
}); | ||
test('moderation works', (t) => { | ||
co(function*() { | ||
const db = yield setup.up(); | ||
const SocialContent = db.models().get('SocialContent'); | ||
let count; | ||
let data = yield setup.createSocialContent(db.models()); | ||
t.notOk(data.get('moderation'), 'content is note moderated initially'); | ||
data = yield data.moderate(true).save(); | ||
count = yield SocialContent.countModerationStatus('twitter', true); | ||
t.equal(count, 1, 'there is 1 approved item'); | ||
t.equal(data.get('moderation'), true, 'content is approved'); | ||
data = yield data.moderate(false).save(); | ||
count = yield SocialContent.countModerationStatus('twitter', true); | ||
t.equal(count, 0, 'there are 0 approved items'); | ||
count = yield SocialContent.countModerationStatus('twitter', false); | ||
t.equal(count, 1, 'there is 1 denied items'); | ||
t.equal(data.get('moderation'), false, 'content is denied'); | ||
data = yield data.moderate(null).save(); | ||
t.notOk(data.get('moderation'), 'content is unmoderated'); | ||
count = yield SocialContent.countModerationStatus('twitter', false); | ||
t.equal(count, 0, 'there are 0 denied items'); | ||
count = yield SocialContent.countModerationStatus('twitter', null); | ||
t.equal(count, 1, 'there is 1 unmoderated item'); | ||
try { | ||
data = yield data.moderate('foo').save(); | ||
} catch(e) { | ||
t.ok(e, 'catch an error for non boolean moderation attempt'); | ||
} | ||
yield setup.down(db, true); | ||
t.end(); | ||
}) | ||
.catch((err) => t.fail(err)); | ||
}); | ||
test('social content creation works', (t) => { | ||
co(function*() { | ||
const db = yield setup.up(); | ||
const data = yield setup.createSocialContent(db.models()); | ||
t.ok(data.get('id'), 'social content has id'); | ||
yield setup.down(db, true); | ||
t.end(); | ||
}) | ||
.catch((err) => t.fail(err)); | ||
}); | ||
test('exporting items works', (t) => { | ||
co(function*() { | ||
const db = yield setup.up(); | ||
const SocialContent = db.models().get('SocialContent'); | ||
let data1 = yield setup.createSocialContent(db.models()); | ||
let data2 = yield setup.createSocialContent(db.models()); | ||
let data4 = yield setup.createSocialContent(db.models()); | ||
let data5 = yield setup.createSocialContent(db.models()); | ||
let tag = yield setup.createTag(db.models(), 'foo', 'bar'); | ||
yield data1.tagContentWith(tag); | ||
yield data2.moderate(true).save(); | ||
const items = yield SocialContent.export(); | ||
items.forEach((item) => { | ||
if (item.id === data1.get('id')) { | ||
t.equal(item.tags.length, 1); | ||
t.equal(item.tags[0], 'foo=bar'); | ||
} else if (item.id === data2.get('id')) { | ||
t.equal(item.moderation, 'approved'); | ||
} | ||
}); | ||
yield setup.down(db, true); | ||
t.end(); | ||
}) | ||
.catch((err) => t.fail(err)); | ||
}); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 4 instances in 1 package
1
35
1
23555
8
3
14
643
1
+ Addedco@^4.6.0
+ Addedcher-tools@0.5.7(transitive)
+ Addedco@4.6.0(transitive)
+ Addedluvely@0.10.3(transitive)
- Removedcher-tools@0.4.3(transitive)
- Removedluvely@0.9.1(transitive)
Updatedcher-tools@^0.5.6