Skip to content

Commit 3b0717f

Browse files
committed
rework to have TypedDocumentStore methods have same async interface as sqlite-documentstore library for compatibility. yarn -> pnpm, update github workflows
1 parent 9bb5efb commit 3b0717f

5 files changed

Lines changed: 141 additions & 104 deletions

File tree

.github/workflows/ci.yml

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,25 @@ jobs:
1010
build:
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: actions/checkout@v3
13+
- uses: actions/checkout@v4
1414
with:
15-
# Get all history for version calculation
16-
fetch-depth: 0
17-
- uses: actions/setup-node@v3
15+
fetch-depth: 0 # Get all history for version calculation
16+
- uses: pnpm/action-setup@v4
17+
- uses: actions/setup-node@v5
1818
with:
1919
node-version: lts/*
2020
registry-url: 'https://registry.npmjs.org'
2121
scope: '@dnvgl'
22-
- name: Install GitVersion
23-
uses: gittools/actions/gitversion/setup@v0.9.7
22+
cache: 'pnpm'
23+
- uses: gittools/actions/gitversion/setup@v0.9.7
2424
with:
2525
versionSpec: '5.x'
26-
- name: Determine Version
27-
uses: gittools/actions/gitversion/execute@v0.9.7
26+
- uses: gittools/actions/gitversion/execute@v0.9.7
2827
with:
2928
useConfigFile: true
30-
- run: yarn version --new-version $GITVERSION_SEMVER --no-git-tag-version
31-
- run: yarn install --frozen-lockfile
32-
- run: yarn build
33-
- run: yarn publish --access public
29+
- run: pnpm version $GITVERSION_SEMVER --no-git-tag-version
30+
- run: pnpm install --frozen-lockfile
31+
- run: pnpm build
32+
- run: pnpm publish --access public
3433
env:
3534
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
},
3535
"devDependencies": {
3636
"@types/async-lock": "^1.4.2",
37-
"@types/lodash": "^4.17.17",
37+
"@types/lodash": "^4.17.20",
3838
"@types/sql.js": "^1.4.9",
39-
"typescript": "^5.8.3"
39+
"typescript": "^5.9.2"
4040
},
41-
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
41+
"packageManager": "pnpm@10.15.1+sha512.34e538c329b5553014ca8e8f4535997f96180a1d0f614339357449935350d924e22f8614682191264ec33d1462ac21561aff97f6bb18065351c162c7e8f6de67"
4242
}

pnpm-lock.yaml

Lines changed: 101 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/TypedDocumentStore.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ export class TypedDocumentStore<T extends IdInterface, TIndex extends TIndexType
4343
/**
4444
* ensure table (schema) exists
4545
*/
46-
init(options: { autoMigrateIndexChanges: boolean } = { autoMigrateIndexChanges: true }): void {
46+
async init(options: { autoMigrateIndexChanges: boolean } = { autoMigrateIndexChanges: true }) {
4747
const tableExists = sqljsHelpers.isTable(this.db, this.tableName);
4848
if (!tableExists) {
49-
this.db.txn(`${this.tableName} create table`, txnId => {
49+
await this.db.txnAsync(`${this.tableName} create table`, async txnId => {
5050
const createTableSql = `create table if not exists ${this.tableName} (${dbRow.id} primary key not null, ${dbRow.json} ${this._indexColumnNamesSql});`;
5151
this.db.run(txnId, createTableSql);
5252
});
@@ -63,20 +63,20 @@ export class TypedDocumentStore<T extends IdInterface, TIndex extends TIndexType
6363
return;
6464
}
6565
console.log(`adding missing columns to ${this.tableName}`, missingColumns);
66-
this.db.txn(`${this.tableName} add missing columns`, txnId => {
66+
await this.db.txnAsync(`${this.tableName} add missing columns`, async txnId => {
6767
missingColumns.forEach(columnName => this.db.run(txnId, `alter table ${this.tableName} add column ${columnName};`));
68-
this.rebuildIndexes(txnId);
68+
await this.rebuildIndexes(txnId);
6969
});
7070
}
7171
}
7272

73-
get(id: unknown) : T {
73+
async get(id: unknown) {
7474
const result = this._tryGet(id);
7575
if (result === null || result === undefined) throw new Error(`${TypedDocumentStore.name}<${this.tableName}>.get(${id}) was undefined`);
7676
return <T>result;
7777
}
7878

79-
tryGet(id: unknown) : T|undefined {
79+
async tryGet(id: unknown) {
8080
return this._tryGet(id);
8181
}
8282

@@ -86,7 +86,7 @@ export class TypedDocumentStore<T extends IdInterface, TIndex extends TIndexType
8686
return <T>JSON.parse(rows[0].json);
8787
}
8888

89-
getMany(ids: unknown[]): T[] {
89+
async getMany(ids: unknown[]) {
9090
const results = this._tryGetMany(ids);
9191
const missingIds = ids.filter((_, i) => results[i] === null || results[i] === undefined);
9292
if (missingIds.length > 0) throw new Error(`${TypedDocumentStore.name}<${this.tableName}>.getMany(...) was undefined for ids ${missingIds.join(', ')}`);
@@ -97,7 +97,7 @@ export class TypedDocumentStore<T extends IdInterface, TIndex extends TIndexType
9797
* returns ids => in order set of results, if no result for given id, array item will be null
9898
* prefer use of 'getMany' if all items are expected to exist
9999
*/
100-
tryGetMany(ids: unknown[]) {
100+
async tryGetMany(ids: unknown[]) {
101101
return this._tryGetMany(ids);
102102
}
103103

@@ -118,12 +118,12 @@ export class TypedDocumentStore<T extends IdInterface, TIndex extends TIndexType
118118
}
119119
}
120120

121-
exists(id: unknown) : boolean {
121+
async exists(id: unknown) {
122122
const result = (this.db.exec(`select 1 from ${this.tableName} where ${dbRow.id} = ?;`, [<SqlValue>id]));
123123
return (result.values?.length ?? 0) > 0;
124124
}
125125

126-
getAll(): T[] {
126+
async getAll() {
127127
const results = sqljsHelpers.query<DbRow>(this.db, `select ${dbRow.json} from ${this.tableName};`);
128128
return results.map(x => <T>JSON.parse(x.json));
129129
}
@@ -133,7 +133,7 @@ export class TypedDocumentStore<T extends IdInterface, TIndex extends TIndexType
133133
* @example .query(x => `where ${x.name} like ? and ${x.active} = ?`, [nameSearchValue, isActive]);
134134
* @example .query(x => `where ${x.name} like ?1 and ${x.active} = ?2`, [nameSearchValue, isActive]);
135135
*/
136-
query(whereSql: ((x: Record<keyof TIndex, string>) => string), params: unknown[]): T[] {
136+
async query(whereSql: ((x: Record<keyof TIndex, string>) => string), params: unknown[]) {
137137
const querySql = `select ${dbRow.id}, ${dbRow.json} from ${this.tableName} ${whereSql(this._buildQueryObject())};`;
138138
const results = sqljsHelpers.query<DbRow>(this.db, querySql, params);
139139
return results.map(x => <T>JSON.parse(x.json));
@@ -142,56 +142,56 @@ export class TypedDocumentStore<T extends IdInterface, TIndex extends TIndexType
142142
/**
143143
* Return just index values, helpful for doing fast queries on indexed fields without needing to fetch and deserialize the entire object
144144
*/
145-
queryIndexes(whereSql?: ((x: Record<keyof TIndex | 'id', string>) => string), params?: unknown[]): ({ [k in keyof(TIndex)]: ReturnType<TIndex[k]>} & Pick<T, 'id'>)[] {
145+
async queryIndexes(whereSql?: ((x: Record<keyof TIndex | 'id', string>) => string), params?: unknown[]): Promise<({ [k in keyof(TIndex)]: ReturnType<TIndex[k]>} & Pick<T, 'id'>)[]> {
146146
const querySql = `select ${dbRow.id}${this._indexColumnNamesSql} from ${this.tableName} ${whereSql !== undefined ? whereSql(this._buildQueryObject()) : ''};`;
147147
const results = sqljsHelpers.query<({ [k in keyof(TIndex)]: ReturnType<TIndex[k]>} & Pick<T, 'id'>)>(this.db, querySql, params);
148148
return results;
149149
}
150150

151-
count(): number {
151+
async count() {
152152
const result = this.db.exec(`select count(1) from ${this.tableName};`);
153153
return result[0].values[0][0] as number;
154154
}
155155

156156
/**
157157
* insert or update a single document
158158
*/
159-
set(txnId: string, value: T) : void { this.db.run(txnId, this.setSql, this._buildParams(value)); }
159+
async set(txnId: string, value: T) { this.db.run(txnId, this.setSql, this._buildParams(value)); }
160160

161161
/**
162162
* insert or update many documents, prefer use of insertMany if data is expected to not exist
163163
*/
164-
setMany(txnId: string, values: T[]) : void {
164+
async setMany(txnId: string, values: T[]) {
165165
if (values.length === 0) return;
166166
values.forEach(value => this.db.run(txnId, this.setSql, this._buildParams(value)));
167167
}
168168

169-
insertMany(txnId: string, values: T[]) : void {
169+
async insertMany(txnId: string, values: T[]) {
170170
if (values.length === 0) return;
171171
values.forEach(value => this.db.run(txnId, this.insertSql, this._buildParams(value)));
172172
}
173173

174174
/**
175175
* fetch, modify, and update a document
176176
*/
177-
update(txnId: string, id: Pick<T, 'id'>['id'], updateAction: (existing: Omit<T, 'id'>) => void) : void {
178-
const value = this.get(id);
177+
async update(txnId: string, id: Pick<T, 'id'>['id'], updateAction: (existing: Omit<T, 'id'>) => void) {
178+
const value = await this.get(id);
179179
updateAction(value);
180-
this.set(txnId, value);
180+
await this.set(txnId, value);
181181
}
182182

183183
/**
184184
* like update, but falls back to initializer value if document doesn't already exist
185185
*/
186-
upsert(txnId: string, initializer: T, updateAction: (existing: Omit<T, 'id'>) => void) : void {
187-
const value = this.tryGet(initializer.id) ?? initializer;
186+
async upsert(txnId: string, initializer: T, updateAction: (existing: Omit<T, 'id'>) => void) {
187+
const value = await this.tryGet(initializer.id) ?? initializer;
188188
updateAction(value);
189-
this.set(txnId, value);
189+
await this.set(txnId, value);
190190
}
191191

192-
remove(txnId: string, id: Pick<T, 'id'>['id']): void { this.db.run(txnId, `delete from ${this.tableName} where ${dbRow.id} = ?;`, sqljsHelpers.sanitizeParams([id])); }
193-
removeMany(txnId: string, ids: Pick<T, 'id'>['id'][]) : void { if (ids.length === 0) return; this.db.run(txnId, `delete from ${this.tableName} where ${dbRow.id} in (${'?,'.repeat(ids.length).slice(0, -1)});`, sqljsHelpers.sanitizeParams(ids)); }
194-
removeAll(txnId: string): void { this.db.run(txnId, `delete from ${this.tableName};`); }
192+
async remove(txnId: string, id: Pick<T, 'id'>['id']) { this.db.run(txnId, `delete from ${this.tableName} where ${dbRow.id} = ?;`, sqljsHelpers.sanitizeParams([id])); }
193+
async removeMany(txnId: string, ids: Pick<T, 'id'>['id'][]) { if (ids.length === 0) return; this.db.run(txnId, `delete from ${this.tableName} where ${dbRow.id} in (${'?,'.repeat(ids.length).slice(0, -1)});`, sqljsHelpers.sanitizeParams(ids)); }
194+
async removeAll(txnId: string) { this.db.run(txnId, `delete from ${this.tableName};`); }
195195

196196
private _buildQueryObject(): Record<keyof TIndex, string> {
197197
const queryObject = <any>{};
@@ -209,7 +209,7 @@ export class TypedDocumentStore<T extends IdInterface, TIndex extends TIndexType
209209

210210
private _indexValues(obj: T) { return _.map(this.indexedFields, accessor => accessor(obj)); }
211211

212-
private rebuildIndexes(txnId: string) { this.setMany(txnId, this.getAll()); }
212+
private async rebuildIndexes(txnId: string) { await this.setMany(txnId, await this.getAll()); }
213213
}
214214

215215
export interface ITypedDocumentStore<T extends IdInterface, TIndex extends TIndexType<T>> extends TypedDocumentStore<T, TIndex>{};

yarn.lock

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)