@storecraft/database-postgres 1.0.1
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/README.md +90 -0
- package/index.js +30 -0
- package/migrate.js +5 -0
- package/package.json +39 -0
- package/tests/runner.test.js +96 -0
- package/tsconfig.json +18 -0
- package/types.public.d.ts +33 -0
package/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# Storecraft `Postgres` driver
|
2
|
+
|
3
|
+
<div style="text-align:center">
|
4
|
+
<img src='https://storecraft.app/storecraft-color.svg'
|
5
|
+
width='90%' />
|
6
|
+
</div><hr/><br/>
|
7
|
+
|
8
|
+
Official `Postgres` driver for `StoreCraft` using `pg` package.
|
9
|
+
|
10
|
+
```bash
|
11
|
+
npm i @storecraft/database-postgres
|
12
|
+
```
|
13
|
+
|
14
|
+
## usage
|
15
|
+
|
16
|
+
```js
|
17
|
+
import 'dotenv/config';
|
18
|
+
import http from "node:http";
|
19
|
+
|
20
|
+
import { App } from '@storecraft/core'
|
21
|
+
import { NodePlatform } from '@storecraft/platforms/node';
|
22
|
+
import { Postgres } from '@storecraft/database-postgres';
|
23
|
+
import { migrateToLatest } from '@storecraft/database-sql-base/migrate.js'
|
24
|
+
import { NodeLocalStorage } from '@storecraft/storage-local/node'
|
25
|
+
|
26
|
+
const app = new App(
|
27
|
+
{
|
28
|
+
auth_admins_emails: ['admin@sc.com'],
|
29
|
+
auth_secret_access_token: 'auth_secret_access_token',
|
30
|
+
auth_secret_refresh_token: 'auth_secret_refresh_token'
|
31
|
+
}
|
32
|
+
)
|
33
|
+
.withPlatform(new NodePlatform())
|
34
|
+
.withDatabase(
|
35
|
+
new Postgres({
|
36
|
+
pool_config: {
|
37
|
+
host: process.env.POSTGRES_HOST,
|
38
|
+
port: parseInt(process.env.POSTGRES_PORT),
|
39
|
+
user: process.env.POSTGRES_USER,
|
40
|
+
password: process.env.POSTGRES_PASSWORD,
|
41
|
+
}
|
42
|
+
})
|
43
|
+
)
|
44
|
+
.withStorage(new NodeLocalStorage(join(homedir(), 'tomer')))
|
45
|
+
|
46
|
+
await app.init();
|
47
|
+
await migrateToLatest(app.db, false);
|
48
|
+
|
49
|
+
const server = http.createServer(app.handler).listen(
|
50
|
+
8000,
|
51
|
+
() => {
|
52
|
+
console.log(`Server is running on http://localhost:8000`);
|
53
|
+
}
|
54
|
+
);
|
55
|
+
|
56
|
+
```
|
57
|
+
|
58
|
+
## Testing Locally
|
59
|
+
|
60
|
+
1. First setup a `postgres` server
|
61
|
+
|
62
|
+
```zsh
|
63
|
+
docker pull postgres
|
64
|
+
docker run --name some-postgres -e POSTGRES_USER=admin -e POSTGRES_PASSWORD=admin \
|
65
|
+
-e PGDATA=/var/lib/postgresql/data/pgdata \
|
66
|
+
-v $(pwd):/var/lib/postgresql/data \
|
67
|
+
-p 5432:5432 -d postgres
|
68
|
+
```
|
69
|
+
|
70
|
+
2. create Environment
|
71
|
+
|
72
|
+
create `.env` file with
|
73
|
+
|
74
|
+
```bash
|
75
|
+
POSTGRES_USER='user'
|
76
|
+
POSTGRES_PASSWORD='password'
|
77
|
+
POSTGRES_PORT=5432
|
78
|
+
POSTGRES_HOST='localhost'
|
79
|
+
```
|
80
|
+
|
81
|
+
3. Run `tests/runner.test.js`
|
82
|
+
|
83
|
+
```bash
|
84
|
+
npm run database-postgres:test
|
85
|
+
```
|
86
|
+
|
87
|
+
|
88
|
+
```text
|
89
|
+
Author: Tomer Shalev <tomer.shalev@gmail.com>
|
90
|
+
```
|
package/index.js
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
import { SQL } from '@storecraft/database-sql-base';
|
2
|
+
import { PostgresDialect } from 'kysely';
|
3
|
+
import pg from 'pg';
|
4
|
+
|
5
|
+
/**
|
6
|
+
* @description `postgres` driver for storecraft using the `pg` driver
|
7
|
+
*
|
8
|
+
*/
|
9
|
+
export class Postgres extends SQL {
|
10
|
+
|
11
|
+
/**
|
12
|
+
*
|
13
|
+
* @param {import('./types.public.d.ts').Config} [config] config
|
14
|
+
*/
|
15
|
+
constructor(config) {
|
16
|
+
super(
|
17
|
+
{
|
18
|
+
dialect_type: 'POSTGRES',
|
19
|
+
dialect: new PostgresDialect(
|
20
|
+
{
|
21
|
+
pool: new pg.Pool(config.pool_config),
|
22
|
+
cursor: config.cursor
|
23
|
+
}
|
24
|
+
),
|
25
|
+
}
|
26
|
+
);
|
27
|
+
|
28
|
+
}
|
29
|
+
|
30
|
+
}
|
package/migrate.js
ADDED
package/package.json
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
{
|
2
|
+
"name": "@storecraft/database-postgres",
|
3
|
+
"version": "1.0.1",
|
4
|
+
"description": "Official Postgres Database driver for storecraft",
|
5
|
+
"license": "MIT",
|
6
|
+
"author": "Tomer Shalev (https://github.com/store-craft)",
|
7
|
+
"homepage": "https://github.com/store-craft/storecraft",
|
8
|
+
"repository": {
|
9
|
+
"type": "git",
|
10
|
+
"url": "https://github.com/store-craft/storecraft.git",
|
11
|
+
"directory": "packages/databases/database-postgres"
|
12
|
+
},
|
13
|
+
"keywords": [
|
14
|
+
"commerce",
|
15
|
+
"dashboard",
|
16
|
+
"code",
|
17
|
+
"storecraft"
|
18
|
+
],
|
19
|
+
"type": "module",
|
20
|
+
"main": "index.js",
|
21
|
+
"types": "types.public.d.ts",
|
22
|
+
"scripts": {
|
23
|
+
"database-postgres:test": "node ./tests/runner.test.js",
|
24
|
+
"database-postgres:publish": "npm publish --access public"
|
25
|
+
},
|
26
|
+
"dependencies": {
|
27
|
+
"@storecraft/core": "^1.0.0",
|
28
|
+
"@storecraft/database-sql-base": "^1.0.0",
|
29
|
+
"pg": "^8.11.3"
|
30
|
+
},
|
31
|
+
"devDependencies": {
|
32
|
+
"@storecraft/platforms": "^1.0.0",
|
33
|
+
"@storecraft/test-runner": "^1.0.0",
|
34
|
+
"@types/pg": "^8.11.2",
|
35
|
+
"@types/node": "^20.11.0",
|
36
|
+
"dotenv": "^16.3.1",
|
37
|
+
"uvu": "^0.5.6"
|
38
|
+
}
|
39
|
+
}
|
@@ -0,0 +1,96 @@
|
|
1
|
+
import { App } from '@storecraft/core';
|
2
|
+
import { Postgres } from '@storecraft/database-postgres';
|
3
|
+
import { migrateToLatest } from '@storecraft/database-sql-base/migrate.js';
|
4
|
+
import { NodePlatform } from '@storecraft/platforms/node';
|
5
|
+
import { api_index } from '@storecraft/test-runner'
|
6
|
+
|
7
|
+
export const create_app = async () => {
|
8
|
+
|
9
|
+
const app = new App(
|
10
|
+
{
|
11
|
+
auth_admins_emails: ['admin@sc.com'],
|
12
|
+
auth_secret_access_token: 'auth_secret_access_token',
|
13
|
+
auth_secret_refresh_token: 'auth_secret_refresh_token'
|
14
|
+
}
|
15
|
+
)
|
16
|
+
.withPlatform(new NodePlatform())
|
17
|
+
.withDatabase(
|
18
|
+
new Postgres({
|
19
|
+
pool_config: {
|
20
|
+
host: process.env.POSTGRES_HOST,
|
21
|
+
port: parseInt(process.env.POSTGRES_PORT),
|
22
|
+
user: process.env.POSTGRES_USER,
|
23
|
+
password: process.env.POSTGRES_PASSWORD,
|
24
|
+
}
|
25
|
+
})
|
26
|
+
);
|
27
|
+
|
28
|
+
await app.init();
|
29
|
+
await migrateToLatest(app.db, false);
|
30
|
+
|
31
|
+
return app;
|
32
|
+
}
|
33
|
+
|
34
|
+
async function test() {
|
35
|
+
const app = await create_app();
|
36
|
+
|
37
|
+
Object.entries(api_index).slice(0, -1).forEach(
|
38
|
+
([name, runner]) => {
|
39
|
+
runner.create(app).run();
|
40
|
+
}
|
41
|
+
);
|
42
|
+
const last_test = Object.values(api_index).at(-1).create(app);
|
43
|
+
last_test.after(async () => { await app.db.disconnect() });
|
44
|
+
last_test.run();
|
45
|
+
}
|
46
|
+
|
47
|
+
test();
|
48
|
+
|
49
|
+
async function test2() {
|
50
|
+
const app = await create_app();
|
51
|
+
|
52
|
+
api_index.api_auth_test.create(app).run();
|
53
|
+
|
54
|
+
api_index.api_tags_crud_test.create(app).run();
|
55
|
+
api_index.api_tags_list_test.create(app).run();
|
56
|
+
|
57
|
+
api_index.api_collections_crud_test.create(app).run();
|
58
|
+
api_index.api_collections_list_test.create(app).run();
|
59
|
+
api_index.api_collections_products_test.create(app).run();
|
60
|
+
|
61
|
+
api_index.api_products_crud_test.create(app).run();
|
62
|
+
api_index.api_products_collections_test.create(app).run();
|
63
|
+
api_index.api_products_list_test.create(app).run();
|
64
|
+
api_index.api_products_discounts_test.create(app).run();
|
65
|
+
api_index.api_products_variants_test.create(app).run();
|
66
|
+
|
67
|
+
api_index.api_shipping_crud_test.create(app).run();
|
68
|
+
api_index.api_shipping_list_test.create(app).run();
|
69
|
+
|
70
|
+
api_index.api_posts_crud_test.create(app).run();
|
71
|
+
api_index.api_posts_list_test.create(app).run();
|
72
|
+
|
73
|
+
api_index.api_customers_crud_test.create(app).run();
|
74
|
+
api_index.api_customers_list_test.create(app).run();
|
75
|
+
|
76
|
+
api_index.api_orders_crud_test.create(app).run();
|
77
|
+
api_index.api_orders_list_test.create(app).run();
|
78
|
+
|
79
|
+
api_index.api_storefronts_crud_test.create(app).run();
|
80
|
+
api_index.api_storefronts_list_test.create(app).run();
|
81
|
+
api_index.api_storefronts_all_connections_test.create(app).run();
|
82
|
+
|
83
|
+
api_index.api_notifications_crud_test.create(app).run();
|
84
|
+
api_index.api_notifications_list_test.create(app).run();
|
85
|
+
|
86
|
+
api_index.api_images_crud_test.create(app).run();
|
87
|
+
api_index.api_images_list_test.create(app).run();
|
88
|
+
|
89
|
+
api_index.api_discounts_crud_test.create(app).run();
|
90
|
+
api_index.api_discounts_list_test.create(app).run();
|
91
|
+
api_index.api_discounts_products_test.create(app).run();
|
92
|
+
|
93
|
+
|
94
|
+
}
|
95
|
+
|
96
|
+
// test2();
|
package/tsconfig.json
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
"checkJs": true,
|
4
|
+
"allowJs": true,
|
5
|
+
"maxNodeModuleJsDepth": 10,
|
6
|
+
"moduleResolution": "NodeNext",
|
7
|
+
"module": "NodeNext",
|
8
|
+
"composite": true,
|
9
|
+
},
|
10
|
+
"include": [
|
11
|
+
"*.js",
|
12
|
+
"src/*",
|
13
|
+
"migrations.sqlite/*",
|
14
|
+
"migrations.postgres/*",
|
15
|
+
"migrations.mysql/*",
|
16
|
+
"test/*.js"
|
17
|
+
]
|
18
|
+
}
|
@@ -0,0 +1,33 @@
|
|
1
|
+
import { PostgresCursorConstructor } from 'kysely';
|
2
|
+
import { PoolConfig } from 'pg';
|
3
|
+
|
4
|
+
export { Postgres } from './index.js';
|
5
|
+
|
6
|
+
/**
|
7
|
+
* @description The `postgres` config
|
8
|
+
*/
|
9
|
+
export type Config = {
|
10
|
+
|
11
|
+
/**
|
12
|
+
* @description A postgres Pool instance or a function that returns one.
|
13
|
+
*
|
14
|
+
* If a function is provided, it's called once when the first query is executed.
|
15
|
+
*
|
16
|
+
* https://node-postgres.com/apis/pool
|
17
|
+
*/
|
18
|
+
pool_config: PoolConfig;
|
19
|
+
|
20
|
+
/**
|
21
|
+
* @description https://github.com/brianc/node-postgres/tree/master/packages/pg-cursor
|
22
|
+
* ```ts
|
23
|
+
* import Cursor from 'pg-cursor'
|
24
|
+
* // or
|
25
|
+
* import * as Cursor from 'pg-cursor'
|
26
|
+
*
|
27
|
+
* new PostgresDialect({
|
28
|
+
* cursor: Cursor
|
29
|
+
* })
|
30
|
+
* ```
|
31
|
+
*/
|
32
|
+
cursor?: PostgresCursorConstructor;
|
33
|
+
}
|