Skip to content

Commit efa9dc1

Browse files
committed
Organize tysts
1 parent 7976747 commit efa9dc1

File tree

1 file changed

+62
-35
lines changed

1 file changed

+62
-35
lines changed

Diff for: src/tysts/core.ts

+62-35
Original file line numberDiff line numberDiff line change
@@ -302,33 +302,37 @@ const db = schema(
302302
{ server: { preferRest: true } },
303303
);
304304

305-
async function custom() {
305+
//#region schema
306+
async function schema_() {
306307
// Creating custom collection
308+
{
309+
// Via constant named property
307310

308-
// Via constant named property
311+
const doc = await db[customCollection].get(await db[customCollection].id());
312+
db[customCollection].get;
313+
db.customCollectionName.get;
314+
doc?.data.hello;
309315

310-
const doc = await db[customCollection].get(await db[customCollection].id());
311-
db[customCollection].get;
312-
db.customCollectionName.get;
313-
doc?.data.hello;
316+
// Via name variable
314317

315-
// Via name variable
318+
async function createDb(collection: string) {
319+
const customDB = schema(($) => ({
320+
users: $.collection<User>(),
321+
customCollection: $.collection<CustomCollection>().name(collection),
322+
}));
316323

317-
async function createDb(collection: string) {
318-
const customDB = schema(($) => ({
319-
users: $.collection<User>(),
320-
customCollection: $.collection<CustomCollection>().name(collection),
321-
}));
322-
323-
const doc = await customDB.customCollection.get(
324-
await customDB.customCollection.id(),
325-
);
324+
const doc = await customDB.customCollection.get(
325+
await customDB.customCollection.id(),
326+
);
326327

327-
doc?.data.hello;
328+
doc?.data.hello;
329+
}
328330
}
329331
}
332+
//#endregion
330333

331-
async function colleciton() {
334+
//#region collection
335+
async function collection() {
332336
/// Reading generic collections
333337

334338
//// Model as generic
@@ -365,8 +369,25 @@ async function colleciton() {
365369
]);
366370
things.map((nested) => nested.map((thing) => thing.data.name));
367371
}
372+
373+
// Count
374+
375+
const docsCount = await db.users.count();
376+
docsCount.toFixed();
377+
378+
const nestedDB = schema(($) => ({
379+
users: $.collection<User>().sub({
380+
settings: $.collection<{}>(),
381+
}),
382+
}));
383+
const nestedDocsCount = await nestedDB
384+
.users(nestedDB.users.id("whatever"))
385+
.settings.count();
386+
nestedDocsCount.toFixed();
368387
}
388+
//#endregion
369389

390+
//#region Ref
370391
async function ref() {
371392
/// Reading generic refs
372393

@@ -499,7 +520,9 @@ async function ref() {
499520
);
500521
}
501522
}
523+
//#endregion
502524

525+
//#region Doc
503526
async function doc() {
504527
const user = db.users.doc(db.users.id("sasha"), {
505528
name: "Sasha",
@@ -625,24 +648,9 @@ async function doc() {
625648
TypeEqual<typeof serverUser.data.alias, string | undefined | null>
626649
>(true);
627650
}
651+
//#endregion
628652

629-
async function collection() {
630-
// Count
631-
632-
const docsCount = await db.users.count();
633-
docsCount.toFixed();
634-
635-
const nestedDB = schema(($) => ({
636-
users: $.collection<User>().sub({
637-
settings: $.collection<{}>(),
638-
}),
639-
}));
640-
const nestedDocsCount = await nestedDB
641-
.users(nestedDB.users.id("whatever"))
642-
.settings.count();
643-
nestedDocsCount.toFixed();
644-
}
645-
653+
//#region get
646654
async function get() {
647655
const user = await db.users.get(db.users.id("sasha"));
648656
if (!user) return;
@@ -741,7 +749,9 @@ async function get() {
741749
true,
742750
);
743751
}
752+
//#endregion
744753

754+
//#region many
745755
async function many() {
746756
const [user] = await db.users.many([db.users.id("sasha")]);
747757
if (!user) return;
@@ -830,7 +840,9 @@ async function many() {
830840
true,
831841
);
832842
}
843+
//#endregion
833844

845+
//#region all
834846
async function all() {
835847
const [user] = await db.users.all();
836848
if (!user) return;
@@ -923,7 +935,9 @@ async function all() {
923935
true,
924936
);
925937
}
938+
//#endregion
926939

940+
//#region query
927941
async function query() {
928942
const [user] = await db.users.query(($) => $.field("name").eq("Sasha"));
929943
if (!user) return;
@@ -1326,7 +1340,9 @@ async function query() {
13261340
),
13271341
);
13281342
}
1343+
//#endregion
13291344

1345+
//#region
13301346
async function add() {
13311347
// Simple add
13321348

@@ -1390,7 +1406,9 @@ async function add() {
13901406
status: null,
13911407
}));
13921408
}
1409+
//#endregion
13931410

1411+
//#region set
13941412
async function set() {
13951413
// Simple set
13961414

@@ -1507,7 +1525,9 @@ async function set() {
15071525
status: null,
15081526
}));
15091527
}
1528+
//#endregion
15101529

1530+
//#region upset
15111531
async function upset() {
15121532
// Simple set
15131533

@@ -1688,7 +1708,9 @@ async function upset() {
16881708
status: null,
16891709
}));
16901710
}
1711+
//#endregion
16911712

1713+
//#region update
16921714
async function update() {
16931715
// Simple update
16941716

@@ -2503,6 +2525,7 @@ async function update() {
25032525
active: true;
25042526
}
25052527
}
2528+
//#endregion
25062529

25072530
async function sharedIds() {
25082531
interface Settings {
@@ -2521,6 +2544,7 @@ async function sharedIds() {
25212544

25222545
// @tysts-start: strict - with strictNullChecks disabled the tysts fail
25232546

2547+
//#region InferSchema
25242548
async function inferSchema() {
25252549
type Schema1 = Core.InferSchema<typeof db>;
25262550

@@ -2565,7 +2589,9 @@ async function inferSchema() {
25652589
>
25662590
>(true);
25672591
}
2592+
//#endregion
25682593

2594+
//#region NarrowDoc
25692595
async function narrowDoc() {
25702596
interface TwitterAccount {
25712597
type: "twitter";
@@ -2601,6 +2627,7 @@ async function narrowDoc() {
26012627
>
26022628
>(true);
26032629
}
2630+
//#endregion
26042631

26052632
async function edgeCases() {
26062633
interface ServerChapter {}

0 commit comments

Comments
 (0)