@orbito/api-lib (1.3.3)
Published 2026-05-26 13:48:49 +02:00 by wiktor
Installation
@orbito:registry=https://git.kozminski.net.pl/api/packages/orbito/npm/npm install @orbito/api-lib@1.3.3"@orbito/api-lib": "1.3.3"About this package
Na razie w kodzie modułu, ale docelowo do będzie standalone biblioteka do pobrania z npm
Orbito API
API obsługujące protokół binarny
Zmienne konfiguracyjne
ORBITO_HOST- Adres url RPC Api lub sock pathORBITO_PORT- Port RPC Api lub null jeśli połączenie przez sockORBITO_TOKEN- token autoryzacyjnyORBITO_DEBUG- jeśli == 1 to w konsoli bilbioteka wyrzuca wszystkie zapytania
DB - szybki start
Modul DB udostepnia proste operacje na kolekcjach (find, insert, update, delete)
oraz klase Schema, ktora waliduje dane przed zapisem i pozwala definiowac hooki.
Definicja schematu
const { DB } = require('@orbito/api-lib');
const OrdersSchema = new DB.Schema('orders', {
companyId: { type: 'ObjectId', required: true },
orderNumber: { type: 'string', required: true },
cart: { type: 'array', items: { type: 'object' }, default: [] },
total: { type: 'number', default: 0 },
createdAt: { type: 'date', default: () => new Date() }
});
OrdersSchema.pre('validate', function(data) {
if (data.cart && Array.isArray(data.cart)) {
data.total = data.cart.reduce((sum, item) => sum + (item.price * item.quantity), 0);
}
});
OrdersSchema.post('create', function(doc) {
// np. emit event po utworzeniu
});
DB.createIndex('orders', { companyId: 1, orderNumber: 1 }, { unique: true });
module.exports = OrdersSchema;
Operacje na danych
const { DB } = require('@orbito/api-lib');
// create z walidacja schematu
const doc = await OrdersSchema.create({
companyId: '65f1b3e8c1f1a0a0a0a0a0a0',
orderNumber: 'ORD-123',
cart: [{ price: 10, quantity: 2 }]
});
// find / findOne
const list = await OrdersSchema.find({ companyId: '65f1b3e8c1f1a0a0a0a0a0a0' }, 50, 0, { createdAt: -1 });
const one = await OrdersSchema.findOne({ orderNumber: 'ORD-123' });
// update
const result = await OrdersSchema.update({ _id: doc._id }, { $set: { total: 30 } });
// delete
const deleted = await OrdersSchema.delete({ _id: doc._id });
Uwagi
Schemaautomatycznie mapujeObjectIdw definicji na validatorobjectID.- W
find(schemat) string w formacie 24-hex zostanie rzutowany naObjectId, jesli pole w schemacie ma typObjectId. - Operacje
DB.find/insert/update/delete/createIndexdzialaja bezposrednio na kolekcjach.
Dependencies
Dependencies
| ID | Version |
|---|---|
| bson | ^6.10.4 |
| fastest-validator | ^1.19.1 |
Development Dependencies
| ID | Version |
|---|---|
| eslint | ^8.29.0 |
| eslint-plugin-jest | ^28.11.0 |
| jest | ^29.7.0 |
| nodemon | ^2.0.19 |
| supertest | ^7.0.0 |