Comparing version 0.1.2 to 0.1.3
import { Repository } from "./core"; | ||
import { SQLiteDatabase } from "./core/sqlite-database"; | ||
export declare class LilORM { | ||
@@ -28,2 +29,3 @@ private readonly databaseString; | ||
tableExists(tableName: string): Promise<boolean>; | ||
get dbInstance(): SQLiteDatabase; | ||
} |
@@ -54,4 +54,7 @@ "use strict"; | ||
} | ||
get dbInstance() { | ||
return this.sqliteDatabase; | ||
} | ||
} | ||
exports.LilORM = LilORM; | ||
//# sourceMappingURL=api.js.map |
import { LilORMType } from "./types"; | ||
/** | ||
* @interface ColumnOtps | ||
* @description Options for defining a column in the entity. | ||
* @property {string} [name] - The name of the column. | ||
* @property {LilORMType} type - The data type of the column. | ||
* @property {*} [defaultValue] - The default value for the column. | ||
* @property {boolean} [notNull] - Indicates if the column is not nullable. | ||
*/ | ||
export interface ColumnOtps { | ||
@@ -8,7 +16,27 @@ name?: string; | ||
} | ||
/** | ||
* @interface PrimaryKeyOpts | ||
* @description Options for defining a primary key in the entity. | ||
* @property {boolean} [autoIncrement] - Indicates if the primary key should auto-increment. | ||
*/ | ||
export interface PrimaryKeyOpts { | ||
autoIncrement?: boolean; | ||
} | ||
/** | ||
* @function Entity | ||
* @description Decorator function to define an entity. | ||
* @param {string} [tableName] - The name of the table associated with the entity. | ||
*/ | ||
export declare function Entity(tableName?: string): ClassDecorator; | ||
/** | ||
* @function PrimaryKey | ||
* @description Decorator function to define a primary key. | ||
* @param {PrimaryKeyOpts} [opts] - Options for the primary key. | ||
*/ | ||
export declare function PrimaryKey(opts?: PrimaryKeyOpts): PropertyDecorator; | ||
/** | ||
* @function Column | ||
* @description Decorator function to define a column. | ||
* @param {ColumnOtps} opts - Options for the column. | ||
*/ | ||
export declare function Column(opts: ColumnOtps): PropertyDecorator; |
@@ -5,2 +5,7 @@ "use strict"; | ||
const constants_1 = require("./constants"); | ||
/** | ||
* @function Entity | ||
* @description Decorator function to define an entity. | ||
* @param {string} [tableName] - The name of the table associated with the entity. | ||
*/ | ||
function Entity(tableName) { | ||
@@ -12,2 +17,7 @@ return (target) => { | ||
exports.Entity = Entity; | ||
/** | ||
* @function PrimaryKey | ||
* @description Decorator function to define a primary key. | ||
* @param {PrimaryKeyOpts} [opts] - Options for the primary key. | ||
*/ | ||
function PrimaryKey(opts) { | ||
@@ -19,5 +29,10 @@ return (target, propertyKey) => { | ||
exports.PrimaryKey = PrimaryKey; | ||
/** | ||
* @function Column | ||
* @description Decorator function to define a column. | ||
* @param {ColumnOtps} opts - Options for the column. | ||
*/ | ||
function Column(opts) { | ||
return (target, propertyKey) => { | ||
Reflect.defineMetadata(constants_1.COLUMN_METADATA_KEY, { name: opts === null || opts === void 0 ? void 0 : opts.name, type: opts.type }, target, propertyKey); | ||
Reflect.defineMetadata(constants_1.COLUMN_METADATA_KEY, { name: opts === null || opts === void 0 ? void 0 : opts.name, type: opts.type, notNull: opts === null || opts === void 0 ? void 0 : opts.notNull }, target, propertyKey); | ||
}; | ||
@@ -24,0 +39,0 @@ } |
@@ -21,3 +21,3 @@ "use strict"; | ||
static transformClassInstanceToEntityColumns(entityInstance) { | ||
const columns = metadata_1.MetadataExtractor.getEnrichedEntityColumnsName(entityInstance); | ||
const columns = metadata_1.MetadataExtractor.getEnrichedEntityColumnsMetadata(entityInstance); | ||
columns | ||
@@ -33,2 +33,4 @@ .filter((col) => col.value !== undefined) | ||
static valueQueryFormatter(value) { | ||
if (value === null) | ||
return `NULL`; | ||
if (types_helper_1.TypesHelper.isString(value)) | ||
@@ -35,0 +37,0 @@ return `'${value}'`; |
@@ -26,3 +26,3 @@ import { ColumnMetadata } from "./types"; | ||
static getEntityColumnsName(entityInstance: any): string[]; | ||
static getEnrichedEntityColumnsName(entityInstance: any): ColumnMetadata[]; | ||
static getEnrichedEntityColumnsMetadata(entityInstance: any): ColumnMetadata[]; | ||
/** | ||
@@ -29,0 +29,0 @@ * Retrieves the formatted column values associated with an entity instance. |
@@ -52,3 +52,3 @@ "use strict"; | ||
} | ||
static getEnrichedEntityColumnsName(entityInstance) { | ||
static getEnrichedEntityColumnsMetadata(entityInstance) { | ||
const columns = []; | ||
@@ -55,0 +55,0 @@ const properties = Object.keys(entityInstance); |
@@ -31,5 +31,6 @@ "use strict"; | ||
const columnName = propertyMetadata.name || propertyKey.toString(); | ||
const columnNotNull = (propertyMetadata === null || propertyMetadata === void 0 ? void 0 : propertyMetadata.notNull) || false; | ||
const columnType = types_1.MapTypes[propertyMetadata.type]; | ||
const primaryKeyOptions = primaryKeyMetadata || {}; | ||
let columnDefinition = `${columnName} ${columnType}`; | ||
let columnDefinition = `${columnName} ${columnType} ${columnNotNull ? `NOT NULL` : ``}`; | ||
if (primaryKeyOptions.autoIncrement) { | ||
@@ -36,0 +37,0 @@ columnDefinition += " PRIMARY KEY AUTOINCREMENT"; |
import { SQLiteDatabase } from "./sqlite-database"; | ||
export declare class Transaction { | ||
private readonly db; | ||
private statemanets; | ||
private statements; | ||
/** | ||
* Creates an instance of Transaction. | ||
* @param {SQLiteDatabase} db - The database instance. | ||
*/ | ||
constructor(db: SQLiteDatabase); | ||
begin(): this; | ||
/** | ||
* Begins the transaction. | ||
*/ | ||
begin(): void; | ||
/** | ||
* Commits the transaction. | ||
*/ | ||
commit(): void; | ||
/** | ||
* Adds a query to the transaction. | ||
* @param {string} query - The query to be added. | ||
*/ | ||
addQuery(query: string): void; | ||
/** | ||
* Rolls back the transaction. | ||
*/ | ||
rollback(): void; | ||
/** | ||
* Executes a transactional callback function. | ||
* @template T - The type of the result returned by the callback. | ||
* @param {function} callback - The transactional callback function. | ||
* @returns {Promise<T>} A promise that resolves to the result of the callback. | ||
* @throws {Error} If an error occurs during the transaction or query execution. | ||
*/ | ||
transaction<T>(callback: (transaction: Transaction) => Promise<T>): Promise<T>; | ||
/** | ||
* Executes the statements in the transaction. | ||
* @private | ||
* @returns {Promise<void>} A promise that resolves when all statements have been executed successfully. | ||
* @throws {Error} If an error occurs during the execution of the statements. | ||
*/ | ||
private executeStatements; | ||
} |
@@ -5,2 +5,6 @@ "use strict"; | ||
class Transaction { | ||
/** | ||
* Creates an instance of Transaction. | ||
* @param {SQLiteDatabase} db - The database instance. | ||
*/ | ||
constructor(db) { | ||
@@ -13,3 +17,3 @@ Object.defineProperty(this, "db", { | ||
}); | ||
Object.defineProperty(this, "statemanets", { | ||
Object.defineProperty(this, "statements", { | ||
enumerable: true, | ||
@@ -21,30 +25,65 @@ configurable: true, | ||
} | ||
/** | ||
* Begins the transaction. | ||
*/ | ||
begin() { | ||
this.statemanets = []; | ||
this.statemanets.push("BEGIN TRANSACTION"); | ||
return this; | ||
this.statements = []; | ||
this.db.sqliteInstance.exec("BEGIN"); | ||
} | ||
/** | ||
* Commits the transaction. | ||
*/ | ||
commit() { | ||
this.statemanets.push("COMMIT"); | ||
this.db.sqliteInstance.exec(this.statemanets.join(";")); | ||
this.db.sqliteInstance.exec("COMMIT"); | ||
} | ||
/** | ||
* Adds a query to the transaction. | ||
* @param {string} query - The query to be added. | ||
*/ | ||
addQuery(query) { | ||
this.statemanets.push(query); | ||
this.statements.push(query); | ||
} | ||
/** | ||
* Rolls back the transaction. | ||
*/ | ||
rollback() { | ||
this.statemanets.push("ROLLBACK"); | ||
this.db.sqliteInstance.exec(this.statemanets.join(";")); | ||
this.db.sqliteInstance.exec("ROLLBACK"); | ||
} | ||
transaction(callback) { | ||
return new Promise(async (resolve, reject) => { | ||
this.begin(); | ||
try { | ||
const result = await callback(this); | ||
this.commit(); | ||
resolve(result); | ||
} | ||
catch (error) { | ||
this.rollback(); | ||
reject(error); | ||
} | ||
/** | ||
* Executes a transactional callback function. | ||
* @template T - The type of the result returned by the callback. | ||
* @param {function} callback - The transactional callback function. | ||
* @returns {Promise<T>} A promise that resolves to the result of the callback. | ||
* @throws {Error} If an error occurs during the transaction or query execution. | ||
*/ | ||
async transaction(callback) { | ||
this.begin(); | ||
try { | ||
const result = await callback(this); | ||
await this.executeStatements(); | ||
this.commit(); | ||
return result; | ||
} | ||
catch (error) { | ||
this.rollback(); | ||
throw error; | ||
} | ||
} | ||
/** | ||
* Executes the statements in the transaction. | ||
* @private | ||
* @returns {Promise<void>} A promise that resolves when all statements have been executed successfully. | ||
* @throws {Error} If an error occurs during the execution of the statements. | ||
*/ | ||
executeStatements() { | ||
const statements = this.statements.join(";"); | ||
return new Promise((resolve, reject) => { | ||
this.db.sqliteInstance.exec(statements, (error) => { | ||
if (error) { | ||
reject(error); | ||
} | ||
else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
@@ -51,0 +90,0 @@ } |
{ | ||
"name": "lil-orm", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Lil ORM is a super lightweight SQLite ORM for Node.js. With its clear API, you can easily interact with SQLite databases.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -6,2 +6,4 @@ ![ORM](https://github.com/scassius/lil-orm/assets/35706430/5fd46412-ea3d-40b8-a56a-20450e9e2986) | ||
⚠️ **API are subjected to change** ⚠️ | ||
# Install | ||
@@ -8,0 +10,0 @@ ```shell |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
89563
1052
182