
分散されたさまざまなデータを保存する必要がある場合はどうなりますか?オブジェクト、配列、日付、数値、文字列、なんでもあり。これには強力なDBMSを考え出す必要がありますか?実際、多くの場合、分散したオープンな方法でデータを格納および受信する必要がありますが、できるだけ単純で、特別な要求はありません。
この記事では、上記の問題を簡単に解決できる、いくつかの制限付きの小さなmetastocleライブラリを紹介します。
少し背景
約1年前、音楽レポジトリを作成したいという要望がありました。この記事の詳細。初めから、将来的には他のエンティティ(本、ビデオなど)でも同じことができるように、すべてを書く必要があることは明らかでした。すべてを独立して使用できるレイヤーに分割することが決定されました。
Metastocleは、ファイルでの作業を実装するstoracleレイヤーとは対照的に、多くのタイプのデータ(ファイルではない)を格納および受信できるレイヤーの1つです。
, - , . 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']
});
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
}
}
:
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 , , , .
, , , , . , , .
: