Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow project_id to be optional in projection class #78

Merged
merged 16 commits into from
Sep 26, 2024
41 changes: 32 additions & 9 deletions src/projection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ export class AtlasProjection extends BaseAtlasClass<
* at a point in time. Every projection belongs to a Dataset.
*/
_project?: AtlasDataset;
project_id: UUID;
project_id?: UUID;
_project_id_promise?: Promise<UUID>;
_index?: AtlasIndex;

/**
Expand All @@ -119,16 +120,13 @@ export class AtlasProjection extends BaseAtlasClass<
const { project, project_id } = options;
super(user || project?.viewer);

if (project_id === undefined && project === undefined) {
throw new Error('project_id or project is required');
}
if (project_id !== undefined && project !== undefined) {
throw new Error('project_id and project are mutually exclusive');
}

if (project_id !== undefined) {
this.project_id = project_id;
} else {
} else if (project !== undefined) {
this.project_id = project!.id;
this._project = project;
}
Expand All @@ -137,6 +135,27 @@ export class AtlasProjection extends BaseAtlasClass<
}
}

async datasetId(): Promise<UUID> {
bmschmidt marked this conversation as resolved.
Show resolved Hide resolved
if (this.project_id !== undefined) {
return this.project_id;
}
if (this._project !== undefined) {
return this._project.id;
}

const endpoint = `/v1/project/projection/${this.id}/get/dataset_id`;

this._project_id_promise = (async () => {
const { dataset_id } = (await this.apiCall(endpoint, 'GET')) as {
dataset_id: UUID;
};
this.project_id = dataset_id;
return dataset_id;
})();
bmschmidt marked this conversation as resolved.
Show resolved Hide resolved

return this._project_id_promise;
}

async createTag(options: CreateTagOptions): Promise<TagResponse> {
const endpoint = '/v1/project/projection/tags/create';
const { tag_name, dsl_rule, tag_definition_id } = options;
Expand Down Expand Up @@ -200,8 +219,9 @@ export class AtlasProjection extends BaseAtlasClass<

async getTags(): Promise<Array<TagResponse>> {
const endpoint = '/v1/project/projection/tags/get/all';
const project_id = await this.datasetId();
const params = new URLSearchParams({
project_id: this.project_id,
project_id: project_id,
projection_id: this.id,
}).toString();
return this.apiCall(`${endpoint}?${params}`, 'GET') as Promise<
Expand All @@ -214,9 +234,10 @@ export class AtlasProjection extends BaseAtlasClass<
if (tag_id === undefined) {
throw new Error('tag_id is required');
}
const project_id = await this.datasetId();
const endpoint = '/v1/project/projection/tags/status';
const params = new URLSearchParams({
project_id: this.project_id,
project_id: project_id,
tag_id,
}).toString();
return this.apiCall(`${endpoint}?${params}`, 'GET') as Promise<TagStatus>;
Expand All @@ -228,6 +249,7 @@ export class AtlasProjection extends BaseAtlasClass<
): Promise<void> {
const endpoint = '/v1/project/projection/tags/update/mask';
const { tag_id, tag_definition_id, complete } = options;
const project_id = await this.datasetId();

// Upsert tag mask with tag definition id
let post_tag_definition_id = tag_definition_id;
Expand All @@ -240,7 +262,7 @@ export class AtlasProjection extends BaseAtlasClass<
const bitmask = tableFromIPC(bitmask_bytes);

bitmask.schema.metadata.set('tag_id', tag_id as string);
bitmask.schema.metadata.set('project_id', this.project_id);
bitmask.schema.metadata.set('project_id', project_id);
bitmask.schema.metadata.set(
'tag_definition_id',
post_tag_definition_id as string
Expand Down Expand Up @@ -287,7 +309,8 @@ export class AtlasProjection extends BaseAtlasClass<

async project(): Promise<AtlasDataset> {
if (this._project === undefined) {
this._project = new AtlasDataset(this.project_id, this.viewer);
const project_id = await this.datasetId();
this._project = new AtlasDataset(project_id, this.viewer);
}
return this._project;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/project.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ test('Full project flow', async () => {
// Re-instantiate with just the project; test if we properly infer the index.
console.log('re-instantiating projection');
const projection = new AtlasProjection(orig_projection.id, user, { project });
// Fetch dataset id from projection
const projectionWithoutId = new AtlasProjection(orig_projection.id, user);
assert.is(await projectionWithoutId.datasetId(), project.id);
const inferred_index = await projection.index();
assert.is(inferred_index.id, index.id);
// // Create a tag
Expand Down