Skip to content

Commit 8dd76dd

Browse files
authored
Merge pull request #31 from datapartyjs/nodejs-bundle
zangodb support
2 parents c964660 + 0ae3ccb commit 8dd76dd

11 files changed

Lines changed: 425 additions & 11 deletions

File tree

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@dataparty/api",
33
"private": false,
4-
"version": "1.2.11",
4+
"version": "1.2.12",
55
"main": "dist/dataparty.js",
66
"frontend": "dist/dataparty-browser.js",
77
"backend": "dist/dataparty.js",
@@ -95,14 +95,16 @@
9595
"uuid": "^3.2.1",
9696
"uuidv4": "^6.2.12",
9797
"vm2": "^3.9.2",
98-
"websocket": "github:sevenbitbyte/WebSocket-Node#parcel-build"
98+
"websocket": "github:sevenbitbyte/WebSocket-Node#parcel-build",
99+
"zangodb": "github:sevenbitbyte/zangodb#hash-patch"
99100
},
100101
"devDependencies": {
101102
"@dataparty/bouncer-model": "1.4.3",
102103
"@hapi/code": "^9.0.1",
103104
"@hapi/lab": "^25.0.1",
104105
"better-docs": "^1.1.6",
105106
"docdash": "^1.1.1",
107+
"fake-indexeddb": "^4.0.0",
106108
"jsdoc": "^3.6.2",
107109
"minami": "^1.2.3",
108110
"mongodb-client-encryption": "^2.2.1",

src/bouncer/crufler-admin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ module.exports = class AdminCrufler extends ICrufler {
9595

9696
let resultSet = await this.db.find(crufl.type, mongoQuery)
9797

98-
debug('set',resultSet)
98+
debug('resultSet',resultSet)
9999

100100
let msgs = []
101101

@@ -118,7 +118,7 @@ module.exports = class AdminCrufler extends ICrufler {
118118
}
119119
}
120120

121-
debug(msgs)
121+
debug('applyFind found msgs', msgs)
122122
return msgs
123123
}
124124

src/bouncer/db/loki-db.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const ObjectId = require('bson-objectid')
99

1010
const MongoQuery = require('../mongo-query')
1111
const { promisfy } = require('promisfy')
12-
const debug = require('debug')('dataparty.local.loki-db')
12+
const debug = require('debug')('bouncer.db.loki-db')
1313

1414

