5
5
import isEmpty from '@trash/utils/is_empty' ;
6
6
import isNumber from '@trash/utils/is_number' ;
7
7
8
+ export type FieldDef = { ...} ;
9
+
8
10
const RecordHandler = {
9
11
// $FlowFixMe
10
12
get ( target : Object , prop : mixed ) {
@@ -13,33 +15,31 @@ const RecordHandler = {
13
15
prop === 'toWrite' ||
14
16
prop === 'rollback' ||
15
17
prop === 'persist' ||
18
+ prop === '__info' ||
16
19
typeof prop === 'symbol'
17
20
) {
18
21
const ref = target [ prop ] ;
19
- if ( typeof ref === 'function' ) {
20
- return ref . bind ( target ) ;
21
- }
22
- return target [ prop ] ;
22
+ return ( typeof ref === 'function' ) ? ref . bind ( target ) : ref ;
23
23
}
24
- return target . values [ prop ] ;
24
+ return target . __values [ prop ] ;
25
25
} ,
26
26
// $FlowFixMe
27
27
set ( target : Object , prop : mixed , value : mixed ) {
28
- target. modified_fields . push ( prop ) ;
29
- if ( target . values . length === 1 ) {
30
- return Reflect . set ( target . values [ 0 ] , prop , value ) ;
28
+ target. __modified_fields . push ( prop ) ;
29
+ if ( target . __values . length === 1 ) {
30
+ return Reflect . set ( target . __values [ 0 ] , prop , value ) ;
31
31
}
32
- return Reflect . set ( target . values , prop , value ) ;
32
+ return Reflect . set ( target . __values , prop , value ) ;
33
33
} ,
34
34
35
35
// $FlowFixMe
36
36
ownKeys ( target : Object ) {
37
- return Reflect . ownKeys ( target . values ) ;
37
+ return Reflect . ownKeys ( target . __values ) ;
38
38
} ,
39
39
// $FlowFixMe
40
40
has ( target : Object , prop : mixed ) {
41
41
if ( typeof prop === 'string' || typeof prop === 'number' ) {
42
- return prop in target . values ;
42
+ return prop in target . __values ;
43
43
}
44
44
return false ;
45
45
} ,
@@ -50,36 +50,38 @@ const RecordHandler = {
50
50
51
51
export class Record {
52
52
#origin: { [ string ] : mixed } = { } ;
53
- values: { [ string ] : mixed } ;
54
- modified_fields: Array < string > = [ ] ;
53
+ __info: FieldDef ;
54
+ __values: { [ string ] : mixed } ;
55
+ __modified_fields: Array < string > = [ ] ;
55
56
56
- constructor ( values : { ...} ) {
57
+ constructor ( values : { ...} , field_info : FieldDef ) {
57
58
this . #origin = { ...values } ;
58
- this . values = values ;
59
+ this . __values = values ;
60
+ this . __info = field_info ;
59
61
}
60
62
61
63
persist ( ) {
62
- this . #origin = { ...this . values } ;
63
- this . modified_fields = [ ] ;
64
+ this . #origin = { ...this . __values } ;
65
+ this . __modified_fields = [ ] ;
64
66
}
65
67
66
68
toJSON ( ) : { ...} {
67
- return this . values ;
69
+ return this . __values ;
68
70
}
69
71
70
72
toWrite ( ) : { [ string ] : mixed } {
71
73
const write_vals : { [ string ] : mixed } = { } ;
72
- for ( const field_name of this . modified_fields ) {
73
- write_vals [ field_name ] = this . values [ field_name ] ;
74
+ for ( const field_name of this . __modified_fields ) {
75
+ write_vals [ field_name ] = this . __values [ field_name ] ;
74
76
}
75
77
return write_vals ;
76
78
}
77
79
78
80
rollback ( ) {
79
- for ( const field_name of this . modified_fields ) {
80
- this . values [ field_name ] = this . #origin[ field_name ] ;
81
+ for ( const field_name of this . __modified_fields ) {
82
+ this . __values [ field_name ] = this . #origin[ field_name ] ;
81
83
}
82
- this . modified_fields = [ ] ;
84
+ this . __modified_fields = [ ] ;
83
85
}
84
86
85
87
// $FlowFixMe
@@ -90,7 +92,7 @@ export class Record {
90
92
// $FlowFixMe
91
93
[ Symbol . toPrimitive ] ( hint ) {
92
94
if ( hint === 'string' ) {
93
- return JSON . stringify ( this . values ) ;
95
+ return JSON . stringify ( this . __values ) ;
94
96
}
95
97
}
96
98
}
@@ -136,21 +138,23 @@ const RecordsetHandler = {
136
138
export default class Recordset {
137
139
#model: string ;
138
140
#records: Array < Record > = [ ] ;
141
+ #fields: { [ string ] : FieldDef } = { } ;
139
142
140
143
// $FlowFixMe
141
144
static isValid ( obj : Object ) {
142
145
return obj instanceof Recordset ;
143
146
}
144
147
145
- static make ( model : string , values : Array < { [ string ] : mixed } > ) : Recordset {
146
- const rs = new Recordset ( model , values ) ;
148
+ static make ( model : string , values : Array < { [ string ] : mixed } > , fields ? : { [ string ] : FieldDef } ) : Recordset {
149
+ const rs = new Recordset ( model , values , fields ) ;
147
150
return new Proxy ( rs , RecordsetHandler ) ;
148
151
}
149
152
150
- constructor ( model : string , values : Array < { [ string ] : mixed } > ) {
153
+ constructor ( model : string , values : Array < { [ string ] : mixed } > , fields ? : { [ string ] : FieldDef } ) {
151
154
this . #model = model ;
155
+ this . #fields = fields || { } ;
152
156
for ( const rec_vals of values ) {
153
- const record = new Record ( rec_vals ) ;
157
+ const record = new Record ( rec_vals , this . #fields ) ;
154
158
this . #records. push ( new Proxy ( record , RecordHandler ) ) ;
155
159
}
156
160
}
0 commit comments