@flink-app/flink 0.12.1-alpha.4 → 0.12.1-alpha.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/FlinkRepo.d.ts +10 -2
- package/package.json +2 -2
- package/spec/FlinkRepo.spec.ts +11 -0
- package/src/FlinkRepo.ts +8 -2
package/dist/src/FlinkRepo.d.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { Collection, Db, Document, ObjectId } from "mongodb";
|
|
2
2
|
import { FlinkContext } from "./FlinkContext";
|
|
3
|
+
/**
|
|
4
|
+
* Partial model to have intellisense for partial updates but
|
|
5
|
+
* also allow any other properties to be set such as nested objects etc.
|
|
6
|
+
*/
|
|
7
|
+
type PartialModel<Model> = Partial<Model> & {
|
|
8
|
+
[x: string]: any;
|
|
9
|
+
};
|
|
3
10
|
export declare abstract class FlinkRepo<C extends FlinkContext, Model extends Document> {
|
|
4
11
|
private collectionName;
|
|
5
12
|
private db;
|
|
@@ -14,9 +21,10 @@ export declare abstract class FlinkRepo<C extends FlinkContext, Model extends Do
|
|
|
14
21
|
create<C = Omit<Model, "_id">>(model: C): Promise<C & {
|
|
15
22
|
_id: string;
|
|
16
23
|
}>;
|
|
17
|
-
updateOne(id: string | ObjectId, model:
|
|
18
|
-
updateMany<U =
|
|
24
|
+
updateOne(id: string | ObjectId, model: PartialModel<Model>): Promise<Model | null>;
|
|
25
|
+
updateMany<U = PartialModel<Model>>(query: any, model: U): Promise<number>;
|
|
19
26
|
deleteById(id: string | ObjectId): Promise<number>;
|
|
20
27
|
private buildId;
|
|
21
28
|
private objectIdToString;
|
|
22
29
|
}
|
|
30
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flink-app/flink",
|
|
3
|
-
"version": "0.12.1-alpha.
|
|
3
|
+
"version": "0.12.1-alpha.5",
|
|
4
4
|
"description": "Typescript only framework for creating REST-like APIs on top of Express and mongodb",
|
|
5
5
|
"types": "dist/src/index.d.ts",
|
|
6
6
|
"main": "dist/src/index.js",
|
|
@@ -66,5 +66,5 @@
|
|
|
66
66
|
"rimraf": "^3.0.2",
|
|
67
67
|
"ts-node": "^9.1.1"
|
|
68
68
|
},
|
|
69
|
-
"gitHead": "
|
|
69
|
+
"gitHead": "1ff7b8cf9b30287d3be72af96c5767e90a562fbf"
|
|
70
70
|
}
|
package/spec/FlinkRepo.spec.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { FlinkRepo } from "../src/FlinkRepo";
|
|
|
4
4
|
interface Model {
|
|
5
5
|
_id: string;
|
|
6
6
|
name: string;
|
|
7
|
+
nested?: { field: number };
|
|
7
8
|
}
|
|
8
9
|
|
|
9
10
|
class Repo extends FlinkRepo<any, Model> {}
|
|
@@ -61,9 +62,19 @@ describe("FlinkRepo", () => {
|
|
|
61
62
|
|
|
62
63
|
const updatedDoc = await repo.updateOne(createdDoc._id + "", {
|
|
63
64
|
name: "foo",
|
|
65
|
+
"nested.field": 1,
|
|
64
66
|
});
|
|
65
67
|
|
|
66
68
|
expect(updatedDoc).toBeDefined();
|
|
67
69
|
expect(updatedDoc?.name).toBe("foo");
|
|
70
|
+
expect(updatedDoc?.nested?.field).toBe(1);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("should update many documents", async () => {
|
|
74
|
+
await collection.insertMany([{ name: "foo" }, { name: "foo" }, { name: "foo" }]);
|
|
75
|
+
|
|
76
|
+
const updatedCount = await repo.updateMany({ name: "foo" }, { name: "bar", "nested.field": 1 });
|
|
77
|
+
|
|
78
|
+
expect(updatedCount).toBe(3);
|
|
68
79
|
});
|
|
69
80
|
});
|
package/src/FlinkRepo.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { Collection, Db, Document, InsertOneResult, ObjectId } from "mongodb";
|
|
2
2
|
import { FlinkContext } from "./FlinkContext";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Partial model to have intellisense for partial updates but
|
|
6
|
+
* also allow any other properties to be set such as nested objects etc.
|
|
7
|
+
*/
|
|
8
|
+
type PartialModel<Model> = Partial<Model> & { [x: string]: any };
|
|
9
|
+
|
|
4
10
|
export abstract class FlinkRepo<C extends FlinkContext, Model extends Document> {
|
|
5
11
|
collection: Collection;
|
|
6
12
|
|
|
@@ -45,7 +51,7 @@ export abstract class FlinkRepo<C extends FlinkContext, Model extends Document>
|
|
|
45
51
|
return { ...model, _id: result.insertedId.toString() };
|
|
46
52
|
}
|
|
47
53
|
|
|
48
|
-
async updateOne(id: string | ObjectId, model:
|
|
54
|
+
async updateOne(id: string | ObjectId, model: PartialModel<Model>): Promise<Model | null> {
|
|
49
55
|
const oid = this.buildId(id);
|
|
50
56
|
|
|
51
57
|
await this.collection.updateOne({ _id: oid }, { $set: model });
|
|
@@ -58,7 +64,7 @@ export abstract class FlinkRepo<C extends FlinkContext, Model extends Document>
|
|
|
58
64
|
return null;
|
|
59
65
|
}
|
|
60
66
|
|
|
61
|
-
async updateMany<U =
|
|
67
|
+
async updateMany<U = PartialModel<Model>>(query: any, model: U): Promise<number> {
|
|
62
68
|
const { modifiedCount } = await this.collection.updateMany(query, {
|
|
63
69
|
$set: model as any,
|
|
64
70
|
});
|