Skip to content

Commit

Permalink
fix: add schema option (#6)
Browse files Browse the repository at this point in the history
* chore: add schema option

* fix: resolve build errors

* chore: rename schema to schemaName

---------

Co-authored-by: mateusz.anglart <[email protected]>
  • Loading branch information
devjiwonchoi and mateusz.anglart authored May 19, 2023
1 parent beee4f5 commit a2215b1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
15 changes: 9 additions & 6 deletions src/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export class Resource extends BaseResource {

private tableName: string;

private schemaName = 'public';

private _database: string;

private _properties: Property[];
Expand All @@ -36,6 +38,7 @@ export class Resource extends BaseResource {
constructor(info: ResourceMetadata) {
super(info.tableName);
this.knex = info.knex;
this.schemaName = info.schemaName;
this.tableName = info.tableName;
this._database = info.database;
this._properties = info.properties;
Expand Down Expand Up @@ -97,12 +100,12 @@ export class Resource extends BaseResource {
}

override async findOne(id: string): Promise<BaseRecord | null> {
const res = await this.knex(this.tableName).where(this.idColumn, id);
const res = await this.knex(this.tableName).withSchema(this.schemaName).where(this.idColumn, id);
return res[0] ? this.build(res[0]) : null;
}

override async findMany(ids: (string | number)[]): Promise<BaseRecord[]> {
const res = await this.knex(this.tableName).whereIn(this.idColumn, ids);
const res = await this.knex(this.tableName).withSchema(this.schemaName).whereIn(this.idColumn, ids);
return res.map((r) => this.build(r));
}

Expand All @@ -111,7 +114,7 @@ export class Resource extends BaseResource {
}

override async create(params: Record<string, any>): Promise<ParamsType> {
await this.knex(this.tableName).insert(params);
await this.knex(this.tableName).withSchema(this.schemaName).insert(params);

return params;
}
Expand All @@ -124,16 +127,16 @@ export class Resource extends BaseResource {
.from(this.tableName)
.update(params)
.where(this.idColumn, id);
const [row] = await this.knex(this.tableName).where(this.idColumn, id);
const [row] = await this.knex(this.tableName).withSchema(this.schemaName).where(this.idColumn, id);
return row;
}

override async delete(id: string): Promise<void> {
await this.knex.from(this.tableName).delete().where(this.idColumn, id);
await this.knex.withSchema(this.schemaName).from(this.tableName).delete().where(this.idColumn, id);
}

private filterQuery(filter: Filter | undefined): Knex.QueryBuilder {
const q = this.knex(this.tableName);
const q = this.knex(this.tableName).withSchema(this.schemaName);

if (!filter) {
return q;
Expand Down
8 changes: 6 additions & 2 deletions src/dialects/base-database.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ export class BaseDatabaseParser {
throw new Error('Implement "parse" method for your database parser!');
}

public async getTables(): Promise<string[]> {
public async getSchema(): Promise<string> {
throw new Error('Implement "getSchema" method for your database parser!');
}

public async getTables(schemaName: string): Promise<string[]> {
throw new Error('Implement "getTables" method for your database parser!');
}

public async getResources(tables: string[]): Promise<ResourceMetadata[]> {
public async getResources(tables: string[], schemaName: string): Promise<ResourceMetadata[]> {
throw new Error('Implement "getResources" method for your database parser!');
}

Expand Down
26 changes: 16 additions & 10 deletions src/dialects/postgres.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,9 @@ export class PostgresParser extends BaseDatabaseParser {
public static dialects = ['postgresql' as const];

public async parse() {
const tableNames = await this.getTables();
const resources = await this.getResources(
tableNames,
);
const schemaName = await this.getSchema();
const tableNames = await this.getTables(schemaName);
const resources = await this.getResources(tableNames, schemaName);
const resourceMap = new Map<string, ResourceMetadata>();
resources.forEach((r) => {
resourceMap.set(r.tableName, r);
Expand All @@ -85,34 +84,41 @@ export class PostgresParser extends BaseDatabaseParser {
return new DatabaseMetadata(this.connectionOptions.database, resourceMap);
}

public async getTables() {
const query = await this.knex.raw(`
public async getSchema() {
const query = await this.knex.raw('SELECT current_schema() AS schema_name');
const result = await query;
return result.rows?.schema_name.toString();
}

public async getTables(schemaName) {
const query = await this.knex.schema.withSchema(schemaName).raw(`
SELECT table_name
FROM information_schema.tables
WHERE table_schema='public'
AND table_type='BASE TABLE';
`);

const result = await query;
const rows = result[0];

if (!result?.rows?.length) {
if (!rows) {
// eslint-disable-next-line no-console
console.warn(`No tables in database ${this.connectionOptions.database}`);

return [];
}

return result.rows.map(({ table_name: table }) => table);
return rows.map(({ table_name: table }) => table);
}

public async getResources(tables: string[]) {
public async getResources(tables: string[], schemaName: string) {
const resources = await Promise.all(
tables.map(async (tableName) => {
try {
const resourceMetadata = new ResourceMetadata(
this.dialect,
this.knex,
this.connectionOptions.database,
schemaName,
tableName,
await this.getProperties(tableName),
);
Expand Down
1 change: 1 addition & 0 deletions src/metadata/ResourceMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class ResourceMetadata {
public dialect: DatabaseDialect,
public readonly knex: Knex,
public readonly database: string,
public readonly schemaName: string,
public readonly tableName: string,
public readonly properties: Property[],
) {
Expand Down

0 comments on commit a2215b1

Please sign in to comment.