phenyl / sp2 Goto Github PK
View Code? Open in Web Editor NEWImmutable updater of objects using JSON operation.
License: Apache License 2.0
Immutable updater of objects using JSON operation.
License: Apache License 2.0
import { $bind } from "@sp2/updater";
type Hoge = {
fuga: {
key1: string;
key2: string;
};
};
const { $push, $path } = $bind<Hoge>();
$push($path("fuga", "key1"), "hoge"); // valid
const key = Math.random() > 0.5 ? "key1" : "key2";
$push($path("fuga", key), "hoge"); // $path is type error
I see here.
https://github.com/phenyl-js/sp2/blob/5c14b6d31cf228b8403c2f184787eb02cbe41c80/modules/format/src/common/bound-document-path.ts#L21-L28
https://github.com/phenyl-js/sp2/blob/5c14b6d31cf228b8403c2f184787eb02cbe41c80/modules/format/src/common/bound-document-path.ts#L151-L154
The $path method is generating some depth path type and this using like following codes.
K1 extends keyof T
, K2 extends keyof T[K1]
...
However if type K[i] had union type value, looks like TS compiler is checking 'left side value can extends all of expanded union type value?'.
And i think this issue can be solved by using like following codes.
K1 extends Extract<K1, keyof T>
, K2 extends Extract<K2, T[K1]>
...
But if resolved this issue, #37 Error is still showing...
sp2 provides slightly different formats from MongoDB's formats.
To fill the gap, sp2 should provide converters of operations.
function toMongoUpdateOperation(operation: GeneralUpdateOperation): GeneralUpdateOperation
function toMongoFindOperation(operation: FindOperation): FindOperation
DocumentPath [1]
is wrongly converted into DotNotationString .1
.
1
is expected.
import { $bind } from 'sp2'
type SomeType = {
exact: {
deepNestedExact: {
count: number
}
},
optional?: {
deepNestedOptional?: {
count: number
}
}
}
const { $path } = $bind<SomeType>()
$path('exact', 'deepNestedExact', 'count') // OK
$path('optional', 'deepNestedOptional') // Error: Argument of type '"deepNestedOptional"' is not assignable to parameter of type 'never'.
Hi.
I found an error according to the optional property.
How can I traverse optional property?
https://docs.mongodb.com/manual/reference/operator/update/positional-all/
The all positional operator $[] indicates that the update operator should modify all elements in the specified array field
$append
operator is syntax sugar for $set
operator.
{ $set: { 'obj.foo': 123, 'obj.bar': 'abc', 'obj.baz': true } }
can be written as
{ $append: { obj: { foo: 123, bar: 'abc', baz: true } }
https://docs.mongodb.com/manual/reference/operator/update/positional/
The positional $ operator identifies an element in an array to update without explicitly specifying the position of the element in the array.
Explanation for restorable
in README has a link to is-restorable
module, but the link does not exist anymore.
As mongodb throws errors when an update operation ($set, $push, etc...) has no fields, update()
function should follow the spec.
import { $bind } from "@sp2/updater";
type MapKeys = "key1";
interface Map { map: { [key in MapKeys]?: any } };
const { $set, $path } = $bind<Map>();
const operation = $set($path("map", "key1"));
error TS2345: Argument of type 'BoundDocumentPath2<Map, "map", "key1">' is not assignable to parameter of type '[string, any]'.
Type 'String & { keys: ["map", "key1"]; object: Map; }' is missing the following properties from type '[string, any]': 0, 1, pop, push, and 13 more.
const operation = $set($path("map", "key1"));
~~~~~~~~~~~~~~~~~~~~
using $pull with $bind causes the following type error
Type instantiation is excessively deep and possibly infinite. ts(2589)
CircleCI-based coveralls doesn't seem to work. We should move on to CoverAlls-GitHub.
https://github.com/marketplace/actions/coveralls-github-action
https://docs.mongodb.com/manual/reference/operator/update/pullAll/
The $pullAll operator removes all instances of the specified values from an existing array. Unlike the $pull operator that removes elements by specifying a query, $pullAll removes elements that match the listed values.
I forked sp2 into my git and executed static analysis by using two types of tools: SonarCloud and DeepScan. The results between them are different because the analysis policies and the rules used by each other are different.
I'm sure your review will be helpful to others.
Click the badge below to see results.
FYI
SonarCloud ( https://sonarcloud.io/ )
DeepScan ( https://deepscan.io/ )
Add benchmark tests on benchmark
module.
$set
operation$set
operation$push
operationtype TargetObject<T> = { foo: T };
function bar<T>(obj: TargetObject<T>, value: T) {
const { $set, $docPath } = $bind<TargetObject<T>>();
const path = $docPath("foo");
const operation = $set(path, value); // this occurs an error
}
Maybe type inference at 2nd argument of $set
is not working.
const operation = $set(path, value as NestedValue<TargetObject<T>, "foo", "","","","","","","","","");
This can suppress the error, but I want easier way.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.