@creator.co/wapi 1.4.0-alpha4 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@creator.co/wapi",
3
- "version": "1.4.0-alpha4",
3
+ "version": "1.4.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -26,7 +26,7 @@ export declare class KyselyDatabase<DBSchema> extends Database<KyselyTransaction
26
26
  /**
27
27
  * Represents a PostgreSQL client using the PostgresDialect.
28
28
  */
29
- readonly pgClient: PostgresDialect;
29
+ private readonly pgClient;
30
30
  /**
31
31
  * Represents a client for querying the database with the specified schema.
32
32
  * @type {Kysely<DBSchema>} client - The client for querying the database.
@@ -35,7 +35,7 @@ export declare class KyselyDatabase<DBSchema> extends Database<KyselyTransaction
35
35
  /**
36
36
  * Optional property representing the Postgres dialect for reading in a database client.
37
37
  */
38
- readonly pgReadClient?: PostgresDialect;
38
+ private readonly pgReadClient?;
39
39
  /**
40
40
  * Represents a client for reading from the database.
41
41
  * @type {Kysely<DBSchema> | undefined} - The client for reading from the database.
@@ -52,4 +52,16 @@ export declare class KyselyDatabase<DBSchema> extends Database<KyselyTransaction
52
52
  * @returns A Promise that resolves to a KyselyTransaction object for the specified database schema.
53
53
  */
54
54
  transaction(): Promise<KyselyTransaction<DBSchema>>;
55
+ /**
56
+ * Creates a database dialect based on the provided configuration.
57
+ * @param {DbBaseConfig} config - The configuration object for the database connection.
58
+ * @returns A database dialect instance based on the provided configuration.
59
+ */
60
+ private dialectFactory;
61
+ /**
62
+ * Creates a provider for interacting with a database using the specified Postgres dialect.
63
+ * @param {PostgresDialect} dialect - The Postgres dialect to use for the database connection.
64
+ * @returns A database provider for interacting with the database.
65
+ */
66
+ private providerFactory;
55
67
  }
@@ -69,29 +69,11 @@ var KyselyDatabase = /** @class */ (function (_super) {
69
69
  */
70
70
  function KyselyDatabase(config) {
71
71
  var _this = _super.call(this, config) || this;
72
- _this.pgClient = new KyselyDatabase.kyselyPgProvider({
73
- pool: new KyselyDatabase.pgProvider({
74
- host: config.host,
75
- port: config.port,
76
- user: config.username,
77
- password: config.password,
78
- database: config.database,
79
- max: config.maxConnections,
80
- }),
81
- });
82
- _this.client = new KyselyDatabase.kyselyProvider({ dialect: _this.pgClient });
72
+ _this.pgClient = _this.dialectFactory(config);
73
+ _this.client = _this.providerFactory(_this.pgClient);
83
74
  if (config.readReplica) {
84
- _this.pgReadClient = new KyselyDatabase.kyselyPgProvider({
85
- pool: new KyselyDatabase.pgProvider({
86
- host: config.readReplica.host,
87
- port: config.readReplica.port,
88
- user: config.readReplica.username,
89
- password: config.readReplica.password,
90
- database: config.readReplica.database,
91
- max: config.readReplica.maxConnections,
92
- }),
93
- });
94
- _this.readClient = new KyselyDatabase.kyselyProvider({ dialect: _this.pgReadClient });
75
+ _this.pgReadClient = _this.dialectFactory(config.readReplica);
76
+ _this.readClient = _this.providerFactory(_this.pgReadClient);
95
77
  }
96
78
  return _this;
97
79
  }
@@ -106,6 +88,34 @@ var KyselyDatabase = /** @class */ (function (_super) {
106
88
  });
107
89
  });
108
90
  };
