Skip to content

Commit

Permalink
Merge branch 'feature/io' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
anran758 committed Apr 10, 2024
2 parents 3823ca2 + 3ae22dc commit fb934e2
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 69 deletions.
24 changes: 24 additions & 0 deletions apps/faas-demo/src/function/test/testOperationFirst.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createFaas } from '@mincloudx/faas';
import io from '@/io';

export default createFaas(async function main() {
const query = io.getQuery({
description: 'This is description.',
});
const record = await io.product.first<
{
description: string;
id: string;
name: string;
},
false
>(query, {
plain: false,
select: ['description', 'id', 'name'],
});
// const record = await io.product.first(query);

console.log('record: ', record.data);

return record;
});
3 changes: 2 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"useWorkspaces": true,
"npmClient": "pnpm",
"version": "independent",
"version": "0.3.0",
"command": {
"version": {
"createRelease": "github",
"conventionalCommits": true,
"forcePublish": true,
"message": "chore(release): publish"
}
}
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/commands/faas/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import {
resolveCwdAbsolutePath,
scanForFunctionEntries,
} from '@/utils';
import { DEFAULT_FUNCTION_DIR, DEFAULT_BUILT_DIR } from './config';

import baseConf from './webpack.base.config';

const COMMAND_NAME = 'build';
const logger = createLogger(COMMAND_NAME);

const defaultConfig = {
entryDir: './src/function',
outputDir: './dist',
entryDir: DEFAULT_FUNCTION_DIR,
outputDir: DEFAULT_BUILT_DIR,
};

