Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dbMigrationPathOpt - add db migration path to MapeoProject and MapeoManager #384

Merged
merged 6 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/mapeo-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export class MapeoManager extends TypedEmitter {
/** @type {import('./types.js').CoreStorage} */
#coreStorage
#dbFolder
/** @type {string} */
#projectMigrationFolder
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
#deviceId
#localPeers
#invite
Expand All @@ -78,23 +80,34 @@ export class MapeoManager extends TypedEmitter {
* @param {Object} opts
* @param {Buffer} opts.rootKey 16-bytes of random data that uniquely identify the device, used to derive a 32-byte master key, which is used to derive all the keypairs used for Mapeo
* @param {string} opts.dbFolder Folder for sqlite Dbs. Folder must exist. Use ':memory:' to store everything in-memory
* @param {string} [opts.projectMigrationFolder] path for drizzle migration folder for project
* @param {string} [opts.clientMigrationFolder] path for drizzle migration folder for client
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
* @param {string | import('./types.js').CoreStorage} opts.coreStorage Folder for hypercore storage or a function that returns a RandomAccessStorage instance
* @param {{ port?: number, logger: import('fastify').FastifyServerOptions['logger'] }} [opts.mediaServerOpts]
*/
constructor({ rootKey, dbFolder, coreStorage, mediaServerOpts }) {
constructor({
rootKey,
dbFolder,
projectMigrationFolder = '../drizzle/project',
clientMigrationFolder = '../drizzle/client',
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
coreStorage,
mediaServerOpts,
}) {
super()
this.#keyManager = new KeyManager(rootKey)
this.#deviceId = getDeviceId(this.#keyManager)
this.#l = new Logger({ deviceId: this.#deviceId, ns: 'manager' })
this.#dbFolder = dbFolder
this.#projectMigrationFolder = projectMigrationFolder
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
const sqlite = new Database(
dbFolder === ':memory:'
? ':memory:'
: path.join(dbFolder, CLIENT_SQLITE_FILE_NAME)
)
this.#db = drizzle(sqlite)
migrate(this.#db, {
migrationsFolder: new URL('../drizzle/client', import.meta.url).pathname,
migrationsFolder: new URL(clientMigrationFolder, import.meta.url)
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
.pathname,
})

this.#localPeers = new LocalPeers({ logger: this.#l })
Expand Down Expand Up @@ -352,6 +365,7 @@ export class MapeoManager extends TypedEmitter {
return new MapeoProject({
...this.#projectStorage(projectId),
...projectKeys,
projectMigrationFolder: this.#projectMigrationFolder,
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
keyManager: this.#keyManager,
sharedDb: this.#db,
sharedIndexWriter: this.#projectSettingsIndexWriter,
Expand Down
5 changes: 4 additions & 1 deletion src/mapeo-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class MapeoProject {
/**
* @param {Object} opts
* @param {string} opts.dbPath Path to store project sqlite db. Use `:memory:` for memory storage
* @param {string} [opts.projectMigrationFolder] path for drizzle migration folder for project
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
* @param {import('@mapeo/crypto').KeyManager} opts.keyManager mapeo/crypto KeyManager instance
* @param {Buffer} opts.projectKey 32-byte public key of the project creator core
* @param {Buffer} [opts.projectSecretKey] 32-byte secret key of the project creator core
Expand All @@ -83,6 +84,7 @@ export class MapeoProject {
*/
constructor({
dbPath,
projectMigrationFolder = '../drizzle/project',
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
sharedDb,
sharedIndexWriter,
coreStorage,
Expand All @@ -102,7 +104,8 @@ export class MapeoProject {
const sqlite = new Database(dbPath)
const db = drizzle(sqlite)
migrate(db, {
migrationsFolder: new URL('../drizzle/project', import.meta.url).pathname,
migrationsFolder: new URL(projectMigrationFolder, import.meta.url)
tomasciccola marked this conversation as resolved.
Show resolved Hide resolved
.pathname,
})

///////// 2. Setup random-access-storage functions
Expand Down