SQL data connectors for MessageStore, DataStore, and EventLog
npm install @dwn-protocol/sqlSQL backed implementations of DWN MessageStore, DataStore, and EventLog.
- Supported DBs
- Installation
- Usage
- SQLite
- MySQL
- PostgreSQL
- Development
- Prerequisites
- node and npm
- Docker
- Running Tests
- npm scripts
``bash`
npm install @dwn-protocol/sql
`typescript
import Database from 'better-sqlite3';
import { Dwn } from '@dwn-protocol/id-sdk'
import { SqliteDialect, MessageStoreSql, DataStoreSql, EventLogSql } from '@dwn-protocol/sql';
const sqliteDialect = new SqliteDialect({
database: async () => new Database('dwn.sqlite', {
fileMustExist: true,
})
});
const messageStore = new MessageStoreSql(sqliteDialect);
const dataStore = new DataStoreSql(sqliteDialect);
const eventLog = new EventLogSql(sqliteDialect);
const dwn = await Dwn.create({ messageStore, dataStore, eventLog });
`
`typescript
import { createPool } from 'mysql2';
import { Dwn } from '@dwn-protocol/id-sdk'
import { MysqlDialect, MessageStoreSql, DataStoreSql, EventLogSql } from '@dwn-protocol/sql';
const mysqlDialect = new MysqlDialect({
pool: async () => createPool({
host : 'localhost',
port : 3306,
database : 'dwn',
user : 'root',
password : 'dwn'
})
});
const messageStore = new MessageStoreSql(mysqlDialect);
const dataStore = new DataStoreSql(mysqlDialect);
const eventLog = new EventLogSql(mysqlDialect);
const dwn = await Dwn.create({ messageStore, dataStore, eventLog });
`
`typescript
import pg from 'pg';
import Cursor from 'pg-cursor';
import { Dwn } from '@dwn-protocol/id-sdk'
import { PostgresDialect, MessageStoreSql, DataStoreSql, EventLogSql } from '@dwn-protocol/sql';
const postgresDialect = new PostgresDialect({
pool: async () => new pg.Pool({
host : 'localhost',
port : 5432,
database : 'dwn',
user : 'root',
password : 'dwn'
}),
cursor: Cursor
});
const messageStore = new MessageStoreSql(postgresDialect);
const dataStore = new DataStoreSql(postgresDialect);
const eventLog = new EventLogSql(postgresDialect);
const dwn = await Dwn.create({ messageStore, dataStore, eventLog });
`
and npm v9.6.7. You can verify your node and npm installation via the terminal:`
$ node --version
v18.13.0
$ npm --version
9.7.2
`If you don't have
node installed. Feel free to choose whichever approach you feel the most comfortable with. If you don't have a preferred installation method, i'd recommend using nvm (aka node version manager). nvm allows you to install and use different versions of node. It can be installed by running brew install nvm (assuming that you have homebrew)Once you have installed
nvm, install the desired node version with nvm install vX.Y.Z.Running Tests
> 💡 Make sure you have all the prerequisites0. clone the repo and
cd into the project directory
1. Install all project dependencies by running npm install
2. start the test databases using ./scripts/start-databases (requires Docker)
3. run tests using npm run testnpm scripts| Script | Description |
| ----------------------- | ------------------------------------------- |
|
npm run build:esm | compiles typescript into ESM JS |
| npm run build:cjs | compiles typescript into CommonJS |
| npm run build | compiles typescript into ESM JS & CommonJS |
| npm run clean | deletes compiled JS |
| npm run test | runs tests. |
| npm run test-coverage | runs tests and includes coverage |
| npm run lint | runs linter |
| npm run lint:fix` | runs linter and fixes auto-fixable problems |