index.js 24 KB

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