91
+ /**
92
+ * Creates a database dialect based on the provided configuration.
93
+ * @param {DbBaseConfig} config - The configuration object for the database connection.
94
+ * @returns A database dialect instance based on the provided configuration.
95
+ */
96
+ KyselyDatabase.prototype.dialectFactory = function (config) {
97
+ return new KyselyDatabase.kyselyPgProvider({
98
+ pool: new KyselyDatabase.pgProvider({
99
+ host: config.host,
100
+ port: config.port,
101
+ user: config.username,
102
+ password: config.password,
103
+ database: config.database,
104
+ max: config.maxConnections,
105
+ }),
106
+ });
107
+ };
108
+ /**
109
+ * Creates a provider for interacting with a database using the specified Postgres dialect.
110
+ * @param {PostgresDialect} dialect - The Postgres dialect to use for the database connection.
111
+ * @returns A database provider for interacting with the database.
112
+ */
113
+ KyselyDatabase.prototype.providerFactory = function (dialect) {
114
+ return new KyselyDatabase.kyselyProvider({
115
+ dialect: dialect,
116
+ plugins: [new kysely_1.CamelCasePlugin()],
117
+ });
118
+ };
109
119
  /**
110
120
  * Represents a PostgreSQL provider for querying data using the PostgresDialect.
111
121
  * @type {PostgresDialect}
@@ -1 +1 @@
1
- {"version":3,"file":"KyselyDatabase.js","sourceRoot":"","sources":["../../../../../src/Database/integrations/kysely/KyselyDatabase.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iCAAgD;AAChD,yBAAyB;AAEzB,yDAA8E;AAC9E,2CAAyC;AAGzC;;;GAGG;AACH;IAA8C,kCAAqC;IAmCjF;;;;OAIG;IACH,wBAAmB,MAA0B;QAC3C,YAAA,MAAK,YAAC,MAAM,CAAC,SAAA;QACb,KAAI,CAAC,QAAQ,GAAG,IAAI,cAAc,CAAC,gBAAgB,CAAC;YAClD,IAAI,EAAE,IAAI,cAAc,CAAC,UAAU,CAAC;gBAClC,IAAI,EAAE,MAAM,CAAC,IAAI;gBACjB,IAAI,EAAE,MAAM,CAAC,IAAI;gBACjB,IAAI,EAAE,MAAM,CAAC,QAAQ;gBACrB,QAAQ,EAAE,MAAM,CAAC,QAAQ;gBACzB,QAAQ,EAAE,MAAM,CAAC,QAAQ;gBACzB,GAAG,EAAE,MAAM,CAAC,cAAc;aAC3B,CAAC;SACH,CAAC,CAAA;QACF,KAAI,CAAC,MAAM,GAAG,IAAI,cAAc,CAAC,cAAc,CAAW,EAAE,OAAO,EAAE,KAAI,CAAC,QAAQ,EAAE,CAAC,CAAA;QACrF,IAAI,MAAM,CAAC,WAAW,EAAE,CAAC;YACvB,KAAI,CAAC,YAAY,GAAG,IAAI,cAAc,CAAC,gBAAgB,CAAC;gBACtD,IAAI,EAAE,IAAI,cAAc,CAAC,UAAU,CAAC;oBAClC,IAAI,EAAE,MAAM,CAAC,WAAW,CAAC,IAAI;oBAC7B,IAAI,EAAE,MAAM,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export declare class PostgresDatabase extends Database<PostgresTransaction> {
32
32
  * @returns {Promise<PostgresTransaction>} A promise that resolves to a new PostgresTransaction object.
33
33
  */
34
34
  transaction(): Promise<PostgresTransaction>;
35
+ private providerFactory;
35
36
  }
@@ -68,24 +68,9 @@ var PostgresDatabase = /** @class */ (function (_super) {
68
68
  */
69
69
  function PostgresDatabase(config) {
70
70
  var _this = _super.call(this, config) || this;
71
- _this.client = new PostgresDatabase.pgProvider({
72
- host: config.host,
73
- port: config.port,
74
- user: config.username,
75
- password: config.password,
76
- database: config.database,
77
- max: config.maxConnections,
78
- });
79
- if (config.readReplica) {
80
- _this.readClient = new PostgresDatabase.pgProvider({
81
- host: config.readReplica.host,
82
- port: config.readReplica.port,
83
- user: config.readReplica.username,
84
- password: config.readReplica.password,
85
- database: config.readReplica.database,
86
- max: config.readReplica.maxConnections,
87
- });
88
- }
71
+ _this.client = _this.providerFactory(config);
72
+ if (config.readReplica)
73
+ _this.readClient = _this.providerFactory(config.readReplica);
89
74
  return _this;
90
75
  }
91
76
  /**
@@ -99,6 +84,16 @@ var PostgresDatabase = /** @class */ (function (_super) {
99
84
  });
100
85
  });