1515
module.exports = class LokiDb extends IDb {

src/bouncer/db/zango-db.js

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
'use strict'
2+
3+
const IDb = require('../idb')
4+
const Hoek = require('@hapi/hoek')
5+
const zango = require('zangodb')
6+
const reach = require('../../utils/reach')
7+
const ObjectId = require('bson-objectid')
8+
9+
const MongoQuery = require('../mongo-query')
10+
const { promisfy } = require('promisfy')
11+
const debug = require('debug')('bouncer.db.zango-db')
12+
13+
14+
module.exports = class ZangoDb extends IDb {
15+
16+
constructor ({dbname, factory}) {
17+
super(factory)
18+
debug('constructor')
19+
this.zango = null
20+
this.dbname = dbname
21+
this.error = null
22+
}
23+
24+
25+
async start(){
26+
27+
debug('starting')
28+
29+
30+
let collectionSettings = {}
31+
32+
for(const name of this.factory.getValidators()){
33+
debug('creating collection', name)
34+
35+
const indexSettings = reach(this.factory, 'model.IndexSettings.'+name)
36+
37+
const indices = ['$meta.id'].concat(indexSettings.unique).concat(indexSettings.indices)
38+
39+
collectionSettings[this.prefix+name] = indices.length > 0 ? indices : true
40+
}
41+
42+
debug('dbname',this.dbname, collectionSettings)
43+
44+
this.zango = new zango.Db(this.dbname, collectionSettings)
45+
46+
}
47+
48+
async getCollectionNames(){
49+
50+
const names = this.factory.getValidators()
51+
52+
return names.map(name=>{return name.replace(this.prefix,'')})
53+
}
54+
55+
56+
async getCollection(name){
57+
let collection = this.zango.collection(this.prefix+name)
58+
59+
return collection
60+
}
61+
62+
/** convert db documnet to plain object with $meta field */
63+
documentToObject(doc){
64+
let obj = Object.assign({},doc)
65+
obj.$meta = {
66+
id: Hoek.reach(obj,'meta.id', {default: obj._id}),
67+
type: Hoek.reach(doc,'meta.type'),
68+
created: Hoek.reach(obj,'meta.created', {default: (new Date()).toISOString()}),
69+
revision: Hoek.reach(obj,'meta.revision', {default: 1}),
70+
removed: Hoek.reach(obj,'meta.removed')
71+
}
72+
73+
delete obj.meta
74+
delete obj._id
75+
76+
return obj
77+
}
78+
79+
80+
/** convert object with $meta field to db representation*/
81+
documentFromObject(obj){
82+
let doc = Object.assign({},obj)
83+
doc._id = Hoek.reach(obj,'$meta.id', {default: obj._id}),
84+
doc.meta = {
85+
id: Hoek.reach(obj,'$meta.id', {default: obj._id}),
86+
type: Hoek.reach(obj,'$meta.type'),
87+
created: Hoek.reach(obj,'$meta.created', {default: (new Date()).toISOString()}),
88+
revision: Hoek.reach(obj,'$meta.revision', {default: 1}),
89+
removed: Hoek.reach(obj,'$meta.removed')
90+
}
91+
92+
93+
delete doc.$meta
94+
95+
return doc
96+
}
97+
98+
ensureId(obj){
99+
let temp = {...obj}
100+
if(!reach(temp,'$meta.id')){
101+
temp.$meta.id = (new ObjectId()).toHexString()
102+
}
103+
104+
let dbDoc = this.documentFromObject(temp)
105+
106+
return dbDoc
107+
}
108+
109+
async find(collectionName, mongoQuery){
110+
111+
let query = mongoQuery.getQueryDoc()
112+
113+
debug('find collection=', collectionName, ' query=', JSON.stringify(query,null,2))
114+
let collection = await this.getCollection(collectionName)
115+
let resultSet = collection.find(query)
116+
117+
118+
if(mongoQuery.hasLimit()){
119+
resultSet = resultSet.limit(mongoQuery.getLimit())
120+
}
121+
122+
if(mongoQuery.hasSort()){
123+
resultSet = resultSet.sort( mongoQuery.getSort() )
124+
}
125+
126+
return (await resultSet.toArray()).map(this.documentToObject) || []
127+
}
128+
129+
async insertMany(collectionName, docs){
130+
debug('insert collection=', collectionName, ' docs=', JSON.stringify(docs,null,2))
131+
let collection = await this.getCollection(collectionName)
132+
133+
let resultSet = []
134+
135+
for(let obj of docs){
136+
let temp = {...obj}
137+
if(temp._id===undefined){ temp._id = (new ObjectId()).toString(); temp.$meta.id=temp._id; }
138+
139+
let dbDoc = this.documentFromObject(temp)
140+
141+
const stripped = this.stripMeta(temp)
142+
143+
debug('validating', stripped,'from', temp)
144+
145+
await this.factory.validate(collectionName, stripped)
146+
147+
debug('its good, inserting', dbDoc)
148+
149+
await collection.insert( dbDoc )
150+
151+
debug('inserted', dbDoc)
152+
153+
const finalObj = this.documentToObject(dbDoc)
154+
155+
debug('returning', finalObj)
156+
157+
this.emitChange(finalObj, 'create')
158+
159+
resultSet.push(finalObj)
160+
}
161+
162+
163+
return resultSet
164+
165+
}
166+
167+
async update(collectionName, docs){
168+
debug('update collection', collectionName, ' docs', docs)
169+
170+
let collection = await this.getCollection(collectionName)
171+
172+
let objs = []
173+
174+
for(let obj of docs){
175+
let dbDoc = this.documentFromObject(obj)
176+
177+
debug('updating',obj, 'to', dbDoc)
178+
179+
const stripped = this.stripMeta(dbDoc)
180+
const meta = this.onlyMeta(obj)
181+
182+
debug('validating', stripped,'from', dbDoc)
183+
184+
await this.factory.validate(collectionName, stripped)
185+
186+
debug('its good, updating', dbDoc)
187+
188+
let old = await collection.findOne( {'_id': dbDoc._id})
189+
190+
debug('found old', old)
191+
dbDoc.meta.revision = old.meta.revision++
192+
193+
194+
let mergedDoc = {...old, ...dbDoc}
195+
196+
await collection.update({'_id': dbDoc._id}, mergedDoc)
197+
198+
const finalObj = this.documentToObject(mergedDoc)
199+
200+
this.emitChange(finalObj, 'update')
201+
202+
objs.push( finalObj )
203+
204+
}
205+
206+
return objs
207+
}
208+
209+
async findAndRemove(collectionName, obj){
210+
debug('findAndRemove collection', collectionName, ' obj', obj)
211+
212+
let collection = await this.getCollection(collectionName)
213+
214+
const dbDoc = await collection.findOne( {'_id': obj.$meta.id})
215+
216+
debug('found old doc', dbDoc)
217+
218+
await collection.remove( { '_id': obj.$meta.id } )
219+
220+
debug('dbDoc', dbDoc)
221+
222+
let finalObj = this.documentToObject(dbDoc)
223+
224+
finalObj.$meta.removed = true
225+
226+
this.emitChange(finalObj, 'remove')
227+
228+
debug('obj', finalObj)
229+
230+
return finalObj
231+
}
232+
}

src/bouncer/idb.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ module.exports = class IDb extends EventEmitter {
4646
ensureId(obj){ throw new Error('not implemented') }
4747

4848
stripMeta(doc){
49-
const {meta, $meta, ...rawMsg} = doc
49+
const {_id, meta, $meta, ...rawMsg} = doc
5050
return rawMsg
5151
}
5252

src/bouncer/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ module.exports = {
66
MongoQuery: require('./mongo-query'),
77

88
LokiDb: require('./db/loki-db'),
9-
TingoDb: require('./db/tingo-db')
9+
TingoDb: require('./db/tingo-db'),
10+
ZangoDb: require('./db/zango-db')
1011
}

src/party/idocument.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class IDocument extends EventEmitter {
144144
const rawDocument = (await this.party.update(value))[0]
145145
debug('doc updated')
146146
if(expectedRevision != reach(rawDocument, '$meta.revision')){
147-
console.log('pull')
147+
debug('pull')
148148
await this.pull()
149149
}
150150
}

src/party/index-browser.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const IParty = require('./iparty')
22
const PeerParty = require('./peer/peer-party')
33
const CloudParty = require('./cloud/cloud-party')
44
const LokiParty = require('./local/loki-party')
5+
const ZangoParty = require('./local/zango-party')
56

67
const IDocument = require('./idocument')
78
const DocumentFactory = require('./document-factory')
@@ -12,6 +13,6 @@ const LokiDb = require('../bouncer/db/loki-db')
1213
module.exports = {
1314
IDocument, IParty, DocumentFactory,
1415
CloudDocument,
15-
CloudParty, LokiParty, PeerParty,
16+
CloudParty, LokiParty, ZangoParty, PeerParty,
1617
LokiDb
17-
}
18+
}

src/party/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ const PeerParty = require('./peer/peer-party')
33
const CloudParty = require('./cloud/cloud-party')
44
const LokiParty = require('./local/loki-party')
55
const TingoParty = require('./local/tingo-party')
6+
const ZangoParty = require('./local/zango-party')
67
const MongoParty = require('./mongo/mongo-party')
78

9+
810
const IDocument = require('./idocument')
911
const DocumentFactory = require('./document-factory')
1012
const CloudDocument = require('./cloud/cloud-document')
@@ -13,5 +15,5 @@ module.exports = {
1315
IDocument, IParty, DocumentFactory,
1416
CloudDocument,
1517
CloudParty, LokiParty, PeerParty, MongoParty,
16-
TingoParty
18+
TingoParty, ZangoParty
1719
}

0 commit comments

Comments
 (0)