bigquery-client
Version:
A feature-rich Node.js client for Google BigQuery with support for CRUD operations, transactions, query building, and advanced features like aggregate functions, pagination, and logging.
55 lines (54 loc) • 2.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseRepository = void 0;
const QueryBuilder_1 = require("../core/QueryBuilder");
class BaseRepository {
constructor(entity, client) {
this.entity = entity;
this.client = client;
}
getTableName() {
return Reflect.getMetadata('tableName', this.entity);
}
getColumns() {
return Reflect.getMetadata('columns', this.entity) || [];
}
async findAll() {
const table = this.getTableName();
const columns = this.getColumns().map(c => c.columnName);
const qb = new QueryBuilder_1.QueryBuilder(table).select(columns);
const { query, params } = qb.build();
const result = await this.client.query(query, params);
return result.data;
}
async findById(id) {
const table = this.getTableName();
const columns = this.getColumns().map(c => c.columnName);
const primaryKey = this.getColumns()[0].columnName; // Assume first column is PK
const qb = new QueryBuilder_1.QueryBuilder(table).select(columns).where({ [primaryKey]: id });
const { query, params } = qb.build();
const result = await this.client.query(query, params);
return result.data[0] || null;
}
async create(entity) {
const table = this.getTableName();
await this.client.insert({ table, rows: [entity] });
}
async update(id, updateFields) {
const table = this.getTableName();
const primaryKey = this.getColumns()[0].columnName; // Assume first column is PK
await this.client.update({ table, set: updateFields, where: { [primaryKey]: id } });
}
async delete(id) {
const table = this.getTableName();
const primaryKey = this.getColumns()[0].columnName; // Assume first column is PK
await this.client.delete({ table, where: { [primaryKey]: id } });
}
getSelectQuery() {
const table = this.getTableName();
const columns = this.getColumns().map(c => c.columnName);
const qb = new QueryBuilder_1.QueryBuilder(table).select(columns);
return qb.build();
}
}
exports.BaseRepository = BaseRepository;