|
| 1 | +# Upgrading from 0.x to 1.0 |
| 2 | + |
| 3 | +Version 1.0 migrates the entire library to native **ES Modules (ESM)**. If your application was using `require('event-storage')` you will need to update your import statements and a few API references. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 1. Node.js version |
| 8 | + |
| 9 | +ESM support requires **Node.js 18 or later**. Check your version: |
| 10 | + |
| 11 | +```bash |
| 12 | +node --version |
| 13 | +``` |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## 2. Update your `package.json` |
| 18 | + |
| 19 | +Your own project must opt in to ESM. Add `"type": "module"` to your `package.json`: |
| 20 | + |
| 21 | +```json |
| 22 | +{ |
| 23 | + "type": "module" |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +If you cannot migrate your whole project to ESM, you can rename individual files from `.js` to `.mjs` — Node.js treats `.mjs` files as ESM regardless of the `"type"` field. |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## 3. Replace `require()` with `import` |
| 32 | + |
| 33 | +### Basic import |
| 34 | + |
| 35 | +```js |
| 36 | +// Before (0.x) |
| 37 | +const EventStore = require('event-storage'); |
| 38 | + |
| 39 | +// After (1.0) |
| 40 | +import { EventStore } from 'event-storage'; |
| 41 | +``` |
| 42 | + |
| 43 | +### Named exports that were previously properties |
| 44 | + |
| 45 | +In 0.x, constants and sub-classes were attached as properties on the default export. Constants are now individual named exports, while sub-classes remain accessible as properties of the parent class: |
| 46 | + |
| 47 | +| 0.x | 1.0 | |
| 48 | +|-----|-----| |
| 49 | +| `EventStore.ExpectedVersion` | `import { ExpectedVersion } from 'event-storage'` | |
| 50 | +| `EventStore.OptimisticConcurrencyError` | `import { OptimisticConcurrencyError } from 'event-storage'` | |
| 51 | +| `EventStore.LOCK_THROW` | `import { LOCK_THROW } from 'event-storage'` | |
| 52 | +| `EventStore.LOCK_RECLAIM` | `import { LOCK_RECLAIM } from 'event-storage'` | |
| 53 | +| `require('event-storage').Storage` | `import { Storage } from 'event-storage'` | |
| 54 | +| `Storage.ReadOnly` | `Storage.ReadOnly` (unchanged — still a property of `Storage`) | |
| 55 | +| `Storage.StorageLockedError` | `import { StorageLockedError } from 'event-storage'` | |
| 56 | +| `Index.ReadOnly` | `Index.ReadOnly` (unchanged — still a property of `Index`) | |
| 57 | +| `Index.Entry` | `Index.Entry` (unchanged — still a property of `Index`) | |
| 58 | + |
| 59 | +### Full import surface |
| 60 | + |
| 61 | +All public exports from the package entry point: |
| 62 | + |
| 63 | +```js |
| 64 | +import { |
| 65 | + EventStore, ExpectedVersion, OptimisticConcurrencyError, |
| 66 | + EventStream, |
| 67 | + Storage, StorageLockedError, |
| 68 | + Index, |
| 69 | + Consumer, |
| 70 | + LOCK_THROW, LOCK_RECLAIM |
| 71 | +} from 'event-storage'; |
| 72 | +``` |
| 73 | + |
| 74 | +--- |
| 75 | + |
| 76 | +## 4. Common patterns updated |
| 77 | + |
| 78 | +### Creating an event store |
| 79 | + |
| 80 | +```js |
| 81 | +// Before |
| 82 | +const EventStore = require('event-storage'); |
| 83 | +const eventstore = new EventStore('my-store', { storageDirectory: './data' }); |
| 84 | + |
| 85 | +// After |
| 86 | +import { EventStore } from 'event-storage'; |
| 87 | +const eventstore = new EventStore('my-store', { storageDirectory: './data' }); |
| 88 | +``` |
| 89 | + |
| 90 | +### Optimistic concurrency |
| 91 | + |
| 92 | +```js |
| 93 | +// Before |
| 94 | +eventstore.commit('my-stream', events, EventStore.ExpectedVersion.EmptyStream, callback); |
| 95 | +// …and catching errors: |
| 96 | +if (err instanceof EventStore.OptimisticConcurrencyError) { … } |
| 97 | + |
| 98 | +// After |
| 99 | +import { EventStore, ExpectedVersion, OptimisticConcurrencyError } from 'event-storage'; |
| 100 | +eventstore.commit('my-stream', events, ExpectedVersion.EmptyStream, callback); |
| 101 | +if (err instanceof OptimisticConcurrencyError) { … } |
| 102 | +``` |
| 103 | + |
| 104 | +### Lock modes |
| 105 | + |
| 106 | +```js |
| 107 | +// Before |
| 108 | +const EventStore = require('event-storage'); |
| 109 | +const eventstore = new EventStore('my-store', { |
| 110 | + storageConfig: { lock: EventStore.LOCK_RECLAIM } |
| 111 | +}); |
| 112 | + |
| 113 | +// After |
| 114 | +import { EventStore, LOCK_RECLAIM } from 'event-storage'; |
| 115 | +const eventstore = new EventStore('my-store', { |
| 116 | + storageConfig: { lock: LOCK_RECLAIM } |
| 117 | +}); |
| 118 | +``` |
| 119 | + |
| 120 | +### Using the Storage class directly |
| 121 | + |
| 122 | +```js |
| 123 | +// Before |
| 124 | +const Storage = require('event-storage').Storage; |
| 125 | +const ReadOnlyStorage = Storage.ReadOnly; |
| 126 | +const store = new ReadOnlyStorage('mystore', { dataDirectory: './data' }); |
| 127 | + |
| 128 | +// After |
| 129 | +import { Storage } from 'event-storage'; |
| 130 | +const store = new Storage.ReadOnly('mystore', { dataDirectory: './data' }); |
| 131 | +``` |
| 132 | + |
| 133 | +### Custom serialization / compression |
| 134 | + |
| 135 | +```js |
| 136 | +// Before |
| 137 | +const { encode, decode } = require('@msgpack/msgpack'); |
| 138 | + |
| 139 | +// After |
| 140 | +import { encode, decode } from '@msgpack/msgpack'; |
| 141 | +``` |
| 142 | + |
| 143 | +--- |
| 144 | + |
| 145 | +## 5. Relative file imports require `.js` extensions |
| 146 | + |
| 147 | +If your project imports internal modules from `node_modules/event-storage` by path (uncommon), note that ESM requires explicit file extensions. This change is internal to the library and should not affect application code that imports from `'event-storage'`. |
| 148 | + |
| 149 | +--- |
| 150 | + |
| 151 | +## 6. `__dirname` / `__filename` |
| 152 | + |
| 153 | +These globals are not available in ESM. If you reference them in your own application code, replace them with: |
| 154 | + |
| 155 | +```js |
| 156 | +import { fileURLToPath } from 'url'; |
| 157 | +import path from 'path'; |
| 158 | + |
| 159 | +const __filename = fileURLToPath(import.meta.url); |
| 160 | +const __dirname = path.dirname(__filename); |
| 161 | +``` |
| 162 | +
|
| 163 | +--- |
| 164 | +
|
| 165 | +## 7. Dynamic `require()` / `createRequire` |
| 166 | +
|
| 167 | +If you have tooling or test helpers that still need CJS-style loading, use `createRequire`: |
| 168 | +
|
| 169 | +```js |
| 170 | +import { createRequire } from 'module'; |
| 171 | +const require = createRequire(import.meta.url); |
| 172 | +``` |
| 173 | +
|
| 174 | +This is a compatibility shim and is generally not needed when consuming `event-storage` itself. |
0 commit comments