[]
이 튜토리얼은 “실시간 동시 작업 통합 문서” 튜토리얼을 기반으로 데이터베이스 어댑터를 구성하고 사용하는 방법을 설명합니다. 실습 예제로 SQLite3 어댑터를 구현하여 설정 단계를 보여줍니다.
데이터베이스 어댑터에 대한 기본 지식
npm install sqlite3 @mescius/js-collaboration-ot-sqlite기본 튜토리얼의 코드를 다음 내용으로 교체합니다.
import express from 'express';
import http from 'http';
import { Server } from '@mescius/js-collaboration';
import * as OT from '@mescius/js-collaboration-ot';
import { type } from '@mescius/spread-sheets-collaboration';
import sqlite3 from 'sqlite3';
import { SqliteDb } from '@mescius/js-collaboration-ot-sqlite';
// 타입 등록
OT.TypesManager.register(type);
const app = express();
const httpServer = http.createServer(app);
const server = new Server({ httpServer });
const port = 8080;
// DB 초기화
const db = new sqlite3.Database("./docs.db");
const sqliteDbAdapter = new SqliteDb(db);
// OT 문서 서비스 초기화
const documentServices = new OT.DocumentServices({ db: sqliteDbAdapter });
server.useFeature(OT.documentFeature(documentServices));
// 정적 리소스 제공
app.use(express.static('public'));
// 서버 시작
httpServer.listen(port, () => {
console.log(`Server listening on port ${port}`);
console.log(`<http://127.0.0.1:${port}/index.html`);
});루트 디렉터리에 init-database.js 파일을 생성합니다.
import sqlite3 from "sqlite3";
const db = new sqlite3.Database("./docs.db");
async function initSqliteDataTables(db) {
const run = (sql) => {
return new Promise((resolve, reject) => {
db.run(sql, (e) => (e ? reject(e) : resolve()));
});
};
await run(
`CREATE TABLE IF NOT EXISTS documents (
id TEXT PRIMARY KEY,
type TEXT NOT NULL,
version INTEGER NOT NULL,
snapshot_version INTEGER NOT NULL
)`
);
await run(
`CREATE TABLE IF NOT EXISTS operations (
doc_id TEXT NOT NULL,
version INTEGER NOT NULL,
operation TEXT NOT NULL,
PRIMARY KEY (doc_id, version),
FOREIGN KEY (doc_id) REFERENCES documents (id) ON DELETE CASCADE
)`
);
await run(
`CREATE TABLE IF NOT EXISTS snapshot_fragments (
doc_id TEXT NOT NULL,
fragment_id TEXT NOT NULL,
data TEXT NOT NULL,
PRIMARY KEY (doc_id, fragment_id),
FOREIGN KEY (doc_id) REFERENCES documents (id) ON DELETE CASCADE
)`
);
}
// 초기화 함수 호출
initSqliteDataTables(db);다음 명령을 실행하여 데이터베이스 테이블을 초기화합니다.
데이터베이스 초기화는 최초 1회 또는 데이터베이스를 재설정한 이후에만 실행하면 됩니다.
node init-database.js클라이언트 코드 빌드
npm run build서버 시작
npm run start사용자 정의 데이터베이스 구현 방법은 다음을 참고하십시오: