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: #4

Merged
merged 5 commits into from
Oct 25, 2023
Merged
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
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
PUBLIC_APPWRITE_ENDPOINT="http://localhost/"
PUBLIC_APPWRITE_API_VERSION="v1"
PUBLIC_APPWRITE_PROJECT_ID="your-project-id"
PUBLIC_SELECTED_PLATFORM="appwrite"
PUBLIC_SELECTED_PLATFORM="appwrite"
PUBLIC_DEBUG=
7 changes: 6 additions & 1 deletion src/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// See https://kit.svelte.dev/docs/types#app

import type { DebugStoreData } from "$lib/ui/widgets/debug-bar/types";

// for information about these interfaces
declare global {
namespace App {
// interface Error {}
// interface Locals {}
interface Locals {
debugData: DebugStoreData
}
// interface PageData {}
// interface Platform {}
}
Expand Down
10 changes: 10 additions & 0 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Handle } from "@sveltejs/kit";
import type { DebugStoreData } from "$lib/ui/widgets/debug-bar/types";
import { getMemoryUsageInMB } from "$lib/ui/widgets/debug-bar/utils/getMemoryUsageInMb";

export const handle: Handle = async ({ event, resolve }) => {

const memoryUsage = getMemoryUsageInMB();

return resolve(event);
}
3 changes: 2 additions & 1 deletion src/lib/common/constants/core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PUBLIC_SELECTED_PLATFORM } from "$env/static/public";
import { PUBLIC_DEBUG, PUBLIC_SELECTED_PLATFORM } from "$env/static/public";
import type { CoreData } from "./types";
/**
* @type {string}
Expand All @@ -25,6 +25,7 @@ const CORE: CoreData = {
value: 'en_US'
}
],
debug: PUBLIC_DEBUG || false,
}

export default CORE;
8 changes: 7 additions & 1 deletion src/lib/common/constants/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,17 @@ type ExecutionData<T> = {

} & CommonExecutionProperties<T>;

type LeadData = {
name: string
lastName: string
isActive: boolean | false
}

export type {
AccountData, SessionData, CoreData,
LanguageListData, DocumentListData,
UserData, GlobalDocumentProperties, DocumentData,
SEO, CommonExecutionProperties, ExecutionListData,
ExecutionPlaceHolderData,
ExecutionData, ExtraData
ExecutionData, ExtraData, LeadData
};
45 changes: 45 additions & 0 deletions src/lib/common/logger/logger_utility.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import { DebugStore } from "$lib/ui/widgets/debug-bar/store/DebugStore";
import { UpdateDebugStore } from "$lib/ui/widgets/debug-bar/store/DebugStore";
import { get } from "svelte/store";

const currentLogs = get(DebugStore);
let newLogList: any[] = [];

/**
* @class
* @name LoggerUtility
Expand All @@ -10,15 +17,43 @@ export class LoggerUtility {
* @param {string} message - The message to log.
*/
static info(message: string, property: any = '', filePath: string = ''): void {
const logMessage = {
type: 'info',
message: `(${filePath}) ${JSON.stringify(property)} [MESSAGE] ${message}`
};
this.logToDebugConsole(logMessage)
console.log(`[INFO] (${filePath}) ${JSON.stringify(property)} [MESSAGE] ${message}`);
}

static logToDebugConsole(log: any){
if(currentLogs?.logs){
if(currentLogs.logs.length > 0){
newLogList = [...currentLogs.logs, log];
currentLogs.logs = newLogList;
}else{

newLogList = [log];
currentLogs.logs = newLogList;
}
}else{
newLogList = [log];
currentLogs.logs = newLogList;
}

UpdateDebugStore(currentLogs);
}

/**
* Log a warning message.
*
* @param {string} message - The warning message.
*/
static warning(message: string, filePath: string = ''): void {
const logMessage = {
type: 'warning',
message: `(${filePath}): ${message}`
};
this.logToDebugConsole(logMessage)
console.warn(`[WARNING] (${filePath}): ${message}`);
}

