@squidcloud/backend 1.0.0 → 1.0.1

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.
Files changed (45) hide show
  1. package/dist/backend/src/service.d.ts +10 -1
  2. package/dist/common/src/api-call.context.d.ts +17 -0
  3. package/dist/common/src/application.types.d.ts +13 -21
  4. package/dist/common/src/backend-run.types.d.ts +60 -0
  5. package/dist/common/src/bundle-api.types.d.ts +21 -41
  6. package/dist/common/src/context.types.d.ts +0 -1
  7. package/dist/common/src/document.types.d.ts +8 -8
  8. package/dist/common/src/executable.context.d.ts +4 -0
  9. package/dist/common/src/graphql.context.d.ts +14 -0
  10. package/dist/common/src/index.d.ts +16 -7
  11. package/dist/common/src/integration.types.d.ts +3 -1
  12. package/dist/common/src/metrics.types.d.ts +55 -42
  13. package/dist/common/src/mutation.context.d.ts +1 -4
  14. package/dist/common/src/mutation.types.d.ts +8 -4
  15. package/dist/common/src/named-query.context.d.ts +4 -0
  16. package/dist/common/src/query/query-context.d.ts +1 -4
  17. package/dist/common/src/query/simple-query-builder.d.ts +2 -2
  18. package/dist/common/src/query.types.d.ts +4 -4
  19. package/dist/common/src/regions.d.ts +4 -0
  20. package/dist/common/src/schema/schema.types.d.ts +20 -1
  21. package/dist/common/src/secret.types.d.ts +9 -1
  22. package/dist/common/src/socket.types.d.ts +4 -4
  23. package/dist/common/src/trigger.types.d.ts +4 -6
  24. package/dist/common/src/utils/assert.d.ts +2 -0
  25. package/dist/common/src/utils/id.d.ts +1 -0
  26. package/dist/common/src/utils/isDefined.d.ts +1 -0
  27. package/dist/common/src/utils/object.d.ts +5 -3
  28. package/dist/common/src/utils/serialization.d.ts +1 -0
  29. package/dist/common/src/utils/transforms.d.ts +18 -0
  30. package/dist/common/src/utils/url.d.ts +1 -1
  31. package/dist/index.js +5 -5
  32. package/dist/typescript-client/src/collection-reference.d.ts +4 -3
  33. package/dist/typescript-client/src/collection-reference.factory.d.ts +6 -3
  34. package/dist/typescript-client/src/data.manager.d.ts +40 -18
  35. package/dist/typescript-client/src/destruct.manager.d.ts +3 -1
  36. package/dist/typescript-client/src/document-identity.service.d.ts +12 -0
  37. package/dist/typescript-client/src/document-reference.d.ts +7 -4
  38. package/dist/typescript-client/src/document-reference.factory.d.ts +2 -2
  39. package/dist/typescript-client/src/document-store.d.ts +11 -0
  40. package/dist/typescript-client/src/mutation/mutation-sender.d.ts +3 -3
  41. package/dist/typescript-client/src/query/query-builder.factory.d.ts +20 -17
  42. package/dist/typescript-client/src/query/query-subscription.manager.d.ts +14 -11
  43. package/dist/typescript-client/src/squid.d.ts +6 -5
  44. package/package.json +1 -1
  45. package/dist/typescript-client/src/db.dao.d.ts +0 -20
@@ -1,16 +1,17 @@
1
- import { DocId, DocIdObj, IntegrationId, UserFacingDocType } from '@squidcloud/common';
1
+ import { DocId, DocIdObj, DocumentData, IdResolutionMap, IntegrationId } from '@squidcloud/common';
2
2
  import { DocumentReference } from './document-reference';
3
3
  import { DocumentReferenceFactory } from './document-reference.factory';
4
4
  import { QueryBuilder, QueryBuilderFactory } from './query/query-builder.factory';
5
5
  import { Alias } from './query/query.types';
