Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F7862600
single-reference.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
single-reference.ts
View Options
import
Bluebird
from
"bluebird"
;
import
{
Field
,
Context
}
from
"../../../main"
;
import
ItemList
,
{
AttachmentOptions
}
from
"../../../chip-types/item-list"
;
import
{
ValidationError
}
from
"../../../response/errors"
;
type
Filter
=
Record
<
string
,
any
>
;
/** A reference to other item, in the same or other collection. Can point at items from only one, specified collection. Items with field of this type can be filtered by fields of the items it points at. Examples below.
*
*/
export
default
class
SingleReference
extends
Field
{
typeName
=
"single-reference"
;
hasIndex
=
()
=>
true
;
target_collection
:
string
;
filter
:
Filter
;
constructor
(
target_collection
:
string
,
filter
?:
Filter
)
{
super
();
this
.
target_collection
=
target_collection
;
this
.
filter
=
filter
||
{};
}
getTargetCollection
(
context
:
Context
)
{
return
context
.
app
.
collections
[
this
.
target_collection
];
}
async
isProperValue
(
context
:
Context
,
input
:
string
)
{
context
.
app
.
Logger
.
debug2
(
"SINGLE REFERENCE"
,
"isProperValue?"
,
input
);
const
filter
=
this
.
filter
||
{};
if
(
input
===
""
)
{
return
Field
.
valid
();
}
let
stages
=
await
this
.
getTargetCollection
(
context
)
.
list
(
context
)
.
filter
(
filter
)
.
getAggregationStages
();
stages
=
[{
$match
:
{
id
:
input
}
},
...
stages
];
const
results
=
await
this
.
app
.
Datastore
.
aggregate
(
this
.
getTargetCollection
(
context
).
name
,
stages
);
context
.
app
.
Logger
.
debug3
(
"SINGLE REFERENCE"
,
"isProperValue/results"
,
results
);
const
decision
=
results
.
length
>
0
?
Field
.
valid
()
:
Field
.
invalid
(
`Nie masz dostępu do danego zasobu z kolekcji
${
this
.
getTargetCollection
(
context
).
name
}
lub on nie istnieje.`
);
context
.
app
.
Logger
.
debug2
(
"SINGLE REFERENCE"
,
"isProperValue/decision"
,
decision
);
return
decision
;
}
async
filterToQuery
(
context
:
Context
,
filter
:
Filter
)
{
// treating filter as a query here
context
.
app
.
Logger
.
debug3
(
"SINGLE REFERENCE"
,
"FiltertoQuery"
,
{
context
,
filter
,
});
if
(
typeof
filter
!==
"object"
)
{
return
{
$eq
:
filter
,
};
}
const
{
items
}
=
await
this
.
app
.
collections
[
this
.
target_collection
]
.
list
(
context
)
.
filter
(
filter
)
.
fetch
();
return
{
$in
:
items
.
map
((
resource
)
=>
resource
.
id
)
};
}
async
getAggregationStages
(
context
:
Context
,
filter_value
:
unknown
)
{
context
.
app
.
Logger
.
debug3
(
"SINGLE REFERENCE"
,
"getAggregationStages"
,
{
context
,
filter_value
,
});
let
filter
:
{
[
field_name
:
string
]
:
any
}
=
{};
const
temp_field_name
=
`
${
this
.
getTargetCollection
(
context
).
name
}
-lookup
${
Math
.
floor
(
Math
.
random
()
*
Math
.
pow
(
10
,
7
))
}
`
;
if
(
!
filter_value
||
Object
.
keys
(
filter_value
as
Filter
).
length
===
0
)
return
[];
if
(
typeof
filter_value
===
"string"
)
{
return
[{
$match
:
{
[
await
this
.
getValuePath
()]
:
filter_value
}
}];
}
if
(
filter_value
instanceof
Array
)
{
let
_in
=
filter_value
;
if
(
filter_value
[
0
]
instanceof
Array
)
_in
=
filter_value
[
0
];
return
[
{
$match
:
{
[
await
this
.
getValuePath
()]
:
{
$in
:
_in
},
},
},
];
}
if
(
typeof
filter_value
!==
"object"
)
{
throw
new
ValidationError
(
"Invalid filter value"
);
}
for
(
const
field_name
in
filter_value
)
{
const
field
=
this
.
getTargetCollection
(
context
).
fields
[
field_name
];
if
(
!
field
)
return
Promise
.
reject
(
"Unknown field in filter for '"
+
this
.
getTargetCollection
(
context
).
name
+
"': "
+
field_name
);
filter
[
field_name
]
=
field
.
filterToQuery
(
context
,
(
filter_value
as
{
[
field_name
:
string
]
:
any
})[
field_name
]
);
}
filter
=
await
Bluebird
.
props
(
filter
);
const
ret
=
[
{
$lookup
:
{
from
:
this
.
getTargetCollection
(
context
).
name
,
let
:
{
referenced_id
:
`$
${
await
this
.
getValuePath
()
}
`
},
pipeline
:
[
{
$match
:
{
$expr
:
{
$eq
:
[
"$id"
,
"$$referenced_id"
],
},
},
},
{
$match
:
filter
},
{
$count
:
"count"
},
],
as
:
temp_field_name
,
},
},
{
$match
:
{
[
`
${
temp_field_name
}
.count`
]
:
{
$gt
:
0
}
}
},
{
$unset
:
temp_field_name
},
];
return
ret
;
}
async
getAttachments
(
context
:
Context
,
target_ids
:
string
[],
attachment_options
?:
AttachmentOptions
<
any
>
)
{
const
ret
=
new
ItemList
<
any
>
(
this
.
getTargetCollection
(
context
),
context
);
if
(
attachment_options
)
{
// ^ is either a boolean or an object
ret
.
ids
(
target_ids
);
if
(
typeof
attachment_options
===
"object"
)
{
ret
.
attach
(
attachment_options
);
}
}
else
{
ret
.
ids
([]);
// return an empty list;
}
return
ret
.
fetch
();
}
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Wed, Aug 13, 20:33 (4 h, 48 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
874350
Default Alt Text
single-reference.ts (4 KB)
Attached To
Mode
rS Sealious
Attached
Detach File
Event Timeline
Log In to Comment