@pikku/kysely 0.10.0 → 0.11.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/CHANGELOG.md +8 -0
- package/dist/src/index.d.ts +0 -2
- package/dist/src/index.js +0 -2
- package/dist/src/pikku-kysely.d.ts +3 -2
- package/dist/src/pikku-kysely.js +25 -5
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/index.ts +0 -2
- package/src/pikku-kysely.ts +28 -6
- package/dist/src/kysely-channel-store.d.ts +0 -13
- package/dist/src/kysely-channel-store.js +0 -43
- package/dist/src/kysely-eventhub-store.d.ts +0 -9
- package/dist/src/kysely-eventhub-store.js +0 -29
- package/sql/serverless-tables.sql +0 -16
- package/src/kysely-channel-store.ts +0 -53
- package/src/kysely-eventhub-store.ts +0 -32
package/CHANGELOG.md
CHANGED
package/dist/src/index.d.ts
CHANGED
package/dist/src/index.js
CHANGED
|
@@ -3,10 +3,11 @@ import { Kysely } from 'kysely';
|
|
|
3
3
|
import postgres from 'postgres';
|
|
4
4
|
export declare class PikkuKysely<DB> {
|
|
5
5
|
private logger;
|
|
6
|
-
private poolConfig;
|
|
7
6
|
kysely: Kysely<DB>;
|
|
8
7
|
private postgres;
|
|
9
|
-
|
|
8
|
+
private poolConfig?;
|
|
9
|
+
private ownsConnection;
|
|
10
|
+
constructor(logger: Logger, connectionOrConfig: postgres.Sql<{}> | postgres.Options<{}>, defaultSchemaName?: string);
|
|
10
11
|
init(): Promise<void>;
|
|
11
12
|
close(): Promise<void>;
|
|
12
13
|
}
|
package/dist/src/pikku-kysely.js
CHANGED
|
@@ -3,13 +3,24 @@ import { PostgresJSDialect } from 'kysely-postgres-js';
|
|
|
3
3
|
import postgres from 'postgres';
|
|
4
4
|
export class PikkuKysely {
|
|
5
5
|
logger;
|
|
6
|
-
poolConfig;
|
|
7
6
|
kysely;
|
|
8
7
|
postgres;
|
|
9
|
-
|
|
8
|
+
poolConfig;
|
|
9
|
+
ownsConnection;
|
|
10
|
+
constructor(logger, connectionOrConfig, defaultSchemaName) {
|
|
10
11
|
this.logger = logger;
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
// Check if it's a postgres.Sql instance or config options
|
|
13
|
+
if (typeof connectionOrConfig === 'function') {
|
|
14
|
+
// It's a postgres.Sql instance
|
|
15
|
+
this.postgres = connectionOrConfig;
|
|
16
|
+
this.ownsConnection = false;
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
// It's a config object
|
|
20
|
+
this.poolConfig = connectionOrConfig;
|
|
21
|
+
this.postgres = postgres(connectionOrConfig);
|
|
22
|
+
this.ownsConnection = true;
|
|
23
|
+
}
|
|
13
24
|
this.kysely = new Kysely({
|
|
14
25
|
dialect: new PostgresJSDialect({
|
|
15
26
|
postgres: this.postgres,
|
|
@@ -21,7 +32,12 @@ export class PikkuKysely {
|
|
|
21
32
|
}
|
|
22
33
|
}
|
|
23
34
|
async init() {
|
|
24
|
-
|
|
35
|
+
if (this.poolConfig) {
|
|
36
|
+
this.logger.info(`Connecting to database: ${this.poolConfig.host}:${this.poolConfig.port} with name ${this.poolConfig.database}`);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
this.logger.info('Using existing postgres connection');
|
|
40
|
+
}
|
|
25
41
|
try {
|
|
26
42
|
const response = await this.postgres `SELECT version();`;
|
|
27
43
|
const version = response[0]?.version;
|
|
@@ -34,5 +50,9 @@ export class PikkuKysely {
|
|
|
34
50
|
}
|
|
35
51
|
async close() {
|
|
36
52
|
await this.kysely.destroy();
|
|
53
|
+
// Only end the connection if we created it
|
|
54
|
+
if (this.ownsConnection) {
|
|
55
|
+
await this.postgres.end();
|
|
56
|
+
}
|
|
37
57
|
}
|
|
38
58
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["../src/index.ts","../src/
|
|
1
|
+
{"root":["../src/index.ts","../src/pikku-kysely.ts","../bin/pikku-kysely-pure.ts"],"version":"5.9.3"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pikku/kysely",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"author": "yasser.fadl@gmail.com",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"module": "dist/src/index.js",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"release": "npm run build && npm test"
|
|
17
17
|
},
|
|
18
18
|
"peerDependencies": {
|
|
19
|
-
"@pikku/core": "^0.
|
|
19
|
+
"@pikku/core": "^0.11.0"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"kysely": "^0.28.8",
|
package/src/index.ts
CHANGED
package/src/pikku-kysely.ts
CHANGED
|
@@ -6,13 +6,26 @@ import postgres from 'postgres'
|
|
|
6
6
|
export class PikkuKysely<DB> {
|
|
7
7
|
public kysely: Kysely<DB>
|
|
8
8
|
private postgres: postgres.Sql<{}>
|
|
9
|
+
private poolConfig?: postgres.Options<{}>
|
|
10
|
+
private ownsConnection: boolean
|
|
9
11
|
|
|
10
12
|
constructor(
|
|
11
13
|
private logger: Logger,
|
|
12
|
-
|
|
13
|
-
defaultSchemaName
|
|
14
|
+
connectionOrConfig: postgres.Sql<{}> | postgres.Options<{}>,
|
|
15
|
+
defaultSchemaName?: string
|
|
14
16
|
) {
|
|
15
|
-
|
|
17
|
+
// Check if it's a postgres.Sql instance or config options
|
|
18
|
+
if (typeof connectionOrConfig === 'function') {
|
|
19
|
+
// It's a postgres.Sql instance
|
|
20
|
+
this.postgres = connectionOrConfig as postgres.Sql<{}>
|
|
21
|
+
this.ownsConnection = false
|
|
22
|
+
} else {
|
|
23
|
+
// It's a config object
|
|
24
|
+
this.poolConfig = connectionOrConfig
|
|
25
|
+
this.postgres = postgres(connectionOrConfig)
|
|
26
|
+
this.ownsConnection = true
|
|
27
|
+
}
|
|
28
|
+
|
|
16
29
|
this.kysely = new Kysely<DB>({
|
|
17
30
|
dialect: new PostgresJSDialect({
|
|
18
31
|
postgres: this.postgres,
|
|
@@ -26,9 +39,14 @@ export class PikkuKysely<DB> {
|
|
|
26
39
|
}
|
|
27
40
|
|
|
28
41
|
public async init() {
|
|
29
|
-
this.
|
|
30
|
-
|
|
31
|
-
|
|
42
|
+
if (this.poolConfig) {
|
|
43
|
+
this.logger.info(
|
|
44
|
+
`Connecting to database: ${this.poolConfig.host}:${this.poolConfig.port} with name ${this.poolConfig.database}`
|
|
45
|
+
)
|
|
46
|
+
} else {
|
|
47
|
+
this.logger.info('Using existing postgres connection')
|
|
48
|
+
}
|
|
49
|
+
|
|
32
50
|
try {
|
|
33
51
|
const response = await this.postgres`SELECT version();`
|
|
34
52
|
const version = response[0]?.version
|
|
@@ -41,5 +59,9 @@ export class PikkuKysely<DB> {
|
|
|
41
59
|
|
|
42
60
|
public async close() {
|
|
43
61
|
await this.kysely.destroy()
|
|
62
|
+
// Only end the connection if we created it
|
|
63
|
+
if (this.ownsConnection) {
|
|
64
|
+
await this.postgres.end()
|
|
65
|
+
}
|
|
44
66
|
}
|
|
45
67
|
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { CoreUserSession } from '@pikku/core';
|
|
2
|
-
import { Channel, ChannelStore } from '@pikku/core/channel';
|
|
3
|
-
import { Kysely } from 'kysely';
|
|
4
|
-
export declare class KyselyChannelStore extends ChannelStore {
|
|
5
|
-
private database;
|
|
6
|
-
constructor(database: Kysely<any>);
|
|
7
|
-
addChannel({ channelId, channelName, openingData, }: Channel): Promise<void>;
|
|
8
|
-
removeChannels(channelIds: string[]): Promise<void>;
|
|
9
|
-
setUserSession(channelId: string, session: any): Promise<void>;
|
|
10
|
-
getChannelAndSession(channelId: string): Promise<Channel & {
|
|
11
|
-
session: CoreUserSession;
|
|
12
|
-
}>;
|
|
13
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { ChannelStore } from '@pikku/core/channel';
|
|
2
|
-
export class KyselyChannelStore extends ChannelStore {
|
|
3
|
-
database;
|
|
4
|
-
constructor(database) {
|
|
5
|
-
super();
|
|
6
|
-
this.database = database;
|
|
7
|
-
}
|
|
8
|
-
async addChannel({ channelId, channelName, openingData, }) {
|
|
9
|
-
await this.database
|
|
10
|
-
.insertInto('serverless.lambdaChannels')
|
|
11
|
-
.values({
|
|
12
|
-
channelId,
|
|
13
|
-
channelName,
|
|
14
|
-
openingData: openingData,
|
|
15
|
-
})
|
|
16
|
-
.execute();
|
|
17
|
-
}
|
|
18
|
-
async removeChannels(channelIds) {
|
|
19
|
-
await this.database
|
|
20
|
-
.deleteFrom('serverless.lambdaChannels')
|
|
21
|
-
.where('channelId', 'in', channelIds)
|
|
22
|
-
.execute();
|
|
23
|
-
}
|
|
24
|
-
async setUserSession(channelId, session) {
|
|
25
|
-
await this.database
|
|
26
|
-
.updateTable('serverless.lambdaChannels')
|
|
27
|
-
.where('channelId', '=', channelId)
|
|
28
|
-
.set('userSession', session)
|
|
29
|
-
.executeTakeFirstOrThrow();
|
|
30
|
-
}
|
|
31
|
-
async getChannelAndSession(channelId) {
|
|
32
|
-
const result = await this.database
|
|
33
|
-
.selectFrom('serverless.lambdaChannels')
|
|
34
|
-
.selectAll()
|
|
35
|
-
.where('channelId', '=', channelId)
|
|
36
|
-
.executeTakeFirstOrThrow();
|
|
37
|
-
return {
|
|
38
|
-
openingData: result.openingData,
|
|
39
|
-
session: result.userSession,
|
|
40
|
-
channelName: result.channelName,
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { EventHubStore } from '@pikku/core/channel';
|
|
2
|
-
import { Kysely } from 'kysely';
|
|
3
|
-
export declare class KyselyEventHubStore implements EventHubStore {
|
|
4
|
-
private database;
|
|
5
|
-
constructor(database: Kysely<any>);
|
|
6
|
-
getChannelIdsForTopic(topic: string): Promise<string[]>;
|
|
7
|
-
subscribe(topic: string, channelId: string): Promise<boolean>;
|
|
8
|
-
unsubscribe(topic: string, channelId: string): Promise<boolean>;
|
|
9
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
export class KyselyEventHubStore {
|
|
2
|
-
database;
|
|
3
|
-
constructor(database) {
|
|
4
|
-
this.database = database;
|
|
5
|
-
}
|
|
6
|
-
async getChannelIdsForTopic(topic) {
|
|
7
|
-
const result = await this.database
|
|
8
|
-
.selectFrom('serverless.lambdaChannelSubscriptions')
|
|
9
|
-
.select('channelId')
|
|
10
|
-
.where('topic', '=', topic)
|
|
11
|
-
.execute();
|
|
12
|
-
return result.map((row) => row.channelId);
|
|
13
|
-
}
|
|
14
|
-
async subscribe(topic, channelId) {
|
|
15
|
-
await this.database
|
|
16
|
-
.insertInto('serverless.lambdaChannelSubscriptions')
|
|
17
|
-
.values({ channelId, topic })
|
|
18
|
-
.execute();
|
|
19
|
-
return true;
|
|
20
|
-
}
|
|
21
|
-
async unsubscribe(topic, channelId) {
|
|
22
|
-
await this.database
|
|
23
|
-
.deleteFrom('serverless.lambdaChannelSubscriptions')
|
|
24
|
-
.where('channelId', '=', channelId)
|
|
25
|
-
.where('topic', '=', topic)
|
|
26
|
-
.execute();
|
|
27
|
-
return true;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
CREATE SCHEMA serverless;
|
|
2
|
-
|
|
3
|
-
CREATE TABLE serverless.lambda_channels (
|
|
4
|
-
channel_id TEXT PRIMARY KEY,
|
|
5
|
-
channel_name TEXT NOT NULL,
|
|
6
|
-
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
7
|
-
opening_data JSONB NOT NULL DEFAULT '{}',
|
|
8
|
-
user_session JSONB,
|
|
9
|
-
last_interaction TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
10
|
-
);
|
|
11
|
-
|
|
12
|
-
CREATE TABLE serverless.lambda_channel_subscriptions (
|
|
13
|
-
channel_id TEXT NOT NULL REFERENCES serverless.lambda_channels(channel_id) ON DELETE CASCADE,
|
|
14
|
-
topic TEXT NOT NULL,
|
|
15
|
-
PRIMARY KEY (channel_id, topic)
|
|
16
|
-
);
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { CoreUserSession } from '@pikku/core'
|
|
2
|
-
import { Channel, ChannelStore } from '@pikku/core/channel'
|
|
3
|
-
import { Kysely } from 'kysely'
|
|
4
|
-
|
|
5
|
-
export class KyselyChannelStore extends ChannelStore {
|
|
6
|
-
constructor(private database: Kysely<any>) {
|
|
7
|
-
super()
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
public async addChannel({
|
|
11
|
-
channelId,
|
|
12
|
-
channelName,
|
|
13
|
-
openingData,
|
|
14
|
-
}: Channel): Promise<void> {
|
|
15
|
-
await this.database
|
|
16
|
-
.insertInto('serverless.lambdaChannels')
|
|
17
|
-
.values({
|
|
18
|
-
channelId,
|
|
19
|
-
channelName,
|
|
20
|
-
openingData: openingData as any,
|
|
21
|
-
})
|
|
22
|
-
.execute()
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public async removeChannels(channelIds: string[]): Promise<void> {
|
|
26
|
-
await this.database
|
|
27
|
-
.deleteFrom('serverless.lambdaChannels')
|
|
28
|
-
.where('channelId', 'in', channelIds)
|
|
29
|
-
.execute()
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
public async setUserSession(channelId: string, session: any): Promise<void> {
|
|
33
|
-
await this.database
|
|
34
|
-
.updateTable('serverless.lambdaChannels')
|
|
35
|
-
.where('channelId', '=', channelId)
|
|
36
|
-
.set('userSession', session)
|
|
37
|
-
.executeTakeFirstOrThrow()
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public async getChannelAndSession(channelId: string) {
|
|
41
|
-
const result = await this.database
|
|
42
|
-
.selectFrom('serverless.lambdaChannels')
|
|
43
|
-
.selectAll()
|
|
44
|
-
.where('channelId', '=', channelId)
|
|
45
|
-
.executeTakeFirstOrThrow()
|
|
46
|
-
|
|
47
|
-
return {
|
|
48
|
-
openingData: result.openingData as any,
|
|
49
|
-
session: result.userSession as CoreUserSession,
|
|
50
|
-
channelName: result.channelName,
|
|
51
|
-
} as Channel & { session: CoreUserSession }
|
|
52
|
-
}
|
|
53
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { EventHubStore } from '@pikku/core/channel'
|
|
2
|
-
import { Kysely } from 'kysely'
|
|
3
|
-
|
|
4
|
-
export class KyselyEventHubStore implements EventHubStore {
|
|
5
|
-
constructor(private database: Kysely<any>) {}
|
|
6
|
-
|
|
7
|
-
public async getChannelIdsForTopic(topic: string): Promise<string[]> {
|
|
8
|
-
const result = await this.database
|
|
9
|
-
.selectFrom('serverless.lambdaChannelSubscriptions')
|
|
10
|
-
.select('channelId')
|
|
11
|
-
.where('topic', '=', topic)
|
|
12
|
-
.execute()
|
|
13
|
-
return result.map((row) => row.channelId)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
public async subscribe(topic: string, channelId: string): Promise<boolean> {
|
|
17
|
-
await this.database
|
|
18
|
-
.insertInto('serverless.lambdaChannelSubscriptions')
|
|
19
|
-
.values({ channelId, topic })
|
|
20
|
-
.execute()
|
|
21
|
-
return true
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
public async unsubscribe(topic: string, channelId: string): Promise<boolean> {
|
|
25
|
-
await this.database
|
|
26
|
-
.deleteFrom('serverless.lambdaChannelSubscriptions')
|
|
27
|
-
.where('channelId', '=', channelId)
|
|
28
|
-
.where('topic', '=', topic)
|
|
29
|
-
.execute()
|
|
30
|
-
return true
|
|
31
|
-
}
|
|
32
|
-
}
|