Metastocle - dezentrales Data Warehouse

Bild


Was ist, wenn Sie eine Vielzahl von Daten dezentral speichern mĂŒssen? Objekte, Arrays, Datumsangaben, Zahlen, Zeichenfolgen, ja alles. Ist es notwendig, dafĂŒr ein leistungsfĂ€higes DBMS zu entwickeln? In der Tat mĂŒssen wir Daten oft nur verteilt und offen speichern und empfangen, aber so einfach wie möglich und ohne besondere AnsprĂŒche.


In diesem Artikel möchte ich eine kleine Metastocle- Bibliothek vorstellen , mit der Sie das oben genannte Problem leicht, aber mit einigen EinschrÀnkungen lösen können.


Ein kleiner Hintergrund


Vor etwa einem Jahr gab es den Wunsch und die Notwendigkeit, ein Musik-Repository zu erstellen. Dieser Artikel Details. Von Anfang an war klar, dass Sie alles schreiben mĂŒssen, damit Sie in Zukunft dasselbe mit anderen EntitĂ€ten tun können: BĂŒchern, Videos usw. Es wurde beschlossen, alles in Schichten zu unterteilen, die unabhĂ€ngig voneinander verwendet werden können.


Metastocle ist eine der Ebenen, mit denen Sie viele Arten von Daten (aber keine Dateien) speichern und empfangen können, im Gegensatz zur Storacle- Ebene , die die Arbeit mit Dateien implementiert.


, - , . metastocle. : , ...


, , :


  • — nosql , , ...
  • — , .
  • () — : , , ...

:


:


const Node = require('metastocle').Node;

(async () => {
  try {
    const node = new Node({
      port: 4000,
      hostname: 'localhost'
    });

    //  
    await node.addCollection('test', { limit:  10000, pk:  'id' });
    await node.init();
  }
  catch(err) {
    console.error(err.stack);
    process.exit(1);
  }
})();

:


const Client = require('metastocle').Client;

(async () => {
  try {
    const client = new Client({
      address: 'localhost:4000'
    });
    await client.init();

    //  
    const doc = await client.addDocument('test', { text: 'hi' });

    //   
    await client.updateDocuments('test', { text: 'bye' }, {
      filter: { id: doc.id }
    });

    //    
    await client.addDocument('test', { id: 2, text: 'new' });

    //   
    const results = await client.getDocuments('test', {
      filter: { id: 2 }
    });

    //   
    const doc2 = await client.getDocumentById('test', 2));

    //   
    for(let i = 10; i <= 20; i++) {
      await client.addDocument('test', { id: i, x: i });
    }

    //  ,   
    const results2 = await client.getDocuments('test', {
      filter: { id: { $gt: 15 } },
      sort: [['x', 'desc']],
      limit:  2,
      offset:  1,
      fields: ['id']
    });

    //  ,   id > 15
    await client.deleteDocuments('test', {
      filter: { id: { $gt: 15 } }
    });
  }
  catch(err) {
    console.error(err.stack);
    process.exit(1);
  }
})();

, , . , :


const node = new Node({
   port: 4000,
   hostname: 'localhost',
   collections: {
     test: { limit: 10000, pk: 'id' }
   }
 });

:


  • pk — . , . , , , uuid , .
  • limit —
  • queue — : ,   ,
  • limitationOrder — , . , , .
  • schema —
  • defaults — . 
  • hooks — . 
  • preferredDuplicates — .

(schema) :


{ 
  type: 'object',
  props: {
    count: 'number',
    title: 'string',
    description: { type: 'string' },
    priority: {
      type: 'number',
      value: val => val >= -1 && val <= 1
    },
    goods: {
      type: 'array',
      items: {
        type: 'object',
        props: {
          title: 'string',
          isAble: 'boolean'
        }
      }
    }
  }
}

utils.validateSchema() https://github.com/ortexx/spreadable/blob/master/src/utils.js


:


{ 
  defaults: {
    date: Date.now
    priority: 0
    'nested.prop': (key, doc) => Date.now() - doc.date
  },
  hooks: {
    priority: (val, key, doc, prevDoc) => prevDoc? prevDoc.priority + 1: val
  }
}

:


  • CRUD
  • Javascript, , .


javascript , . 


https://github.com/ortexx/metastocle/blob/master/dist/metastocle.client.js window.ClientMetastocle ...


Api


  • async Client.prototype.addDocument() —
  • async Client.prototype.getDocuments() — -
  • async Client.prototype.getDocumentsount() —
  • async Client.prototype.getDocumentByPk() —
  • async Client.prototype.updateDocuments() — -
  • async Client.prototype.deleteDocuments() — -

():


.filter — , :


{ 
  a: { $lt: 1 },
  $and: [
    { x: 1 },
    { y: { $gt: 2 } },
    { 
      $or: [
        { z: 1 },
        { "b.c": 2 }
      ] 
    }
  ]
}

.sort — , :


{ sort: [['x', 'asc'], ['y.z', 'desc']] }

.limit —


.offset —


.fields —


readme.



. : npm i -g metastocle --unsafe-perm=true --allow-root. , .


, metastocle -a getDocumentByPk -o test -p 1 -c ./config.js, 1 test. https://github.com/ortexx/metastocle/blob/master/bin/actions.js



  • , , , . , -, , -, , .
  • . , - , : , ... , , http , .

, .


, , :


  • , , , - , . 
  • - , , , . , 10000 - 100 , , , .

, , , , . , , .


:



All Articles