Ivan Asmer 6 years ago
parent
commit
8b0202f429
1 changed files with 71 additions and 0 deletions
  1. 71 0
      index.js

+ 71 - 0
index.js

@@ -0,0 +1,71 @@
+(async() => {
+    const Sequelize = require("sequelize");
+    const sequelize = new Sequelize("test", "","",{
+		host: 'localhost',
+		dialect: 'mysql',
+
+		pool: {
+			max: 5,
+			min: 0,
+			idle: 10000
+		},
+		//logging: false
+	});
+
+	const Slice = sequelize.define("slice",{
+		permission: Sequelize.STRING, //create, update, delete, read, etc
+		model: Sequelize.STRING,
+		modelId: Sequelize.INTEGER,
+		//plain list of: "tags" like: admin, manager, user, anon, User can be tagged by this word in string list variable
+		//OR: just userId.
+		//OR, if negative number (or hash #100500) - other slice id (use abs to get proper table id)
+		//this way optimizing 
+		slice: {type: Sequelize.TEXT, //PROBABLY STRING
+			get(){
+				return this.getDataValue("slice").split(",")
+			},
+			set(newValue){
+				newValue = "length" in newValue ? newValue.join(",") : newValue
+				return this.setDataValue("slice", newValue)
+			}
+		}
+	},{
+		getterMethods: {
+			async all(){
+				const Op = Sequelize.Op
+				this._subSlicesList = this.slice.filter(id => id[0] == "#" || id[0] == "-") //detect - or #
+												.map(   id => +id.substr(1)) //cut first char + toInt
+				let subSlices = await Slice.find({where: {id: {[Op.in]: this._subSlicesList}}})
+				for (let subSlice of subSlices){
+					this._subSlicesList = this._subSlicesList.filter(id => subSlice.id !== id)
+					this._subSlicesList = [...this._subSlicesList,... await subSlice.all()]
+				}
+				return this._subSlicesList
+			}
+		},
+		indexes: [
+			{
+				fields: ["modelId", "model", "permission"]
+			},
+		]
+	})
+
+
+	const User = sequelize.define("user", {
+		login: Sequelize.STRING,
+		password: Sequelize.STRING,
+	})
+
+	const Content = sequelize.define("content", {
+		title: Sequelize.STRING,
+		data: Sequelize.TEXT
+	})
+
+	await sequelize.sync()
+
+	function sliced(model){
+
+	}
+
+	let delay = ms => new Promise(r => setTimeout(r, ms))
+})()