Skip to content

Commit 6f303c2

Browse files
committed
Finalize the sharing feature
1 parent 2ea6356 commit 6f303c2

File tree

12 files changed

+548
-192
lines changed

12 files changed

+548
-192
lines changed

Diff for: src/adapter/admin/core.mjs

+8
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ export class Ref {
264264
async remove() {
265265
return this.collection.remove(this.id);
266266
}
267+
268+
as() {
269+
return this;
270+
}
267271
}
268272

269273
export class Doc {
@@ -310,6 +314,10 @@ export class Doc {
310314
const result = cb(this.data);
311315
if (result) return this;
312316
}
317+
318+
as() {
319+
return this;
320+
}
313321
}
314322

315323
export function all(adapter) {

Diff for: src/adapter/web/core.mjs

+8
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ export class Ref {
274274
async remove() {
275275
return this.collection.remove(this.id);
276276
}
277+
278+
as() {
279+
return this;
280+
}
277281
}
278282

279283
export class Doc {
@@ -320,6 +324,10 @@ export class Doc {
320324
const result = cb(this.data);
321325
if (result) return this;
322326
}
327+
328+
as() {
329+
return this;
330+
}
323331
}
324332

325333
export function all(adapter) {

Diff for: src/index.ts

+44-20
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,56 @@ export namespace Typesaurus {
5252
/**
5353
* Narrows doc type. If your doc has a variable model, the type will help you
5454
* narrow down the doc type to a specific data type.
55+
*
56+
* [Learn more on the docs website](https://typesaurus.com/types/typesaurus/#narrowdoc)
5557
*/
5658
export type NarrowDoc<
5759
OriginalDoc extends Core.Doc<any, any>,
5860
NarrowToModel extends Core.ModelObjectType,
5961
> = Core.NarrowDoc<OriginalDoc, NarrowToModel>;
6062

63+
/**
64+
* Shared ref type. Unlike regular ref, shared ref lacks methods which
65+
* type-safety depends on knowing the full type of the model: `set`, `upset`,
66+
* and `as`. The `collection` is also limited.
67+
*
68+
* [Learn more on the docs website](https://typesaurus.com/types/typesaurus/#sharedref)
69+
*/
70+
export type SharedRef<Model extends Core.ModelObjectType> = Shared.Ref<Model>;
71+
72+
/**
73+
* Shared doc type. Unlike regular doc, shared doc lacks methods which
74+
* type-safety depends on knowing the full type of the model: `set`, `upset`,
75+
* and `as`. The `ref` is also limited.
76+
*
77+
* [Learn more on the docs website](https://typesaurus.com/types/typesaurus/#shareddoc)
78+
*/
79+
export type SharedDoc<Model extends Core.ModelObjectType> = Shared.Doc<
80+
Model,
81+
Core.DocProps
82+
>;
83+
84+
/**
85+
* Shared ref or doc type. Unlike regular ref and doc, shared doc lacks
86+
* methods which type-safety depends on knowing the full type of the model:
87+
* `set`, `upset`, and `as`.
88+
*
89+
* [Learn more on the docs website](https://typesaurus.com/types/typesaurus/#sharedentity)
90+
*/
91+
export type SharedEntity<Model extends Core.ModelObjectType> =
92+
| Shared.Ref<Model>
93+
| Shared.Doc<Model, Core.DocProps>;
94+
95+
/**
96+
* Shared collection type. Unlike regular collection, shared collection lacks
97+
* methods which type-safety depends on knowing the full type of the model:
98+
* `add`, `set`, `upset`, and `update`.
99+
*
100+
* [Learn more on the docs website](https://typesaurus.com/types/typesaurus/#sharedcollection)
101+
*/
102+
export type SharedCollection<Model extends Core.ModelObjectType> =
103+
Shared.Collection<Model>;
104+
61105
/**
62106
* Concats models into single variable model type. Useful to define and export
63107
* variable models ouside of the centraliazed schema definition.
@@ -141,24 +185,4 @@ export namespace Typesaurus {
141185
WideModel: Core.NullifyModel<WideModel>;
142186
Flags: Flags;
143187
};
144-
145-
/**
146-
* TODO:
147-
*/
148-
export type SharedCollection<Model extends Core.ModelObjectType> =
149-
Shared.Collection<Model>;
150-
151-
/**
152-
* TODO:
153-
*/
154-
export type SharedRef<Model extends Core.ModelObjectType> = Shared.Ref<Model>;
155-
156-
/**
157-
* TODO:
158-
*/
159-
export type SharedDoc<Model extends Core.ModelObjectType> = Shared.Doc<
160-
Model,
161-
// TODO: Allow constraining the props type via Environment
162-
Core.DocProps
163-
>;
164188
}

Diff for: src/tests/doc.ts

+11
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,15 @@ describe("doc", () => {
266266
);
267267
});
268268
});
269+
270+
describe("as", () => {
271+
const doc = db.content.doc(db.content.id("42"), {
272+
type: "text",
273+
text: "Hello, world!",
274+
});
275+
276+
it("returns the doc", () => {
277+
expect(doc.as()).toBe(doc);
278+
});
279+
});
269280
});

Diff for: src/tests/ref.ts

+8
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,12 @@ describe("ref", () => {
2222
expect(userRef.collection.path).toBe("users");
2323
expect("get" in userRef.collection).toBe(true);
2424
});
25+
26+
describe("as", () => {
27+
const ref = db.users.ref(db.users.id("42"));
28+
29+
it("returns the ref", () => {
30+
expect(ref.as()).toBe(ref);
31+
});
32+
});
2533
});

0 commit comments

Comments
 (0)