-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
436 additions
and
388 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 0 additions & 130 deletions
130
packages/common/infra/src/orm/core/__tests__/hook.spec.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
packages/common/infra/src/orm/core/adapters/indexeddb/db.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import type { IDBPDatabase } from 'idb'; | ||
import { openDB } from 'idb'; | ||
|
||
import type { DBSchema } from '../../schema'; | ||
import type { | ||
AsyncDBAdapter, | ||
AsyncTableAdapter, | ||
TableAdapterOptions, | ||
} from '../types'; | ||
import { IndexedDBTableAdapter } from './table'; | ||
|
||
interface IndexedDBORMAdapterOptions { | ||
db: string; | ||
version: number; | ||
} | ||
|
||
export class IndexedDBORMAdapter implements AsyncDBAdapter { | ||
private db: IDBPDatabase | null = null; | ||
|
||
private schema!: DBSchema; | ||
|
||
private readonly tables = new Map<string, AsyncTableAdapter>(); | ||
|
||
constructor(private readonly opts: IndexedDBORMAdapterOptions) {} | ||
|
||
async setup(schema: DBSchema) { | ||
this.schema = schema; | ||
} | ||
|
||
async connect() { | ||
this.db = await openDB(this.opts.db, 1, { | ||
upgrade: db => { | ||
this.upgradeDB(db); | ||
}, | ||
}); | ||
} | ||
|
||
disconnect() { | ||
this.db = null; | ||
return Promise.resolve(); | ||
} | ||
|
||
table(opts: TableAdapterOptions) { | ||
let table = this.tables.get(opts.schema.name); | ||
|
||
if (!table) { | ||
table = new IndexedDBTableAdapter(opts); | ||
this.tables.set(opts.schema.name, table); | ||
} | ||
|
||
return table; | ||
} | ||
|
||
private upgradeDB(db: IDBPDatabase) { | ||
const createdStores = new Set(db.objectStoreNames); | ||
const storesInNewVersion = new Set(Object.keys(this.schema)); | ||
|
||
// create | ||
for (const tableName of storesInNewVersion) { | ||
const tableSchema = this.schema[tableName]; | ||
|
||
if (!createdStores.has(tableName)) { | ||
const store = db.createObjectStore(tableName, { | ||
keyPath: tableSchema.primaryKey, | ||
autoIncrement: tableSchema.autoincremental, | ||
}); | ||
|
||
const createdIndexes = Array.from(store.indexNames); | ||
const idxName = (prefix: string, keys: string[]) => | ||
`${prefix}_${keys.join('_')}`; | ||
const indexesInNewVersion = tableSchema.indexes.map( | ||
({ keys, unique }) => idxName(unique ? 'unique' : 'index', keys) | ||
); | ||
|
||
// create index | ||
for (const { keys, unique } of tableSchema.indexes) { | ||
const name = idxName(unique ? 'unique' : 'index', keys); | ||
const existsName = createdIndexes.find(index => | ||
index.endsWith(idxName('', keys)) | ||
); | ||
|
||
// index type has changed | ||
// TODO(@forehalo): updating from index to unique is not tested | ||
if (existsName && existsName !== name) { | ||
store.deleteIndex(existsName); | ||
} | ||
|
||
store.createIndex(name, keys, { unique }); | ||
} | ||
|
||
//delete index | ||
for (const index of store.indexNames) { | ||
if (!indexesInNewVersion.includes(index)) { | ||
store.deleteIndex(index); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// delete | ||
for (const tableName of createdStores) { | ||
if (!storesInNewVersion.has(tableName)) { | ||
db.deleteObjectStore(tableName); | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/common/infra/src/orm/core/adapters/indexeddb/table.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { | ||
DeleteQuery, | ||
FindQuery, | ||
InsertQuery, | ||
ObserveQuery, | ||
UpdateQuery, | ||
} from '../types'; | ||
import { AsyncTableAdapter } from '../types'; | ||
|
||
export class IndexedDBTableAdapter extends AsyncTableAdapter { | ||
toObject(record: any): Record<string, any> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
async insert(query: InsertQuery) { | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
async update(query: UpdateQuery): Promise<any[]> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
async delete(query: DeleteQuery): Promise<void> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
async find(query: FindQuery): Promise<any[]> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
observe(query: ObserveQuery): () => void { | ||
throw new Error('Method not implemented.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import type { DBAdapter } from '../types'; | ||
import type { DBAdapter, TableAdapterOptions } from '../types'; | ||
import { MemoryTableAdapter } from './table'; | ||
|
||
export class MemoryORMAdapter implements DBAdapter { | ||
table(tableName: string) { | ||
return new MemoryTableAdapter(tableName); | ||
setup(): void {} | ||
table(opts: TableAdapterOptions) { | ||
return new MemoryTableAdapter(opts); | ||
} | ||
} |
Oops, something went wrong.