101
86
  };
87
+ PostgresDatabase.prototype.providerFactory = function (config) {
88
+ return new PostgresDatabase.pgProvider({
89
+ host: config.host,
90
+ port: config.port,
91
+ user: config.username,
92
+ password: config.password,
93
+ database: config.database,
94
+ max: config.maxConnections,
95
+ });
96
+ };
102
97
  /**
103
98
  * A public static property that represents a connection pool for a PostgreSQL database.
104
99
  * This property is used to manage and provide connections to the PostgreSQL database.
@@ -1 +1 @@
1
- {"version":3,"file":"PostgresDatabase.js","sourceRoot":"","sources":["../../../../../src/Database/integrations/pgsql/PostgresDatabase.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yBAAyB;AAEzB,6DAAoF;AACpF,2CAAyC;AAGzC;;;GAGG;AACH;IAAsC,oCAA6B;IAgBjE;;;;O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;;;;;;;;;;;;;;;AAAA,yBAAyB;AAEzB,6DAAoF;AACpF,2CAAyC;AAGzC;;;GAGG;AACH;IAAsC,oCAA6B;IAgBjE;;;;OAIG;IACH,0BAAmB,MAAsB;QACvC,YAAA,MAAK,YAAC,MAAM,CAAC,SAAA;QACb,KAAI,CAAC,MAAM,GAAG,KAAI,CAAC,eAAe,CAAC,MAAM,CAAC,CAAA;QAC1C,IAAI,MAAM,CAAC,WAAW;YAAE,KAAI,CAAC,UAAU,GAAG,KAAI,CAAC,eAAe,CAAC,MAAM,CAAC,WAAW,CAAC,CAAA;;IACpF,CAAC;IAED;;;OAGG;IACmB,sCAAW,GAAjC;;;gBACE,sBAAO,6CAAuB,CAAC,cAAc,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,UAAU,CAAC,EAAA;;;KAClF;IACO,0CAAe,GAAvB,UAAwB,MAAoB;QAC1C,OAAO,IAAI,gBAAgB,CAAC,UAAU,CAAC;YACrC,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,IAAI,EAAE,MAAM,CAAC,QAAQ;YACrB,QAAQ,EAAE,MAAM,CAAC,QAAQ;YACzB,QAAQ,EAAE,MAAM,CAAC,QAAQ;YACzB,GAAG,EAAE,MAAM,CAAC,cAAc;SAC3B,CAAC,CAAA;IACJ,CAAC;IA1CD;;;OAGG;IACW,2BAAU,GAAG,SAAI,CAAA;IAuCjC,uBAAC;CAAA,AA5CD,CAAsC,mBAAQ,GA4C7C;AA5CY,4CAAgB"}
@@ -48,7 +48,7 @@ export type DatabaseTransactionType<Type extends DatabaseType, DBSchema> = Datab
48
48
  * @typedef {Object} DbConfig
49
49
  * @property {string} type - The type of
50
50
  */
