mirror of
https://github.com/appwrite/console.git
synced 2026-06-06 19:27:48 +00:00
fix: faker.
This commit is contained in:
+81
-28
@@ -6,17 +6,42 @@ import { sdk } from '$lib/stores/sdk';
|
||||
export async function generateColumns(
|
||||
project: Models.Project,
|
||||
databaseId: string,
|
||||
collectionId: string
|
||||
tableId: string
|
||||
): Promise<Columns[]> {
|
||||
const client = sdk.forProject(project.region, project.$id);
|
||||
|
||||
return await Promise.all([
|
||||
client.grids.createStringColumn(databaseId, collectionId, 'name', 255, false),
|
||||
client.grids.createEmailColumn(databaseId, collectionId, 'email', false),
|
||||
client.grids.createIntegerColumn(databaseId, collectionId, 'age', false, 0, 150),
|
||||
client.grids.createStringColumn(databaseId, collectionId, 'city', 100, false),
|
||||
client.grids.createStringColumn(databaseId, collectionId, 'description', 1000, false),
|
||||
client.grids.createBooleanColumn(databaseId, collectionId, 'active', false)
|
||||
client.grids.createStringColumn({
|
||||
databaseId,
|
||||
tableId,
|
||||
key: 'name',
|
||||
size: 255,
|
||||
required: false
|
||||
}),
|
||||
client.grids.createEmailColumn({ databaseId, tableId, key: 'email', required: false }),
|
||||
client.grids.createIntegerColumn({
|
||||
databaseId,
|
||||
tableId,
|
||||
key: 'age',
|
||||
required: false,
|
||||
min: 0,
|
||||
max: 150
|
||||
}),
|
||||
client.grids.createStringColumn({
|
||||
databaseId,
|
||||
tableId,
|
||||
key: 'city',
|
||||
size: 100,
|
||||
required: false
|
||||
}),
|
||||
client.grids.createStringColumn({
|
||||
databaseId,
|
||||
tableId,
|
||||
key: 'description',
|
||||
size: 1000,
|
||||
required: false
|
||||
}),
|
||||
client.grids.createBooleanColumn({ databaseId, tableId, key: 'active', required: false })
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -38,7 +63,9 @@ export function generateFakeRecords(
|
||||
const id = ID.unique();
|
||||
ids.push(id);
|
||||
|
||||
let row: Record<string, string | number | boolean> = { $id: id };
|
||||
let row: Record<string, string | number | boolean | Array<string | number | boolean>> = {
|
||||
$id: id
|
||||
};
|
||||
|
||||
if (filteredColumns.length === 0) {
|
||||
row = {
|
||||
@@ -84,23 +111,61 @@ function generateStringValue(key: string, maxLength: number): string {
|
||||
return text.length <= maxLength ? text : text.substring(0, maxLength);
|
||||
}
|
||||
|
||||
function generateValueForColumn(attribute: Columns): string | number | boolean | null {
|
||||
switch (attribute.type) {
|
||||
function generateValueForColumn(
|
||||
column: Columns
|
||||
): string | number | boolean | null | Array<string | number | boolean> {
|
||||
if (column.array) {
|
||||
const arraySize = faker.number.int({ min: 1, max: 5 });
|
||||
const items: Array<string | number | boolean> = [];
|
||||
|
||||
for (let i = 0; i < arraySize; i++) {
|
||||
const itemAttribute = { ...column, array: false };
|
||||
const item = generateSingleValue(itemAttribute);
|
||||
if (item !== null) {
|
||||
items.push(item);
|
||||
}
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
return generateSingleValue(column);
|
||||
}
|
||||
|
||||
function generateSingleValue(column: Columns): string | number | boolean | null {
|
||||
switch (column.type) {
|
||||
case 'string': {
|
||||
const stringAttr = attribute as Models.ColumnString;
|
||||
const maxLength = Math.min(stringAttr.size ?? 255, 1000);
|
||||
return generateStringValue(attribute.key, maxLength);
|
||||
if ('format' in column && column.format) {
|
||||
switch (column.format) {
|
||||
case 'email': {
|
||||
return faker.internet.email();
|
||||
}
|
||||
|
||||
case 'ip': {
|
||||
return faker.internet.ip();
|
||||
}
|
||||
|
||||
case 'url': {
|
||||
return faker.internet.url();
|
||||
}
|
||||
}
|
||||
return ''
|
||||
} else {
|
||||
const stringAttr = column as Models.ColumnString;
|
||||
const maxLength = Math.min(stringAttr.size ?? 255, 1000);
|
||||
return generateStringValue(column.key, maxLength);
|
||||
}
|
||||
}
|
||||
|
||||
case 'integer': {
|
||||
const intAttr = attribute as Models.ColumnInteger;
|
||||
const intAttr = column as Models.ColumnInteger;
|
||||
const min = intAttr.min ?? 0;
|
||||
const max = intAttr.max ?? 1000000;
|
||||
return faker.number.int({ min, max });
|
||||
}
|
||||
|
||||
case 'float': {
|
||||
const floatAttr = attribute as Models.ColumnFloat;
|
||||
const floatAttr = column as Models.ColumnFloat;
|
||||
const min = floatAttr.min ?? 0;
|
||||
const max = floatAttr.max ?? 1000000;
|
||||
const precision = 2;
|
||||
@@ -117,20 +182,8 @@ function generateValueForColumn(attribute: Columns): string | number | boolean |
|
||||
return faker.date.recent({ days: 365 }).toISOString();
|
||||
}
|
||||
|
||||
case 'email': {
|
||||
return faker.internet.email();
|
||||
}
|
||||
|
||||
case 'ip': {
|
||||
return faker.internet.ip();
|
||||
}
|
||||
|
||||
case 'url': {
|
||||
return faker.internet.url();
|
||||
}
|
||||
|
||||
case 'enum': {
|
||||
const enumAttr = attribute as Models.ColumnEnum;
|
||||
const enumAttr = column as Models.ColumnEnum;
|
||||
if (enumAttr.elements && enumAttr.elements.length > 0) {
|
||||
return faker.helpers.arrayElement(enumAttr.elements);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user