models.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. const ObjectID = require("mongodb").ObjectID;
  2. const {connect} = require('mm')
  3. module.exports = async (dbName='player') => {
  4. const {Savable, slice} = await connect(dbName)
  5. async function getModels({id}){
  6. const SlicedSavable = slice([id, 'user'])
  7. class User extends SlicedSavable {
  8. constructor(...params){
  9. super(...params)
  10. //TODO: calc likes count by getter (no two-way relation for this to avoid overflow on many Kilos of likes
  11. //cached like count, which incremented and decremented
  12. //
  13. //following and followers array
  14. }
  15. static get relations(){ //don't needed due to ___owner in most cases
  16. return {
  17. avatar : "userAvatar",
  18. }
  19. }
  20. }
  21. SlicedSavable.addClass(User)
  22. class OwnerSlicedSavable extends SlicedSavable {
  23. get owner(){
  24. if (!this.___owner) return this.___owner
  25. return SlicedSavable.m.User.findOne({_id: ObjectID(this.___owner)})
  26. }
  27. }
  28. class Image extends OwnerSlicedSavable {
  29. constructor(...params){
  30. super(...params)
  31. }
  32. static async fromFileData(fileData){
  33. let image = new Image({})
  34. image.fileData = fileData
  35. image.url = `images/${fileData.filename}`
  36. image.originalFileName = fileData.originalname
  37. await image.save()
  38. return image;
  39. }
  40. async save(...params){
  41. if (this.userAvatar){
  42. if (this.userAvatar._id.toString() !== id){
  43. throw new ReferenceError(`You can't set ava for other user`)
  44. }
  45. }
  46. return await super.save(...params)
  47. }
  48. static get relations(){
  49. return {
  50. userAvatar: "avatar", //if it is ava
  51. }
  52. }
  53. }
  54. SlicedSavable.addClass(Image)
  55. class Track extends OwnerSlicedSavable {
  56. constructor(...params){
  57. super(...params)
  58. }
  59. static async fromFileData(fileData){
  60. try {
  61. let track = new Track({})
  62. track.fileData = fileData
  63. track.url = `track/${fileData.filename}`
  64. track.originalFileName = fileData.originalname
  65. const NodeID3 = require('node-id3')
  66. track.id3 = await NodeID3.read(fileData.path)
  67. await track.save()
  68. return track;
  69. }
  70. catch(e){
  71. return {error:e}
  72. }
  73. }
  74. static get relations(){
  75. return {
  76. playlists: ["tracks"], //if it is ava
  77. }
  78. }
  79. }
  80. SlicedSavable.addClass(Track)
  81. class Playlist extends OwnerSlicedSavable {
  82. constructor(...params){
  83. super(...params)
  84. }
  85. static get relations(){
  86. return {
  87. tracks: ["playlists"]
  88. }
  89. }
  90. static get defaultPermissions(){
  91. return {
  92. read: ['owner', 'user', 'admin'],
  93. create: ['user', 'admin'],
  94. write: ['owner', 'admin'],
  95. delete: ['owner', 'admin'],
  96. }
  97. }
  98. }
  99. SlicedSavable.addClass(Playlist)
  100. const thisUser = await Savable.m.User.findOne({_id: ObjectID(id)})
  101. return {models: {
  102. SlicedSavable, ...SlicedSavable.classes
  103. },
  104. thisUser}
  105. }
  106. return {
  107. Savable,
  108. slice,
  109. getModels
  110. }
  111. }