元存储-分散数据仓库

图片


如果您需要存储分散的各种数据怎么办?对象,数组,日期,数字,字符串等等。为此有必要提出一个功能强大的DBMS吗?确实,我们通常只需要公开地存储和接收分布式数据,但要尽可能简单并且没有任何特殊要求。


在本文中,我想揭示一个小型的metastocle,使用它可以轻松解决上述问题,但有一些限制。


一点背景


大约一年前,有一种创建音乐库的愿望和需求。本文详细介绍。从一开始就很明显,您需要编写所有内容,以便将来可以与其他实体(书籍,视频等)进行相同的操作。决定将所有内容划分为可以独立使用的层。


storacle层相反,Metastocle是允许您存储和接收许多类型的数据(但不是文件)的层之一,而storacle则实现文件的工作。


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