const MongoClient = require("mongodb").MongoClient; const ObjectID = require("mongodb").ObjectID; const mm = require('./index.js').mm const delay = ms => new Promise(r => setTimeout(r.bind(ms), ms)) ;(async () => { const mongoClient = new MongoClient("mongodb://localhost:27017/", { useNewUrlParser: true }); const client = await mongoClient.connect() const db = client.db('mm') const Savable = mm(db).Savable const SlicedSavable = mm(db).sliceSavable([ObjectID("5c9571219be797377361c65a"), 'user', 'admin']) //const SlicedSavable = mm(db).sliceSavable([]) // class Notebook extends SlicedSavable{ static get relations(){ return { owner: "notebook" } } } class User extends SlicedSavable{ static get relations(){ return { children: "parent", parent: ["children"], friends: ["friends"], notebook: "owner", } } } Savable.addClass(Notebook) Savable.addClass(User) let names = ['Ivan', 'Petro', 'Mykola', 'Sashko'] let surnames = ['Ivanopulo', 'Petrov', 'Mykolyiv', 'Alexandrov'] let rndItem = arr => arr[Math.floor(Math.random()*arr.length)] let person = new User({ name: 'Mykola', surname: 'Silniy', phones: ['105', '1'], children: [ new User({ name: 'Marina', surname: 'Silnaya', phones: ['105', '1000503'], }), new User({ name: 'Andrey', surname: 'Silniy', phones: ['103', '1000502'], }), new User({ name: 'Fedor', surname: 'Ivanova', phones: ['102', '1000504'], notebook: new Notebook({ brand: 'dubovo' }) }) ] }) await person.save() console.log(person.children.length) person.children[0].parent = null; await person.children[0].save() console.log(person.children.length) //let orphan = new User({ //name: 'Left', //surname: 'Guy', //parent: person //check for push into and $addToSet //}) //await orphan.save() //person.parent = orphan //check for arrize //await person.save() //console.log(orphan.children) //person.parent = null //check for arrize //await person.save() //person.children.pop(); //person.children.pop(); //console.log(person.children.length) //await person.save() let stamp = (new Date()).getTime() let prevI = 0; const persons = [] //for (var i=0;i<1e10;i++){ //let person = new User({ //name: rndItem(names), //surname: rndItem(surnames), //phones: ['105', '1'], //friends: persons.slice(-(Math.random()*80)) //}) //await person.save(true) //persons.push(person) //if (persons.length > 200){ //await (Math.random() > 0.5 ? persons.shift() : persons.pop()).save(true) //} //let now = (new Date()).getTime() //if (stamp < now - 1000){ ////results: ////objects w/o relations: 2500 writes per second ////objects w relations: pessimistic backrelations sync, 0..80 friends of 200 latest created, ~25 per second due 0..80 saves of other friends with new one relation ////objects w relations: ~500 per second, save w/o backref save (but it updated in object), than, when object removed from random buffer, re-save it with updated relations //console.log(i, i - prevI) //prevI = i //stamp = now //} //} async function walker(limit=10) { let start = (new Date()).getTime() let stamp = start let now = start let prevI = 0 let person for (let uP of Savable.m.User.find({},null,{sort:['_id', -1], limit: [1]})){ person = await uP console.log(person) } //let person = [...Savable.m.User.find({},null,{sort:['_id', 1], limit: [1]})][0] //console.log('order-huyorder', person) let prevPerson; for (var i=0;i cb(obj)) ////} ////} ////console.log(await obj) ////console.log('empty await', await person)//.then(p => console.log(p)) ////console.log('sub await', (await person.children[0]))//.then(p => console.log(p)) client.close(); })()