51
- type DbBaseConfig = {
51
+ export type DbBaseConfig = {
52
52
  host: string;
53
53
  port: number;
54
54
  username: string;
@@ -57,6 +57,16 @@ type DbBaseConfig = {
57
57
  autoCommit: boolean;
58
58
  maxConnections: number;
59
59
  };
60
+ /**
61
+ * Defines the configuration options for different types of databases.
62
+ * @template S - The type of database (e.g., 'knex', 'pg', 'kysely')
63
+ * @typedef {Object} DbConfig
64
+ * @property {DbBaseConfig} DbBaseConfig - The base configuration for the database.
65
+ * @property {S} type - The type of the database.
66
+ * @property {string} [driver] - The driver for the 'knex' database type.
67
+ * @property {boolean} [convertCamelToSnake] - Whether to convert camel case to snake case for 'knex' database type.
68
+ * @property {DbBaseConfig} [readReplica] - The read replica configuration for '
69
+ */
60
70
  export type DbConfig<S extends DatabaseType> = DbBaseConfig & {
61
71
  type: S;
62
72
  } & (S extends 'knex' ? {
@@ -65,4 +75,3 @@ export type DbConfig<S extends DatabaseType> = DbBaseConfig & {
65
75
  } : unknown) & (S extends 'pg' | 'kysely' ? {
66
76
  readReplica?: DbBaseConfig;
67
77
  } : unknown);
68
- export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@creator.co/wapi",
3
- "version": "1.4.0-alpha4",
3
+ "version": "1.4.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,9 +1,9 @@
1
- import { Kysely, PostgresDialect } from 'kysely'
1
+ import { CamelCasePlugin, Kysely, PostgresDialect } from 'kysely'
2
2
  import { Pool } from 'pg'
3
3
 
4
4
  import { KyselyTransaction, KyselyTransactionImpl } from './KyselyTransaction'
5
5
  import { Database } from '../../Database'
6
- import type { DbConfig } from '../../types'
6
+ import type { DbBaseConfig, DbConfig } from '../../types'
7
7
 
8
8
  /**
9
9
  * Represents a database connection using the Kysely library with support for transactions.
@@ -28,7 +28,7 @@ export class KyselyDatabase<DBSchema> extends Database<KyselyTransaction<DBSchem
28
28
  /**
29
29
  * Represents a PostgreSQL client using the PostgresDialect.
30
30
  */
31
- public readonly pgClient: PostgresDialect
31
+ private readonly pgClient: PostgresDialect
32
32
  /**
33
33
  * Represents a client for querying the database with the specified schema.
34
34
  * @type {Kysely<DBSchema>} client - The client for querying the database.
@@ -37,7 +37,7 @@ export class KyselyDatabase<DBSchema> extends Database<KyselyTransaction<DBSchem
37
37
  /**
38
38
  * Optional property representing the Postgres dialect for reading in a database client.
39
39
  */
40
- public readonly pgReadClient?: PostgresDialect
40
+ private readonly pgReadClient?: PostgresDialect
41
41
  /**
42
42
  * Represents a client for reading from the database.
43
43
  * @type {Kysely<DBSchema> | undefined} - The client for reading from the database.
@@ -51,7 +51,28 @@ export class KyselyDatabase<DBSchema> extends Database<KyselyTransaction<DBSchem
51
51
  */
52
52
  public constructor(config: DbConfig<'kysely'>) {
53
53
  super(config)
54
- this.pgClient = new KyselyDatabase.kyselyPgProvider({
54
+ this.pgClient = this.dialectFactory(config)
55
+ this.client = this.providerFactory(this.pgClient)
56
+ if (config.readReplica) {
57
+ this.pgReadClient = this.dialectFactory(config.readReplica)
58
+ this.readClient = this.providerFactory(this.pgReadClient)
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Override method that starts a new transaction using the provided client and read client.
64
+ * @returns A Promise that resolves to a KyselyTransaction object for the specified database schema.
65
+ */
66
+ public override async transaction(): Promise<KyselyTransaction<DBSchema>> {
67
+ return KyselyTransactionImpl.newTransaction<DBSchema>(this.client, this, this.readClient)
68
+ }
69
+ /**
70
+ * Creates a database dialect based on the provided configuration.
71
+ * @param {DbBaseConfig} config - The configuration object for the database connection.
72
+ * @returns A database dialect instance based on the provided configuration.
73
+ */
74
+ private dialectFactory(config: DbBaseConfig) {
75
+ return new KyselyDatabase.kyselyPgProvider({
55
76
  pool: new KyselyDatabase.pgProvider({
56
77
  host: config.host,
57
78
  port: config.port,
@@ -61,27 +82,16 @@ export class KyselyDatabase<DBSchema> extends Database<KyselyTransaction<DBSchem
61
82
  max: config.maxConnections,
62
83
  }),
63
84
  })
64
- this.client = new KyselyDatabase.kyselyProvider<DBSchema>({ dialect: this.pgClient })
65
- if (config.readReplica) {
66
- this.pgReadClient = new KyselyDatabase.kyselyPgProvider({
67
- pool: new KyselyDatabase.pgProvider({
68
- host: config.readReplica.host,
69
- port: config.readReplica.port,
70
- user: config.readReplica.username,
71
- password: config.readReplica.password,
72
- database: config.readReplica.database,
73
- max: config.readReplica.maxConnections,
74
- }),
75
- })
76
- this.readClient = new KyselyDatabase.kyselyProvider<DBSchema>({ dialect: this.pgReadClient })
77
- }
78
85
  }
79
-
80
86
  /**
81
- * Override method that starts a new transaction using the provided client and read client.
82
- * @returns A Promise that resolves to a KyselyTransaction object for the specified database schema.
87
+ * Creates a provider for interacting with a database using the specified Postgres dialect.
88
+ * @param {PostgresDialect} dialect - The Postgres dialect to use for the database connection.
89
+ * @returns A database provider for interacting with the database.
83
90
  */
84
- public override async transaction(): Promise<KyselyTransaction<DBSchema>> {
85
- return KyselyTransactionImpl.newTransaction<DBSchema>(this.client, this, this.readClient)
91
+ private providerFactory(dialect: PostgresDialect) {
92
+ return new KyselyDatabase.kyselyProvider<DBSchema>({
93
+ dialect,
94
+ plugins: [new CamelCasePlugin()],
95
+ })
86
96
  }
87
97
  }
@@ -2,7 +2,7 @@ import { Pool } from 'pg'
2
2
 
3
3
  import { PostgresTransaction, PostgresTransactionImpl } from './PostgresTransaction'
4
4
  import { Database } from '../../Database'
5
- import type { DbConfig } from '../../types'
5
+ import type { DbBaseConfig, DbConfig } from '../../types'
6
6
 
7
7
  /**
8
8
  * Represents a Postgres database connection that extends the Database class.
@@ -31,24 +31,8 @@ export class PostgresDatabase extends Database<PostgresTransaction> {
31
31
  */
32
32
  public constructor(config: DbConfig<'pg'>) {
33
33
  super(config)
34
- this.client = new PostgresDatabase.pgProvider({
35
- host: config.host,
36
- port: config.port,
37
- user: config.username,
38
- password: config.password,
39
- database: config.database,
40
- max: config.maxConnections,
41
- })
42
- if (config.readReplica) {
43
- this.readClient = new PostgresDatabase.pgProvider({
44
- host: config.readReplica.host,
45
- port: config.readReplica.port,
46
- user: config.readReplica.username,
47
- password: config.readReplica.password,
48
- database: config.readReplica.database,
49
- max: config.readReplica.maxConnections,
50
- })
51
- }
34
+ this.client = this.providerFactory(config)
35
+ if (config.readReplica) this.readClient = this.providerFactory(config.readReplica)
52
36
  }
53
37
 
54
38
  /**
@@ -58,4 +42,14 @@ export class PostgresDatabase extends Database<PostgresTransaction> {
58
42
  public override async transaction(): Promise<PostgresTransaction> {
59
43
  return PostgresTransactionImpl.newTransaction(this.client, this, this.readClient)
60
44
  }
45
+ private providerFactory(config: DbBaseConfig) {
46
+ return new PostgresDatabase.pgProvider({
47
+ host: config.host,
48
+ port: config.port,
49
+ user: config.username,
50
+ password: config.password,
51
+ database: config.database,
52
+ max: config.maxConnections,
53
+ })
54
+ }
61
55
  }
@@ -54,7 +54,7 @@ export type DatabaseTransactionType<Type extends DatabaseType, DBSchema> =
54
54
  * @typedef {Object} DbConfig
55
55
  * @property {string} type - The type of
56
56
  */
57
- type DbBaseConfig = {
57
+ export type DbBaseConfig = {
58
58
  host: string
59
59
  port: number
60
60
  username: string
@@ -63,6 +63,16 @@ type DbBaseConfig = {
63
63
  autoCommit: boolean
64
64
  maxConnections: number
65
65
  }
66
+ /**
67
+ * Defines the configuration options for different types of databases.
68
+ * @template S - The type of database (e.g., 'knex', 'pg', 'kysely')
69
+ * @typedef {Object} DbConfig
70
+ * @property {DbBaseConfig} DbBaseConfig - The base configuration for the database.
71
+ * @property {S} type - The type of the database.
72
+ * @property {string} [driver] - The driver for the 'knex' database type.
73
+ * @property {boolean} [convertCamelToSnake] - Whether to convert camel case to snake case for 'knex' database type.
74
+ * @property {DbBaseConfig} [readReplica] - The read replica configuration for '
75
+ */
66
76
  export type DbConfig<S extends DatabaseType> = DbBaseConfig & { type: S } & (S extends 'knex'
67
77
  ? {
68
78
  driver?: string