Skip to content

Commit

Permalink
Merge pull request #102 from nitrictech/fix/document-typing
Browse files Browse the repository at this point in the history
Fix document typing.
  • Loading branch information
tjholm committed Feb 28, 2022
2 parents df4bd10 + 9c5f06e commit 94407e5
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/api/documents/v0/collection-group-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { DocumentServiceClient } from '@nitric/api/proto/document/v1/document_gr
import { InvalidArgumentError } from '../../errors';
import { CollectionRef } from './collection-ref';
import { MAX_COLLECTION_DEPTH } from './constants';
import { DocumentRef } from './document-ref';
import { DocumentRef, DocumentStructure } from './document-ref';
import { Query } from './query';

const NIL_DOC_ID = '';
Expand All @@ -25,7 +25,7 @@ const NIL_DOC_ID = '';
*
* Provides a Document API CollectionGroupRef class.
*/
export class CollectionGroupRef<T extends { [key: string]: any }> {
export class CollectionGroupRef<T extends DocumentStructure> {
private documentClient: DocumentServiceClient;
public readonly parent: CollectionGroupRef<any>;
public readonly name: string;
Expand All @@ -44,7 +44,7 @@ export class CollectionGroupRef<T extends { [key: string]: any }> {
* Create a CollectionGroupRef referencing a sub-collection of this collection
* @param name
*/
public collection<T extends { [key: string]: any }>(
public collection<T extends DocumentStructure>(
name: string
): CollectionGroupRef<T> {
if (this.depth() >= MAX_COLLECTION_DEPTH) {
Expand Down
4 changes: 2 additions & 2 deletions src/api/documents/v0/collection-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
import { Collection } from '@nitric/api/proto/document/v1/document_pb';
import { DocumentServiceClient } from '@nitric/api/proto/document/v1/document_grpc_pb';
import { Query } from './query';
import { DocumentRef } from './document-ref';
import { DocumentRef, DocumentStructure } from './document-ref';
import { CollectionGroupRef } from './collection-group-ref';

/**
* CollectionRef
*
* Provides a Document API CollectionRef class.
*/
export class CollectionRef<T extends { [key: string]: any }> {
export class CollectionRef<T extends DocumentStructure> {
private documentClient: DocumentServiceClient;
public readonly name: string;
public readonly parent?: DocumentRef<any>;
Expand Down
6 changes: 4 additions & 2 deletions src/api/documents/v0/document-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ import { fromGrpcError, InvalidArgumentError } from '../../errors';
import { CollectionRef } from './collection-ref';
import { MAX_COLLECTION_DEPTH } from './constants';

export type DocumentStructure = Record<string, any>;

/**
* Document Ref
*
* Provides a Document Reference class.
* Used to create references to collections.
*/
export class DocumentRef<T extends { [key: string]: any }> {
export class DocumentRef<T extends DocumentStructure> {
private documentClient: DocumentServiceClient;
public readonly parent: CollectionRef<T>;
public readonly id: string;
Expand Down Expand Up @@ -138,7 +140,7 @@ export class DocumentRef<T extends { [key: string]: any }> {
* @param name The name of the collection (required)
* @returns The Collection instance
*/
public collection<T extends { [key: string]: any }>(
public collection<T extends DocumentStructure>(
name: string
): CollectionRef<T> {
if (this.depth() >= MAX_COLLECTION_DEPTH) {
Expand Down
4 changes: 2 additions & 2 deletions src/api/documents/v0/document-snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { DocumentRef } from './document-ref';
import { DocumentRef, DocumentStructure } from './document-ref';

export class DocumentSnapshot<T> {
export class DocumentSnapshot<T extends DocumentStructure> {
public readonly ref: DocumentRef<T>;
public readonly content: T;

Expand Down
3 changes: 2 additions & 1 deletion src/api/documents/v0/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { SERVICE_BIND } from '../../../constants';
import { DocumentServiceClient } from '@nitric/api/proto/document/v1/document_grpc_pb';
import * as grpc from '@grpc/grpc-js';
import { CollectionRef } from './collection-ref';
import { DocumentStructure } from './document-ref';

/**
* Documents
Expand All @@ -37,7 +38,7 @@ export class Documents {
* @param name The name of the collection (required)
* @returns The Collection instance
*/
public collection<T extends { [key: string]: any }>(name: string) {
public collection<T extends DocumentStructure>(name: string) {
return new CollectionRef<T>(this.documentClient, name);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/api/documents/v0/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
import { DocumentServiceClient } from '@nitric/api/proto/document/v1/document_grpc_pb';
import { WhereQueryOperator, WhereValueExpression } from '../../../types';
import type { Map as ProtobufMap } from 'google-protobuf';
import { DocumentRef } from './document-ref';
import { DocumentRef, DocumentStructure } from './document-ref';
import { CollectionRef } from './collection-ref';
import { DocumentSnapshot } from './document-snapshot';
import { fromGrpcError, InvalidArgumentError } from '../../errors';
Expand Down Expand Up @@ -61,7 +61,7 @@ function protoMapToMap(
* Provides a Document API client.
* Used to create references to collections.
*/
export class Query<T extends { [key: string]: any }> {
export class Query<T extends DocumentStructure> {
private documentClient: DocumentServiceClient;
public readonly collection: CollectionRef<T>;
private expressions: Expression[];
Expand Down
3 changes: 2 additions & 1 deletion src/resources/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { fromGrpcError } from '../api/errors';
import { documents } from '../api/documents';
import resourceClient from './client';
import { make, Resource as Base } from './common';
import { DocumentStructure } from 'src/api/documents/v0/document-ref';

type CollectionPermission = 'reading' | 'writing' | 'deleting';

Expand Down Expand Up @@ -94,7 +95,7 @@ class CollectionResource extends Base<CollectionPermission> {
* @param perms the required permission set
* @returns a usable collection reference
*/
public for<T>(...perms: CollectionPermission[]) {
public for<T extends DocumentStructure>(...perms: CollectionPermission[]) {
this.registerPolicy(...perms);

return documents().collection<T>(this.name);
Expand Down

0 comments on commit 94407e5

Please sign in to comment.