Skip to content

Commit a2215b1

Browse files
최지원mateusz.anglart
andauthored
fix: add schema option (#6)
* chore: add schema option * fix: resolve build errors * chore: rename schema to schemaName --------- Co-authored-by: mateusz.anglart <[email protected]>
1 parent beee4f5 commit a2215b1

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

src/Resource.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export class Resource extends BaseResource {
2727

2828
private tableName: string;
2929

30+
private schemaName = 'public';
31+
3032
private _database: string;
3133

3234
private _properties: Property[];
@@ -36,6 +38,7 @@ export class Resource extends BaseResource {
3638
constructor(info: ResourceMetadata) {
3739
super(info.tableName);
3840
this.knex = info.knex;
41+
this.schemaName = info.schemaName;
3942
this.tableName = info.tableName;
4043
this._database = info.database;
4144
this._properties = info.properties;
@@ -97,12 +100,12 @@ export class Resource extends BaseResource {
97100
}
98101

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

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

@@ -111,7 +114,7 @@ export class Resource extends BaseResource {
111114
}
112115

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

116119
return params;
117120
}
@@ -124,16 +127,16 @@ export class Resource extends BaseResource {
124127
.from(this.tableName)
125128
.update(params)
126129
.where(this.idColumn, id);
127-
const [row] = await this.knex(this.tableName).where(this.idColumn, id);
130+
const [row] = await this.knex(this.tableName).withSchema(this.schemaName).where(this.idColumn, id);
128131
return row;
129132
}
130133

131134
override async delete(id: string): Promise<void> {
132-
await this.knex.from(this.tableName).delete().where(this.idColumn, id);
135+
await this.knex.withSchema(this.schemaName).from(this.tableName).delete().where(this.idColumn, id);
133136
}
134137

135138
private filterQuery(filter: Filter | undefined): Knex.QueryBuilder {
136-
const q = this.knex(this.tableName);
139+
const q = this.knex(this.tableName).withSchema(this.schemaName);
137140

138141
if (!filter) {
139142
return q;

src/dialects/base-database.parser.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,15 @@ export class BaseDatabaseParser {
4242
throw new Error('Implement "parse" method for your database parser!');
4343
}
4444

45-
public async getTables(): Promise<string[]> {
45+
public async getSchema(): Promise<string> {
46+
throw new Error('Implement "getSchema" method for your database parser!');
47+
}
48+
49+
public async getTables(schemaName: string): Promise<string[]> {
4650
throw new Error('Implement "getTables" method for your database parser!');
4751
}
4852

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

src/dialects/postgres.parser.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@ export class PostgresParser extends BaseDatabaseParser {
7373
public static dialects = ['postgresql' as const];
7474

7575
public async parse() {
76-
const tableNames = await this.getTables();
77-
const resources = await this.getResources(
78-
tableNames,
79-
);
76+
const schemaName = await this.getSchema();
77+
const tableNames = await this.getTables(schemaName);
78+
const resources = await this.getResources(tableNames, schemaName);
8079
const resourceMap = new Map<string, ResourceMetadata>();
8180
resources.forEach((r) => {
8281
resourceMap.set(r.tableName, r);
@@ -85,34 +84,41 @@ export class PostgresParser extends BaseDatabaseParser {
8584
return new DatabaseMetadata(this.connectionOptions.database, resourceMap);
8685
}
8786

88-
public async getTables() {
89-
const query = await this.knex.raw(`
87+
public async getSchema() {
88+
const query = await this.knex.raw('SELECT current_schema() AS schema_name');
89+
const result = await query;
90+
return result.rows?.schema_name.toString();
91+
}
92+
93+
public async getTables(schemaName) {
94+
const query = await this.knex.schema.withSchema(schemaName).raw(`
9095
SELECT table_name
9196
FROM information_schema.tables
92-
WHERE table_schema='public'
9397
AND table_type='BASE TABLE';
9498
`);
9599

96100
const result = await query;
101+
const rows = result[0];
97102

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

102107
return [];
103108
}
104109

105-
return result.rows.map(({ table_name: table }) => table);
110+
return rows.map(({ table_name: table }) => table);
106111
}
107112

108-
public async getResources(tables: string[]) {
113+
public async getResources(tables: string[], schemaName: string) {
109114
const resources = await Promise.all(
110115
tables.map(async (tableName) => {
111116
try {
112117
const resourceMetadata = new ResourceMetadata(
113118
this.dialect,
114119
this.knex,
115120
this.connectionOptions.database,
121+
schemaName,
116122
tableName,
117123
await this.getProperties(tableName),
118124
);

src/metadata/ResourceMetadata.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export class ResourceMetadata {
1010
public dialect: DatabaseDialect,
1111
public readonly knex: Knex,
1212
public readonly database: string,
13+
public readonly schemaName: string,
1314
public readonly tableName: string,
1415
public readonly properties: Property[],
1516
) {

0 commit comments

Comments
 (0)