Ah, got it. That sounds kind of painful

Ah, got it. That sounds kind of painful too. Any future plans for something similar to secrets store that can be used for sharing bindings across workers?
2 Replies
JacobMGEvans
JacobMGEvans3d ago
Maybe something like this?
import {
Worker,
WranglerJson,
KVNamespace,
R2Bucket,
DurableObjectNamespace,
Queue,
Secret,
Service,
} from 'alchemy/cloudflare';

async function deployWorkers() {
const sharedBindings = {
USERS_KV: await KVNamespace('users-kv', {
title: 'User Data Store',
}),

SETTINGS_KV: await KVNamespace('settings-kv', {
title: 'App Settings Store',
}),

ASSETS_BUCKET: await R2Bucket('assets-bucket', {
name: 'assets-storage',
}),

USER_DO: await DurableObjectNamespace('user-do', {
className: 'UserDO',
scriptName: 'user-service',
}),

SESSION_DO: await DurableObjectNamespace('session-do', {
className: 'SessionDO',
scriptName: 'session-manager',
}),

LOG_QUEUE: await Queue('log-queue', {
name: 'log-events',
}),

API_KEY: await Secret('api-key', {
name: 'external-api-key',
}),

AUTH_SERVICE: await Service('auth-service', {
service: 'auth-worker',
environment: 'production',
}),
};

const apiWorker = await Worker('api-worker', {
name: 'api-worker',
entrypoint: './src/api/index.ts',
bindings: {
USERS_KV: sharedBindings.USERS_KV,
SETTINGS_KV: sharedBindings.SETTINGS_KV,
USER_DO: sharedBindings.USER_DO,
SESSION_DO: sharedBindings.SESSION_DO,
LOG_QUEUE: sharedBindings.LOG_QUEUE,
API_KEY: sharedBindings.API_KEY,
AUTH_SERVICE: sharedBindings.AUTH_SERVICE,
},
});
import {
Worker,
WranglerJson,
KVNamespace,
R2Bucket,
DurableObjectNamespace,
Queue,
Secret,
Service,
} from 'alchemy/cloudflare';

async function deployWorkers() {
const sharedBindings = {
USERS_KV: await KVNamespace('users-kv', {
title: 'User Data Store',
}),

SETTINGS_KV: await KVNamespace('settings-kv', {
title: 'App Settings Store',
}),

ASSETS_BUCKET: await R2Bucket('assets-bucket', {
name: 'assets-storage',
}),

USER_DO: await DurableObjectNamespace('user-do', {
className: 'UserDO',
scriptName: 'user-service',
}),

SESSION_DO: await DurableObjectNamespace('session-do', {
className: 'SessionDO',
scriptName: 'session-manager',
}),

LOG_QUEUE: await Queue('log-queue', {
name: 'log-events',
}),

API_KEY: await Secret('api-key', {
name: 'external-api-key',
}),

AUTH_SERVICE: await Service('auth-service', {
service: 'auth-worker',
environment: 'production',
}),
};

const apiWorker = await Worker('api-worker', {
name: 'api-worker',
entrypoint: './src/api/index.ts',
bindings: {
USERS_KV: sharedBindings.USERS_KV,
SETTINGS_KV: sharedBindings.SETTINGS_KV,
USER_DO: sharedBindings.USER_DO,
SESSION_DO: sharedBindings.SESSION_DO,
LOG_QUEUE: sharedBindings.LOG_QUEUE,
API_KEY: sharedBindings.API_KEY,
AUTH_SERVICE: sharedBindings.AUTH_SERVICE,
},
});
const contentWorker = await Worker('content-worker', {
name: 'content-worker',
entrypoint: './src/content/index.ts',
bindings: {
USERS_KV: sharedBindings.USERS_KV,
ASSETS_BUCKET: sharedBindings.ASSETS_BUCKET,
API_KEY: sharedBindings.API_KEY,
},
});

const analyticsWorker = await Worker('analytics-worker', {
name: 'analytics-worker',
entrypoint: './src/analytics/index.ts',
bindings: {
SETTINGS_KV: sharedBindings.SETTINGS_KV,
LOG_QUEUE: sharedBindings.LOG_QUEUE,
},
});

await WranglerJson('api-config', {
worker: apiWorker,
path: './workers/api-worker/wrangler.json',
});

await WranglerJson('content-config', {
worker: contentWorker,
path: './workers/content-worker/wrangler.json',
});

await WranglerJson('analytics-config', {
worker: analyticsWorker,
path: './workers/analytics-worker/wrangler.json',
});

console.log('Generated wrangler.json files for all workers');
}
deployWorkers().catch((error) => {
console.error('Deployment failed:', error);
process.exit(1);
});
const contentWorker = await Worker('content-worker', {
name: 'content-worker',
entrypoint: './src/content/index.ts',
bindings: {
USERS_KV: sharedBindings.USERS_KV,
ASSETS_BUCKET: sharedBindings.ASSETS_BUCKET,
API_KEY: sharedBindings.API_KEY,
},
});

const analyticsWorker = await Worker('analytics-worker', {
name: 'analytics-worker',
entrypoint: './src/analytics/index.ts',
bindings: {
SETTINGS_KV: sharedBindings.SETTINGS_KV,
LOG_QUEUE: sharedBindings.LOG_QUEUE,
},
});

await WranglerJson('api-config', {
worker: apiWorker,
path: './workers/api-worker/wrangler.json',
});

await WranglerJson('content-config', {
worker: contentWorker,
path: './workers/content-worker/wrangler.json',
});

await WranglerJson('analytics-config', {
worker: analyticsWorker,
path: './workers/analytics-worker/wrangler.json',
});

console.log('Generated wrangler.json files for all workers');
}
deployWorkers().catch((error) => {
console.error('Deployment failed:', error);
process.exit(1);
});
Tried to make sure it wasnt too painful Probably needs to be modified for a repo or monorepo with multiple preexisting Workers
ac
acOP3d ago
Interesting -- I'll look into this more. Thank you!

Did you find this page helpful?