From 17beb7ecdf3e89b0ffee43f4365f3b2dc4950f99 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Fri, 22 Jul 2022 04:39:00 +0430 Subject: [PATCH 01/13] feat(jatabase): demo --- demo/jatabase.ts | 39 +++++++++++++++++++++++++++++ demo/tsconfig.json | 3 ++- packages/core/jatabase/package.json | 4 ++- tsconfig.json | 2 +- 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 demo/jatabase.ts diff --git a/demo/jatabase.ts b/demo/jatabase.ts new file mode 100644 index 000000000..341bd23ad --- /dev/null +++ b/demo/jatabase.ts @@ -0,0 +1,39 @@ +import {Jatabase} from '@alwatr/jatabase'; + +import type {DocumentObject} from '@alwatr/jatabase'; + +interface User extends DocumentObject { + fname: string; + lname: string; + email: string; + token?: string; +} + +const db = new Jatabase('user-list'); + +// await db.ready +// or +db.ready.then(() => { + console.log('db loaded and ready to access.'); + + const ali = db.get('alimd'); + + if (ali == null) { + console.log('ali not found'); + return; + } + + console.log(ali); + /** + * { + * _id: 'alimd', + * fname: 'Ali', + * lname: 'MM', + * email: 'i@ali.md', + * } + */ + + ali.token = '123qwe'; + + db.set(ali); +}); diff --git a/demo/tsconfig.json b/demo/tsconfig.json index e8a9d84b3..41f266830 100644 --- a/demo/tsconfig.json +++ b/demo/tsconfig.json @@ -14,7 +14,8 @@ {"path": "../packages/core/router"}, {"path": "../packages/core/i18n"}, {"path": "../packages/core/math"}, - {"path": "../packages/core/element"} + {"path": "../packages/core/element"}, + {"path": "../packages/core/jatabase"}, ], "exclude": ["*.d.ts", "node_modules"] } diff --git a/packages/core/jatabase/package.json b/packages/core/jatabase/package.json index b71636007..4a56aa4bb 100644 --- a/packages/core/jatabase/package.json +++ b/packages/core/jatabase/package.json @@ -1,11 +1,13 @@ { "name": "@alwatr/jatabase", "version": "0.0.0", - "description": "Elegant powerful micro in-memory Database with JSON disk backed, written in tiny TypeScript ES module.", + "description": "Elegant powerful micro in-memory document Database with disk backed, written in tiny TypeScript ES module.", "keywords": [ "database", "storage", "json", + "data", + "data-storage", "file", "typescript", "esm", diff --git a/tsconfig.json b/tsconfig.json index 07386c836..ecbcd115d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -109,13 +109,13 @@ "exclude": [], "references": [ {"path": "./packages/core/logger"}, - {"path": "./packages/core/jatabase"}, {"path": "./packages/core/fetch"}, {"path": "./packages/core/signal"}, {"path": "./packages/core/router"}, {"path": "./packages/core/i18n"}, {"path": "./packages/core/math"}, {"path": "./packages/core/micro-server"}, + {"path": "./packages/core/jatabase"}, {"path": "./demo"} ] } From 0fdd533446943502c187d961ebfcbf872f7c15e1 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Fri, 22 Jul 2022 04:39:12 +0430 Subject: [PATCH 02/13] feat(jatabase): impeliment class --- packages/core/jatabase/src/jatabase.ts | 123 ++++++++++++++++++++++++- packages/core/jatabase/src/type.ts | 9 ++ packages/core/jatabase/src/util.ts | 2 +- 3 files changed, 132 insertions(+), 2 deletions(-) diff --git a/packages/core/jatabase/src/jatabase.ts b/packages/core/jatabase/src/jatabase.ts index cb0ff5c3b..01470fc64 100644 --- a/packages/core/jatabase/src/jatabase.ts +++ b/packages/core/jatabase/src/jatabase.ts @@ -1 +1,122 @@ -export {}; +import {alwatrRegisteredList, createLogger} from '@alwatr/logger'; + +import {readJsonFile, writeJsonFile} from './util'; + +import type {DocumentObject, DocumentListStorage} from './type'; +import type {Logger} from '@alwatr/logger/type'; + +export * from './type'; + +alwatrRegisteredList.push({ + name: '@alwatr/jatabase', + version: '{{ALWATR_VERSION}}', +}); + +/** + * Elegant powerful micro in-memory document Database with disk backed. + * + * @example + * import {Jatabase} from '@alwatr/jatabase'; + * const db = new Jatabase('user-list'); + * await db.ready + * const user = db.get('my-user-id', true); + */ +export class Jatabase { + isReady = false; + readonly ready: Promise; + readonly name: string; + + protected _logger: Logger; + protected _storage: DocumentListStorage = {}; + protected _storagePath: string; + + constructor(name: string, pathPrefix = 'data/') { + this._logger = createLogger(`jatabase:${name}`); + this.name = name; + this._storagePath = `${pathPrefix}/${name}.json`; + this.ready = this._init(); + } + + private async _init(): Promise { + this._logger.logMethod('_init'); + this._storage = await readJsonFile>(this._storagePath); + this.isReady = true; + this._logger.logProperty('isReady', this.isReady); + } + + /** + * Get a document object by id. + * + * @param documentId The id of the document object. + * @param fastInstance by default it will return a copy of the document. + * if you set fastInstance to true, it will return the original document. + * This is dangerous but much faster and you should use it only if you know what you are doing. + */ + get(documentId: string, fastInstance?: boolean): DocumentType | null { + this._logger.logMethodArgs('get', documentId); + const documentObject = this._storage[documentId]; + if (documentObject == null) { + return null; + } else if (fastInstance) { + return documentObject; + } else { + return JSON.parse(JSON.stringify(documentObject)); + } + } + + /** + * Insert/update a document object in the storage. + * + * @param documentObject The document object to insert/update contain `_id`. + * @param fastInstance by default it will make a copy of the document before set. + * if you set fastInstance to true, it will set the original document. + * This is dangerous but much faster and you should use it only if you know what you are doing. + */ + set(documentObject: DocumentType, fastInstance?: boolean): void { + this._logger.logMethodArgs('set', documentObject._id); + + // update meta + documentObject._updated = Date.now(); + if (documentObject._created == null) { + documentObject._created = documentObject._updated; + } + if (documentObject._rev == null) { + documentObject._rev = 0; + } else { + documentObject._rev++; + } + + if (fastInstance !== true) { + // clone + documentObject = JSON.parse(JSON.stringify(documentObject)); + } + + this._storage[documentObject._id] = documentObject; + this._storage._last = documentObject._id; + } + + /** + * Remove a document object from the storage. + */ + remove(documentId: string): void { + this._logger.logMethodArgs('remove', documentId); + delete this._storage[documentId]; + } + + private _saveTimer?: NodeJS.Timeout | number; + /** + * Save the storage to disk. + */ + save(): void { + this._logger.logMethod('save'); + if (this._saveTimer != null) { + return; + } + this._saveTimer = setTimeout(() => { + this._logger.logMethod('save.timeout'); + clearTimeout(this._saveTimer); + delete this._saveTimer; + writeJsonFile(this._storagePath, this._storage); + }, 100); + } +} diff --git a/packages/core/jatabase/src/type.ts b/packages/core/jatabase/src/type.ts index c3f022db3..dc7f6a403 100644 --- a/packages/core/jatabase/src/type.ts +++ b/packages/core/jatabase/src/type.ts @@ -1 +1,10 @@ export type JSON = Record; + +export interface DocumentObject { + _id: string; + _rev?: number; + _created?: number; + _updated?: number; +} + +export type DocumentListStorage = Record & {_last?: string}; diff --git a/packages/core/jatabase/src/util.ts b/packages/core/jatabase/src/util.ts index 90ab51582..9fab784af 100644 --- a/packages/core/jatabase/src/util.ts +++ b/packages/core/jatabase/src/util.ts @@ -26,7 +26,7 @@ export async function readJsonFile(path: string): Promise { } try { - return JSON.parse(fileContent); + return JSON.parse(fileContent) as T; } catch (err) { throw new Error('invalid_json'); } From 545fe3e7be9cc2d44e83d6f138c2ed0192dd1e24 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Fri, 22 Jul 2022 17:30:22 +0430 Subject: [PATCH 03/13] fix(storage): import ext for node --- packages/core/jatabase/src/jatabase.ts | 10 ++++++---- packages/core/jatabase/src/util.ts | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/core/jatabase/src/jatabase.ts b/packages/core/jatabase/src/jatabase.ts index 01470fc64..f030b087c 100644 --- a/packages/core/jatabase/src/jatabase.ts +++ b/packages/core/jatabase/src/jatabase.ts @@ -1,11 +1,13 @@ +import {existsSync} from 'fs'; + import {alwatrRegisteredList, createLogger} from '@alwatr/logger'; -import {readJsonFile, writeJsonFile} from './util'; +import {readJsonFile, writeJsonFile} from './util.js'; -import type {DocumentObject, DocumentListStorage} from './type'; -import type {Logger} from '@alwatr/logger/type'; +import type {DocumentObject, DocumentListStorage} from './type.js'; +import type {Logger} from '@alwatr/logger/type.js'; -export * from './type'; +export * from './type.js'; alwatrRegisteredList.push({ name: '@alwatr/jatabase', diff --git a/packages/core/jatabase/src/util.ts b/packages/core/jatabase/src/util.ts index 9fab784af..d722ffd1e 100644 --- a/packages/core/jatabase/src/util.ts +++ b/packages/core/jatabase/src/util.ts @@ -1,7 +1,7 @@ import {existsSync, promises as fs} from 'fs'; import {resolve, dirname} from 'path'; -import type {JSON} from './type'; +import type {JSON} from './type.js'; // @TODO: add debug log From 010555176ea262080b774fb4c51a16acc91bccf1 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Fri, 22 Jul 2022 17:31:02 +0430 Subject: [PATCH 04/13] fix(storage): make empty storage if file not exist --- packages/core/jatabase/src/jatabase.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/core/jatabase/src/jatabase.ts b/packages/core/jatabase/src/jatabase.ts index f030b087c..13a65e1b6 100644 --- a/packages/core/jatabase/src/jatabase.ts +++ b/packages/core/jatabase/src/jatabase.ts @@ -32,7 +32,7 @@ export class Jatabase { protected _storage: DocumentListStorage = {}; protected _storagePath: string; - constructor(name: string, pathPrefix = 'data/') { + constructor(name: string, pathPrefix = 'data') { this._logger = createLogger(`jatabase:${name}`); this.name = name; this._storagePath = `${pathPrefix}/${name}.json`; @@ -41,7 +41,11 @@ export class Jatabase { private async _init(): Promise { this._logger.logMethod('_init'); - this._storage = await readJsonFile>(this._storagePath); + if (existsSync(this._storagePath)) { + this._storage = await readJsonFile>(this._storagePath); + } else { + this._storage = {}; + } this.isReady = true; this._logger.logProperty('isReady', this.isReady); } From a1b671d9beb8909d1fd89a1ffb7f80e1115b3cc3 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Fri, 22 Jul 2022 17:31:31 +0430 Subject: [PATCH 05/13] fix(storage): auto save request on set --- packages/core/jatabase/src/jatabase.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/core/jatabase/src/jatabase.ts b/packages/core/jatabase/src/jatabase.ts index 13a65e1b6..d59851c62 100644 --- a/packages/core/jatabase/src/jatabase.ts +++ b/packages/core/jatabase/src/jatabase.ts @@ -97,8 +97,9 @@ export class Jatabase { documentObject = JSON.parse(JSON.stringify(documentObject)); } - this._storage[documentObject._id] = documentObject; this._storage._last = documentObject._id; + this._storage[documentObject._id] = documentObject; + this.save(); } /** @@ -114,12 +115,12 @@ export class Jatabase { * Save the storage to disk. */ save(): void { - this._logger.logMethod('save'); + this._logger.logMethod('save.request'); if (this._saveTimer != null) { return; } this._saveTimer = setTimeout(() => { - this._logger.logMethod('save.timeout'); + this._logger.logMethod('save.action'); clearTimeout(this._saveTimer); delete this._saveTimer; writeJsonFile(this._storagePath, this._storage); From 508bd56ee179d326762ffcb0a4e749bc7f74057f Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Fri, 22 Jul 2022 17:32:01 +0430 Subject: [PATCH 06/13] fix(storage): prevent to lost old meta data --- packages/core/jatabase/src/jatabase.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/packages/core/jatabase/src/jatabase.ts b/packages/core/jatabase/src/jatabase.ts index d59851c62..f147dcbea 100644 --- a/packages/core/jatabase/src/jatabase.ts +++ b/packages/core/jatabase/src/jatabase.ts @@ -82,18 +82,12 @@ export class Jatabase { this._logger.logMethodArgs('set', documentObject._id); // update meta + const oldData = this._storage[documentObject._id]; documentObject._updated = Date.now(); - if (documentObject._created == null) { - documentObject._created = documentObject._updated; - } - if (documentObject._rev == null) { - documentObject._rev = 0; - } else { - documentObject._rev++; - } + documentObject._created = oldData?._created ?? documentObject._updated; + documentObject._rev = (oldData?._rev ?? 0) + 1; if (fastInstance !== true) { - // clone documentObject = JSON.parse(JSON.stringify(documentObject)); } From f1c90b4b00a8696375e1a2f15d8b17378a33c7f2 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Fri, 22 Jul 2022 17:32:20 +0430 Subject: [PATCH 07/13] fix(storage): DocumentListStorage type issue --- packages/core/jatabase/src/type.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/core/jatabase/src/type.ts b/packages/core/jatabase/src/type.ts index dc7f6a403..33efb35cb 100644 --- a/packages/core/jatabase/src/type.ts +++ b/packages/core/jatabase/src/type.ts @@ -7,4 +7,5 @@ export interface DocumentObject { _updated?: number; } -export type DocumentListStorage = Record & {_last?: string}; +export type DocumentListStorage = + Record & {_last?: string}; From dcd0abc66882c914ce57aace8fdf68e58995cda0 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Fri, 22 Jul 2022 17:32:45 +0430 Subject: [PATCH 08/13] fix(storage): minify saved json --- packages/core/jatabase/src/util.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/jatabase/src/util.ts b/packages/core/jatabase/src/util.ts index d722ffd1e..c9393420e 100644 --- a/packages/core/jatabase/src/util.ts +++ b/packages/core/jatabase/src/util.ts @@ -50,7 +50,7 @@ export async function writeJsonFile(path: string, dataObject: T) let jsonContent; try { - jsonContent = JSON.stringify(dataObject, undefined, 2); + jsonContent = JSON.stringify(dataObject); } catch (err) { throw new Error('stringify_failed'); } From feef1cfc27ccd11299bc3a446f75d3ccde6ef207 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Fri, 22 Jul 2022 17:33:27 +0430 Subject: [PATCH 09/13] doc(storage): improve demo --- .gitignore | 2 ++ demo/jatabase-size-test.ts | 25 ++++++++++++++++++++++ demo/jatabase.ts | 43 +++++++++++++++++++++++++------------- 3 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 demo/jatabase-size-test.ts diff --git a/.gitignore b/.gitignore index 936896803..ce3421872 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,5 @@ package-lock.json .env .env.test test.json + +temp diff --git a/demo/jatabase-size-test.ts b/demo/jatabase-size-test.ts new file mode 100644 index 000000000..0e51e0b4c --- /dev/null +++ b/demo/jatabase-size-test.ts @@ -0,0 +1,25 @@ +import {Jatabase} from '@alwatr/jatabase'; +import {random} from '@alwatr/math'; + +import type {DocumentObject} from '@alwatr/jatabase'; + +interface User extends DocumentObject { + fname: string; + lname: string; + email: string; + token: string; +} + +const db = new Jatabase('junk-data', 'temp'); + +db.ready.then(() => { + for (let i = 0; i < 10000; i++) { + db.set({ + _id: random.string(4, 16), + fname: random.string(4, 16), + lname: random.string(4, 32), + email: random.string(8, 32), + token: random.string(16), + }); + } +}); diff --git a/demo/jatabase.ts b/demo/jatabase.ts index 341bd23ad..4dedc336e 100644 --- a/demo/jatabase.ts +++ b/demo/jatabase.ts @@ -9,31 +9,44 @@ interface User extends DocumentObject { token?: string; } -const db = new Jatabase('user-list'); +const db = new Jatabase('user-list', 'temp'); // await db.ready // or db.ready.then(() => { console.log('db loaded and ready to access.'); - const ali = db.get('alimd'); + let ali = db.get('alimd'); if (ali == null) { console.log('ali not found'); - return; + ali = { + _id: 'alimd', + fname: 'Ali', + lname: 'Mihandoost', + email: 'ali@mihandoost.com', + }; + } else { + console.log('ali found: %o', ali); + /** + * { + * _id: 'alimd', + * fname: 'Ali', + * lname: 'MM', + * email: 'i@ali.md', + * } + */ + + ali.token = Math.random().toString(36).substring(2, 15); } - console.log(ali); - /** - * { - * _id: 'alimd', - * fname: 'Ali', - * lname: 'MM', - * email: 'i@ali.md', - * } - */ - - ali.token = '123qwe'; - db.set(ali); + + db.set({ + _id: 'fmd', + fname: 'Fatemeh', + lname: 'Mihandoost', + email: 'Fatemeh@mihandoost.com', + token: Math.random().toString(36).substring(2, 15), + }); }); From 9763eb24a7a7d0174c5b1b1e080efea1d05511d7 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Fri, 22 Jul 2022 17:37:02 +0430 Subject: [PATCH 10/13] feat(math); add random.string() --- packages/core/math/README.md | 11 +++++++++++ packages/core/math/src/math.ts | 26 +++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/packages/core/math/README.md b/packages/core/math/README.md index fda9ba54c..684652a85 100644 --- a/packages/core/math/README.md +++ b/packages/core/math/README.md @@ -151,6 +151,17 @@ Generate a random float between min and max. console.log(random.float(1, 10)); // somewhere between 1 and 10 ``` +### `string: (min: number, max?: number): string` + +Generate a random string with random length. +The string will contain only characters from the characters list. +The length of the string will be between min and max (max included). +If max not specified, the length will be set to min. + +```js +console.log(random.string(6)); // something like 'Aab1V2' +``` + ### `step(min: number, max: number, step: number): number` Generate a random integer between min and max with a step. diff --git a/packages/core/math/src/math.ts b/packages/core/math/src/math.ts index 9d06b04cb..acbc0a102 100644 --- a/packages/core/math/src/math.ts +++ b/packages/core/math/src/math.ts @@ -66,6 +66,9 @@ export const transformToRange = (x: number, options: TransformRangeOptions): num return y; }; +const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; +const charactersLength = characters.length; + export const random = { /** * Returns a float random number between 0 and 1 (1 Not included). @@ -81,7 +84,7 @@ export const random = { }, /** - * Generate a random integer between min and max. + * Generate a random integer number between min and max (max included). * * Example: * @@ -92,6 +95,7 @@ export const random = { integer: (min: number, max: number): number => Math.floor(random.float(min, max + 1)), /** + * Generate a random float number between min and max (max not included). * * Example: * @@ -101,6 +105,26 @@ export const random = { */ float: (min: number, max: number): number => random.value * (max - min) + min, + /** + * Generate a random string with random length. + * The string will contain only characters from the characters list. + * The length of the string will be between min and max (max included). + * If max not specified, the length will be set to min. + * + * Example: + * + *```js + * console.log(random.string(6)); // something like 'Aab1V2' + * ``` + */ + string: (min: number, max?: number): string => { + let result = ''; + for (let i = max != null ? random.integer(min, max) : min; i > 0; i--) { + result += characters.charAt(Math.floor(random.value * charactersLength)); + } + return result; + }, + /** * Generate a random integer between min and max with a step. * From cb459bf88d12c9debcedd9f53f32eab80f414105 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Fri, 22 Jul 2022 17:43:46 +0430 Subject: [PATCH 11/13] doc(storage): update desc --- packages/core/jatabase/README.md | 2 +- packages/core/jatabase/package.json | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/core/jatabase/README.md b/packages/core/jatabase/README.md index 7857a97ea..8959f8c8b 100644 --- a/packages/core/jatabase/README.md +++ b/packages/core/jatabase/README.md @@ -1,3 +1,3 @@ # @alwatr/jatabase -Elegant powerful micro in-memory Database with JSON disk backed, written in tiny TypeScript ES module. +Elegant micro in-memory json-like storage with disk backed, Faster NoSQL Database written in tiny TypeScript ES module. diff --git a/packages/core/jatabase/package.json b/packages/core/jatabase/package.json index 4a56aa4bb..0fcc7a51b 100644 --- a/packages/core/jatabase/package.json +++ b/packages/core/jatabase/package.json @@ -1,11 +1,13 @@ { "name": "@alwatr/jatabase", "version": "0.0.0", - "description": "Elegant powerful micro in-memory document Database with disk backed, written in tiny TypeScript ES module.", + "description": "Elegant micro in-memory json-like storage with disk backed, Faster NoSQL Database written in tiny TypeScript ES module.", "keywords": [ "database", "storage", "json", + "nosql", + "no-sql", "data", "data-storage", "file", From fbbd0a2c22e1c6f1705af4872cd7f1ae3b7937d6 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Fri, 22 Jul 2022 17:51:38 +0430 Subject: [PATCH 12/13] refactor(storage): rename package --- .../big-data-test.ts} | 6 +++--- demo/{jatabase.ts => storage/index.ts} | 6 +++--- demo/tsconfig.json | 2 +- packages/core/{jatabase => storage}/README.md | 2 +- packages/core/{jatabase => storage}/package.json | 10 +++++----- .../src/jatabase.ts => storage/src/storage.ts} | 10 +++++----- packages/core/{jatabase => storage}/src/type.ts | 0 packages/core/{jatabase => storage}/src/util.ts | 0 packages/core/{jatabase => storage}/tsconfig.json | 0 tsconfig.json | 2 +- 10 files changed, 19 insertions(+), 19 deletions(-) rename demo/{jatabase-size-test.ts => storage/big-data-test.ts} (72%) rename demo/{jatabase.ts => storage/index.ts} (84%) rename packages/core/{jatabase => storage}/README.md (86%) rename packages/core/{jatabase => storage}/package.json (86%) rename packages/core/{jatabase/src/jatabase.ts => storage/src/storage.ts} (93%) rename packages/core/{jatabase => storage}/src/type.ts (100%) rename packages/core/{jatabase => storage}/src/util.ts (100%) rename packages/core/{jatabase => storage}/tsconfig.json (100%) diff --git a/demo/jatabase-size-test.ts b/demo/storage/big-data-test.ts similarity index 72% rename from demo/jatabase-size-test.ts rename to demo/storage/big-data-test.ts index 0e51e0b4c..449909ac3 100644 --- a/demo/jatabase-size-test.ts +++ b/demo/storage/big-data-test.ts @@ -1,7 +1,7 @@ -import {Jatabase} from '@alwatr/jatabase'; import {random} from '@alwatr/math'; +import {AlwatrStorage} from '@alwatr/storage'; -import type {DocumentObject} from '@alwatr/jatabase'; +import type {DocumentObject} from '@alwatr/storage'; interface User extends DocumentObject { fname: string; @@ -10,7 +10,7 @@ interface User extends DocumentObject { token: string; } -const db = new Jatabase('junk-data', 'temp'); +const db = new AlwatrStorage('junk-data', 'temp'); db.ready.then(() => { for (let i = 0; i < 10000; i++) { diff --git a/demo/jatabase.ts b/demo/storage/index.ts similarity index 84% rename from demo/jatabase.ts rename to demo/storage/index.ts index 4dedc336e..00d9b4e5b 100644 --- a/demo/jatabase.ts +++ b/demo/storage/index.ts @@ -1,6 +1,6 @@ -import {Jatabase} from '@alwatr/jatabase'; +import {AlwatrStorage} from '@alwatr/storage'; -import type {DocumentObject} from '@alwatr/jatabase'; +import type {DocumentObject} from '@alwatr/storage'; interface User extends DocumentObject { fname: string; @@ -9,7 +9,7 @@ interface User extends DocumentObject { token?: string; } -const db = new Jatabase('user-list', 'temp'); +const db = new AlwatrStorage('user-list', 'temp'); // await db.ready // or diff --git a/demo/tsconfig.json b/demo/tsconfig.json index 41f266830..565a7fb4c 100644 --- a/demo/tsconfig.json +++ b/demo/tsconfig.json @@ -15,7 +15,7 @@ {"path": "../packages/core/i18n"}, {"path": "../packages/core/math"}, {"path": "../packages/core/element"}, - {"path": "../packages/core/jatabase"}, + {"path": "../packages/core/storage"}, ], "exclude": ["*.d.ts", "node_modules"] } diff --git a/packages/core/jatabase/README.md b/packages/core/storage/README.md similarity index 86% rename from packages/core/jatabase/README.md rename to packages/core/storage/README.md index 8959f8c8b..bd204c0ac 100644 --- a/packages/core/jatabase/README.md +++ b/packages/core/storage/README.md @@ -1,3 +1,3 @@ -# @alwatr/jatabase +# @alwatr/storage Elegant micro in-memory json-like storage with disk backed, Faster NoSQL Database written in tiny TypeScript ES module. diff --git a/packages/core/jatabase/package.json b/packages/core/storage/package.json similarity index 86% rename from packages/core/jatabase/package.json rename to packages/core/storage/package.json index 0fcc7a51b..1ccd8d79b 100644 --- a/packages/core/jatabase/package.json +++ b/packages/core/storage/package.json @@ -1,5 +1,5 @@ { - "name": "@alwatr/jatabase", + "name": "@alwatr/storage", "version": "0.0.0", "description": "Elegant micro in-memory json-like storage with disk backed, Faster NoSQL Database written in tiny TypeScript ES module.", "keywords": [ @@ -15,9 +15,9 @@ "esm", "alwatr" ], - "main": "jatabase.js", + "main": "storage.js", "type": "module", - "types": "jatabase.d.ts", + "types": "storage.d.ts", "author": "S. Ali Mihandoost (https://ali.mihandoost.com)", "license": "MIT", "files": [ @@ -29,9 +29,9 @@ "repository": { "type": "git", "url": "https://github.com/AliMD/alwatr", - "directory": "package/jatabase" + "directory": "package/storage" }, - "homepage": "https://github.com/AliMD/alwatr/tree/main/package/jatabase#readme", + "homepage": "https://github.com/AliMD/alwatr/tree/main/package/storage#readme", "bugs": { "url": "https://github.com/AliMD/alwatr/issues" }, diff --git a/packages/core/jatabase/src/jatabase.ts b/packages/core/storage/src/storage.ts similarity index 93% rename from packages/core/jatabase/src/jatabase.ts rename to packages/core/storage/src/storage.ts index f147dcbea..80362519b 100644 --- a/packages/core/jatabase/src/jatabase.ts +++ b/packages/core/storage/src/storage.ts @@ -10,7 +10,7 @@ import type {Logger} from '@alwatr/logger/type.js'; export * from './type.js'; alwatrRegisteredList.push({ - name: '@alwatr/jatabase', + name: '@alwatr/storage', version: '{{ALWATR_VERSION}}', }); @@ -18,12 +18,12 @@ alwatrRegisteredList.push({ * Elegant powerful micro in-memory document Database with disk backed. * * @example - * import {Jatabase} from '@alwatr/jatabase'; - * const db = new Jatabase('user-list'); + * import {AlwatrStorage} from '@alwatr/storage'; + * const db = new AlwatrStorage('user-list'); * await db.ready * const user = db.get('my-user-id', true); */ -export class Jatabase { +export class AlwatrStorage { isReady = false; readonly ready: Promise; readonly name: string; @@ -33,7 +33,7 @@ export class Jatabase { protected _storagePath: string; constructor(name: string, pathPrefix = 'data') { - this._logger = createLogger(`jatabase:${name}`); + this._logger = createLogger(`alwatr-storage:${name}`); this.name = name; this._storagePath = `${pathPrefix}/${name}.json`; this.ready = this._init(); diff --git a/packages/core/jatabase/src/type.ts b/packages/core/storage/src/type.ts similarity index 100% rename from packages/core/jatabase/src/type.ts rename to packages/core/storage/src/type.ts diff --git a/packages/core/jatabase/src/util.ts b/packages/core/storage/src/util.ts similarity index 100% rename from packages/core/jatabase/src/util.ts rename to packages/core/storage/src/util.ts diff --git a/packages/core/jatabase/tsconfig.json b/packages/core/storage/tsconfig.json similarity index 100% rename from packages/core/jatabase/tsconfig.json rename to packages/core/storage/tsconfig.json diff --git a/tsconfig.json b/tsconfig.json index ecbcd115d..be9336cba 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -115,7 +115,7 @@ {"path": "./packages/core/i18n"}, {"path": "./packages/core/math"}, {"path": "./packages/core/micro-server"}, - {"path": "./packages/core/jatabase"}, + {"path": "./packages/core/storage"}, {"path": "./demo"} ] } From 73dbf029325169d900558e2361fec9fa4303e7e4 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Fri, 22 Jul 2022 17:51:59 +0430 Subject: [PATCH 13/13] fix(micro-server): logger scope --- packages/core/micro-server/src/micro-server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/micro-server/src/micro-server.ts b/packages/core/micro-server/src/micro-server.ts index 46f8f5c07..a0669d5a2 100644 --- a/packages/core/micro-server/src/micro-server.ts +++ b/packages/core/micro-server/src/micro-server.ts @@ -11,7 +11,7 @@ alwatrRegisteredList.push({ }); export class AlwatrMicroServer { - protected logger = createLogger(`micro-server:${this.port}`); + protected logger = createLogger(`alwatr-micro-server:${this.port}`); protected server = createServer(this.handleRequest); constructor(protected port: number, autoListen = true) {