Skip to content

Commit

Permalink
Merge branch 'release/v1.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
sebelga committed Sep 26, 2017
2 parents 7dbd473 + 95edcee commit 5cf162f
Show file tree
Hide file tree
Showing 23 changed files with 3,646 additions and 182 deletions.
8 changes: 7 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
"no-use-before-define": ["error", {"functions": false}],
"prefer-rest-params": "off",
"prefer-spread": "off",
"no-restricted-globals": "off",
"no-underscore-dangle": "off",
"no-param-reassign": "off",
"max-len": ["error", 120],
"mocha/no-exclusive-tests": "off"
"mocha/no-exclusive-tests": "off",
"comma-dangle": ["error", {
"arrays": "always-multiline",
"objects": "always-multiline",
"functions": "ignore"
}]
}
}
9 changes: 3 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
language: node_js
node_js:
- "8"
- "7"
- "6"
- "5"
- "4"
- "4.2"
- "4.1"
- "4.0"

branches:
only:
Expand All @@ -16,4 +13,4 @@ script:
- npm test

after_success:
- npm run coveralls
- npm run coveralls
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This library is in active development, please report any issue you might find.
npm install gstore-node --save
```

IMPORTANT: Since v1.4.0, gstore-node requires Node version **6 or superior**
INFO: With npm v3+ **you don't need** to install @google-cloud/datastore as a dependency of your project as it is already a dependency of gstore-node.

# Getting started
Expand Down
10 changes: 5 additions & 5 deletions lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ class Entity {
}

addVirtuals() {
const virtuals = this.schema.virtuals;
const entityData = this.entityData;
const { virtuals } = this.schema;
const { entityData } = this;

Object.keys(virtuals).forEach((k) => {
if ({}.hasOwnProperty.call(entityData, k)) {
Expand Down Expand Up @@ -159,7 +159,7 @@ function createKey(self, id, ancestors, namespace) {
}

function buildEntityData(self, data) {
const schema = self.schema;
const { schema } = self;
const entityData = {};

if (data) {
Expand Down Expand Up @@ -194,7 +194,7 @@ function buildEntityData(self, data) {
value = defaultValues.__handler__(value);
} else if (value === null && {}.hasOwnProperty.call(schemaProperty, 'values') && !isRequired) {
// Default to first value of the allowed values is **not** required
value = schemaProperty.values[0];
[value] = schemaProperty.values;
}

entityData[k] = value;
Expand Down Expand Up @@ -242,4 +242,4 @@ function registerHooksFromSchema(self) {
// Promisify Entity methods
Entity.prototype.datastoreEntity = utils.promisify(Entity.prototype.datastoreEntity);

module.exports = exports = Entity;
module.exports = Entity;
1 change: 0 additions & 1 deletion lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
'use strict';

class GstoreError extends Error {

constructor(msg) {
super();
this.constructor.captureStackTrace(this);
Expand Down
12 changes: 5 additions & 7 deletions lib/helpers/queryhelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ function buildFromOptions(query, options, ds) {
options.order = [options.order];
}
options.order.forEach((order) => {
query.order(order.property,
{
descending: {}.hasOwnProperty.call(order, 'descending') ?
order.descending : false,
});
query.order(order.property, {
descending: {}.hasOwnProperty.call(order, 'descending') ? order.descending : false,
});
});
}

Expand All @@ -38,8 +36,8 @@ function buildFromOptions(query, options, ds) {
}

const ancestorKey = options.namespace ?
ds.key({ namespace: options.namespace, path: options.ancestors.slice() }) :
ds.key(options.ancestors.slice());
ds.key({ namespace: options.namespace, path: options.ancestors.slice() }) :
ds.key(options.ancestors.slice());

query.hasAncestor(ancestorKey);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,4 @@ class Gstore {
}
}

module.exports = exports = new Gstore();
module.exports = new Gstore();
21 changes: 11 additions & 10 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ class Model extends Entity {
applyMethods(ModelInstance.prototype, schema);
applyStatics(ModelInstance, schema);

ModelInstance.prototype.entityKind = ModelInstance.entityKind = kind;
ModelInstance.prototype.gstore = ModelInstance.gstore = gstore;
ModelInstance.prototype.entityKind = kind;
ModelInstance.entityKind = kind;

ModelInstance.prototype.gstore = gstore;
ModelInstance.gstore = gstore;

/**
* Create virtual properties (getters and setters for entityData object)
Expand Down Expand Up @@ -201,7 +204,7 @@ class Model extends Entity {
}

return getAndUpdate()
.catch(onTransactionError);
.catch(onTransactionError);

// ---------------------------------------------------------

Expand Down Expand Up @@ -467,7 +470,7 @@ class Model extends Entity {
// ------------------------------------------------

function onEntities(data) {
entities = data[0];
[entities] = data;

// We calculate the total batches we will need to process
// The Datastore does not allow more than 500 keys at once when deleting.
Expand Down Expand Up @@ -511,7 +514,7 @@ class Model extends Entity {
}

function chainPromise(promise, entity) {
return promise.then(_this.delete.call(_this, null, null, null, null, entity[_this.gstore.ds.KEY]));
return promise.then(() => _this.delete.call(_this, null, null, null, null, entity[_this.gstore.ds.KEY]));
}
}

Expand Down Expand Up @@ -579,9 +582,7 @@ class Model extends Entity {
});
}

property = args[0];
value = args[1];
options = args[2];
[property, value, options] = args;
namespace = args.length > 3 ? args[3] : undefined;

if (!is.object(options)) {
Expand Down Expand Up @@ -940,7 +941,7 @@ class Model extends Entity {
validate() {
const errors = {};
const self = this;
const schema = this.schema;
const { schema } = this;

let skip;
let schemaHasProperty;
Expand Down Expand Up @@ -1199,4 +1200,4 @@ Model.findAround = utils.promisify(Model.findAround);
Model.findOne = utils.promisify(Model.findOne);
Model.prototype.save = utils.promisify(Model.prototype.save);

module.exports = exports = Model;
module.exports = Model;
2 changes: 1 addition & 1 deletion lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,4 @@ function defaultOptions(options) {
return options;
}

module.exports = exports = Schema;
module.exports = Schema;
14 changes: 10 additions & 4 deletions lib/serializers/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function fromDatastore(entity, options) {
options = options || {};

switch (options.format) {
case 'ENTITY' :
case 'ENTITY':
return convertToEntity.call(this);
default:
return convertToJson.call(this);
Expand All @@ -35,8 +35,8 @@ function fromDatastore(entity, options) {
function convertToJson() {
options.readAll = typeof options.readAll === 'undefined' ? false : options.readAll;

const schema = this.schema;
const KEY = this.gstore.ds.KEY;
const { schema } = this;
const { KEY } = this.gstore.ds;
const entityKey = entity[KEY];
const data = {
id: idFromKey(entityKey),
Expand All @@ -45,7 +45,13 @@ function fromDatastore(entity, options) {

Object.keys(entity).forEach((k) => {
if (options.readAll || !{}.hasOwnProperty.call(schema.paths, k) || schema.paths[k].read !== false) {
data[k] = entity[k];
let value = entity[k];
if ({}.hasOwnProperty.call(this.schema.paths, k)
&& this.schema.paths[k].type === 'datetime' && is.number(value)) {
// During queries @google-cloud converts datetime to number
value = new Date(value / 1000);
}
data[k] = value;
}
});

Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gstore-node",
"version": "1.3.1",
"version": "1.4.0",
"description": "gstore-node is a Google Datastore Entity Models tools",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -45,28 +45,28 @@
],
"license": "ISC",
"dependencies": {
"@google-cloud/datastore": "1.0.0",
"@google-cloud/datastore": "1.1.0",
"arrify": "^1.0.1",
"extend": "^3.0.0",
"is": "^3.2.1",
"moment": "^2.18.1",
"promised-hooks": "^1.1.0",
"validator": "^7.0.0"
"validator": "^8.2.0"
},
"devDependencies": {
"babel-cli": "^6.9.0",
"babel-preset-es2015": "^6.9.0",
"chai": "^3.5.0",
"coveralls": "^2.11.9",
"eslint": "^3.18.0",
"eslint-config-airbnb-base": "^10.0.1",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-mocha": "^4.7.0",
"eslint": "^4.7.2",
"eslint-config-airbnb-base": "^12.0.0",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-mocha": "^4.11.0",
"istanbul": "^0.4.3",
"mocha": "^3.2.0",
"mocha": "^3.5.3",
"mocha-lcov-reporter": "^1.2.0",
"nconf": "^0.8.4",
"sinon": "^1.17.4",
"sinon-as-promised": "^4.0.2"
"sinon": "^4.0.0",
"sinon-as-promised": "^4.0.3"
}
}
17 changes: 6 additions & 11 deletions test/entity-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ const chai = require('chai');
const sinon = require('sinon');
const ds = require('./mocks/datastore')();
const datastoreSerializer = require('../lib/serializer').Datastore;
const Schema = require('../lib').Schema;
const { Schema } = require('../lib');
const gstore = require('../lib');

require('sinon-as-promised');

const expect = chai.expect;
const assert = chai.assert;
const { expect, assert } = chai;
gstore.connect(ds);

describe('Entity', () => {
Expand All @@ -34,8 +31,7 @@ describe('Entity', () => {

schema.virtual('fullname').set(function setFullName(name) {
const split = name.split(' ');
this.name = split[0];
this.lastname = split[1];
[this.name, this.lastname] = split;
});

ModelInstance = gstore.model('User', schema);
Expand Down Expand Up @@ -392,7 +388,7 @@ describe('Entity', () => {

it('should call datastoreSerializer "fromDatastore"', () => {
const model = new ModelInstance({ name: 'John', password: 'test' });
const entityData = model.entityData;
const { entityData } = model;

const output = model.plain();

Expand Down Expand Up @@ -513,7 +509,7 @@ describe('Entity', () => {
const ImageModel = gstore.model('Image', imageSchema);
const mockEntities = [{ key: ds.key(['BlogPost', 1234]) }];

sinon.stub(ImageModel, 'get', (cb) => {
sinon.stub(ImageModel, 'get').callsFake((cb) => {
cb(null, mockEntities[0]);
});

Expand All @@ -538,8 +534,7 @@ describe('Entity', () => {

schema.virtual('fullname').set(function setFullName(name) {
const split = name.split(' ');
this.firstname = split[0];
this.lastname = split[1];
[this.firstname, this.lastname] = split;
});

User = gstore.model('Client', schema);
Expand Down
8 changes: 3 additions & 5 deletions test/error-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@

const chai = require('chai');
const gstore = require('../');
const GstoreError = require('../lib/error').GstoreError;
const { GstoreError } = require('../lib/error');
const Model = require('../lib/model');
const Schema = require('../lib/schema');

const ValidationError = GstoreError.ValidationError;
const ValidatorError = GstoreError.ValidatorError;
const expect = chai.expect;
const assert = chai.assert;
const { ValidationError, ValidatorError } = GstoreError;
const { expect, assert } = chai;

describe('Datastools Errors', () => {
it('should extend Error', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/defaultValues.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const chai = require('chai');
const defaultValues = require('../../lib/helpers/defaultValues');

const expect = chai.expect;
const { expect } = chai;

describe('Query Helpers', () => {
describe('defaultValues constants handler()', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/queryhelpers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const sinon = require('sinon');
const ds = require('@google-cloud/datastore')();
const queryHelpers = require('../../lib/helper').QueryHelpers;

const expect = chai.expect;
const { expect } = chai;

describe('Query Helpers', () => {
let query;
Expand Down
Loading

0 comments on commit 5cf162f

Please sign in to comment.