in memory data layer for fast access to plain normalized data
npm install basis-company/data-player.js basis-company/helpers.js
ΠΠΎΠ΄Π΅Π»Ρ (model) ΠΎΠΏΠΈΡΡΠ²Π°Π΅Ρ ΡΠΎΡΡΠ°Π² ΡΠΎΠ±ΡΡΠ²Π΅Π½Π½ΡΡ ΠΈ Π²ΠΈΡΡΡΠ°Π»ΡΠ½ΡΡ ΠΏΠΎΠ»Π΅ΠΉ ΡΡΡΠ½ΠΎΡΡΠΈ.
ΠΠΎΠ»Π»Π΅ΠΊΡΠΈΡ (collection) ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΠ½Π½ΠΎΠΉ ΠΌΠΎΠ΄Π΅Π»ΠΈ ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ Π·Π°ΠΏΠΈΡΠΈ Ρ Π΄Π°Π½Π½ΡΠΌΠΈ, ΡΠ°ΡΠΏΡΠ΅Π΄Π΅Π»ΡΠ½Π½ΡΠ΅ ΠΏΠΎ Π΄ΡΠ΅Π²ΠΎΠ²ΠΈΠ΄Π½ΡΠΌ ΠΈΠ½Π΄Π΅ΠΊΡΠ°ΠΌ (index).
ΠΡΡ (view) ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΠ½Π½ΠΎΠΉ ΠΌΠΎΠ΄Π΅Π»ΠΈ
- Π·Π°Π³ΡΡΠΆΠ°Π΅Ρ ΡΠΊΠΏΠ΅Π΄ΠΈΡΠΎΡΠΎΠΌ Π½Π° ΠΎΡΠ½ΠΎΠ²Π΅ ΠΏΠΎΠ»Π΅ΠΉ ΠΎΡΠ½ΠΎΠ²Π½ΡΠ΅ ΠΈ Π·Π°Π²ΠΈΡΠΈΠΌΡΠ΅ Π·Π°ΠΏΠΈΡΠΈ,
- Π³Π΅Π½Π΅ΡΠΈΡΡΠ΅Ρ ΠΏΠΎ Π½ΠΈΠΌ Π½ΠΎΠ²ΡΠ΅ Π·Π°ΠΏΠΈΡΠΈ Π΄Π»Ρ ΠΏΡΠ΅Π΄ΡΡΠ°Π²Π»Π΅Π½ΠΈΡ,
- ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°Π΅Ρ Π°ΠΊΡΡΠ°Π»ΡΠ½ΠΎΡΡΡ Π½ΠΎΠ²ΡΡ Π·Π°ΠΏΠΈΡΠ΅ΠΉ ΠΏΡΠΈ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠΈ ΠΈΡΡ ΠΎΠ΄Π½ΡΡ Π·Π°ΠΏΠΈΡΠ΅ΠΉ.
ΠΠΊΠΏΠ΅Π΄ΠΈΡΠΎΡ (expeditor) Π½Π° ΠΎΡΠ½ΠΎΠ²Π΅ ΠΏΠΎΠ»Π΅ΠΉ ΠΏΡΠΎΠΈΠ·Π²ΠΎΠ΄ΠΈΡ ΠΊΠ°ΡΠΊΠ°Π΄Π½ΡΡ Π²ΡΠ±ΠΎΡΠΊΡ (cascade) ΠΈΠ· ΠΊΠΎΠ»Π»Π΅ΠΊΡΠΈΠΉ ΠΏΡΠΈ ΠΏΠΎΠΌΠΎΡΠΈ Π·Π°Π³ΡΡΠ·ΡΠΈΠΊΠ°.
ΠΠ°Π³ΡΡΠ·ΡΠΈΠΊ (loader)
- ΠΏΠΎΠ»ΡΡΠ°Π΅Ρ Π·Π°ΠΏΠΈΡΠΈ ΠΈΠ· ΠΊΠΎΠ»Π»Π΅ΠΊΡΠΈΠΈ, Π΅ΡΠ»ΠΈ Π·Π°ΠΏΡΠΎΡ ΠΏΠΎΠ²ΡΠΎΡΠ½ΡΠΉ (similar),
- ΠΈΠ»ΠΈ Π·Π°Π³ΡΡΠΆΠ°Π΅Ρ Π·Π°ΠΏΠΈΡΠΈ Ρ ΡΠ΅ΡΠ²Π΅ΡΠ° (request) Π² ΠΏΡΠ΅Π΄Π΅Π»Π°Ρ Π·ΠΎΠ½Ρ Π΄ΠΎΡΡΡΠΏΠ½ΠΎΡΡΠΈ Π΄Π°Π½Π½ΡΡ (scope),
- ΠΏΡΠΎΠ²Π΅ΡΡΠ΅Ρ Π½Π°Π»ΠΈΡΠΈΠ΅ Π·Π°ΠΏΡΠΎΡΠ΅Π½Π½ΡΡ Π·Π°ΠΏΠΈΡΠ΅ΠΉ ΠΈ Π΄ΠΎΠ³ΡΡΠΆΠ°Π΅Ρ ΠΈΡ , Π΅ΡΠ»ΠΈ ΠΎΠ½ΠΈ Π²Π½Π΅ Π·ΠΎΠ½Ρ Π΄ΠΎΡΡΡΠΏΠ½ΠΎΡΡΠΈ (extra).
import { register as mr } from 'data-player/model';
mr({
name: 'user',
// own fields
fields: [
'id', // same as "{ name: 'id' }"
'firstname',
'lastname',
{
name: 'company',
reference: 'company', // link to another model
property: 'id', // id-field name of "company"
},
],
// virtual fields
roles() {
return this.query('user_role.role.nick');
},
});
mr({
name: 'company',
fields: [
'id',
'name',
],
});
mr({
name: 'role',
fields: [
'id',
'nick',
],
});
mr({
name: 'user_role',
fields: [
'id',
{ name: 'user', reference: 'user', property: 'id' },
{ name: 'role', reference: 'role', property: 'id' },
],
});
import { register as cr } from 'data-player/collection';
cr('user', [
[ 1, 'Helen', 'Fletcher', 1 ],
[ 2, 'Jacob', 'Evans', 2 ],
[ 3, 'Lloyd', 'Henry', 2 ],
]);
cr('company', [
[ 1, 'Google' ],
[ 2, 'Microsoft' ],
]);
cr('role', [
[ 10, 'admin' ],
[ 20, 'manager' ],
]);
cr('user_role', [
[ 1, 1, 10 ],
[ 2, 1, 20 ],
[ 3, 2, 20 ],
[ 4, 3, 20 ],
]);
import { find, findOne, findOrFail } from 'data-player/data';
// > get all records
find('user')
// <
[
{ "id": 1, "firstname": "Helen", "lastname": "Fletcher", "company": 1 },
{ "id": 2, "firstname": "Jacob", "lastname": "Evans", "company": 2 },
{ "id": 3, "firstname": "Lloyd", "lastname": "Henry", "company": 2 },
]
// > get by id
find('user', 2)
// <
[
{ "id": 2, "firstname": "Jacob", "lastname": "Evans", "company": 2 },
]
// > get by field
find('user', { company: 2 })
// <
[
{ "id": 2, "firstname": "Jacob", "lastname": "Evans", "company": 2 },
{ "id": 3, "firstname": "Lloyd", "lastname": "Henry", "company": 2 },
]
// > get first match
findOne('user', { company: 2 })
// <
{ "id": 2, "firstname": "Jacob", "lastname": "Evans", "company": 2 }
// > get undefined
find('user', { company: 3 })
// <
[]
// > get undefined
findOrFail('user', { company: 3 })
// <
throw Error
var user2 = findOne('user', 2)
// user2 = { "id": 2, "firstname": "Jacob", "lastname": "Evans", "company": 2 }
// get field value
user2.get('company')
// 2
// get field record
user2.query('company')
// <
[
{ "id": 2, "name": "Microsoft" },
]
// get several fields
user2.get('firstname lastname')
// < "Jacob Evans"
user2.get('company.name')
// "Microsoft"
user2.query('company.name')
// <
[
"Microsoft",
]
var user1 = findOne('user', 1)
// user1 = { "id": 1, "firstname": "Helen", "lastname": "Fletcher", "company": 1 }
user1.query('user_role.role.nick')
// <
[
"admin",
"manager",
]
user1.get('user_role.role.nick')
// < "admin, manager"
user1.get('roles')
// < "admin, manager"
import { collection, findOne } from 'data-player/data';
// origin:
// { "id": 1, "firstname": "Helen", "lastname": "Fletcher", "company": 1 }
collection('user').splice([
[ 1, "Dorothy", "Fletcher", 1],
]);
findOne('user', 1)
// <
{ "id": 1, "firstname": "Dorothy", "lastname": "Fletcher", "company": 1 }
//
// tuple must be full
//
collection('user').splice([
[ 1, "Dorothy" ],
]);
findOne('user', 1)
// <
{ "id": 1, "firstname": "Dorothy" }
//
// object may be partial
//
collection('user').splice([
{ id: 1, lastname: 'Fletcher', company: 2 },
]);
findOne('user', 1)
// <
{ "id": 1, "firstname": "Dorothy", "lastname": "Fletcher", "company": 2 }
//
// remove records from collection
//
collection('user').splice([
{ id: 1 },
],
true // = remove
);
findOne('user', 1)
// < null
import { Collection } from 'data-player/collection';
var proto = Collection.prototype;
// make Collection eventable by external kit
proto.on = observable.addListener;
proto.fireEvent = observable.fireEvent;
// listen to changes
collection('user').on('change', function onChange(records) {
// ...
});
import { View } from 'data-player/view';
var proto = View.prototype;
// make View eventable by external kit
// changes in collection will apply to records in view
proto.on = observable.addListener;
proto.mon = observable.addManagedListener;
proto.fireEvent = observable.fireEvent;
var view = new View({
model: 'user',
fields: [
'id',
'firstname lastname',
'company.name',
'roles',
],
});
// load all records from 'user' collection
view.load();
// "view was loaded"
view.on('refresh', function(view, data, expeditor) {
// update 'data' in ui
});
// "some data was changed"
view.on('update', function(view, data, expeditor) {
// update 'data' in ui
});
id | firstname lastname | company.name | roles |
---|---|---|---|
1 | Helen Fletcher | admin, manager | |
2 | Jacob Evans | Microsoft | manager |
3 | Lloyd Henry | Microsoft | manager |