Expand All @@ -28,6 +63,11 @@ export class LoggerUtility {
* @param {string} message - The error message.
*/
static error(message: string, filePath: string = ''): void {
const logMessage = {
type: 'error',
message: `(${filePath}): ${message}`
};
this.logToDebugConsole(logMessage)
console.error(`[ERROR] (${filePath}): ${message}`);
}

Expand All @@ -37,6 +77,11 @@ export class LoggerUtility {
* @param {string} message - The debug message.
*/
static debug(message: string, filePath: string = ''): void {
const logMessage = {
type: 'debug',
message: `(${filePath}): ${message}`
};
this.logToDebugConsole(logMessage)
console.debug(`[DEBUG] (${filePath}): ${message}`);
}
}
10 changes: 5 additions & 5 deletions src/lib/features/dashboard/widgets/recent-sales.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="space-y-8">
<div class="flex items-center">
<Avatar.Root class="h-9 w-9">
<Avatar.Image src="/avatars/01.png" alt="Avatar" />
<!-- <Avatar.Image src="/avatars/01.png" alt="Avatar" /> -->
<Avatar.Fallback>OM</Avatar.Fallback>
</Avatar.Root>
<div class="ml-4 space-y-1">
Expand All @@ -18,7 +18,7 @@
<Avatar.Root
class="flex h-9 w-9 items-center justify-center space-y-0 border"
>
<Avatar.Image src="/avatars/02.png" alt="Avatar" />
<!-- <Avatar.Image src="/avatars/02.png" alt="Avatar" /> -->
<Avatar.Fallback>JL</Avatar.Fallback>
</Avatar.Root>
<div class="ml-4 space-y-1">
Expand All @@ -29,7 +29,7 @@
</div>
<div class="flex items-center">
<Avatar.Root class="h-9 w-9">
<Avatar.Image src="/avatars/03.png" alt="Avatar" />
<!-- <Avatar.Image src="/avatars/03.png" alt="Avatar" /> -->
<Avatar.Fallback>IN</Avatar.Fallback>
</Avatar.Root>
<div class="ml-4 space-y-1">
Expand All @@ -42,7 +42,7 @@
</div>
<div class="flex items-center">
<Avatar.Root class="h-9 w-9">
<Avatar.Image src="/avatars/04.png" alt="Avatar" />
<!-- <Avatar.Image src="/avatars/04.png" alt="Avatar" /> -->
<Avatar.Fallback>WK</Avatar.Fallback>
</Avatar.Root>
<div class="ml-4 space-y-1">
Expand All @@ -53,7 +53,7 @@
</div>
<div class="flex items-center">
<Avatar.Root class="h-9 w-9">
<Avatar.Image src="/avatars/05.png" alt="Avatar" />
<!-- <Avatar.Image src="/avatars/05.png" alt="Avatar" /> -->
<Avatar.Fallback>SD</Avatar.Fallback>
</Avatar.Root>
<div class="ml-4 space-y-1">
Expand Down
42 changes: 22 additions & 20 deletions src/lib/services/databases.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LoggerUtility } from "$lib/common/logger/logger_utility";
import BackendPlatform from "$lib/platforms";
import type { DocumentListData, DocumentData, GlobalDocumentProperties } from "$lib/common/constants/types";
import type { DocumentListData } from "$lib/common/constants/types";
import { DocumentModel, createDocumentModel } from "./models/DocumentModel";

