Add sqlite database for ID mappings

This commit is contained in:
Henrik Hüttemann 2023-05-26 14:47:46 +02:00
parent 3d0f505961
commit c9537ab909
No known key found for this signature in database
GPG Key ID: 9F7BD10E0A8A111E
6 changed files with 2560 additions and 97 deletions

2
.gitignore vendored
View File

@ -133,3 +133,5 @@ dist
files/ files/
src/config/synapse_access_token.json src/config/synapse_access_token.json
inputs/ inputs/
db.sqlite
db.sqlite-journal

2601
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -46,6 +46,9 @@
"dependencies": { "dependencies": {
"axios": "^1.4.0", "axios": "^1.4.0",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"reflect-metadata": "^0.1.13",
"sqlite3": "^5.1.6",
"typeorm": "^0.3.16",
"winston": "^3.8.2" "winston": "^3.8.2"
} }
} }

View File

@ -6,9 +6,20 @@ import readline from 'node:readline'
import { RcUser, createUser } from './users' import { RcUser, createUser } from './users'
import { storage } from './storage' import { storage } from './storage'
import { whoami } from './synapse' import { whoami } from './synapse'
import 'reflect-metadata'
import { DataSource } from 'typeorm'
import { IdMapping } from './entity/IdMapping'
log.info('rocketchat2matrix starts.') log.info('rocketchat2matrix starts.')
const AppDataSource = new DataSource({
type: 'sqlite',
database: 'db.sqlite',
entities: [IdMapping],
synchronize: true,
logging: false,
})
const enum Entities { const enum Entities {
Users = 'users.json', Users = 'users.json',
Rooms = 'rocketchat_room.json', Rooms = 'rocketchat_room.json',
@ -39,17 +50,21 @@ function loadRcExport(entity: Entities): Promise<void> {
break break
} }
let userMapping = storage.users.find((e) => e.rcId === rcUser._id) // Lookup mapping let mapping = await AppDataSource.manager.findOneBy(IdMapping, {
if (userMapping && userMapping.matrixId) { rcId: rcUser._id,
log.debug('Mapping exists:', userMapping) type: 0,
})
if (mapping && mapping.matrixId) {
log.debug('Mapping exists:', mapping)
} else { } else {
const matrixUser = await createUser(rcUser) const matrixUser = await createUser(rcUser)
userMapping = { mapping = new IdMapping()
rcId: rcUser._id, mapping.rcId = rcUser._id
matrixId: matrixUser.user_id, mapping.matrixId = matrixUser.user_id
} mapping.type = 0
storage.users.push(userMapping) // Save new mapping
log.debug('Mapping added:', userMapping) AppDataSource.manager.save(mapping) // Save new mapping
log.debug('Mapping added:', mapping)
// Add user to room mapping (specific to users) // Add user to room mapping (specific to users)
rcUser.__rooms.forEach((rcRoomId: string) => { rcUser.__rooms.forEach((rcRoomId: string) => {
@ -94,6 +109,7 @@ function loadRcExport(entity: Entities): Promise<void> {
async function main() { async function main() {
try { try {
await whoami() await whoami()
await AppDataSource.initialize()
await loadRcExport(Entities.Users) await loadRcExport(Entities.Users)
log.info('Done.') log.info('Done.')
} catch (error) { } catch (error) {

13
src/entity/IdMapping.ts Normal file
View File

@ -0,0 +1,13 @@
import { Entity, Column, PrimaryColumn } from 'typeorm'
@Entity()
export class IdMapping {
@PrimaryColumn()
rcId!: string
@Column()
matrixId?: string
@Column('integer')
type!: number
}

View File

@ -14,8 +14,8 @@
"target": "ES2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ "target": "ES2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */ // "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */