Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 198 additions & 0 deletions handwritten/firestore/dev/src/pipelines/pipelines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ import {
Sample,
Union,
Unnest,
DeleteStage,
UpsertStage,
InsertStage,
InternalWhereStageOptions,
InternalOffsetStageOptions,
InternalLimitStageOptions,
Expand All @@ -95,6 +98,7 @@ import {
InternalDocumentsStageOptions,
InternalCollectionGroupStageOptions,
InternalCollectionStageOptions,
UpdateStage,
} from './stage';
import {StructuredPipeline} from './structured-pipeline';
import Selectable = FirebaseFirestore.Pipelines.Selectable;
Expand Down Expand Up @@ -1500,6 +1504,200 @@ export class Pipeline implements firestore.Pipelines.Pipeline {
return this._addStage(new Sort(internalOptions));
}

/**
* @beta
* Performs a delete operation on documents from previous stages.
*
* @example
* ```typescript
* // Deletes all documents in the "books" collection.
* firestore.pipeline().collection("books")
* .delete();
* ```
*
* @return A new {@code Pipeline} object with this stage appended to the stage list.
*/
delete(): Pipeline;
/**
* @beta
* Performs a delete operation on documents from previous stages.
*
* TODO(dlarocque): Verify we want this function.
*
* @param collectionNameOrRef - The collection to delete from.
* @return A new {@code Pipeline} object with this stage appended to the stage list.
*/
delete(collectionNameOrRef: string | firestore.CollectionReference): Pipeline;
/**
* @beta
* Performs a delete operation on documents from previous stages.
*
* @example
* ```typescript
* // Deletes all documents in the books collection and returns their IDs.
* firestore.pipeline().collection("books")
* .delete({
* returns: "DOCUMENT_ID",
* });
* ```
*
* @param options - The {@code DeleteStageOptions} to apply to the stage.
* @return A new {@code Pipeline} object with this stage appended to the stage list.
*/
delete(options: firestore.Pipelines.DeleteStageOptions): Pipeline;
delete(
optionsOrCollection?:
| string
| firestore.CollectionReference
| firestore.Pipelines.DeleteStageOptions,
): Pipeline {
let target = undefined;
if (typeof optionsOrCollection === 'string') {
target = this.db.collection(optionsOrCollection);
} else if (isCollectionReference(optionsOrCollection)) {
target = optionsOrCollection;
}
const options = (
!isCollectionReference(optionsOrCollection) &&
typeof optionsOrCollection !== 'string'
? optionsOrCollection
: undefined
) as firestore.Pipelines.DeleteStageOptions | undefined;
return this._addStage(new DeleteStage(target, options));
}

/**
* @beta
* Performs an update operation using documents from previous stages.
*
* @return A new {@code Pipeline} object with this stage appended to the stage list.
*/
update(): Pipeline;
/**
* @beta
* Performs an update operation using documents from previous stages.
*
* @param collectionNameOrRef - The collection to update.
* @return A new {@code Pipeline} object with this stage appended to the stage list.
*/
update(collectionNameOrRef: string | firestore.CollectionReference): Pipeline;
/**
* @beta
* Performs an update operation using documents from previous stages.
*
* @param options - The {@code UpdateStageOptions} to apply to the stage.
* @return A new {@code Pipeline} object with this stage appended to the stage list.
*/
update(options: firestore.Pipelines.UpdateStageOptions): Pipeline;
update(
optionsOrCollection?:
| string
| firestore.CollectionReference
| firestore.Pipelines.UpdateStageOptions,
): Pipeline {
let target = undefined;
if (typeof optionsOrCollection === 'string') {
target = this.db.collection(optionsOrCollection);
} else if (isCollectionReference(optionsOrCollection)) {
target = optionsOrCollection;
}
const options = (
!isCollectionReference(optionsOrCollection) &&
typeof optionsOrCollection !== 'string'
? optionsOrCollection
: undefined
) as firestore.Pipelines.UpdateStageOptions | undefined;
return this._addStage(new UpdateStage(target, options));
}

/**
* @beta
* Performs an upsert operation using documents from previous stages.
*
* @return A new {@code Pipeline} object with this stage appended to the stage list.
*/
upsert(): Pipeline;
/**
* @beta
* Performs an upsert operation using documents from previous stages.
*
* @param collectionNameOrRef - The collection to upsert to.
* @return A new {@code Pipeline} object with this stage appended to the stage list.
*/
upsert(collectionNameOrRef: string | firestore.CollectionReference): Pipeline;
/**
* @beta
* Performs an upsert operation using documents from previous stages.
*
* @param options - The {@code UpsertStageOptions} to apply to the stage.
* @return A new {@code Pipeline} object with this stage appended to the stage list.
*/
upsert(options: firestore.Pipelines.UpsertStageOptions): Pipeline;
upsert(
optionsOrCollection?:
| string
| firestore.CollectionReference
| firestore.Pipelines.UpsertStageOptions,
): Pipeline {
let target = undefined;
if (typeof optionsOrCollection === 'string') {
target = this.db.collection(optionsOrCollection);
} else if (isCollectionReference(optionsOrCollection)) {
target = optionsOrCollection;
}
const options = (
!isCollectionReference(optionsOrCollection) &&
typeof optionsOrCollection !== 'string'
? optionsOrCollection
: undefined
) as firestore.Pipelines.UpsertStageOptions | undefined;
return this._addStage(new UpsertStage(target, options));
}

/**
* @beta
* Performs an insert operation using documents from previous stages.
*
* @return A new {@code Pipeline} object with this stage appended to the stage list.
*/
insert(): Pipeline;
/**
* @beta
* Performs an insert operation using documents from previous stages.
*
* @param collectionNameOrRef - The collection to insert to.
* @return A new {@code Pipeline} object with this stage appended to the stage list.
*/
insert(collectionNameOrRef: string | firestore.CollectionReference): Pipeline;
/**
* @beta
* Performs an insert operation using documents from previous stages.
*
* @param options - The {@code InsertStageOptions} to apply to the stage.
* @return A new {@code Pipeline} object with this stage appended to the stage list.
*/
insert(options: firestore.Pipelines.InsertStageOptions): Pipeline;
insert(
optionsOrCollection?:
| string
| firestore.CollectionReference
| firestore.Pipelines.InsertStageOptions,
): Pipeline {
let target = undefined;
if (typeof optionsOrCollection === 'string') {
target = this.db.collection(optionsOrCollection);
} else if (isCollectionReference(optionsOrCollection)) {
target = optionsOrCollection;
}
const options = (
!isCollectionReference(optionsOrCollection) &&
typeof optionsOrCollection !== 'string'
? optionsOrCollection
: undefined
) as firestore.Pipelines.InsertStageOptions | undefined;
return this._addStage(new InsertStage(target, options));
}

/**
* @beta
* Adds a raw stage to the pipeline.
Expand Down
135 changes: 135 additions & 0 deletions handwritten/firestore/dev/src/pipelines/stage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,3 +677,138 @@ export class RawStage implements Stage {
};
}
}

/**
* Delete stage.
*/
export class DeleteStage implements Stage {
name = 'delete';
readonly optionsUtil = new OptionsUtil({
returns: {serverName: 'returns'},
});

constructor(
private target?: firestore.CollectionReference,
private rawOptions?: firestore.Pipelines.DeleteStageOptions,
) {}

_toProto(serializer: Serializer): api.Pipeline.IStage {
const args: api.IValue[] = [];
if (this.target) {
args.push({referenceValue: this.target.path});
}

return {
name: this.name,
args,
options: this.optionsUtil.getOptionsProto(
serializer,
{},
this.rawOptions,
),
};
}
}
/**
* Update stage.
*/
export class UpdateStage implements Stage {
name = 'update';
readonly optionsUtil = new OptionsUtil({
returns: {serverName: 'returns'},
conflict_resolution: {serverName: 'conflict_resolution'},
transformations: {serverName: 'transformations'},
transactional: {serverName: 'transactional'},
});

constructor(
private target?: firestore.CollectionReference,
private rawOptions?: firestore.Pipelines.UpdateStageOptions,
) {}

_toProto(serializer: Serializer): api.Pipeline.IStage {
const args: api.IValue[] = [];
if (this.target) {
args.push({referenceValue: this.target.path});
}

return {
name: this.name,
args,
options: this.optionsUtil.getOptionsProto(
serializer,
{},
this.rawOptions,
),
};
}
}

/**
* Upsert stage.
*/
export class UpsertStage implements Stage {
name = 'upsert';
readonly optionsUtil = new OptionsUtil({
returns: {serverName: 'returns'},
conflict_resolution: {serverName: 'conflict_resolution'},
transformations: {serverName: 'transformations'},
transactional: {serverName: 'transactional'},
});

constructor(
private target?: firestore.CollectionReference,
private rawOptions?: firestore.Pipelines.UpsertStageOptions,
) {}

_toProto(serializer: Serializer): api.Pipeline.IStage {
const args: api.IValue[] = [];
if (this.target) {
args.push({referenceValue: this.target.path});
}

return {
name: this.name,
args,
options: this.optionsUtil.getOptionsProto(
serializer,
{},
this.rawOptions,
),
};
}
}

/**
* Insert stage.
*/
export class InsertStage implements Stage {
name = 'insert';
readonly optionsUtil = new OptionsUtil({
returns: {serverName: 'returns'},
transformations: {serverName: 'transformations'},
transactional: {serverName: 'transactional'},
});

constructor(
private target?: firestore.CollectionReference,
private rawOptions?: firestore.Pipelines.InsertStageOptions,
) {}

_toProto(serializer: Serializer): api.Pipeline.IStage {
const args: api.IValue[] = [];
if (this.target) {
args.push({referenceValue: this.target.path});
}

return {
name: this.name,
args,
options: this.optionsUtil.getOptionsProto(
serializer,
{},
this.rawOptions,
),
};
}
}
Loading
Loading