Commit 2f6f4d29 authored by Victor Andritoiu's avatar Victor Andritoiu
Browse files

sync

parent d2b92d88
......@@ -156,7 +156,7 @@ datum options _appendOnly_ is not set to true.
fieldOne: '...',
// ..
}, userId).then(result => {
console.log(result.ops[0]._id)
console.log(result._id)
}).catch(err => {})
```
......
......@@ -55,7 +55,7 @@ module.exports = {
},
/* mongodb */
mongo: {
uri: process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017',
uri: process.env.MONGODB_URI || 'mongodb://127.0.0.1:40000',
dbName: process.env.MONGODB_DBNAME || 'ignitialio',
options: process.env.MONGODB_OPTIONS,
maxAttempts: process.env.MONGODB_CONN_MAX_ATTEMPTS || 30,
......
......@@ -27,7 +27,7 @@ services:
image: registry.ignitial.io/ignitial/dlake
container_name: dlake
volumes:
- /home/vandrito/Documents/Projects/io.ignitial/iio/uservices/jsiios/dlake-service:/opt/dlake
- /home/vandrito/Documents/Projects/io.ignitial/core/uservices/jsiios/dlake-service:/opt/dlake
depends_on:
- redis
ports:
......
......@@ -169,7 +169,9 @@ class User extends Item {
user.password = hash
this.put(user, userId).then(result => {
resolve({ doc: result._updated.ops[0] })
this.get({ _id: result._id }).then(doc => {
resolve({ doc: doc })
}).catch(err => reject(err))
}).catch(err => reject(err))
} catch (err) {
reject(err)
......
......@@ -177,7 +177,7 @@ class ItemCouch extends IItem {
},
fields: fields
}).then(result => {
resolve({ docs: result.docs })
resolve(result.docs)
}).catch(err => {
pino.error(err, 'find method failed')
reject(err)
......@@ -214,7 +214,7 @@ class ItemCouch extends IItem {
sort.push(args.sort[s] === 1 ? 'asc' : 'desc')
}
let docs = _.orderBy(result.docs, fields, sort)
resolve({ docs: docs })
resolve(docs)
} else {
resolve(result)
}
......
const ObjectID = require('mongodb').ObjectID
const lzString = require('lz-string')
const dayjs = require('dayjs')
const IItem = require('./iitem').IItem
const utils = require('../utils')
......@@ -135,7 +136,7 @@ class ItemMongo extends IItem {
}
this._rawCollection.find(query, options).toArray().then(docs => {
resolve({ docs: docs })
resolve(docs)
}).catch(err => {
pino.error(err, 'find method failed')
reject(err)
......@@ -235,7 +236,7 @@ class ItemMongo extends IItem {
let query = args.query || args || {}
let options = args.query ? args.options : undefined
let page = args.page || 1
let page = args.page || 0
let pageSize = args.pageSize || 50
// build ObjectID object from string
......@@ -373,7 +374,7 @@ class ItemMongo extends IItem {
this._updateOne({
_id: args._id
}, { $set: args }, { upsert: true, w: 1 }).then(result => {
resolve({ _updated: result })
resolve({ _id: args._id })
// log activity: don't care
this.$datum.activities._log(userId || 0, this._name + ':update')
......@@ -409,7 +410,7 @@ class ItemMongo extends IItem {
}
this._rawCollection.insertOne(args, { w: 1 }).then(result => {
resolve({ _updated: { ops: result.ops } })
resolve({ _id: result.insertedId })
// log activity: don't care
this.$datum.activities._log(userId || 0, this._name + ':create')
......@@ -471,18 +472,19 @@ class ItemMongo extends IItem {
if (!this._appendOnly) {
this._updateOne(query, update, { upsert: true, w: 1 })
.then(result => {
resolve({ _updated: result })
if (result.modifiedCount === 1) {
// log activity: don't care
this.$datum.activities._log(userId || 0, this._name + ':update')
this._rawCollection.findOne(query, { _id: 1 }).then(doc => {
resolve({ _id: doc._id })
this.emit('update', doc._id)
this.$app._emitPushEvent('data:' + this._name + ':update', doc._id)
}).catch(err => {
pino.error(err, 'missed raising event')
})
} else {
reject(new Error('failed to update'))
}
}).catch(err => {
pino.error(err, 'putsert method failed', args)
......@@ -526,8 +528,8 @@ class ItemMongo extends IItem {
}
this._rawCollection.findOneAndDelete({ _id: args._id }).then(result => {
if (result.value) {
resolve({ _updated: result })
if (result) {
resolve({ _id: args._id })
// log activity: don't care
this.$datum.activities._log(userId || 0, this._name + ':delete')
......@@ -543,8 +545,8 @@ class ItemMongo extends IItem {
})
} else {
this._rawCollection.findOneAndDelete(args).then(result => {
if (result.value) {
resolve({ _updated: result })
if (result) {
resolve({ _id: result._id })
// log activity: don't care
this.$datum.activities._log(userId || 0, this._name + ':delete')
......@@ -577,6 +579,50 @@ class ItemMongo extends IItem {
})
}
/* offline sync method */
sync(args, userId) {
return new Promise(async (resolve, reject) => {
if (args.updated && args.updated.length > 0) {
let updated = _.map(args.updated, e => {
e._id = new ObjectID(e._id)
return e
})
try {
for (let e of updated) {
let doc = await this.get({ _id: e._id }, userId)
if (dayjs(doc._lastModified).diff(dayjs(e._lastModified)) < 0) {
e._lastModified = new Date(e._lastModified)
delete e._notSynched
await this.put(e, userId)
}
}
} catch(err) {
reject(err)
return
}
}
if (args.deleted && args.deleted.length > 0) {
let deleted = _.map(args.deleted, e => {
e = new ObjectID(e)
return e
})
try {
for (let e of deleted) {
await this.del({ _id: e }, userId)
}
} catch(err) {
reject(err)
}
}
resolve()
})
}
_createRESTEndpoints() {
let protector = async (req, pathname, version) => {
let creds = Buffer.from(req.headers.authorization.replace('Basic ', ''), 'base64').toString('utf8')
......
{
"name": "@ignitial/dlake-service",
"version": "1.3.3",
"version": "1.3.6",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
......@@ -3261,12 +3261,14 @@
"balanced-match": {
"version": "1.0.0",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"brace-expansion": {
"version": "1.1.11",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
......@@ -3281,17 +3283,20 @@
"code-point-at": {
"version": "1.1.0",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"concat-map": {
"version": "0.0.1",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"console-control-strings": {
"version": "1.1.0",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"core-util-is": {
"version": "1.0.2",
......@@ -3408,7 +3413,8 @@
"inherits": {
"version": "2.0.3",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"ini": {
"version": "1.3.5",
......@@ -3420,6 +3426,7 @@
"version": "1.0.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"number-is-nan": "^1.0.0"
}
......@@ -3434,6 +3441,7 @@
"version": "3.0.4",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"brace-expansion": "^1.1.7"
}
......@@ -3441,12 +3449,14 @@
"minimist": {
"version": "0.0.8",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"minipass": {
"version": "2.2.4",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"safe-buffer": "^5.1.1",
"yallist": "^3.0.0"
......@@ -3465,6 +3475,7 @@
"version": "0.5.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"minimist": "0.0.8"
}
......@@ -3545,7 +3556,8 @@
"number-is-nan": {
"version": "1.0.1",
"bundled": true,
"dev": true
"dev": true,
"optional": true
},
"object-assign": {
"version": "4.1.1",
......@@ -3557,6 +3569,7 @@
"version": "1.4.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"wrappy": "1"
}
......@@ -3678,6 +3691,7 @@
"version": "1.0.2",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0",
......
{
"name": "@ignitial/dlake-service",
"version": "1.3.6",
"version": "2.0.0",
"description": "Ignitial.io Data Lake service",
"main": "index.js",
"author": "Victor Andritoiu <devandrito@gmail.com>",
......
#!/bin/sh
docker-compose -f docker-compose.yml up -d mongo
sleep 1
node tools/populate_db-mongo.js
docker-compose stop
docker-compose rm -f
......@@ -14,9 +14,9 @@ function run() {
gateway._waitForService('dlake', 10000).then(async dlake => {
let cfg = await dlake.dbCfg()
console.log(chalk.green('wait for service: service up ✔'))
dlake.users.find({}, '5b0d843b2a7f1d105348dc96').then(response => {
(response.docs.length >= 2).should.be.true()
should.not.exist(response.docs[0].password)
dlake.users.find({}, '5b0d843b2a7f1d105348dc96').then(docs => {
(docs.length >= 2).should.be.true()
should.not.exist(docs[0].password)
console.log(chalk.green('wait for service: find by unkown user ✔'))
}).catch(err => {
console.log(chalk.red('wait for service: find by unkown user ✘'), '' + err)
......@@ -33,16 +33,16 @@ function run() {
console.log(chalk.green('users service:registered ✔'))
// test with anonymous
users.find({}).then(response => {
should(response.docs.length).be.equal(2)
should.not.exist(response.docs[0].contactInfo)
should.not.exist(response.docs[0].password)
should.exist(response.docs[0].lastname)
users.find({}).then(docs => {
should(docs.length).be.equal(2)
should.not.exist(docs[0].contactInfo)
should.not.exist(docs[0].password)
should.exist(docs[0].lastname)
console.log(chalk.green('find by anonymous user ✔'))
let admin, stdUser
for (let user of response.docs) {
for (let user of docs) {
if (user.role === 'admin') admin = user
if (user.role === 'user') stdUser = user
}
......@@ -88,7 +88,7 @@ function run() {
console.log(chalk.red('find and sort by admin user ✘'), '' + err))
users.findPaginated({}, admin._id).then(result => {
should(response.docs.length).be.equal(2)
should(result.docs.length).be.equal(2)
console.log(chalk.green('find paginated by admin user ✔'))
}).catch(err =>
console.log(chalk.red('find paginated by admin user ✘'), '' + err))
......@@ -96,24 +96,24 @@ function run() {
let roles = gateway.services.dlake.roles
console.log(chalk.green('roles service:registered ✔'))
roles.load().then(response => {
roles.load().then(() => {
console.log(chalk.green('roles load ✔'))
roles.save(rolesData, admin._id).then(response => {
roles.save(rolesData, admin._id).then(() => {
console.log(chalk.green('roles save by admin ✔'))
}).catch(err => {
console.log(chalk.red('roles save by admin ✘'), '' + err)
})
users.find({}, admin._id).then(result => {
should(result.docs.length).be.equal(2)
should.exist(result.docs[0].password)
users.find({}, admin._id).then(docs => {
should(docs.length).be.equal(2)
should.exist(docs[0].password)
console.log(chalk.green('find by admin user ✔'))
}).catch(err =>
console.log(chalk.red('find by admin user ✘'), '' + err))
users.find({}, stdUser._id).then(result => {
should(result.docs.length).be.equal(2)
should.exist(result.docs[0].password)
users.find({}, stdUser._id).then(docs => {
should(docs.length).be.equal(2)
should.exist(docs[0].password)
console.log(chalk.green('find by standard user ✔'))
}).catch(err =>
console.log(chalk.red('find by standard user ✘'), '' + err))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment