Comparing version 0.0.7 to 0.0.8
@@ -10,6 +10,8 @@ 'use strict'; | ||
var _redinkErrors = require('redink-errors'); | ||
/* eslint-disable no-unused-vars */ | ||
var create = exports.create = function create(type, data) { | ||
return (0, _dbSingleton.db)().instance().create(type, data); | ||
}; /* eslint-disable no-unused-vars */ | ||
}; | ||
var update = exports.update = function update(type, id, data) { | ||
@@ -19,3 +21,3 @@ return (0, _dbSingleton.db)().instance().update(type, id, data); | ||
var archive = exports.archive = function archive(type, id) { | ||
return (0, _dbSingleton.db)().instance().delete(type, id); | ||
return (0, _dbSingleton.db)().instance().archive(type, id); | ||
}; | ||
@@ -39,3 +41,5 @@ var find = exports.find = function find(type) { | ||
return new Promise(function (resolve, reject) { | ||
(0, _dbSingleton.db)(schemas, host, name).start().then(resolve).catch(reject); | ||
(0, _dbSingleton.db)(schemas, host, name).start().then(resolve).catch( /* istanbul ignore next */function (err) { | ||
return reject(new _redinkErrors.RedinkError('Could not start Redink: ' + err.message)); | ||
}); | ||
}); | ||
@@ -45,3 +49,5 @@ }, | ||
return new Promise(function (resolve, reject) { | ||
(0, _dbSingleton.db)().stop().then(resolve(true)).catch(reject); | ||
(0, _dbSingleton.db)().stop().then(resolve(true)).catch( /* istanbul ignore next */function (err) { | ||
return reject(new _redinkErrors.RedinkError('Could not stop Redink: ' + err.message)); | ||
}); | ||
}); | ||
@@ -48,0 +54,0 @@ } |
@@ -13,3 +13,3 @@ 'use strict'; | ||
var _httpErrors = require('http-errors'); | ||
var _redinkErrors = require('redink-errors'); | ||
@@ -60,2 +60,12 @@ var _sanitizeRequest = require('./utils/sanitizeRequest'); | ||
/** | ||
* Connect to a RethinkDB database. | ||
* Requires host and name to be defined or will reject the promise. | ||
* | ||
* @throws {RedinkDatabaseError} - Error describing why the connection could not be made. | ||
* | ||
* @return {Object} - RethinkDB connection. | ||
*/ | ||
_createClass(Redink, [{ | ||
@@ -74,4 +84,4 @@ key: 'connect', | ||
return resolve(conn); | ||
}).catch(function (err) { | ||
return reject(new _httpErrors.UnprocessableEntity(err)); | ||
}).catch( /* istanbul ignore next */function (err) { | ||
return reject(new _redinkErrors.RedinkDatabaseError('Could not connect to the database: ' + err.message)); | ||
}); | ||
@@ -101,2 +111,4 @@ }); | ||
* | ||
* @throws {RedinkDatabaseError} - Chained error message passed to @see reject(). | ||
* | ||
* @param {String} type - The table name. | ||
@@ -118,2 +130,5 @@ * @param {Object} data - Flattened JSON representing attributes and relationships. | ||
var fieldsToMerge = (0, _getFieldsToMerge2.default)(schemas, type); | ||
var cascade = function cascade(postArray) { | ||
return _rethinkdb2.default.do(postArray).run(conn); | ||
}; | ||
var fetch = function fetch(_ref2) { | ||
@@ -124,12 +139,10 @@ var keys = _ref2.generated_keys; | ||
var cascade = function cascade(record) { | ||
returnObject = record; | ||
return _rethinkdb2.default.do((0, _cascadePost2.default)(record, type, conn, schemas)).run(conn); | ||
}; | ||
return new Promise(function (resolve, reject) { | ||
table.insert(sanitizedData).run(conn).then(fetch).then(cascade).then(function () { | ||
table.insert(sanitizedData).run(conn).then(fetch).then(function (record) { | ||
returnObject = record; | ||
return (0, _cascadePost2.default)(record, type, conn, schemas); | ||
}).then(cascade).then(function () { | ||
return resolve((0, _serializeResponse2.default)(schemas[type], returnObject)); | ||
}).catch(function (err) { | ||
return reject(new _httpErrors.UnprocessableEntity(err)); | ||
return reject(new _redinkErrors.RedinkDatabaseError('Error creating record of type \'' + type + '\': ' + err.message)); | ||
}); | ||
@@ -151,2 +164,4 @@ }); | ||
* | ||
* @throws {RedinkDatabaseError} - Chained error message passed to @see reject(). | ||
* | ||
* @param {String} type - The table name. | ||
@@ -168,5 +183,5 @@ * @param {String)} id - The ID of the record that is going to be updated. | ||
var sanitizedData = (0, _sanitizeRequest2.default)(schemas[type], data, 'update'); | ||
var sanitized = (0, _sanitizeRequest2.default)(schemas[type], data, 'update'); | ||
var updateArray = (0, _cascadeUpdate2.default)(type, id, data, schemas); | ||
var fieldsToMerge = (0, _getFieldsToMerge2.default)(schemas, type); | ||
var updateArray = (0, _cascadeUpdate2.default)(type, id, data, schemas); | ||
var fetch = function fetch() { | ||
@@ -180,6 +195,6 @@ return table.get(id).merge(fieldsToMerge).run(conn); | ||
return new Promise(function (resolve, reject) { | ||
table.get(id).update(sanitizedData).run(conn).then(cascade).then(fetch).then(function (record) { | ||
table.get(id).update(sanitized).run(conn).then(cascade).then(fetch).then(function (record) { | ||
return resolve((0, _serializeResponse2.default)(schemas[type], record)); | ||
}).catch(function (err) { | ||
return reject(new _httpErrors.UnprocessableEntity(err)); | ||
}).catch( /* istanbul ignore next */function (err) { | ||
return reject(new _redinkErrors.RedinkDatabaseError('Error updating record of type \'' + type + '\': ' + err.message)); | ||
}); | ||
@@ -190,3 +205,3 @@ }); | ||
/** | ||
* Deletes the record with id `id` from the table `type`. | ||
* Archives the record with id `id` from the table `type`. | ||
* | ||
@@ -199,3 +214,3 @@ * ```js | ||
* | ||
* TODO: Change name of method to `archive`. | ||
* @throws {RedinkDatabaseError} - Chained error message passed to @see reject(). | ||
* | ||
@@ -208,4 +223,4 @@ * @param {String} type - The table name. | ||
}, { | ||
key: 'delete', | ||
value: function _delete(type, id) { | ||
key: 'archive', | ||
value: function archive(type, id) { | ||
/* eslint-disable no-param-reassign */ | ||
@@ -227,4 +242,4 @@ id = '' + id; | ||
return new Promise(function (resolve, reject) { | ||
_rethinkdb2.default.do(reql).run(conn).then(didSucceed).then(resolve).catch(function (err) { | ||
return reject(new _httpErrors.UnprocessableEntity(err)); | ||
_rethinkdb2.default.do(reql).run(conn).then(didSucceed).then(resolve).catch( /* istanbul ignore next */function (err) { | ||
return reject(new _redinkErrors.RedinkDatabaseError('Error archiving record of type \'' + type + '\': ' + err.message)); | ||
}); | ||
@@ -244,2 +259,4 @@ }); | ||
* | ||
* @throws {RedinkDatabaseError} - Chained error message passed to @see reject(). | ||
* | ||
* @param {String} type - The table name. | ||
@@ -261,4 +278,4 @@ * @return {Boolean} | ||
return new Promise(function (resolve, reject) { | ||
table.delete().run(conn).then(didSucceed).then(resolve).catch(function (err) { | ||
return reject(new _httpErrors.UnprocessableEntity(err)); | ||
table.delete().run(conn).then(didSucceed).then(resolve).catch( /* istanbul ignore next */function (err) { | ||
return reject(new _redinkErrors.RedinkDatabaseError('Error clearing table of type \'' + type + '\': ' + err.message)); | ||
}); | ||
@@ -279,2 +296,4 @@ }); | ||
* | ||
* @throws {RedinkDatabaseError} - Chained error message passed to @see reject(). | ||
* | ||
* @param {String} type - The table name. | ||
@@ -300,4 +319,4 @@ * @param {(Object|Function)} [fiter={}] - The RethinkDB filter object or function. | ||
})); | ||
}).catch(function (err) { | ||
return reject(new _httpErrors.UnprocessableEntity(err)); | ||
}).catch( /* istanbul ignore next */function (err) { | ||
return reject(new _redinkErrors.RedinkDatabaseError('Error finding record of type \'' + type + '\': ' + err.message)); | ||
}); | ||
@@ -316,2 +335,4 @@ }); | ||
* | ||
* @throws {RedinkDatabaseError} - Chained error message passed to @see reject(). | ||
* | ||
* @param {String} type - The table name. | ||
@@ -336,4 +357,4 @@ * @param {String} id - The ID of the record that is going to be fetched. | ||
return resolve((0, _serializeResponse2.default)(schemas[type], record)); | ||
}).catch(function (err) { | ||
return reject(new _httpErrors.UnprocessableEntity(err)); | ||
}).catch( /* istanbul ignore next */function (err) { | ||
return reject(new _redinkErrors.RedinkDatabaseError('Error fetching record of type \'' + type + '\': ' + err.message)); | ||
}); | ||
@@ -340,0 +361,0 @@ }); |
@@ -7,3 +7,3 @@ 'use strict'; | ||
var _httpErrors = require('http-errors'); | ||
var _redinkErrors = require('redink-errors'); | ||
@@ -80,3 +80,3 @@ var _getRelationships = require('../utils/getRelationships'); | ||
if (record[relationship] === null) { | ||
throw new _httpErrors.Conflict('Cannot cascade post, entity does not exist but record was created.'); | ||
throw new _redinkErrors.RedinkUtilError('Cascade post failed: \'' + relationship + '\' does not exist but \'' + table + '\' was created.'); | ||
} | ||
@@ -83,0 +83,0 @@ |
@@ -7,4 +7,2 @@ 'use strict'; | ||
var _httpErrors = require('http-errors'); | ||
var _getRelationships = require('../utils/getRelationships'); | ||
@@ -74,5 +72,2 @@ | ||
if (!data[relationship].old && !data[relationship].new) { | ||
throw new _httpErrors.Conflict('Missing old and/or new fields.'); | ||
} | ||
updateArray.push.apply(updateArray, _toConsumableArray(createPostArray(entity, data[relationship], id))); | ||
@@ -79,0 +74,0 @@ } |
@@ -9,2 +9,4 @@ 'use strict'; | ||
var _redinkErrors = require('redink-errors'); | ||
/** | ||
@@ -31,4 +33,8 @@ * Return all original and inverse relationships of a given table. | ||
var objectVerify = function objectVerify(object) { | ||
if (Object.keys(object).length !== 2) throw new Error('Incorrect number of properties.'); | ||
if (!types.includes(Object.keys(object)[0])) throw new Error('Invalid relationship type.'); | ||
if (Object.keys(object).length !== 2) { | ||
throw new _redinkErrors.RedinkUtilError('Incorrect number of properties, expecting 2.'); | ||
} | ||
if (!types.includes(Object.keys(object)[0])) { | ||
throw new _redinkErrors.RedinkUtilError('Invalid relationship type, expecting `hasMany`, `belongsTo`, or `hasOne`.'); | ||
} | ||
}; | ||
@@ -35,0 +41,0 @@ |
@@ -6,2 +6,5 @@ 'use strict'; | ||
}); | ||
var _redinkErrors = require('redink-errors'); | ||
/** | ||
@@ -143,4 +146,14 @@ * Parses `data` and purges data fields that are not present in `schema`. | ||
if (sanitized.hasOwnProperty(relationship)) { | ||
var ids = isUpdate ? sanitized[relationship].new : sanitized[relationship]; | ||
var ids = void 0; | ||
if (isUpdate) { | ||
if (!data[relationship].old || !data[relationship].new) { | ||
throw new _redinkErrors.RedinkUtilError('Missing old and/or new fields for \'' + relationship + '\'.'); | ||
} | ||
ids = sanitized[relationship].new; | ||
} else { | ||
ids = sanitized[relationship]; | ||
} | ||
if (keys(schema.relationships[relationship]).includes('hasMany') && !Array.isArray(ids)) { | ||
@@ -171,4 +184,3 @@ sanitized[relationship] = [{ id: ids, archived: false }]; | ||
if (data.hasOwnProperty('id')) sanitized.id = data.id; | ||
return sanitized; | ||
}; |
{ | ||
"name": "redink", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "RethinkDB ORM", | ||
@@ -57,4 +57,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"http-errors": "^1.5.0", | ||
"lodash": "^4.13.1", | ||
"redink-errors": "0.0.2", | ||
"rethinkdb": "^2.3.2" | ||
@@ -84,3 +84,3 @@ }, | ||
"src/**/*.{js}", | ||
"!build/**/*" | ||
"!dist/**/*" | ||
], | ||
@@ -87,0 +87,0 @@ "require": [ |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
63262
1709
2
+ Addedredink-errors@0.0.2
+ Addedredink-errors@0.0.2(transitive)
- Removedhttp-errors@^1.5.0