@easy-cms/db-postgres 0.1.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/LICENSE +21 -0
- package/README.md +25 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +105 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kanawoot K.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# @easy-cms/db-postgres
|
|
2
|
+
|
|
3
|
+
PostgreSQL adapter for [Easy CMS](https://github.com/maritonx/easy-crm).
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { postgres } from '@easy-cms/db-postgres'
|
|
7
|
+
|
|
8
|
+
export default defineConfig({
|
|
9
|
+
db: process.env.DATABASE_URL
|
|
10
|
+
? postgres({ url: process.env.DATABASE_URL }) // a Postgres server, via postgres.js
|
|
11
|
+
: postgres({ pglite: '.pglite' }), // Postgres in WebAssembly for local development
|
|
12
|
+
// ...
|
|
13
|
+
})
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
| Option | Default | |
|
|
17
|
+
|---|---|---|
|
|
18
|
+
| `url` | — | Connection string |
|
|
19
|
+
| `max` | `10` | Pool size |
|
|
20
|
+
| `pglite` | — | Data directory (relative to the project), `memory://`, or a `PGlite` instance. Needs `@electric-sql/pglite`. |
|
|
21
|
+
| `tablePrefix` | `ecms_` | Prefix of every table Easy CMS creates |
|
|
22
|
+
| `migrationDir` | `easy-cms/migrations` | Where migration files are written |
|
|
23
|
+
|
|
24
|
+
Migration files record the database they were made for; files created for SQLite are refused.
|
|
25
|
+
Text sorting follows the database collation (server defaults like `en_US.UTF-8` sort case-insensitively).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { DatabaseAdapter } from '@easy-cms/core';
|
|
2
|
+
import { DrizzleAdapterOptions, Dialect } from '@easy-cms/drizzle';
|
|
3
|
+
export { DEFAULT_MIGRATION_DIR, DEFAULT_TABLE_PREFIX } from '@easy-cms/drizzle';
|
|
4
|
+
import { PGlite } from '@electric-sql/pglite';
|
|
5
|
+
|
|
6
|
+
interface PostgresAdapterOptions extends DrizzleAdapterOptions {
|
|
7
|
+
/** Connection string, e.g. `postgres://user:pass@host:5432/db`. Uses postgres.js. */
|
|
8
|
+
readonly url?: string;
|
|
9
|
+
/** Maximum connections in the pool. Default 10. */
|
|
10
|
+
readonly max?: number;
|
|
11
|
+
/**
|
|
12
|
+
* Use PGlite (Postgres in WebAssembly) instead of a server: a data directory
|
|
13
|
+
* (relative to the project root), `memory://`, or a PGlite instance you manage.
|
|
14
|
+
* Handy for local development and tests. Requires `@electric-sql/pglite`.
|
|
15
|
+
*/
|
|
16
|
+
readonly pglite?: string | PGlite;
|
|
17
|
+
}
|
|
18
|
+
declare const postgresDialect: Dialect;
|
|
19
|
+
/** PostgreSQL database adapter (postgres.js, or PGlite for local use). */
|
|
20
|
+
declare function postgres(options: PostgresAdapterOptions): DatabaseAdapter;
|
|
21
|
+
|
|
22
|
+
export { type PostgresAdapterOptions, postgres, postgresDialect };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { isAbsolute, resolve } from "path";
|
|
3
|
+
import {
|
|
4
|
+
connectDatabase
|
|
5
|
+
} from "@easy-cms/drizzle";
|
|
6
|
+
import { sql } from "drizzle-orm";
|
|
7
|
+
import {
|
|
8
|
+
boolean,
|
|
9
|
+
doublePrecision,
|
|
10
|
+
index,
|
|
11
|
+
integer,
|
|
12
|
+
jsonb,
|
|
13
|
+
pgTable,
|
|
14
|
+
serial,
|
|
15
|
+
text,
|
|
16
|
+
uniqueIndex
|
|
17
|
+
} from "drizzle-orm/pg-core";
|
|
18
|
+
import { drizzle as drizzlePostgresJs } from "drizzle-orm/postgres-js";
|
|
19
|
+
import postgresJs from "postgres";
|
|
20
|
+
import { DEFAULT_MIGRATION_DIR, DEFAULT_TABLE_PREFIX } from "@easy-cms/drizzle";
|
|
21
|
+
var kit = () => import("drizzle-kit/api");
|
|
22
|
+
var postgresDialect = {
|
|
23
|
+
name: "postgres",
|
|
24
|
+
table: (name, columns, indexes) => pgTable(name, columns, indexes),
|
|
25
|
+
index: (name) => index(name),
|
|
26
|
+
uniqueIndex: (name) => uniqueIndex(name),
|
|
27
|
+
serial: (name) => serial(name).primaryKey(),
|
|
28
|
+
text: (name) => text(name),
|
|
29
|
+
integer: (name) => integer(name),
|
|
30
|
+
number: (name) => doublePrecision(name),
|
|
31
|
+
boolean: (name) => boolean(name),
|
|
32
|
+
json: (name) => jsonb(name),
|
|
33
|
+
like: (column, pattern) => sql`${column} ILIKE ${pattern} ESCAPE '\\'`,
|
|
34
|
+
param: (n) => `$${n}`,
|
|
35
|
+
migrationsTableSQL: (name) => `CREATE TABLE IF NOT EXISTS "${name}" (
|
|
36
|
+
"id" serial PRIMARY KEY NOT NULL,
|
|
37
|
+
"name" text NOT NULL UNIQUE,
|
|
38
|
+
"hash" text,
|
|
39
|
+
"snapshot" text,
|
|
40
|
+
"applied_at" text NOT NULL
|
|
41
|
+
)`,
|
|
42
|
+
snapshot: async (tables) => (await kit()).generateDrizzleJson({ ...tables }),
|
|
43
|
+
migration: async (prev, cur) => (await kit()).generateMigration(prev, cur)
|
|
44
|
+
};
|
|
45
|
+
function postgres(options) {
|
|
46
|
+
if (!options.url && !options.pglite) {
|
|
47
|
+
throw new Error("@easy-cms/db-postgres: pass `url` (a connection string) or `pglite`");
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
name: "postgres",
|
|
51
|
+
init: async (args) => {
|
|
52
|
+
const connection = options.pglite ? await pgliteConnection(options.pglite, args.cwd) : postgresJsConnection(options.url, options.max ?? 10);
|
|
53
|
+
return connectDatabase(postgresDialect, connection, args, options);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function postgresJsConnection(url, max) {
|
|
58
|
+
const client = postgresJs(url, { max, onnotice: () => {
|
|
59
|
+
} });
|
|
60
|
+
const runner = {
|
|
61
|
+
async query(text2, params = []) {
|
|
62
|
+
return [...await client.unsafe(text2, [...params])];
|
|
63
|
+
},
|
|
64
|
+
async transaction(statements) {
|
|
65
|
+
await client.begin(async (tx) => {
|
|
66
|
+
for (const s of statements) await tx.unsafe(s.sql, [...s.params ?? []]);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
return { db: drizzlePostgresJs(client), runner, close: () => client.end({ timeout: 5 }) };
|
|
71
|
+
}
|
|
72
|
+
async function pgliteConnection(source, cwd) {
|
|
73
|
+
const owned = typeof source === "string";
|
|
74
|
+
let pg;
|
|
75
|
+
if (typeof source === "string") {
|
|
76
|
+
const { PGlite } = await import("@electric-sql/pglite");
|
|
77
|
+
const dataDir = source.includes("://") || isAbsolute(source) ? source : resolve(cwd, source);
|
|
78
|
+
pg = new PGlite(dataDir);
|
|
79
|
+
} else {
|
|
80
|
+
pg = source;
|
|
81
|
+
}
|
|
82
|
+
await pg.waitReady;
|
|
83
|
+
const { drizzle } = await import("drizzle-orm/pglite");
|
|
84
|
+
const runner = {
|
|
85
|
+
async query(text2, params = []) {
|
|
86
|
+
return (await pg.query(text2, [...params])).rows;
|
|
87
|
+
},
|
|
88
|
+
async transaction(statements) {
|
|
89
|
+
await pg.transaction(async (tx) => {
|
|
90
|
+
for (const s of statements) await tx.query(s.sql, [...s.params ?? []]);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
return {
|
|
95
|
+
db: drizzle({ client: pg }),
|
|
96
|
+
runner,
|
|
97
|
+
close: async () => owned ? pg.close() : void 0
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
export {
|
|
101
|
+
DEFAULT_MIGRATION_DIR,
|
|
102
|
+
DEFAULT_TABLE_PREFIX,
|
|
103
|
+
postgres,
|
|
104
|
+
postgresDialect
|
|
105
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@easy-cms/db-postgres",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "PostgreSQL database adapter for Easy CMS",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=22.12"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"drizzle-kit": "0.31.11",
|
|
23
|
+
"drizzle-orm": "0.45.3",
|
|
24
|
+
"postgres": "^3.4.9",
|
|
25
|
+
"@easy-cms/drizzle": "^0.1.0"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"@electric-sql/pglite": ">=0.3.0",
|
|
29
|
+
"@easy-cms/core": "^0.1.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependenciesMeta": {
|
|
32
|
+
"@electric-sql/pglite": {
|
|
33
|
+
"optional": true
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@electric-sql/pglite": "^0.5.8",
|
|
38
|
+
"@easy-cms/core": "^0.1.0"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "git+https://github.com/maritonx/easy-crm.git",
|
|
46
|
+
"directory": "packages/db-postgres"
|
|
47
|
+
},
|
|
48
|
+
"keywords": [
|
|
49
|
+
"easy-cms",
|
|
50
|
+
"postgres",
|
|
51
|
+
"postgresql",
|
|
52
|
+
"pglite",
|
|
53
|
+
"drizzle"
|
|
54
|
+
],
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "tsup",
|
|
57
|
+
"typecheck": "tsc --noEmit",
|
|
58
|
+
"test": "vitest run"
|
|
59
|
+
}
|
|
60
|
+
}
|