-
Notifications
You must be signed in to change notification settings - Fork 1
/
sandbox.js
76 lines (60 loc) · 1.98 KB
/
sandbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const TinyJsDb = require('./')
// create a new database instance
const db = new TinyJsDb()
// create tables
const heroes = db.createTable('heroes')
const abilities = db.createTable('abilities')
// set relationships
db.createRelationship(heroes, abilities)
console.log(db.relationships)
// add records to the database
heroes.insert({ name: 'Thor', age: 437 })
heroes.insert({ name: 'Hulk', age: 40 })
// add record and get its instance
const ironMan = heroes.insert({ name: 'Iron Man', age: 42 })
abilities.insert({ type: 'Super force' })
abilities.insert({ type: 'Resistance' })
abilities.insert({ type: 'Money' })
abilities.insert({ type: 'Thunder' })
// retrieve a record by filter
const thor = heroes.getFirst({ name: 'Thor' })
// retrieve a record by id
const thunder = abilities.getById(3)
// add a relation between records - using its instances
heroes.addRelation(thor, 'abilities', thunder)
// add a relation between records - using its ids
heroes.addRelation(1, 'abilities', 1)
// relations can be set from both sides
abilities.addRelation(2, 'abilities', ironMan)
// get a record with its relationships
const item = heroes.getById(1, ['abilities'])
console.log(item)
/*
{
_id: 1,
name: 'Hulk',
age: 40,
abilities: [ { type: 'Resistance', _id: 1 }, { type: 'Super force', _id: 0 } ]
}
*/
const myHeroes = heroes.getAll()
console.log(myHeroes)
/*
[
{ name: 'Thor', age: 437, _id: 0 },
{ name: 'Hulk', age: 40, _id: 1 },
{ name: 'Iron Man', age: 42, _id: 2 }
]
*/
// const cars = Db.createTable('cars')
// const newCar = { name: 'Ferrari', country: 'Italy' }
// cars.insert({ name: 'Porsche', country: 'Germany' })
// cars.insert(newCar)
// console.log(cars.name, ' - length: ', cars.length)
// const myCars = cars.getAll()
// console.log('myCars', myCars)
// console.log('-----------------------------------------------------')
// cars.updateFirstWhere({ name: 'Porsche' }, { name: '147', changed: true })
// const my3 = cars.getAll()
// console.log('myCars', myCars)
// console.log('my3', my3)