6
- export declare class CollectionReference<T extends UserFacingDocType> {
6
+ export declare class CollectionReference<T extends DocumentData> {
7
7
  private readonly collectionName;
8
8
  private integrationId;
9
9
  private readonly documentReferenceFactory;
10
10
  private readonly queryBuilderFactory;
11
11
  private readonly documents;
12
12
  constructor(collectionName: string, integrationId: IntegrationId, documentReferenceFactory: DocumentReferenceFactory, queryBuilderFactory: QueryBuilderFactory);
13
- doc(docId: DocId | DocIdObj): DocumentReference<T>;
13
+ doc(docId?: DocId | DocIdObj): DocumentReference<T>;
14
14
  query(): Omit<QueryBuilder<T, any, Record<Alias, DocumentReference<T>>, false>, 'join'>;
15
15
  joinQuery<A extends Alias>(alias: A): QueryBuilder<T, A, Record<A, DocumentReference<T>>, true>;
16
+ migrateDocIds(idResolutionMap: IdResolutionMap): void;
16
17
  }
@@ -1,11 +1,14 @@
1
- import { CollectionName, IntegrationId, UserFacingDocType } from '@squidcloud/common';
1
+ import { CollectionName, DocumentData, IntegrationId } from '@squidcloud/common';
2
2
  import { CollectionReference } from './collection-reference';
3
+ import DocumentIdentityService from './document-identity.service';
3
4
  import { DocumentReferenceFactory } from './document-reference.factory';
4
5
  import { QueryBuilderFactory } from './query/query-builder.factory';
5
6
  export declare class CollectionReferenceFactory {
6
7
  private readonly documentReferenceFactory;
7
8
  private readonly queryBuilderFactory;
9
+ private readonly documentIdentityService;
8
10
  private readonly collections;
9
- constructor(documentReferenceFactory: DocumentReferenceFactory, queryBuilderFactory: QueryBuilderFactory);
10
- get<T extends UserFacingDocType>(collectionName: CollectionName, integrationId: IntegrationId): CollectionReference<T>;
11
+ constructor(documentReferenceFactory: DocumentReferenceFactory, queryBuilderFactory: QueryBuilderFactory, documentIdentityService: DocumentIdentityService);
12
+ get<T extends DocumentData>(collectionName: CollectionName, integrationId: IntegrationId): CollectionReference<T>;
13
+ private migrateDocIds;
11
14
  }
@@ -1,20 +1,26 @@
1
- import { DocType, FullDocIdStr, LockManager, Mutation } from '@squidcloud/common';
2
- import { DbDao } from './db.dao';
1
+ import { DocTimestamp, LockManager, Mutation, SquidDocId, SquidDocument } from '@squidcloud/common';
3
2
  import { DestructManager } from './destruct.manager';
3
+ import DocumentIdentityService from './document-identity.service';
4
+ import { DocumentStore } from './document-store';
4
5
  import { MutationSender } from './mutation/mutation-sender';
5
6
  import { QueryBuilderFactory } from './query/query-builder.factory';
6
7
  import { QuerySubscriptionManager } from './query/query-subscription.manager';
7
8
  import { SocketManagerInterface } from './socket.manager';
8
9
  import { TransactionId } from './types';
10
+ export interface DocTimestampMetadata {
11
+ timestamp: DocTimestamp;
12
+ expireTimestamp?: number;
13
+ }
9
14
  export declare class DataManager {
10
- private readonly dbDao;
15
+ private readonly documentStore;
11
16
  private readonly mutationSender;
12
17
  private readonly socketManager;
13
18
  private readonly querySubscriptionManager;
14
19
  private readonly queryBuilderFactory;
15
20
  private readonly lockManager;
16
21
  private readonly destructManager;
17
- private readonly fullDocIdToLocalTimestamp;
22
+ private readonly documentIdentityService;
23
+ private readonly docIdToLocalTimestamp;
18
24
  private currentTransactionId;
19
25
  /**
20
26
  * During a batch, any update to a document that may trigger an update to a query is collected here and once the batch
@@ -31,7 +37,7 @@ export declare class DataManager {
31
37
  * Eventually, this map is used as a gatekeeper for preventing old versions of a document (based on timestamp) to
32
38
  * appear on the client.
33
39
  */
34
- private readonly docToTimestamp;
40
+ private readonly docIdToServerTimestamp;
35
41
  /**
36
42
  * In the case of a local change (outgoing change) without a server timestamp, an incoming server update cannot be
37
43
  * applied and needs to be queued until the local state allows it. In this case the incoming update will be queued in
@@ -53,7 +59,7 @@ export declare class DataManager {
53
59
  * cannot accept the server change and will wait for another update from the server. For this purpose, timestamp
54
60
  * will be stored in docsToTimestamp for ~20 more seconds.
55
61
  *
56
- * Note: Only one entry per fullDocId can be with sentToServer=false. This is true since all updates to the same doc
62
+ * Note: Only one entry per squidDocId can be with sentToServer=false. This is true since all updates to the same doc
57
63
  * in the same batch are appended (and reduced) to the same outgoing mutation object.
58
64
  */
59
65
  private readonly pendingOutgoingMutations;
@@ -73,10 +79,11 @@ export declare class DataManager {
73
79
  private readonly outgoingMutationsEmpty;
74
80
  private readonly docsToResync;
75
81
  private deleteExpiredTimestampsInterval;
76
- private handleIncomingMessages;
77
- constructor(dbDao: DbDao, mutationSender: MutationSender, socketManager: SocketManagerInterface, querySubscriptionManager: QuerySubscriptionManager, queryBuilderFactory: QueryBuilderFactory, lockManager: LockManager, destructManager: DestructManager);
78
- getProperties(fullDocIdStr: FullDocIdStr): DocType | undefined;
79
- isDirty(fullDocIdStr: FullDocIdStr): boolean;
82
+ private handleIncomingMessagesForTests;
83
+ constructor(documentStore: DocumentStore, mutationSender: MutationSender, socketManager: SocketManagerInterface, querySubscriptionManager: QuerySubscriptionManager, queryBuilderFactory: QueryBuilderFactory, lockManager: LockManager, destructManager: DestructManager, documentIdentityService: DocumentIdentityService);
84
+ getProperties(squidDocId: SquidDocId): SquidDocument | undefined;
85
+ /** Whether a document has changes that are out of sync with the server. */
86
+ isDirty(squidDocId: SquidDocId): boolean;
80
87
  /**
81
88
  * Runs the provided function without sending mutations to the server while collecting the updates to the different
82
89
  * queries. Local updates will still be applied. Once the batch finishes, all the updates will be sent to the server
@@ -105,7 +112,7 @@ export declare class DataManager {
105
112
  private stopDeleteExpiredTimestampsJob;
106
113
  /**
107
114
  * Removes entries from the docToTimestamp map for all the documents that are no longer relevant for this client.
108
- * If a document exists locally, remove it as well.
115
+ * If a document is currently tracked, we forget it.
109
116
  * Cases a document is considered not relevant anymore:
110
117
  * 1 - There are no outgoing or incoming updates for this document AND:
111
118
  * a - The document was deleted on the server and this client already received a notification about it OR
@@ -113,17 +120,29 @@ export declare class DataManager {
113
120
  */
114
121
  private startDeleteExpiredTimestampsJob;
115
122
  /**
116
- * Returns whether it is safe to remove the document locally. A document is safe to be removed if:
117
- * 1 - It has no pending incoming mutations
118
- * 2 - All the outgoing mutations already have timestamp
119
- * 3 - The document is orphan - there is no outgoing query for it
123
+ * Whether the document is tracked by any pending mutations or ongoing queries.
124
+ */
125
+ isTracked(squidDocId: string): boolean;
126
+ /**
127
+ * Whether a document exists locally, but is no longer tracked by any mutations or queries. This is often the case
128
+ * for in-memory DocumentReferences that are not part of any query.
129
+ */
130
+ isForgotten(squidDocId: SquidDocId): boolean;
131
+ /**
132
+ * Whether a document only exists locally. This means that the document has never by sent to or received from the
133
+ * server.
134
+ */
135
+ isLocalOnly(squidDocId: SquidDocId): boolean;
136
+ /**
137
+ * Whether the document has even been acknowledged by the server. Acknowledgements occur when an incoming query or
138
+ * mutation is received, and when an outgoing mutation is acknowledged.
120
139
  */
121
- private isDocumentSafeForDelete;
140
+ hasBeenAcknowledged(squidDocId: SquidDocId): boolean;
122
141
  /**
123
142
  * Updates the document with the new properties, returns true if an update was done or false in case the doc did not
124
143
  * change.
125
144
  */
126
- private applyLocalDbUpdateFromSnapshot;
145
+ private updateDocumentFromSnapshot;
127
146
  private finishTransaction;
128
147
  private sendAllUnsentOutgoingMutations;
129
148
  private sendMutationsForIntegration;
@@ -136,5 +155,8 @@ export declare class DataManager {
136
155
  * An orphan document should not stay locally since it may be stale after some time.
137
156
  */
138
157
  private handleOrphanDocs;
139
- private removeDocumentLocally;
158
+ private acknowledgeDocument;
159
+ private setExpiration;
160
+ private forgetDocument;
161
+ private migrateDocIds;
140
162
  }
@@ -1,7 +1,9 @@
1
1
  export type DestructorFn = () => Promise<void> | void;
2
2
  export declare class DestructManager {
3
+ private readonly predestructors;
3
4
  private readonly destructors;
4
5
  private isDestructed;
5
- registerDestructor(fn: DestructorFn): void;
6
+ onPreDestruct(fn: DestructorFn): void;
7
+ onDestruct(fn: DestructorFn): void;
6
8
  destruct(): Promise<void>;
7
9
  }
@@ -0,0 +1,12 @@
1
+ import { IdResolutionMap } from '@squidcloud/common';
2
+ import { Observable } from 'rxjs';
3
+ import { DestructManager } from './destruct.manager';
4
+ import { DocumentStore } from './document-store';
5
+ export default class DocumentIdentityService {
6
+ private readonly documentStore;
7
+ private readonly destructManager;
8
+ private readonly changeNotifier;
9
+ constructor(documentStore: DocumentStore, destructManager: DestructManager);
10
+ migrate(idResolutionMap: IdResolutionMap): void;
11
+ observeChanges(): Observable<IdResolutionMap>;
12
+ }
@@ -1,18 +1,21 @@
1
- import { FullDocIdStr, Paths, UserFacingDocType } from '@squidcloud/common';
1
+ import { DocumentData, IdResolutionMap, Paths, SquidDocId } from '@squidcloud/common';
2
2
  import { DataManager } from './data.manager';
3
3
  import { QueryBuilderFactory } from './query/query-builder.factory';
4
4
  import { TransactionId } from './types';
5
- export declare class DocumentReference<T extends UserFacingDocType = any> {
6
- readonly fullDocId: FullDocIdStr;
5
+ export declare class DocumentReference<T extends DocumentData = any> {
6
+ private _squidDocId;
7
7
  private readonly dataManager;
8
8
  private readonly queryBuilderFactory;
9
- constructor(fullDocId: FullDocIdStr, dataManager: DataManager, queryBuilderFactory: QueryBuilderFactory);
9
+ constructor(_squidDocId: SquidDocId, dataManager: DataManager, queryBuilderFactory: QueryBuilderFactory);
10
+ get squidDocId(): SquidDocId;
10
11
  data(): T;
11
12
  get(): Promise<DocumentReference<T> | undefined>;
12
13
  isDirty(): boolean;
14
+ private isTracked;
13
15
  update(data: Partial<Record<Paths<T>, any>>, transactionId?: TransactionId): Promise<void>;
14
16
  setInPath(path: Paths<T>, value: any, transactionId?: TransactionId): Promise<void>;
15
17
  deleteInPath(path: Paths<T>, transactionId?: TransactionId): Promise<void>;
16
18
  insert(data: T, transactionId?: TransactionId): Promise<void>;
17
19
  delete(transactionId?: TransactionId): Promise<void>;
20
+ migrateDocIds(idResolutionMap: IdResolutionMap): void;
18
21
  }
@@ -1,9 +1,9 @@
1
- import { FullDocIdStr } from '@squidcloud/common';
1
+ import { SquidDocId } from '@squidcloud/common';
2
2
  import { DataManager } from './data.manager';
3
3
  import { DocumentReference } from './document-reference';
4
4
  import { QueryBuilderFactory } from './query/query-builder.factory';
5
5
  export declare class DocumentReferenceFactory {
6
6
  private dataManager;
7
- create(fullDocId: FullDocIdStr, queryBuilderFactory: QueryBuilderFactory): DocumentReference;
7
+ create(squidDocId: SquidDocId, queryBuilderFactory: QueryBuilderFactory): DocumentReference;
8
8
  setDataManager(dataManager: DataManager): void;
9
9
  }
@@ -0,0 +1,11 @@
1
+ import { Query, SquidDocId, SquidDocument } from '@squidcloud/common';
2
+ export declare class DocumentStore {
3
+ private readonly squidDocIdToDoc;
4
+ saveDocument(squidDocId: SquidDocId, properties: SquidDocument | undefined): SquidDocument | undefined;
5
+ hasData(squidDocId: SquidDocId): boolean;
6
+ getDocument(squidDocId: SquidDocId): SquidDocument;
7
+ getDocumentOrUndefined(squidDocId: SquidDocId): SquidDocument | undefined;
8
+ sortAndLimitDocs(docIdSet: Set<SquidDocId> | undefined, query: Query): Array<SquidDocument>;
9
+ private removeInternalProperties;
10
+ migrateDocId(squidDocId: SquidDocId, newSquidDocId: SquidDocId): void;
11
+ }
@@ -1,11 +1,11 @@
1
- import { ClientId, DocTimestamp, IntegrationId, LockManager, Mutation } from '@squidcloud/common';
2
- import { RpcManager } from '../rpc.manager';
1
+ import { ClientId, IntegrationId, LockManager, MutateResponse, Mutation } from '@squidcloud/common';
3
2
  import { QuerySubscriptionManager } from '../query/query-subscription.manager';
3
+ import { RpcManager } from '../rpc.manager';
4
4
  export declare class MutationSender {
5
5
  private readonly clientId;
6
6
  private readonly rpcManager;
7
7
  private readonly lockManager;
8
8
  private readonly querySubscriptionManager;
9
9
  constructor(clientId: ClientId, rpcManager: RpcManager, lockManager: LockManager, querySubscriptionManager: QuerySubscriptionManager);
10
- sendMutations(mutations: Array<Mutation>, integrationId: IntegrationId): Promise<DocTimestamp>;
10
+ sendMutations(mutations: Array<Mutation>, integrationId: IntegrationId): Promise<MutateResponse>;
11
11
  }
@@ -1,5 +1,6 @@
1
- import { CollectionName, FieldName, FullDocIdStr, IntegrationId, Operator, PrimitiveFieldType, Query, UserFacingDocType } from '@squidcloud/common';
1
+ import { CollectionName, DocumentData, FieldName, IntegrationId, Operator, PrimitiveFieldType, Query, SquidDocId } from '@squidcloud/common';
2
2
  import { Observable } from 'rxjs';
3
+ import DocumentIdentityService from '../document-identity.service';
3
4
  import { DocumentReference } from '../document-reference';
4
5
  import { DocumentReferenceFactory } from '../document-reference.factory';
5
6
  import { QuerySubscriptionManager } from './query-subscription.manager';
@@ -7,12 +8,13 @@ import { Alias } from './query.types';
7
8
  export declare class QueryBuilderFactory {
8
9
  private readonly querySubscriptionManager;
9
10
  private readonly documentReferenceFactory;
10
- constructor(querySubscriptionManager: QuerySubscriptionManager, documentReferenceFactory: DocumentReferenceFactory);
11
- getForDocument<MyDocType extends UserFacingDocType, ReturnType extends Record<Alias, DocumentReference<MyDocType>>>(fullDocId: FullDocIdStr): Omit<QueryBuilder<MyDocType, any, ReturnType, false>, 'join'>;
12
- get<MyDocType extends UserFacingDocType, ReturnType extends Record<Alias, DocumentReference<MyDocType>>>(collectionName: CollectionName, integrationId: IntegrationId): Omit<QueryBuilder<MyDocType, any, ReturnType, false>, 'join'>;
13
- getForJoin<MyDocType extends UserFacingDocType, MyAlias extends Alias, ReturnType extends Record<MyAlias, DocumentReference<MyDocType>>>(collectionName: CollectionName, integrationId: IntegrationId, alias: MyAlias): QueryBuilder<MyDocType, MyAlias, ReturnType, true>;
11
+ private readonly documentIdentityService;
12
+ constructor(querySubscriptionManager: QuerySubscriptionManager, documentReferenceFactory: DocumentReferenceFactory, documentIdentityService: DocumentIdentityService);
13
+ getForDocument<DocumentType extends DocumentData, ReturnType extends Record<Alias, DocumentReference<DocumentType>>>(squidDocId: SquidDocId): Omit<QueryBuilder<DocumentType, any, ReturnType, false>, 'join'>;
14
+ get<DocumentType extends DocumentData, ReturnType extends Record<Alias, DocumentReference<DocumentType>>>(collectionName: CollectionName, integrationId: IntegrationId): Omit<QueryBuilder<DocumentType, any, ReturnType, false>, 'join'>;
15
+ getForJoin<DocumentType extends DocumentData, MyAlias extends Alias, ReturnType extends Record<MyAlias, DocumentReference<DocumentType>>>(collectionName: CollectionName, integrationId: IntegrationId, alias: MyAlias): QueryBuilder<DocumentType, MyAlias, ReturnType, true>;
14
16
  }
15
- export declare class QueryBuilder<MyDocType extends UserFacingDocType, MyAlias extends Alias, ReturnType extends Record<MyAlias, DocumentReference<MyDocType> | undefined>, SupportsJoin extends boolean> {
17
+ export declare class QueryBuilder<DocumentType extends DocumentData, MyAlias extends Alias, ReturnType extends Record<MyAlias, DocumentReference<DocumentType> | undefined>, SupportsJoin extends boolean> {
16
18
  private readonly collectionName;
17
19
  private readonly integrationId;
18
20
  private readonly querySubscriptionManager;
@@ -20,22 +22,23 @@ export declare class QueryBuilder<MyDocType extends UserFacingDocType, MyAlias e
20
22
  private readonly alias;
21
23
  private readonly supportsJoin;
22
24
  private readonly queryBuilderFactory;
25
+ private readonly documentIdentityService;
23
26
  private readonly simpleQueryBuilder;
24
27
  private readonly joins;
25
28
  /** Record that maps the right alias to the left condition */
26
29
  private readonly joinConditions;
27
- constructor(collectionName: CollectionName, integrationId: IntegrationId, querySubscriptionManager: QuerySubscriptionManager, documentReferenceFactory: DocumentReferenceFactory, alias: MyAlias, supportsJoin: SupportsJoin, queryBuilderFactory: QueryBuilderFactory);
28
- where(fieldName: (keyof MyDocType & FieldName) | string, operator: Operator | 'in' | 'not in', value: PrimitiveFieldType | Array<PrimitiveFieldType>): SupportsJoin extends true ? this : Omit<this, 'join'>;
30
+ constructor(collectionName: CollectionName, integrationId: IntegrationId, querySubscriptionManager: QuerySubscriptionManager, documentReferenceFactory: DocumentReferenceFactory, alias: MyAlias, supportsJoin: SupportsJoin, queryBuilderFactory: QueryBuilderFactory, documentIdentityService: DocumentIdentityService);
31
+ where(fieldName: (keyof DocumentType & FieldName) | string, operator: Operator | 'in' | 'not in', value: PrimitiveFieldType | Array<PrimitiveFieldType>): SupportsJoin extends true ? this : Omit<this, 'join'>;
29
32
  limit(limit: number): SupportsJoin extends true ? this : Omit<this, 'join'>;
30
- sortBy(fieldName: keyof MyDocType & FieldName, asc?: boolean): SupportsJoin extends true ? this : Omit<this, 'join'>;
31
- join<J extends UserFacingDocType, RightAlias extends Alias, RightReturnType extends Record<RightAlias, DocumentReference<J> | undefined>>(queryBuilder: QueryBuilder<J, RightAlias, RightReturnType, true>, leftFieldName: keyof MyDocType & FieldName, rightFieldName: keyof J & FieldName): QueryBuilder<MyDocType, MyAlias, ReturnType & Partial<RightReturnType>, true>;
32
- snapshot(): SupportsJoin extends true ? Promise<Array<ReturnType>> : Promise<Array<DocumentReference<MyDocType>>>;
33
- snapshots(subscribe?: boolean): SupportsJoin extends true ? Observable<Array<ReturnType>> : Observable<Array<DocumentReference<MyDocType>>>;
34
- changes(): Observable<Changes<MyDocType>>;
33
+ sortBy(fieldName: keyof DocumentType & FieldName, asc?: boolean): SupportsJoin extends true ? this : Omit<this, 'join'>;
34
+ join<J extends DocumentData, RightAlias extends Alias, RightReturnType extends Record<RightAlias, DocumentReference<J> | undefined>>(queryBuilder: QueryBuilder<J, RightAlias, RightReturnType, true>, leftFieldName: keyof DocumentType & FieldName, rightFieldName: keyof J & FieldName): QueryBuilder<DocumentType, MyAlias, ReturnType & Partial<RightReturnType>, true>;
35
+ snapshot(): SupportsJoin extends true ? Promise<Array<ReturnType>> : Promise<Array<DocumentReference<DocumentType>>>;
36
+ snapshots(subscribe?: boolean): SupportsJoin extends true ? Observable<Array<ReturnType>> : Observable<Array<DocumentReference<DocumentType>>>;
37
+ changes(): Observable<Changes<DocumentType>>;
35
38
  build(): Query;
36
39
  }
37
- export interface Changes<MyDocType extends UserFacingDocType> {
38
- inserts: Array<DocumentReference<MyDocType>>;
39
- updates: Array<DocumentReference<MyDocType>>;
40
- deletes: Array<MyDocType>;
40
+ export interface Changes<DocumentType extends DocumentData> {
41
+ inserts: Array<DocumentReference<DocumentType>>;
42
+ updates: Array<DocumentReference<DocumentType>>;
43
+ deletes: Array<DocumentType>;
41
44
  }
@@ -1,14 +1,16 @@
1
- import { ClientId, ClientRequestId, DocType, FullDocIdStr, Query, QuerySubscriptionId } from '@squidcloud/common';
1
+ import { ClientId, ClientRequestId, Query, QuerySubscriptionId, SquidDocId, SquidDocument } from '@squidcloud/common';
2
2
  import { BehaviorSubject, Observable, Subject } from 'rxjs';
3
- import { DbDao } from '../db.dao';
4
3
  import { DestructManager } from '../destruct.manager';
4
+ import DocumentIdentityService from '../document-identity.service';
5
+ import { DocumentStore } from '../document-store';
5
6
  import { RpcManager } from '../rpc.manager';
6
7
  import { Alias, JoinCondition } from './query.types';
7
8
  export declare class QuerySubscriptionManager {
8
9
  private readonly rpcManager;
9
10
  private readonly clientId;
10
- private readonly dbDao;
11
+ private readonly documentStore;
11
12
  private readonly destructManager;
13
+ private readonly documentIdentityService;
12
14
  readonly safeToSendQueriesToServer: BehaviorSubject<boolean>;
13
15
  onOrphanDocuments: Subject<string[]>;
14
16
  private readonly ongoingQueries;
@@ -20,22 +22,22 @@ export declare class QuerySubscriptionManager {
20
22
  private readonly clientRequestIdToLocalDocuments;
21
23
  private readonly localDocumentToClientRequestIds;
22
24
  private readonly queryMappingManager;
23
- constructor(rpcManager: RpcManager, clientId: ClientId, dbDao: DbDao, destructManager: DestructManager);
25
+ constructor(rpcManager: RpcManager, clientId: ClientId, documentStore: DocumentStore, destructManager: DestructManager, documentIdentityService: DocumentIdentityService);
24
26
  hasOngoingQuery(clientRequestId: ClientRequestId): boolean;
25
27
  getQuery(clientRequestId: ClientRequestId): Query;
26
28
  setGotResponseFromServer(clientRequestId: ClientRequestId): void;
27
- findQueriesForDocument(doc: DocType, fullDocId: FullDocIdStr): Array<QuerySubscriptionId>;
29
+ findQueriesForDocument(doc: SquidDocument, squidDocId: SquidDocId): Array<QuerySubscriptionId>;
28
30
  /**
29
31
  * Given the new document's properties, finds all the queries that should be notified with the new properties and
30
- * updates the internal mappings (fullDocId --> client request Ids and, clientRequestId --> fullDocIds).
32
+ * updates the internal mappings (squidDocId --> client request Ids and, clientRequestId --> squidDocIds).
31
33
  * Returns an array with all the previous and current client request ids (basically all the client request ids that
32
34
  * will need to be notified due to the change of properties).
33
35
  */
34
- setClientRequestIdsForLocalDoc(fullDocId: FullDocIdStr, properties: DocType | undefined): Array<ClientRequestId>;
35
- errorOutAllQueries(fullDocId: FullDocIdStr, err: any): void;
36
+ setClientRequestIdsForLocalDoc(squidDocId: SquidDocId, properties: SquidDocument | undefined): Array<ClientRequestId>;
37
+ errorOutAllQueries(squidDocId: SquidDocId, err: any): void;
36
38
  notifyAllSubscriptions(clientRequestIds: ClientRequestId[]): void;
37
- processQuery(query: Query, rootAlias: Alias, joins: Record<string, Query>, joinConditions: Record<Alias, JoinCondition>, subscribe: boolean): Observable<Array<Record<Alias, DocType | undefined>>>;
38
- hasOngoingQueryForDocId(fullDocIdStr: string): boolean;
39
+ processQuery(query: Query, rootAlias: Alias, joins: Record<string, Query>, joinConditions: Record<Alias, JoinCondition>, subscribe: boolean): Observable<Array<Record<Alias, SquidDocument | undefined>>>;
40
+ hasOngoingQueryForDocId(squidDocId: string): boolean;
39
41
  private removeClientRequestId;
40
42
  waitForAllQueriesToFinish(): Promise<void>;
41
43
  private registerQueryFinalizer;
@@ -48,8 +50,9 @@ export declare class QuerySubscriptionManager {
48
50
  private updateOngoingQueryWithNewDataFromSupportingQuery;
49
51
  private allOngoingQueriesGotServerResult;
50
52
  private completeAllSupportedQueries;
51
- private destruct;
53
+ private predestruct;
52
54
  hasSubscription(clientRequestId: ClientRequestId): boolean;
53
55
  /** Sends the query request to the server and makes sure to unsubscribe once the subject completes. */
54
56
  private sendQueryToServer;
57
+ private migrateDocIds;
55
58
  }
@@ -1,12 +1,12 @@
1
- import { ApiEndpointId, ApiKey, AppId, CollectionName, IntegrationId, QueryName, UserFacingDocType } from '@squidcloud/common';
1
+ import { ApiEndpointId, ApiKey, AppId, CollectionName, DocumentData, IntegrationId, QueryName, SquidRegion } from '@squidcloud/common';
2
2
  import { CollectionReference } from './collection-reference';
3
- import { TransactionId } from './types';
4
3
  import { GraphQLClient } from './graphql-client';
4
+ import { TransactionId } from './types';
5
5
  export interface SquidOptions {
6
6
  messageNotificationWrapper?: (fn: () => any) => any;
7
7
  appId: AppId;
8
8
  apiKey?: ApiKey;
9
- baseUrl?: string;
9
+ region: SquidRegion;
10
10
  apiServerUrlOverrideMapping?: Record<IntegrationId, string>;
11
11
  }
12
12
  /** Reference to the squid db. Provides all the available actions on the DB. */
@@ -16,7 +16,7 @@ export declare class Squid {
16
16
  private readonly rpcManager;
17
17
  private readonly dataManager;
18
18
  private readonly documentReferenceFactory;
19
- private readonly dbDao;
19
+ private readonly documentStore;
20
20
  private readonly lockManager;
21
21
  private readonly querySubscriptionManager;
22
22
  private readonly queryBuilderFactory;
@@ -26,6 +26,7 @@ export declare class Squid {
26
26
  private readonly apiManager;
27
27
  private readonly graphqlClientFactory;
28
28
  private readonly destructManager;
29
+ private readonly documentIdentityService;
29
30
  private static readonly squidInstancesMap;
30
31
  constructor(options: SquidOptions);
31
32
  /**
@@ -38,7 +39,7 @@ export declare class Squid {
38
39
  * to the security rules.
39
40
  */
40
41
  setAuthIdToken(idToken: string | undefined): void;
41
- collection<T extends UserFacingDocType>(collectionName: CollectionName, integrationId?: IntegrationId): CollectionReference<T>;
42
+ collection<T extends DocumentData>(collectionName: CollectionName, integrationId?: IntegrationId): CollectionReference<T>;
42
43
  runInTransaction(fn: (transactionId: TransactionId) => Promise<void>): Promise<void>;
43
44
  executeFunction<T = any>(functionName: string, ...params: any[]): Promise<T>;
44
45
  executeNamedQuery<T = any>(integrationId: IntegrationId, queryName: QueryName, params: Record<string, any>): Promise<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squidcloud/backend",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Squid Cloud's backend SDK",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/backend/src/index.d.ts",
@@ -1,20 +0,0 @@
1
- import { DocType, FullDocIdStr, Query } from '@squidcloud/common';
2
- export type DbId = number;
3
- export declare class DbDao {
4
- private readonly db;
5
- private readonly dbIdToDoc;
6
- private readonly fullDocIdToDbId;
7
- constructor();
8
- saveDocument(fullDocIdStr: FullDocIdStr, properties: DocType | undefined): DbId | undefined;
9
- removeDocumentFromDb(fullDocIdStr: FullDocIdStr): void;
10
- isDbDifferentThanInMemory(fullDocIdStr: FullDocIdStr): boolean;
11
- getDocumentProperties(fullDocIdStr: FullDocIdStr): DocType;
12
- getDocumentPropertiesOrUndefined(fullDocIdStr: FullDocIdStr): DocType | undefined;
13
- executeQuery(query: Query): Array<DocType>;
14
- private convertToDocType;
15
- private convertToLokiCondition;
16
- private getDbCollection;
17
- private getDbIdToDocForCollection;
18
- /** Converts multiple == conditions to $in, and multiple != conditions to $nin */
19
- private applyEqualityConditions;
20
- }