Skip to content

Commit 2f00610

Browse files
committed
More examples on the playground
1 parent 83f6c36 commit 2f00610

File tree

2 files changed

+103
-65
lines changed

2 files changed

+103
-65
lines changed

Diff for: src/index.ts

+44-50
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,16 @@ export namespace Typesaurus {
249249
[Key in keyof Model]?: UpdateModel<Model[Key]> | UpdateValue<Model, Key>
250250
}
251251

252+
/**
253+
*
254+
*/
255+
export type UpdateFields<Model> = UpdateField<Model>[]
256+
257+
/**
258+
*
259+
*/
260+
export type UpdateField<Model> = []
261+
252262
/**
253263
* The value types to use for update operation.
254264
*/
@@ -264,25 +274,6 @@ export namespace Typesaurus {
264274
: never
265275
: never
266276

267-
/**
268-
* Type of the data passed to the upset function. It extends the model
269-
* allowing to set server date field value.
270-
*/
271-
export type UpsetModel<
272-
Model,
273-
Environment extends RuntimeEnvironment | undefined
274-
> = {
275-
[Key in keyof Model]:
276-
| (Exclude<Model[Key], undefined> extends ServerDate // First, ensure ServerDate is properly set
277-
? Environment extends 'server' // Date can be used only in the node environment
278-
? Date | ServerDate
279-
: ServerDate
280-
: Model[Key] extends object // If it's an object, recursively pass through SetModel
281-
? UpsetModel<Model[Key], Environment>
282-
: Model[Key])
283-
| UpsetValue<Model[Key]>
284-
}
285-
286277
/**
287278
* The value types to use for upset operation.
288279
*/
@@ -374,7 +365,7 @@ export namespace Typesaurus {
374365
FirestoreWhereFilterOp,
375366
FirestoreOrderByDirection
376367
> =
377-
| OrderQuery<Model, Key, FirestoreWhereFilterOp>
368+
| OrderQuery<Model, Key, FirestoreOrderByDirection>
378369
| WhereQuery<Model, FirestoreWhereFilterOp>
379370
| LimitQuery
380371

@@ -517,7 +508,7 @@ export namespace Typesaurus {
517508
): Promise<void>
518509

519510
upset<Environment extends RuntimeEnvironment | undefined = undefined>(
520-
data: ($: WriteHelpers<Model>) => UpsetModel<Model, Environment>,
511+
data: ($: WriteHelpers<Model>) => WriteModel<Model, Environment>,
521512
options?: OperationOptions<Environment>
522513
): Promise<void>
523514

@@ -621,11 +612,11 @@ export namespace Typesaurus {
621612
* @param id - the id of the document to set
622613
* @param data - the document data
623614
*/
624-
upset(id: string, data: UpsetModel<Model, Environment>): void
615+
upset(id: string, data: WriteModel<Model, Environment>): void
625616

626617
upset(
627618
id: string,
628-
data: ($: WriteHelpers<Model>) => UpsetModel<Model, Environment>
619+
data: ($: WriteHelpers<Model>) => WriteModel<Model, Environment>
629620
): void
630621

631622
/**
@@ -757,12 +748,16 @@ export namespace Typesaurus {
757748

758749
get(id: string): PromiseWithGetSubscription<Model>
759750

760-
getMany(
751+
getMany<OnMissing extends OnMissingMode<unknown> | undefined = undefined>(
761752
ids: string[],
762753
options?: {
763-
onMissing: OnMissing<Model>
754+
onMissing: OnMissing
764755
}
765-
): PromiseWithListSubscription<Model>
756+
): OnMissing extends 'ignore' | undefined
757+
? PromiseWithListSubscription<Model>
758+
: OnMissing extends OnMissingCallback<infer OnMissingResult>
759+
? PromiseWithListSubscription<Model | OnMissingResult>
760+
: never
766761

767762
query(
768763
queries: (
@@ -782,12 +777,12 @@ export namespace Typesaurus {
782777
add<Environment extends RuntimeEnvironment | undefined = undefined>(
783778
data: WriteModel<Model, Environment>,
784779
options?: OperationOptions<Environment>
785-
): Promise<PlainRef<Model>>
780+
): Promise<RichRef<Model>>
786781

787782
add<Environment extends RuntimeEnvironment | undefined = undefined>(
788783
data: ($: WriteHelpers<Model>) => WriteModel<Model, Environment>,
789784
options?: OperationOptions<Environment>
790-
): Promise<PlainRef<Model>>
785+
): Promise<RichRef<Model>>
791786

792787
set<Environment extends RuntimeEnvironment | undefined = undefined>(
793788
id: string,
@@ -803,31 +798,33 @@ export namespace Typesaurus {
803798

804799
update<Environment extends RuntimeEnvironment | undefined = undefined>(
805800
id: string,
806-
data: UpdateModel<Model>,
801+
data: UpdateModel<Model> | UpdateFields<Model>,
807802
options?: { as: Environment }
808803
): Promise<void>
809804

810805
update<Environment extends RuntimeEnvironment | undefined = undefined>(
811806
id: string,
812-
data: ($: WriteHelpers<Model>) => UpdateModel<Model>,
807+
data: (
808+
$: WriteHelpers<Model>
809+
) => UpdateModel<Model> | UpdateFields<Model>,
813810
options?: OperationOptions<Environment>
814811
): Promise<void>
815812

816813
upset<Environment extends RuntimeEnvironment | undefined = undefined>(
817814
id: string,
818-
data: UpdateModel<Model>,
815+
data: WriteModel<Model, Environment>,
819816
options?: { as: Environment }
820817
): Promise<void>
821818

822819
upset<Environment extends RuntimeEnvironment | undefined = undefined>(
823820
id: string,
824-
data: ($: WriteHelpers<Model>) => UpsetModel<Model, Environment>,
821+
data: ($: WriteHelpers<Model>) => WriteModel<Model, Environment>,
825822
options?: OperationOptions<Environment>
826823
): Promise<void>
827824

828825
remove(id: string): Promise<void>
829826

830-
ref(id: string): PlainRef<Model>
827+
ref(id: string): RichRef<Model>
831828

832829
doc(id: string, data: Model): PlainDoc<Model>
833830
}
@@ -911,29 +908,24 @@ export namespace Typesaurus {
911908
/**
912909
* 1-level deep
913910
*/
914-
GroupsDB extends DB<infer Schema, unknown, unknown>
915-
? // Infer the nested (1) schema
916-
Schema[keyof Schema] extends
911+
GroupsDB extends DB<infer Schema, unknown, unknown> // Infer the nested (1) schema
912+
? Schema[keyof Schema] extends
917913
| PlainCollection<infer _>
918-
| NestedPlainCollection<infer _, infer NestedSchema>
919-
? // Get the models for the given (1) level
920-
ExtractDBModels<DB<NestedSchema, unknown, unknown>>
914+
| NestedPlainCollection<infer _, infer NestedSchema> // Get the models for the given (1) level
915+
? ExtractDBModels<DB<NestedSchema, unknown, unknown>>
921916
: {}
922917
: {},
923918
/**
924919
* 2-levels deep
925920
*/
926-
GroupsDB extends DB<infer Schema, unknown, unknown>
927-
? // Infer the nested (1) schema
928-
Schema[keyof Schema] extends
921+
GroupsDB extends DB<infer Schema, unknown, unknown> // Infer the nested (1) schema
922+
? Schema[keyof Schema] extends
929923
| PlainCollection<infer _>
930-
| NestedPlainCollection<infer _, infer NestedSchema1>
931-
? // Infer the nested (2) schema
932-
NestedSchema1[keyof NestedSchema1] extends
924+
| NestedPlainCollection<infer _, infer NestedSchema1> // Infer the nested (2) schema
925+
? NestedSchema1[keyof NestedSchema1] extends
933926
| PlainCollection<infer _>
934-
| NestedPlainCollection<infer _, infer NestedSchema2>
935-
? // Get the models for the given (2) level
936-
ExtractDBModels<DB<NestedSchema2, unknown, unknown>>
927+
| NestedPlainCollection<infer _, infer NestedSchema2> // Get the models for the given (2) level
928+
? ExtractDBModels<DB<NestedSchema2, unknown, unknown>>
937929
: {}
938930
: {}
939931
: {}
@@ -994,9 +986,11 @@ export namespace Typesaurus {
994986
>
995987
}
996988

997-
export type OnMissing<Model> = ((id: string) => Model) | 'ignore'
989+
export type OnMissingMode<Model> = OnMissingCallback<Model> | 'ignore'
990+
991+
export type OnMissingCallback<Model> = (id: string) => Model
998992

999993
export interface OnMissingOptions<Model> {
1000-
onMissing?: OnMissing<Model>
994+
onMissing?: OnMissingMode<Model>
1001995
}
1002996
}

Diff for: src/plaground.ts

+59-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ describe('Typesaurus core', () => {
1111
// Flat schema
1212
const db = schema(($) => ({
1313
users: $.collection<User>(),
14-
posts: $.collection<Post>()
14+
posts: $.collection<Post>(),
15+
accounts: $.collection<Account>()
1516
}))
1617

1718
// With subcollectios
@@ -62,6 +63,7 @@ describe('Typesaurus core', () => {
6263
// Quering
6364
const users = await db.users.query(($) => [
6465
$.where('name', '==', 'Sasha'),
66+
// @ts-expect-error
6567
$.where(['contacts', 'emal'], '==', '[email protected]'),
6668
$.order('name'),
6769
$.limit(1)
@@ -74,33 +76,57 @@ describe('Typesaurus core', () => {
7476
const sasha = await sashaPromise
7577

7678
// 2. Subscribe to document
77-
sashaPromise
78-
.on((user, info) => {
79+
const sashaUnsubscribe1 = sashaPromise
80+
.on((user) => {
7981
// Fresh user data
8082
user
8183
})
8284
.catch((error) => {
8385
// Do something with error
8486
})
8587

88+
sashaUnsubscribe1()
89+
8690
// Subscribe to a single document (real-time)
87-
const sashaUnsubscribe = db.users
91+
const sashaUnsubscribe2 = db.users
8892
.get('sasha')
89-
.on((user, info) => {
93+
.on((user) => {
9094
// Fresh user data
9195
user
9296
})
9397
.catch((error) => {
9498
// Do something with error
9599
})
96100

97-
sashaUnsubscribe()
101+
sashaUnsubscribe2()
102+
103+
db.users.getMany(['sasha', 'lesha', 'tati']).on((users) => {
104+
// Fresh users data
105+
})
98106

99107
db.users
100108
.getMany(['sasha', 'lesha', 'tati'], { onMissing: 'ignore' })
101-
.on((users) => {})
109+
.on((users) => {
110+
// Fresh users data
111+
})
112+
113+
db.users
114+
.getMany(['sasha', 'lesha', 'tati'], { onMissing: () => null })
115+
.on((users) => {
116+
// Fresh users data
117+
})
118+
119+
db.users
120+
.getMany(['sasha', 'lesha', 'tati'], {
121+
onMissing: () => {
122+
throw new Error('Oh no')
123+
}
124+
})
125+
.on((users) => {
126+
// Fresh users data
127+
})
102128

103-
await db.users.getMany(['sasha', 'lesha', 'tati'], {
129+
const manyUsers = await db.users.getMany(['sasha', 'lesha', 'tati'], {
104130
onMissing: 'ignore'
105131
})
106132

@@ -114,6 +140,8 @@ describe('Typesaurus core', () => {
114140
.on((users) => {})
115141
.catch((error) => {})
116142

143+
offQuery()
144+
117145
interface User {
118146
name: string
119147
contacts: {
@@ -124,6 +152,7 @@ describe('Typesaurus core', () => {
124152
createdAt: Typesaurus.ServerDate
125153
}
126154

155+
// @ts-expect-error - createdDate is a server date
127156
await db.users.add(($) => ({
128157
name: 'Sasha',
129158
contacts: { email: '[email protected]' },
@@ -162,11 +191,17 @@ describe('Typesaurus core', () => {
162191
name: 'Alexander'
163192
})
164193

165-
// Merge, unlike update it will NOT trigger error
194+
// Force update integrity
195+
196+
await db.accounts.update('sasha', [])
197+
198+
// upset, unlike update it will NOT trigger error
166199
// if the 'sasha` document don't exists.
167-
await db.users.upset('sasha', {
168-
name: 'Sasha'
169-
})
200+
await db.users.upset('sasha', ($) => ({
201+
name: 'Sasha',
202+
contacts: { email: '[email protected]' },
203+
createdAt: $.serverDate()
204+
}))
170205

171206
// Remove
172207
await db.users.remove('sasha')
@@ -199,11 +234,11 @@ describe('Typesaurus core', () => {
199234
as: 'server'
200235
})
201236

202-
// const sasha = db.users.ref('sasha')
237+
const sashaRef = db.users.ref('sasha')
203238

204-
await sasha.remove()
239+
await sashaRef.remove()
205240

206-
await sasha.update({ name: 'Sasha' })
241+
await sashaRef.update({ name: 'Sasha' })
207242

208243
await nestedDB.groups.likes
209244

@@ -213,6 +248,7 @@ describe('Typesaurus core', () => {
213248
return $.execute(db.users).get('asd')
214249
}).then(($) => {
215250
$.execute(db.users).update('asd', {
251+
// @ts-expect-error
216252
ame: 'Alexander'
217253
})
218254

@@ -244,3 +280,11 @@ interface PostLike extends Like {
244280
interface Like {
245281
userId: string
246282
}
283+
284+
interface Account {
285+
name: string
286+
contacts: {
287+
email: string
288+
phone?: string
289+
}
290+
}

0 commit comments

Comments
 (0)