surrealised
Advanced tools
Comparing version 0.1.16 to 0.2.0
@@ -5,2 +5,39 @@ import * as surrealdb_js_script_types from 'surrealdb.js/script/types'; | ||
interface OrderByField { | ||
field: string; | ||
direction?: "ASC" | "DESC"; | ||
} | ||
declare class SurrealQueryBuilder { | ||
private table; | ||
private fields; | ||
private omitFields; | ||
private whereClauses; | ||
private currentClauseGroup; | ||
private orderByFields; | ||
private grouping; | ||
private fetchItems; | ||
private splitItems; | ||
private groupByItems; | ||
private withIndex; | ||
private offsetClause?; | ||
private limitClause?; | ||
constructor(table: string); | ||
select(...fields: string[]): this; | ||
where(condition: string): this; | ||
and(condition: string): this; | ||
or(condition: string): this; | ||
endGroup(): this; | ||
fetch(...fields: string[]): this; | ||
offset(n: number): this; | ||
limit(n: number): this; | ||
groupBy(...fields: string[]): this; | ||
orderBy(...fields: OrderByField[]): this; | ||
split(...fields: string[]): this; | ||
index(...indexes: string[]): this; | ||
build(): string; | ||
queryOne<T>(params: Record<string, any>): Promise<T>; | ||
queryMany<T>(params: Record<string, any>): Promise<T[]>; | ||
execute(params: Record<string, any>): Promise<void>; | ||
} | ||
interface SurrealClientOptions { | ||
@@ -35,3 +72,3 @@ debug?: boolean; | ||
*/ | ||
queryOne<T = any>(query: string, params?: any): Promise<any>; | ||
queryOne<T = any>(query: string, params?: any): Promise<T>; | ||
/** | ||
@@ -90,3 +127,4 @@ * Execute a query and return many rows. | ||
} | ||
declare const surrealQueryBuilder: typeof SurrealQueryBuilder; | ||
export { SurrealClient as default }; | ||
export { SurrealClient as default, surrealQueryBuilder }; |
@@ -22,6 +22,139 @@ var __defProp = Object.defineProperty; | ||
__export(src_exports, { | ||
default: () => SurrealClient | ||
default: () => SurrealClient, | ||
surrealQueryBuilder: () => surrealQueryBuilder | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
var import_surrealdb = require("surrealdb.js"); | ||
// src/SurrealQueryBuilder.ts | ||
var SurrealQueryBuilder = class { | ||
table; | ||
fields = []; | ||
omitFields = []; | ||
whereClauses = []; | ||
currentClauseGroup = []; | ||
orderByFields = []; | ||
grouping = false; | ||
fetchItems = []; | ||
splitItems = []; | ||
groupByItems = []; | ||
withIndex = []; | ||
offsetClause = 0; | ||
limitClause = void 0; | ||
constructor(table) { | ||
this.table = table; | ||
} | ||
select(...fields) { | ||
this.fields = fields; | ||
return this; | ||
} | ||
where(condition) { | ||
if (this.grouping) { | ||
this.currentClauseGroup.push(condition); | ||
} else { | ||
this.whereClauses.push(condition); | ||
} | ||
return this; | ||
} | ||
and(condition) { | ||
return this.where(condition); | ||
} | ||
or(condition) { | ||
if (this.grouping) { | ||
const groupedConditions = this.currentClauseGroup.join(" AND "); | ||
this.whereClauses.push(`(${groupedConditions})`); | ||
this.currentClauseGroup = []; | ||
} | ||
this.grouping = true; | ||
this.currentClauseGroup.push(condition); | ||
return this; | ||
} | ||
endGroup() { | ||
if (this.grouping && this.currentClauseGroup.length > 0) { | ||
const groupedConditions = this.currentClauseGroup.join(" AND "); | ||
this.whereClauses.push(`(${groupedConditions})`); | ||
this.grouping = false; | ||
this.currentClauseGroup = []; | ||
} | ||
return this; | ||
} | ||
fetch(...fields) { | ||
this.fetchItems = fields; | ||
return this; | ||
} | ||
offset(n) { | ||
this.offsetClause = n; | ||
return this; | ||
} | ||
limit(n) { | ||
this.limitClause = n; | ||
return this; | ||
} | ||
groupBy(...fields) { | ||
this.groupByItems = fields; | ||
return this; | ||
} | ||
orderBy(...fields) { | ||
this.orderByFields = fields; | ||
return this; | ||
} | ||
split(...fields) { | ||
this.splitItems = fields; | ||
return this; | ||
} | ||
index(...indexes) { | ||
this.withIndex = indexes; | ||
return this; | ||
} | ||
build() { | ||
let query = `SELECT ${this.fields.length > 0 ? this.fields.join(", ") : "*"}`; | ||
if (this.omitFields.length) { | ||
query += ` OMIT ${this.omitFields.join(", ")}`; | ||
} | ||
query += ` FROM ${this.table}`; | ||
if (this.withIndex.length) { | ||
query += `WITH INDEX ${this.withIndex.join(", ")}`; | ||
} | ||
if (this.whereClauses.length > 0) { | ||
query += ` WHERE ${this.whereClauses.join(" OR ")}`; | ||
} | ||
if (this.splitItems.length > 0) { | ||
query += ` SPLIT ${this.splitItems.join(", ")}`; | ||
} | ||
if (this.groupByItems.length > 0) { | ||
query += ` GROUP BY ${this.groupByItems.join(", ")}`; | ||
} | ||
if (this.orderByFields.length > 0) { | ||
query += ` ORDER BY ${this.orderByFields.map((f) => `${f.field} ${f.direction || "ASC"}`).join(", ")}`; | ||
} | ||
if (this.limitClause) { | ||
query += ` LIMIT ${this.limitClause}`; | ||
} | ||
if (this.offsetClause) { | ||
query += ` START ${this.offsetClause}`; | ||
} | ||
if (this.fetchItems.length > 0) { | ||
query += ` FETCH ${this.fetchItems.join(", ")}`; | ||
} | ||
return query; | ||
} | ||
async queryOne(params) { | ||
let q = this.build(); | ||
const surreal = new SurrealClient(); | ||
return await surreal.queryOne(q, params); | ||
} | ||
async queryMany(params) { | ||
let q = this.build(); | ||
const surreal = new SurrealClient(); | ||
return await surreal.queryMany(q, params); | ||
} | ||
async execute(params) { | ||
let q = this.build(); | ||
const surreal = new SurrealClient(); | ||
return await surreal.execute(q, params); | ||
} | ||
}; | ||
var SurrealQueryBuilder_default = SurrealQueryBuilder; | ||
// src/index.ts | ||
var SurrealClient = class { | ||
@@ -244,2 +377,7 @@ HOST; | ||
}; | ||
var surrealQueryBuilder = SurrealQueryBuilder_default; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
surrealQueryBuilder | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "surrealised", | ||
"version": "0.1.16", | ||
"version": "0.2.0", | ||
"description": "Another SurrealDB Library for NodeJS", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
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
79708
855