/**
* @name listDocuments
Expand All @@ -9,18 +10,19 @@ import type { DocumentListData, DocumentData, GlobalDocumentProperties } from "$
* @param queries
* @param logger
* @description Get a list of all the user's documents in a given collection. You can use the query params to filter your results.
* @returns DocumentList<T>
* @returns DocumentModel<T>
*/
async function listDocuments<T>(databseId: string, collectionId:string, queries?: any[], logger: typeof LoggerUtility = LoggerUtility): Promise<DocumentListData<T>>{
async function listDocuments<T>(databseId: string, collectionId:string, queries?: any[], logger: typeof LoggerUtility = LoggerUtility): Promise<DocumentListData<DocumentModel<T>>>{
const filePath = "lib/services/databases/ listDocuments"
try {
const result = await BackendPlatform.databases.listDocuments(databseId, collectionId, queries);
// @ts-ignore
const documents: T[] = result.documents;
return {

const documents: DocumentListData<DocumentModel<T>> = {
total: result.total,
documents
documents: result.documents.map((documentData) => createDocumentModel<DocumentModel<T>>(documentData))
}

return documents;
}catch(error){
if(error instanceof BackendPlatform.AppwriteException){
logger.error(`Appwrite exception: ${error.message} in:`, filePath);
Expand All @@ -31,7 +33,7 @@ async function listDocuments<T>(databseId: string, collectionId:string, queries?
return {
total: 0,
documents: []
}
} as DocumentListData<DocumentModel<T>>
}
}

Expand All @@ -43,14 +45,14 @@ async function listDocuments<T>(databseId: string, collectionId:string, queries?
* @param permissions
* @param logger
* @description Create a new Document. Before using this route, you should create a new collection resource using either a server integration API or directly from your database console.
* @returns DocumentData<T>
* @returns DocumentModel<T>
*/
async function createDocument<T extends GlobalDocumentProperties>(databseId: string, collectionId:string, data = {}, permissions?: string[],logger: typeof LoggerUtility = LoggerUtility): Promise<DocumentData<T> | null>{
async function createDocument<T>(databseId: string, collectionId:string, data = {}, permissions?: string[],logger: typeof LoggerUtility = LoggerUtility): Promise<DocumentModel<T> | null>{
const filePath = "lib/services/databases/ createDocument"
try {
const result = await BackendPlatform.databases.createDocument(databseId, collectionId, BackendPlatform.ID.unique(), data, permissions);
// @ts-ignore
const document: T = result;

const document: DocumentModel<T> = createDocumentModel(result);
return document;
}catch(error){
if(error instanceof BackendPlatform.AppwriteException){
Expand All @@ -69,14 +71,14 @@ async function createDocument<T extends GlobalDocumentProperties>(databseId: str
* @param queries
* @param logger
* @description Get a document by its unique ID. This endpoint response returns a JSON object with the document data.
* @returns DocumentData<T>
* @returns DocumentModel<T>
*/
async function getDocument<T extends GlobalDocumentProperties>(databseId: string, collectionId:string, documentId: string, queries?: string[], logger: typeof LoggerUtility = LoggerUtility): Promise<DocumentData<T> | null>{
async function getDocument<T>(databseId: string, collectionId:string, documentId: string, queries?: string[], logger: typeof LoggerUtility = LoggerUtility): Promise<DocumentModel<T> | null>{
const filePath = "lib/services/databases/ getDocument"
try {
const result = await BackendPlatform.databases.getDocument(databseId, collectionId, documentId, queries);
// @ts-ignore
const document: T = result;

const document: DocumentModel<T> = createDocumentModel(result);
return document;
}catch(error){
if(error instanceof BackendPlatform.AppwriteException){
Expand All @@ -97,14 +99,14 @@ async function getDocument<T extends GlobalDocumentProperties>(databseId: string
* @param permissions
* @param logger
* @description Update a document by its unique ID. Using the patch method you can pass only specific fields that will get updated.
* @returns DocumentData<T>
* @returns DocumentModel<T>
*/
async function updateDocument<T extends GlobalDocumentProperties>(databseId: string, collectionId:string, documentId: string, data = {}, permissions?: string[], logger: typeof LoggerUtility = LoggerUtility): Promise<DocumentData<T>| null> {
async function updateDocument<T>(databseId: string, collectionId:string, documentId: string, data = {}, permissions?: string[], logger: typeof LoggerUtility = LoggerUtility): Promise<DocumentModel<T>| null> {
const filePath = "lib/services/databases/ updateDocument"
try {
const result = await BackendPlatform.databases.updateDocument(databseId, collectionId, documentId, data, permissions);
// @ts-ignore
const document: T = result;

const document: DocumentModel<T> = createDocumentModel(result);
return document;
}catch(error){
if(error instanceof BackendPlatform.AppwriteException){
Expand Down
84 changes: 84 additions & 0 deletions src/lib/services/models/DocumentModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* Base class for document models.
*/
class BaseDocumentModel {
/**
* The unique identifier for the document.
*/
$id!: string;

/**
* The collection to which the document belongs.
*/
$collectionId!: string;

/**
* The database to which the document belongs.
*/
$databaseId!: string;

/**
* The creation date and time of the document.
*/
$createdAt!: string;

/**
* The last update date and time of the document.
*/
$updatedAt!: string;

/**
* Document permissions lists.
*/
$permissions!: string[];

/**
* Create a new BaseDocumentModel instance.
* @param data - Partial data for initializing the document model.
*/
constructor(data: Partial<BaseDocumentModel>) {
Object.assign(this, data);
}
}

/**
* Class for document models with additional properties.
* @template T - The type for additional properties.
*/
class DocumentModel<T> extends BaseDocumentModel {

/**
* Create a new DocumentModel instance.
* @param data - Data for initializing the document model, including additional properties.
*/
constructor(data: T & Partial<BaseDocumentModel>) {
super(data);
Object.assign(this, data);
//this.atrribures = data;
}
}

/**
* Convert the DocumentModel to a plain object without the DocumentModel wrapper.
* @returns {object} - The plain object with document properties.
*/
function toPlainObject<T>(documentModel: DocumentModel<T>): Omit<DocumentModel<T>, 'toPlainObject'> {
const { ...rest } = documentModel;
return rest;
}

/**
* Create a new document model.
* @template T - The type for additional properties.
* @param data - Data for initializing the document model, including additional properties.
* @returns A DocumentModel Object.
*/
function createDocumentModel<T extends Partial<BaseDocumentModel>>(data: T) {
const documentModel = new DocumentModel<T>(data);

return toPlainObject(documentModel);
}



export { DocumentModel, createDocumentModel, toPlainObject };
Loading