index.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. const { MongoClient, ObjectID } = require("mongodb");
  2. const {asynchronize, openPromise } = require('./asynchronize')
  3. const mm = db => {
  4. class Savable {
  5. constructor(obj, ref, empty=false){
  6. this._id = null
  7. this._ref = ref
  8. this._class = this.__proto__.constructor.name
  9. this._empty = true
  10. Savable.addClass(this.__proto__.constructor)
  11. if (obj){
  12. this.populate(obj)
  13. this._empty = empty
  14. }
  15. }
  16. backupRelations(){
  17. this._loadRelations = {};
  18. for (const relation in this.__proto__.constructor.relations){
  19. this._loadRelations[relation] = this[relation] instanceof Array ? [...this[relation]] : this[relation]
  20. }
  21. }
  22. populate(obj){
  23. const convertSavables = (obj) => {
  24. for (const key in obj){
  25. if (Savable.isSavable(obj[key])){
  26. obj[key] = (this._ref &&
  27. obj[key]._id.toString() == this._ref._id.toString()) ?
  28. this._ref :
  29. Savable.newSavable(obj[key], this)
  30. }
  31. else if (typeof obj[key] === 'object'){
  32. convertSavables(obj[key])
  33. }
  34. }
  35. }
  36. Object.assign(this, obj)
  37. convertSavables(this)
  38. this.backupRelations()
  39. //this._id = obj._id
  40. }
  41. get _empty(){
  42. return !!this.then
  43. }
  44. set _empty(value){
  45. if (value){
  46. //TODO: list of callbacks, because then can be called many times, and
  47. //it's not reason to repeat query to db
  48. this.then = (cb, err) => {
  49. if (!this._id) err(new ReferenceError('Id is empty'))
  50. if (!this._class) err(new ReferenceError('Class is empty'))
  51. const promise = openPromise()
  52. this.collection.findOne(this._id).then( data => {
  53. if (!data){
  54. err(new ReferenceError('Document Not Found'))
  55. }
  56. else {
  57. delete this.then
  58. this.populate(data)
  59. if (typeof cb === 'function')
  60. promise.resolve(cb(this))
  61. else {
  62. promise.resolve(this)
  63. }
  64. }
  65. })
  66. return promise
  67. }
  68. }
  69. else {
  70. delete this.then
  71. }
  72. }
  73. get createdAt(){
  74. return this._id ? new Date(this._id.getTimestamp()) : null
  75. }
  76. get collection(){
  77. return db.collection(this._class)
  78. }
  79. async save(noSync=false){
  80. if (this.empty) return this;
  81. const syncRelations = async () => {
  82. if (noSync) return
  83. if (!(this && this.__proto__ && this.__proto__.constructor && this.__proto__.constructor.relations)) return
  84. async function getValueByField(field, savable) {
  85. let path = field.split('.');
  86. await savable//.catch(e => console.log('GET VALUE BY FIELD ERROR'));
  87. let result = savable;
  88. let prev;
  89. let lastKey = path.pop()
  90. while (prev = result, result = result[path.shift()] && path.length);
  91. return {value: prev[lastKey], obj: prev, lastKey};
  92. }
  93. for (const relation in this.__proto__.constructor.relations){
  94. let backRef = this.__proto__.constructor.relations[relation]
  95. if (Array.isArray(backRef)) backRef = backRef[0]
  96. const loadRelation = this._loadRelations[relation]
  97. const loadRelationAsArray = loadRelation instanceof Savable ? [loadRelation] : loadRelation
  98. let {value, obj, lastKey: key} = await getValueByField(relation, this)
  99. const valueAsArray = value instanceof Savable ? [value] : value
  100. if (loadRelationAsArray){ //check for removed Refs
  101. const removedRefs = valueAsArray ?
  102. loadRelationAsArray.filter(ref => !Savable.existsInArray(valueAsArray, ref)) :
  103. loadRelationAsArray
  104. for (const ref of removedRefs){
  105. try { await ref } catch (e) {console.log('SYNC RELATIONS REMOVE ERROR') }
  106. await ref.removeRelation(this, relation)
  107. }
  108. }
  109. if (valueAsArray){ //check for added refs
  110. for (const foreignSavable of valueAsArray) {
  111. try { await foreignSavable } catch (e) {console.log('SYNC RELATIONS ADD ERROR') }
  112. let foreignLoadRelationsAsArray = Savable.arrize(foreignSavable._loadRelations[backRef])
  113. if (foreignSavable && !Savable.existsInArray(foreignLoadRelationsAsArray, this)){
  114. await foreignSavable.setRelation(this, relation)
  115. }
  116. }
  117. }
  118. }
  119. }
  120. async function recursiveSlicer(obj){
  121. let result = obj instanceof Array ? [] : {}
  122. for (const key in obj){
  123. if (obj[key] && typeof obj[key] === 'object'){
  124. if (obj[key] instanceof Savable){
  125. if (!(obj[key]._id)){
  126. await obj[key].save().catch(err => console.log('ERR', err))
  127. }
  128. result[key] = obj[key].shortData()
  129. }
  130. else {
  131. result[key] = await recursiveSlicer(obj[key])
  132. }
  133. }
  134. else {
  135. result[key] = obj[key]
  136. }
  137. }
  138. return result;
  139. }
  140. const {_id, _empty, _ref, _loadRelations, then, ...toSave} = await recursiveSlicer(this)
  141. //TODO: UPSERT
  142. if (!this._id){ //first time
  143. const { insertedId } = await this.collection.insertOne(toSave)
  144. this._id = insertedId
  145. }
  146. else { //update
  147. await this.collection.updateOne({_id: this._id}, {$set: toSave}).catch(err => console.log('UPDATE ERR', err))
  148. }
  149. await syncRelations()
  150. this.backupRelations()
  151. return this
  152. }
  153. // method to get ref snapshot (empty object with some data). gives optimizations in related objects
  154. // place for permission, owner, probably short relations
  155. shortData(){
  156. const {_id, _class} = this
  157. return { _id, _class }
  158. }
  159. async setRelation(ref, refRelationName){
  160. await this
  161. const ourRelation = ref.__proto__.constructor.relations[refRelationName]
  162. const ourArray = ourRelation instanceof Array
  163. const ourRelationName = ourArray ? ourRelation[0] : ourRelation
  164. let shortQuery = {[ourRelationName]: ref.shortData()}
  165. let query;
  166. if (ourArray || this[ourRelationName] instanceof Array){
  167. this[ourRelationName] = this[ourRelationName] || []
  168. this[ourRelationName] = this[ourRelationName] instanceof Array ? this[ourRelationName] : [this[ourRelationName]]
  169. if (Savable.existsInArray(this[ourRelationName], ref)) {
  170. this[ourRelationName].push(ref)
  171. this._id && this._loadRelations[ourRelationName].push(ref)
  172. }
  173. query = {$addToSet: shortQuery}
  174. }
  175. else {
  176. this[ourRelationName] = ref
  177. this._id && (this._loadRelations[ourRelationName] = ref)
  178. query = {$set: shortQuery}
  179. }
  180. console.log('SET RELATION:', query)
  181. if (this._id){
  182. console.log('SET RELATION:', query)
  183. await this.collection.updateOne({_id: this._id}, query).catch(err => console.log('UPDATE ERR', err))
  184. }
  185. }
  186. async removeRelation(ref, refRelationName){ //i. e. this = child, ref = parent object, refRelationName = children in parent
  187. await this
  188. const ourRelation = ref.__proto__.constructor.relations[refRelationName]
  189. const ourArray = ourRelation instanceof Array
  190. const ourRelationName = ourArray ? ourRelation[0] : ourRelation
  191. if (this._id){
  192. const query = ourArray ? {$pull: {[ourRelationName]: ref.shortData()}}
  193. : {$set: {[ourRelationName]: null}}
  194. console.log('REMOVE RELATION:', query)
  195. await this.collection.updateOne({_id: this._id}, query).catch(err => console.log('UPDATE ERR', err))
  196. }
  197. (this[ourRelationName] instanceof Array) ? this._loadRelations[ourRelationName] = this[ourRelationName] = this[ourRelationName].filter(ourRef => !ourRef.equals(ref))
  198. : this[ourRelationName] = null;
  199. }
  200. async delete(noRefs=false){
  201. if (!noRefs) for (const relation in this.__proto__.constructor.relations){
  202. const backRef = this.__proto__.constructor.relations[relation]
  203. const loadRelation = this._loadRelations && this._loadRelations[relation]
  204. const loadRelationAsArray = loadRelation instanceof Savable ? [loadRelation] : loadRelation
  205. if (loadRelationAsArray){
  206. for (const ref of loadRelationAsArray){
  207. try {
  208. await ref
  209. }
  210. catch (e) {console.log('DELETE SYNC RELATIONS ERROR') }
  211. await ref.removeRelation(this, relation)
  212. }
  213. }
  214. }
  215. const id = this._id
  216. const col = this._class && this.collection
  217. for (let key in this)
  218. delete this[key]
  219. delete this.__proto__
  220. if (col)
  221. return await col.deleteOne({_id: id})
  222. }
  223. static arrize(value){
  224. if (Array.isArray(value)) return value
  225. if (value) return [value]
  226. return []
  227. }
  228. static equals(obj1, obj2){
  229. if (!obj1 || !obj2) return false
  230. if(!obj1._id) return obj1 === obj2
  231. if(!obj2._id) return obj1 === obj2
  232. return obj1._id.toString() === obj2._id.toString()
  233. }
  234. equals(obj){
  235. return Savable.equals(this, obj)
  236. }
  237. static existsInArray(arr, obj){
  238. if (!Array.isArray(arr)) return false
  239. let filtered = arr.filter(item => Savable.equals(item, obj))
  240. return filtered.length
  241. }
  242. static isSavable(obj){
  243. return obj && obj._id && obj._class
  244. }
  245. static newSavable(obj, ref, empty=true){
  246. let className = obj._class || "Savable"
  247. className = Savable.classes[className] ? className : "Savable"
  248. if (obj.__proto__.constructor === Savable.classes[className]){
  249. return obj
  250. }
  251. return new Savable.classes[className](obj, ref, empty)
  252. }
  253. static addClass(_class){ //explicit method to add class to Savable registry for instantiate right class later
  254. (typeof _class == 'function') && (Savable.classes[_class.name] = _class)
  255. }
  256. static get m(){
  257. return new Proxy({}, {
  258. get(obj, _class){
  259. if (_class in obj){
  260. return obj[_class]
  261. }
  262. const applyCursorCalls = (cursor, calls) =>{
  263. for (let [method, params] of Object.entries(calls)){
  264. if (typeof cursor[method] !== "function"){
  265. throw new SyntaxError(`Wrong cursor method ${method}`)
  266. }
  267. cursor = cursor[method](...params)
  268. }
  269. return cursor;
  270. }
  271. return obj[_class] = {
  272. * find(query, cursorCalls={}){
  273. let cursor = applyCursorCalls(db.collection(_class).find(query), cursorCalls)
  274. let cursorGen = asynchronize({s: cursor.stream(),
  275. chunkEventName: 'data',
  276. endEventName: 'close',
  277. errEventName: 'error',
  278. countMethodName: 'count'})
  279. for (const pObj of cursorGen()){
  280. yield new Promise((ok, fail) =>
  281. pObj.then(obj => (/*console.log(obj),*/ok(Savable.newSavable(obj, null, false))),
  282. err => fail(err)))
  283. }
  284. },
  285. async count(query, cursorCalls={}){
  286. let cursor = applyCursorCalls(db.collection(_class).find(query), cursorCalls)
  287. return await cursor.count(true)
  288. },
  289. async findOne(query){
  290. let result = await db.collection(_class).findOne(query)
  291. if (result)
  292. return Savable.newSavable(result, null, false)
  293. return result
  294. }
  295. }
  296. },
  297. set(obj, propName, value){
  298. }
  299. })
  300. }
  301. static get relations(){
  302. //empty default relations, acceptable: {field: foreignField}, where:
  303. //field and foreign field can be Savable, Array or Set
  304. //both fields can be specified as "field", "field.subfield"
  305. //or field: {subfield: foreignField} //TODO later if needed
  306. //TODO: move it into object instead of class to give more flexibility, for example
  307. //if person has children, it can have backRef father or mother depending on sex:
  308. //return {
  309. // children: this.sex === 'male' ? 'father': 'mother'
  310. //}
  311. //
  312. //return {
  313. // parent: ["children"],
  314. // notebooks: "owner"
  315. //}
  316. return {}
  317. }
  318. }
  319. Savable.classes = {Savable}
  320. /**
  321. * sliceSavable - slice (limit) Savables for some permission
  322. * Array userACL - array of objectIDs, words or savable refs - current user, group objectid, or `tags` or `role` (ACL)
  323. */
  324. function sliceSavable(userACL){
  325. userACL = userACL.map(tag => tag.toString())
  326. //console.log(userACL)
  327. class SlicedSavable extends Savable {
  328. constructor(...params){
  329. super (...params)
  330. if (!this._empty){
  331. this.___permissionsPrepare()
  332. }
  333. }
  334. ___permissionsPrepare(){
  335. if (this._empty) return
  336. if (!this.___permissions) this.___permissions = {}
  337. for (let [perm, acl] of Object.entries(this.__proto__.constructor.defaultPermissions)){
  338. if (!this.___permissions[perm]){
  339. this.___permissions[perm] = [...acl]
  340. }
  341. }
  342. }
  343. ___permissionCan(permission, permissions=this.___permissions, obj=this){
  344. const acl = (permissions &&
  345. permissions[permission] ||
  346. this.__proto__.constructor.defaultPermissions[permission]).map(tag => tag.toString())
  347. if (acl.includes('owner') && obj.___owner && userACL.includes(obj.___owner.toString())){
  348. return true
  349. }
  350. for (let uTag of userACL){
  351. if (acl.includes(uTag)){
  352. return true
  353. }
  354. }
  355. return false
  356. }
  357. populate(obj){ //place to check read permission
  358. //console.log(obj)
  359. if (!this.___permissionCan('read', obj.___permissions, obj)){
  360. throw new ReferenceError(`No Access To Entity ${this._id} of class ${this._class}`)
  361. }
  362. super.populate(obj)
  363. }
  364. async save(noSync=false){
  365. if (!this._id && !this.___permissionCan('create'))
  366. throw new ReferenceError(`Permissison denied Create Entity of class ${this._class}`)
  367. if (this._id && !this.___permissionCan('write'))
  368. throw new ReferenceError(`Permissison denied Save Entity ${this._id} of class ${this._class}`)
  369. if (!this._id){
  370. this.___owner = userACL[0] //TODO fix objectid troubles
  371. //console.log(typeof this.___owner, this.___owner)
  372. }
  373. return await super.save(noSync)
  374. }
  375. async setRelation(ref, refRelationName){
  376. await this
  377. const ourRelation = ref.__proto__.constructor.relations[refRelationName]
  378. const ourArray = ourRelation instanceof Array
  379. const ourRelationName = ourArray ? ourRelation[0] : ourRelation
  380. if (!this._id || this.___permissionCan('write') ||
  381. (this.__proto__.constructor.guestRelations.includes(ourRelationName) && this.___permissionCan('read')))
  382. return await super.setRelation(ref, refRelationName)
  383. throw new ReferenceError(`Permissison denied Set Relation Entity ${this._id} of class ${this._class} ref: ${ref._id} of class ${ref._class}`)
  384. }
  385. async removeRelation(ref, refRelationName){
  386. await this;
  387. const ourRelation = ref.__proto__.constructor.relations[refRelationName]
  388. const ourArray = ourRelation instanceof Array
  389. const ourRelationName = ourArray ? ourRelation[0] : ourRelation
  390. if (!this._id || this.___permissionCan('write') ||
  391. (this.__proto__.constructor.guestRelations.includes(ourRelationName) && this.___permissionCan('read')))
  392. return await super.removeRelation(ref, refRelationName)
  393. throw new ReferenceError(`Permissison denied Remove Relation Entity ${this._id} of class ${this._class} ref: ${ref._id} of class ${ref._class}`)
  394. }
  395. async delete(noRefs=false){
  396. if (!this.___permissionCan('delete'))
  397. throw new ReferenceError(`Permissison denied Delete Entity ${this._id} of class ${this._class}`)
  398. return await super.delete(noRefs)
  399. }
  400. static ___permissionQuery(permission){
  401. //const withObjectIDs = userACL.map((a,id) => (id = new ObjectID(a)) && id.toString() === a ? id : a)
  402. const withObjectIDs = userACL
  403. return {
  404. $or: [
  405. {[`___permissions.${permission}`]: {$in: withObjectIDs}},
  406. {$and: [{[`___permissions.${permission}`]: "owner"},
  407. {___owner: userACL[0]}]}]
  408. }
  409. }
  410. static get m() {
  411. return new Proxy({}, {
  412. get(obj, _class){
  413. if (_class in obj){
  414. return obj[_class]
  415. }
  416. return obj[_class] = {
  417. * find(query, cursorCalls={}){
  418. const originalClass = Savable.classes[_class]
  419. Savable.addClass(SlicedSavable.classes[_class])
  420. let permittedQuery = {$and: [SlicedSavable.___permissionQuery('read') ,query]}
  421. //console.log(JSON.stringify(permittedQuery, null, 4))
  422. let iter = Savable.m[_class].find(permittedQuery, null, cursorCalls)
  423. Savable.addClass(originalClass)
  424. yield* iter;
  425. },
  426. async count(query, cursorCalls={}){
  427. let permittedQuery = {$and: [SlicedSavable.___permissionQuery('read') ,query]}
  428. return await Savable.m[_class].count(permittedQuery, cursorCalls)
  429. },
  430. async findOne(query){
  431. const originalClass = Savable.classes[_class]
  432. Savable.addClass(SlicedSavable.classes[_class])
  433. const permittedQuery = {$and: [SlicedSavable.___permissionQuery('read') ,query]}
  434. const p = Savable.m[_class].findOne(permittedQuery)
  435. Savable.addClass(originalClass)
  436. return await p;
  437. }
  438. }
  439. },
  440. set(obj, propName, value){
  441. }
  442. })
  443. }
  444. static get defaultPermissions(){
  445. return {
  446. //savable refs, objectid's, words like 'tags' or 'roles'
  447. read: ['owner', 'user'],
  448. write: ['owner', 'admin'],
  449. create: ['user'],
  450. delete: ['admin'],
  451. /*permission
  452. * TODO: permissions for read and write permissions
  453. *
  454. */
  455. }
  456. }
  457. static get guestRelations(){ //guest relations are accessible to write by setRelation or removeRelation even if no write permission, only with read
  458. return []
  459. }
  460. }
  461. return SlicedSavable
  462. }
  463. return {Savable, sliceSavable}
  464. }
  465. async function connect(dbName, dsn="mongodb://localhost:27017/"){
  466. if (!dbName)
  467. throw new ReferenceError(`db name does not provided`)
  468. const mongoClient = new MongoClient(dsn, { useNewUrlParser: true });
  469. const client = await mongoClient.connect()
  470. const db = client.db(dbName)
  471. const Savable = mm(db).Savable
  472. const slice = mm(db).sliceSavable
  473. return {
  474. Savable,
  475. slice,
  476. }
  477. }
  478. module.exports = {
  479. mm,
  480. connect
  481. }