initial-commit
This commit is contained in:
38
.gitignore
vendored
Normal file
38
.gitignore
vendored
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
pnpm-debug.log*
|
||||||
|
lerna-debug.log*
|
||||||
|
|
||||||
|
.env
|
||||||
|
node_modules
|
||||||
|
.DS_Store
|
||||||
|
dist
|
||||||
|
dist-ssr
|
||||||
|
coverage
|
||||||
|
*.local
|
||||||
|
.npmrc
|
||||||
|
|
||||||
|
# Editor directories and files
|
||||||
|
.vscode/*
|
||||||
|
!.vscode/extensions.json
|
||||||
|
.idea
|
||||||
|
*.suo
|
||||||
|
*.ntvs*
|
||||||
|
*.njsproj
|
||||||
|
*.sln
|
||||||
|
*.sw?
|
||||||
|
|
||||||
|
*.tsbuildinfo
|
||||||
|
|
||||||
|
.eslintcache
|
||||||
|
|
||||||
|
# Cypress
|
||||||
|
/cypress/videos/
|
||||||
|
/cypress/screenshots/
|
||||||
|
|
||||||
|
# Vitest
|
||||||
|
__screenshots__/
|
||||||
20
README.md
Normal file
20
README.md
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# Taker of Notes Plugin: PostgreSQL
|
||||||
|
|
||||||
|
Taker of Notes storage plugin for PostgreSQL databases
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
1. Install dependencies: `npm install`
|
||||||
|
2. Run tests: `npm test`
|
||||||
|
3. Build: `npm run build`
|
||||||
|
|
||||||
|
## Project Structure
|
||||||
|
|
||||||
|
- `src/` - Source code
|
||||||
|
- `test/` - Tests
|
||||||
|
- `dist/` - Built output
|
||||||
|
- `rollup.config.js` - Build configuration
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
Implement your storage adapter in `src/PostgreSQLAdapter.js`.
|
||||||
1866
package-lock.json
generated
Normal file
1866
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
28
package.json
Normal file
28
package.json
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"name": "@takerofnotes/plugin-postgre-sql",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"repository": {
|
||||||
|
"type": "git"
|
||||||
|
},
|
||||||
|
"type": "module",
|
||||||
|
"main": "dist/index.js",
|
||||||
|
"exports": "./dist/index.js",
|
||||||
|
"scripts": {
|
||||||
|
"build": "rollup -c",
|
||||||
|
"test": "vitest",
|
||||||
|
"prepublishOnly": "npm run build"
|
||||||
|
},
|
||||||
|
"author": "nicwands",
|
||||||
|
"license": "MIT",
|
||||||
|
"description": "Taker of Notes storage plugin for PostgreSQL databases",
|
||||||
|
"dependencies": {
|
||||||
|
"@takerofnotes/plugin-sdk": "^0.4.0",
|
||||||
|
"pg": "^8.13.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@rollup/plugin-commonjs": "^29.0.0",
|
||||||
|
"@rollup/plugin-node-resolve": "^16.0.3",
|
||||||
|
"rollup": "^4.59.0",
|
||||||
|
"vitest": "^4.0.18"
|
||||||
|
}
|
||||||
|
}
|
||||||
13
rollup.config.js
Normal file
13
rollup.config.js
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
// rollup.config.js
|
||||||
|
import resolve from '@rollup/plugin-node-resolve'
|
||||||
|
import commonjs from '@rollup/plugin-commonjs'
|
||||||
|
|
||||||
|
export default {
|
||||||
|
input: 'src/index.js',
|
||||||
|
output: {
|
||||||
|
file: 'dist/index.js',
|
||||||
|
format: 'esm',
|
||||||
|
sourcemap: true,
|
||||||
|
},
|
||||||
|
plugins: [resolve(), commonjs()],
|
||||||
|
}
|
||||||
81
src/PostgreSQLAdapter.js
Normal file
81
src/PostgreSQLAdapter.js
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
import { BaseNotesAdapter } from '@takerofnotes/plugin-sdk'
|
||||||
|
import pg from 'pg'
|
||||||
|
|
||||||
|
const { Pool } = pg
|
||||||
|
|
||||||
|
export default class PostgreSQLAdapter extends BaseNotesAdapter {
|
||||||
|
constructor(config) {
|
||||||
|
super()
|
||||||
|
|
||||||
|
for (const field in config) {
|
||||||
|
this[field] = config[field]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async init() {
|
||||||
|
this.pool = new Pool({
|
||||||
|
host: this.host,
|
||||||
|
port: this.port || 5432,
|
||||||
|
database: this.database,
|
||||||
|
user: this.user,
|
||||||
|
password: this.password,
|
||||||
|
ssl: this.ssl ? { rejectUnauthorized: false } : false,
|
||||||
|
})
|
||||||
|
|
||||||
|
const tableName = this.tableName || 'notes'
|
||||||
|
|
||||||
|
await this.pool.query(`
|
||||||
|
CREATE TABLE IF NOT EXISTS ${tableName} (
|
||||||
|
id TEXT PRIMARY KEY,
|
||||||
|
data JSONB NOT NULL,
|
||||||
|
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||||
|
)
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
|
async getAll() {
|
||||||
|
const tableName = this.tableName || 'notes'
|
||||||
|
const result = await this.pool.query(`SELECT id, data FROM ${tableName}`)
|
||||||
|
|
||||||
|
return result.rows.map(row => ({
|
||||||
|
id: row.id,
|
||||||
|
...row.data,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
async create(note) {
|
||||||
|
await this._upsert(note)
|
||||||
|
}
|
||||||
|
|
||||||
|
async update(note) {
|
||||||
|
await this._upsert(note)
|
||||||
|
}
|
||||||
|
|
||||||
|
async delete(id) {
|
||||||
|
const tableName = this.tableName || 'notes'
|
||||||
|
await this.pool.query(`DELETE FROM ${tableName} WHERE id = $1`, [id])
|
||||||
|
}
|
||||||
|
|
||||||
|
async testConnection() {
|
||||||
|
try {
|
||||||
|
await this.pool.query('SELECT 1')
|
||||||
|
return true
|
||||||
|
} catch (error) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async _upsert(note) {
|
||||||
|
const tableName = this.tableName || 'notes'
|
||||||
|
const { id, ...data } = note
|
||||||
|
|
||||||
|
await this.pool.query(
|
||||||
|
`INSERT INTO ${tableName} (id, data, updated_at)
|
||||||
|
VALUES ($1, $2, NOW())
|
||||||
|
ON CONFLICT (id) DO UPDATE SET
|
||||||
|
data = $2,
|
||||||
|
updated_at = NOW()`,
|
||||||
|
[id, JSON.stringify(data)]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
64
src/index.js
Normal file
64
src/index.js
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
import { definePlugin } from '@takerofnotes/plugin-sdk'
|
||||||
|
import PostgreSQLAdapter from './PostgreSQLAdapter'
|
||||||
|
|
||||||
|
export default definePlugin({
|
||||||
|
id: 'postgre-sql',
|
||||||
|
name: 'PostgreSQL',
|
||||||
|
description: 'Taker of Notes storage plugin for PostgreSQL databases',
|
||||||
|
version: '0.1.0',
|
||||||
|
apiVersion: '0.3.1',
|
||||||
|
configSchema: [
|
||||||
|
{
|
||||||
|
key: 'host',
|
||||||
|
label: 'Host',
|
||||||
|
type: 'text',
|
||||||
|
default: 'localhost',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'port',
|
||||||
|
label: 'Port',
|
||||||
|
type: 'number',
|
||||||
|
default: '5432',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'database',
|
||||||
|
label: 'Database Name',
|
||||||
|
type: 'text',
|
||||||
|
default: 'postgres',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'user',
|
||||||
|
label: 'User',
|
||||||
|
type: 'text',
|
||||||
|
default: 'postgres',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'password',
|
||||||
|
label: 'Password',
|
||||||
|
type: 'password',
|
||||||
|
default: '',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'ssl',
|
||||||
|
label: 'Use SSL',
|
||||||
|
type: 'boolean',
|
||||||
|
default: 'false',
|
||||||
|
required: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'tableName',
|
||||||
|
label: 'Table Name',
|
||||||
|
type: 'text',
|
||||||
|
default: 'notes',
|
||||||
|
required: false,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
createAdapter(config) {
|
||||||
|
return new PostgreSQLAdapter(config)
|
||||||
|
},
|
||||||
|
})
|
||||||
12
test/plugin.test.js
Normal file
12
test/plugin.test.js
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import { describePluginTests } from '@takerofnotes/plugin-sdk'
|
||||||
|
import plugin from '../src/index.js'
|
||||||
|
|
||||||
|
const adapter = plugin.createAdapter({
|
||||||
|
host: import.meta.env.VITE_PG_HOST,
|
||||||
|
database: import.meta.env.VITE_PG_DATABASE,
|
||||||
|
user: import.meta.env.VITE_PG_USER,
|
||||||
|
password: import.meta.env.VITE_PG_PASSWORD,
|
||||||
|
ssl: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
describePluginTests({ plugin, adapter }, describe, it, expect)
|
||||||
8
vitest.config.js
Normal file
8
vitest.config.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import { defineConfig } from 'vitest/config'
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
test: {
|
||||||
|
globals: true,
|
||||||
|
testTimeout: 30000,
|
||||||
|
},
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user