Skip to content

Commit

Permalink
- Added Query.notContains() to compliment .contains() as these functi…
Browse files Browse the repository at this point in the history
…ons only perform equals or notEquals functionality

- Allow performing .join() queries within .include() function which is an easier interface to use
- Project name will now use `-sdk` with the name internally
  • Loading branch information
DavidArayan committed Dec 11, 2023
1 parent b277ace commit c43c7bd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
40 changes: 23 additions & 17 deletions sdk-core/src/core/query/core-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CoreObject, CoreObjectAttributes } from '../core-object';
import { GlobalObjectPool } from '../global-object-pool';
import { Service } from '../service';
import { CoreError } from './errors/core-error';
import { QueryContainsOperator, ContainsQuery } from './queries/contains-query';
import { ContainsQuery } from './queries/contains-query';
import { DeletedQuery } from './queries/deleted-query';
import { FieldsQuery } from './queries/fields-query';
import { FilterQuery, FilterQueryOperator } from './queries/filter-query';
Expand Down Expand Up @@ -54,7 +54,11 @@ export abstract class CoreQuery<T extends CoreObject<U>, U extends CoreObjectAtt
*/
public join(...queries: Array<CoreQuery<CoreObject<CoreObjectAttributes>, CoreObjectAttributes>>): this {
queries.forEach((query: CoreQuery<CoreObject<CoreObjectAttributes>, CoreObjectAttributes>) => {
this._queries.push(new JoinQuery(query.toString()));
const queryString: string = query.toString();

if (queryString !== '') {
this._queries.push(new JoinQuery(queryString));
}
});

return this;
Expand All @@ -81,36 +85,38 @@ export abstract class CoreQuery<T extends CoreObject<U>, U extends CoreObjectAtt
return this;
}

public include(...objects: Array<(typeof CoreObject<CoreObjectAttributes>) | Array<string>>): this {
const data: Array<string | Array<string>> = objects.map<string | Array<string>>((object: typeof CoreObject<CoreObjectAttributes> | Array<string>) => {
public include(...objects: Array<(typeof CoreObject<CoreObjectAttributes>) | (typeof CoreQuery<CoreObject<CoreObjectAttributes>, CoreObjectAttributes>) | Array<string>>): this {
const data: Array<string | Array<string>> = objects.map<string | Array<string>>((object: typeof CoreObject<CoreObjectAttributes> | typeof CoreQuery<CoreObject<CoreObjectAttributes>, CoreObjectAttributes> | Array<string>) => {
if (Array.isArray(object)) {
return object.map<string>((object: string) => {
return `${this.instance.type}.${object}`;
});
}

return `${this.instance.type}.${object.type}`;
});

const consolidatedData: Array<string> = new Array<string>();
if (object instanceof CoreQuery) {
this.join(object);

data.forEach((object: string | Array<string>) => {
if (Array.isArray(object)) {
consolidatedData.push(...object);
}
else {
consolidatedData.push(object);
return `${this.instance.type}.${object.instance.type}`;
}

return `${this.instance.type}.${(<any>object).type}`;
});

this._queries.push(new IncludeQuery(consolidatedData));
this._queries.push(new IncludeQuery(data.flat()));

return this;
}

public contains(...objects: Array<typeof CoreObject<CoreObjectAttributes>>): this {
const data: Array<string> = objects.map<string>((object: typeof CoreObject<CoreObjectAttributes>) => object.type);
this._queries.push(new ContainsQuery("==", data));

return this;
}

public contains(operation: QueryContainsOperator = '==', ...objects: Array<typeof CoreObject<CoreObjectAttributes>>): this {
public notContains(...objects: Array<typeof CoreObject<CoreObjectAttributes>>): this {
const data: Array<string> = objects.map<string>((object: typeof CoreObject<CoreObjectAttributes>) => object.type);
this._queries.push(new ContainsQuery(operation, data));
this._queries.push(new ContainsQuery("!=", data));

return this;
}
Expand Down
14 changes: 7 additions & 7 deletions sdk-core/src/generator/generators/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class Project {
let output = `const { CleanWebpackPlugin } = require('clean-webpack-plugin');\n`;
output += `const path = require("path");\n\n`;
output += `module.exports = {\n`;
output += `\t name: '${vars.name}',\n`;
output += `\t name: '${vars.name}-sdk',\n`;
output += `\t mode: 'production',\n`;
output += `\t devtool: 'source-map',\n`;
output += `\t entry: './dist/index.js',\n`;
Expand All @@ -97,7 +97,7 @@ export class Project {
*/
public static generatePackageJson(vars: PackageJsonVars): any {
return {
name: `@plattar/${vars.name}`,
name: `@plattar/${vars.name}-sdk`,
version: vars.version,
description: `Generated using @plattar/sdk-core and used for interfacing with ${vars.name} backend service`,
main: 'dist/index.js',
Expand Down Expand Up @@ -172,14 +172,14 @@ export class Project {
}

public static generateReadme(vars: PackageJsonVars): string {
let output = `[![NPM](https://img.shields.io/npm/v/@plattar/${vars.name})](https://www.npmjs.com/package/@plattar/${vars.name})\n\n`;
let output = `[![NPM](https://img.shields.io/npm/v/@plattar/${vars.name}-sdk)](https://www.npmjs.com/package/@plattar/${vars.name}-sdk)\n\n`;
output += `### About\n\n`;
output += `_${vars.name}_ v${vars.version} is automatically generated using [sdk-core](https://www.npmjs.com/package/@plattar/sdk-core) module\n\n`;
output += `${vars.name}-sdk v${vars.version} is automatically generated using [sdk-core](https://www.npmjs.com/package/@plattar/sdk-core) module\n\n`;
output += `### Installation\n\n`;
output += `- Install using [npm](https://www.npmjs.com/package/@plattar/${vars.name})\n\n`;
output += `\`\`\`console\nnpm install @plattar/${vars.name}\n\`\`\`\n\n`;
output += `- Install using [npm](https://www.npmjs.com/package/@plattar/${vars.name}-sdk)\n\n`;
output += `\`\`\`console\nnpm install @plattar/${vars.name}-sdk\n\`\`\`\n\n`;
output += `### JSDelivr\n\n`;
output += `- Minified Bundle \`https://cdn.jsdelivr.net/npm/@plattar/${vars.name}/build/bundle.min.js\`\n`;
output += `- Minified Bundle \`https://cdn.jsdelivr.net/npm/@plattar/${vars.name}-sdk/build/bundle.min.js\`\n`;

return output;
}
Expand Down

0 comments on commit c43c7bd

Please sign in to comment.