Metastocle - almacén de datos descentralizado

imagen


¿Qué sucede si necesita almacenar una variedad de datos descentralizados? Objetos, matrices, fechas, números, cadenas, sí, cualquier cosa. ¿Es necesario llegar a un poderoso DBMS para esto? De hecho, a menudo solo necesitamos almacenar y recibir datos de manera abierta y distribuida, pero de la manera más simple posible y sin ningún reclamo especial.


En este artículo, me gustaría revelar una pequeña biblioteca de metastocle , con la que puede resolver el problema anterior fácilmente, pero con algunas limitaciones.


Un poco de historia


Hace aproximadamente un año, había un deseo y una necesidad de crear un repositorio de música. Este artículo detalla. Desde el principio, estaba claro que necesita escribir todo para que en el futuro pueda hacer lo mismo con otras entidades: libros, videos, etc. Se decidió dividir todo en capas que se pueden usar de forma independiente.


Metastocle es una de las capas que le permite almacenar y recibir muchos tipos de datos (pero no archivos), a diferencia de la capa storacle , que implementa el trabajo con archivos.


, - , . 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