type BuildFaasParams = typeof defaultConfig & {
Expand Down
10 changes: 10 additions & 0 deletions packages/cli/src/commands/faas/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const DEFAULT_MOCK_DIR = './mock';

export const DEFAULT_LOG_DIR = './log';

export const DEFAULT_BUILT_DIR = './dist';

/**
* cloud function dir
*/
export const DEFAULT_FUNCTION_DIR = './src/function';
79 changes: 57 additions & 22 deletions packages/cli/src/commands/faas/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ import type { Command } from 'commander';
import { createLogger, selectCloudFunction } from '@/utils';
import { deployFunction } from './deploy';
import { invokeMockData } from './mock';
import {
DEFAULT_FUNCTION_DIR,
DEFAULT_BUILT_DIR,
DEFAULT_LOG_DIR,
DEFAULT_MOCK_DIR,
} from './config';

const COMMAND_NAME = 'debug';
const logger = createLogger(COMMAND_NAME);

// TODO: There are variables here that can be shared.
const defaultConfig = {
entryDir: './src/function',
builtDir: './dist',
outputDir: './log',
mockDir: './mock',
entryDir: DEFAULT_FUNCTION_DIR,
builtDir: DEFAULT_BUILT_DIR,
outputDir: DEFAULT_LOG_DIR,
mockDir: DEFAULT_MOCK_DIR,
};

type BuildFaasParams = typeof defaultConfig & {
Expand All @@ -22,6 +27,28 @@ type BuildFaasParams = typeof defaultConfig & {
functionName?: string;
};

async function debugFunction({
functionName,
entryDir,
builtDir,
mockDir,
outputDir,
}: Required<BuildFaasParams>) {
// Deploy the selected or provided cloud function with specified directories for entry and build.
await deployFunction({
functionName,
entryDir,
builtDir,
});

// Invoke the cloud function with mock data, specifying the mock data directory and the output directory for logs.
await invokeMockData({
functionName,
dir: mockDir,
output: outputDir,
});
}

/**
* Debug a specify cloud function with options for custom directories.
*
Expand Down Expand Up @@ -72,31 +99,39 @@ export function registerCommand(program: Command) {
.option('--mock-dir <value>', 'mock data directory', defaultConfig.mockDir)
.action(async (functionName, options: BuildFaasParams) => {
logger.verbose('verbose', 'function name:', functionName);
const { entryDir, builtDir, outputDir, mockDir } = options;

let name = functionName;
const { entryDir, builtDir, outputDir, mockDir } = options;
let name = functionName || '';

// If the cloud function name is not provided, prompt the user to select one from the available functions.
// This step utilizes a search feature for ease of use.
if (!name) {
if (name) {
await debugFunction({
functionName,
entryDir,
builtDir,
mockDir,
outputDir,
});

return;
}

// eslint-disable-next-line no-constant-condition
while (true) {
const answer = await selectCloudFunction(entryDir, {
message: 'Select a cloud function to debug (supports search)',
defaultValue: name,
});
name = answer;
}

// Deploy the selected or provided cloud function with specified directories for entry and build.
await deployFunction({
functionName: name,
entryDir,
builtDir,
});

// Invoke the cloud function with mock data, specifying the mock data directory and the output directory for logs.
await invokeMockData({
functionName: name,
dir: mockDir,
output: outputDir,
});
await debugFunction({
functionName: name,
entryDir,
builtDir,
mockDir,
outputDir,
});
}
});
}
5 changes: 3 additions & 2 deletions packages/cli/src/commands/faas/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { createLogger } from '@/utils';

import { buildFunction } from './build';
import { uploadFunction } from './upload';
import { DEFAULT_FUNCTION_DIR, DEFAULT_BUILT_DIR } from './config';

const COMMAND_NAME = 'deploy';
const logger = createLogger(COMMAND_NAME);
const defaultConfig = {
entryDir: './src/function',
builtDir: './dist',
entryDir: DEFAULT_FUNCTION_DIR,
builtDir: DEFAULT_BUILT_DIR,
};

type BuildFaasParams = typeof defaultConfig & {
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/src/commands/faas/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import {
} from '@/utils';
import { invokeCloudFunction } from '@/request/api';

import { DEFAULT_MOCK_DIR, DEFAULT_LOG_DIR } from './config';

const COMMAND_NAME = 'mock';
const logger = createLogger(COMMAND_NAME);
const defaultConfig = {
dir: './mock',
output: './log',
dir: DEFAULT_MOCK_DIR,
output: DEFAULT_LOG_DIR,
};

type BuildFaasParams = typeof defaultConfig & {
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/commands/faas/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import type { Command } from 'commander';

import { createLogger, resolveCwdAbsolutePath } from '@/utils';
import { createCloudFunction, updateCloudFunction } from '@/request/api';
import { DEFAULT_BUILT_DIR } from './config';

const COMMAND_NAME = 'upload';
const logger = createLogger(COMMAND_NAME);
const defaultConfig = {
builtDir: './dist',
builtDir: DEFAULT_BUILT_DIR,
};

type BuildFaasParams = typeof defaultConfig & {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/utils/cloud-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function scanForFunctionEntries(

export async function selectCloudFunction(
entryDir: string,
{ message = 'Select a cloud function' } = {},
{ message = 'Select a cloud function', defaultValue = '' } = {},
) {
const folderPath = resolveCwdAbsolutePath(entryDir);
const entry = scanForFunctionEntries(folderPath);
Expand All @@ -67,7 +67,7 @@ export async function selectCloudFunction(
transformer: input => {
return `Cloud function: ${input}`;
},
default: '',
default: defaultValue,
pageSize: 10,
validate(value) {
if (!value) {
Expand Down
59 changes: 52 additions & 7 deletions packages/io/src/operations.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { getBaaS, getBaseIo } from './baas';

import type { Operation, DeleteOperation, UpdateOperation } from './type';
import { getBaseIo } from './baas';

import type {
Operation,
DeleteOperation,
UpdateOperation,
OperatorData,
QueryOperationOptions,
MinCloudResponse,
FindListResponseData,
} from './type';
import { DEFAULT_ENABLE_TRIGGER } from './config';

const io = getBaseIo();
export function createTableOperation(tableName: string): Operation {
const BaaS = getBaaS();
return {
get table() {
return new BaaS.TableObject(tableName);
return io.table(tableName);
},

/**
Expand Down Expand Up @@ -63,18 +70,56 @@ export function createTableOperation(tableName: string): Operation {
{
expand,
select,
offset = 0,
limit = 20,
orderBy = '-created_at',
withCount = false,
plain = true,
} = {},
) {
return this.table
.setQuery(query)
.offset(offset)
.limit(limit)
.orderBy(orderBy)
.expand(expand)
.select(select)
.orderBy(orderBy)
.find({ withCount })
.then(res => (plain ? res.data : res));
.then(res => (plain ? res.data.objects : res));
},

/**
* Find the first record that matches `Query`
*/
async first<
Data extends object = OperatorData,
Plain extends boolean = true,
>(
query = io.query,
{
expand,
select,
offset = 0,
orderBy = '-created_at',
plain = true,
}: QueryOperationOptions = {},
) {
const result = await this.find<Data, Plain>(query, {
expand,
select,
offset,
orderBy,
plain,
limit: 1,
});

if (plain) return result[0];

const response = result as MinCloudResponse<FindListResponseData<Data>>;
return {
...result,
data: response.data.objects[0],
};
},

async delete(id, options = {}) {
Expand Down
Loading

0 comments on commit fb934e2

Please sign in to comment.