
E se você precisar armazenar uma variedade de dados descentralizados? Objetos, matrizes, datas, números, seqüências de caracteres, sim qualquer coisa. É necessário criar um DBMS poderoso para isso? De fato, geralmente precisamos apenas armazenar e receber dados de maneira distribuída e aberta, mas o mais simples possível e sem nenhuma reivindicação especial.
Neste artigo, gostaria de revelar uma pequena biblioteca de metastocle , com a qual você pode resolver o problema acima facilmente, mas com algumas limitações.
Um pouco de fundo
Cerca de um ano atrás, havia um desejo e a necessidade de criar um repositório de músicas. Este artigo detalha. Desde o início, ficou claro que você precisa escrever tudo para que no futuro possa fazer o mesmo com outras entidades: livros, vídeos, etc. Foi decidido dividir tudo em camadas que podem ser usadas independentemente.
Metastocle é uma das camadas que permite armazenar e receber muitos tipos de dados (mas não arquivos), em oposição à camada de armazenamento , que implementa o trabalho com arquivos.
, - , . 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 , , , .
, , , , . , , .
: