@aws-amplify/datastore-storage-adapter 2.0.72 → 2.0.73
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.rollup.cache/Volumes/workplace/amplify-js/packages/datastore-storage-adapter/dist/meta/cjs.tsbuildinfo +1 -0
- package/CHANGELOG.md +6 -0
- package/coverage-ts/assets/source-file.css +21 -0
- package/coverage-ts/assets/source-file.js +40 -0
- package/coverage-ts/files/lib/ExpoSQLiteAdapter/ExpoSQLiteAdapter.d.ts.html +21 -0
- package/coverage-ts/files/lib/ExpoSQLiteAdapter/ExpoSQLiteDatabase.d.ts.html +35 -0
- package/coverage-ts/files/lib/SQLiteAdapter/SQLiteAdapter.d.ts.html +21 -0
- package/coverage-ts/files/lib/SQLiteAdapter/SQLiteDatabase.d.ts.html +35 -0
- package/coverage-ts/files/lib/common/CommonSQLiteAdapter.d.ts.html +41 -0
- package/coverage-ts/files/lib/common/SQLiteUtils.d.ts.html +39 -0
- package/coverage-ts/files/lib/common/constants.d.ts.html +19 -0
- package/coverage-ts/files/lib/common/types.d.ts.html +34 -0
- package/coverage-ts/files/lib/index.d.ts.html +20 -0
- package/coverage-ts/files/lib-esm/ExpoSQLiteAdapter/ExpoSQLiteAdapter.d.ts.html +21 -0
- package/coverage-ts/files/lib-esm/ExpoSQLiteAdapter/ExpoSQLiteDatabase.d.ts.html +35 -0
- package/coverage-ts/files/lib-esm/SQLiteAdapter/SQLiteAdapter.d.ts.html +21 -0
- package/coverage-ts/files/lib-esm/SQLiteAdapter/SQLiteDatabase.d.ts.html +35 -0
- package/coverage-ts/files/lib-esm/common/CommonSQLiteAdapter.d.ts.html +41 -0
- package/coverage-ts/files/lib-esm/common/SQLiteUtils.d.ts.html +39 -0
- package/coverage-ts/files/lib-esm/common/constants.d.ts.html +19 -0
- package/coverage-ts/files/lib-esm/common/types.d.ts.html +34 -0
- package/coverage-ts/files/lib-esm/index.d.ts.html +20 -0
- package/coverage-ts/files/src/ExpoSQLiteAdapter/ExpoSQLiteAdapter.ts.html +28 -0
- package/coverage-ts/files/src/ExpoSQLiteAdapter/ExpoSQLiteDatabase.ts.html +302 -0
- package/coverage-ts/files/src/SQLiteAdapter/SQLiteAdapter.ts.html +28 -0
- package/coverage-ts/files/src/SQLiteAdapter/SQLiteDatabase.ts.html +183 -0
- package/coverage-ts/files/src/common/CommonSQLiteAdapter.ts.html +460 -0
- package/coverage-ts/files/src/common/SQLiteUtils.ts.html +520 -0
- package/coverage-ts/files/src/common/constants.ts.html +21 -0
- package/coverage-ts/files/src/common/types.ts.html +54 -0
- package/coverage-ts/files/src/index.ts.html +22 -0
- package/coverage-ts/index.html +15 -0
- package/coverage-ts/typescript-coverage.json +772 -0
- package/dist/aws-amplify-datastore-storage-adapter.js +5 -0
- package/dist/aws-amplify-datastore-storage-adapter.js.map +1 -1
- package/dist/aws-amplify-datastore-storage-adapter.min.js.map +1 -1
- package/lib/SQLiteAdapter/SQLiteAdapter.d.ts +5 -0
- package/lib/SQLiteAdapter/SQLiteAdapter.js +5 -0
- package/lib/SQLiteAdapter/SQLiteAdapter.js.map +1 -1
- package/lib-esm/SQLiteAdapter/SQLiteAdapter.d.ts +5 -0
- package/lib-esm/SQLiteAdapter/SQLiteAdapter.js +5 -0
- package/lib-esm/SQLiteAdapter/SQLiteAdapter.js.map +1 -1
- package/package.json +4 -4
- package/src/SQLiteAdapter/SQLiteAdapter.ts +5 -0
- package/.rollup.cache/home/runner/work/amplify-js/amplify-js/amplify-js/packages/datastore-storage-adapter/dist/meta/cjs.tsbuildinfo +0 -1
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/cjs/ExpoSQLiteAdapter/ExpoSQLiteAdapter.js +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/cjs/ExpoSQLiteAdapter/ExpoSQLiteDatabase.js +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/cjs/SQLiteAdapter/SQLiteAdapter.js +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/cjs/SQLiteAdapter/SQLiteDatabase.js +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/cjs/common/CommonSQLiteAdapter.js +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/cjs/common/SQLiteUtils.js +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/cjs/common/constants.js +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/cjs/common/types.js +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/cjs/index.js +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/esm/ExpoSQLiteAdapter/ExpoSQLiteAdapter.d.ts +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/esm/ExpoSQLiteAdapter/ExpoSQLiteAdapter.js +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/esm/ExpoSQLiteAdapter/ExpoSQLiteDatabase.d.ts +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/esm/ExpoSQLiteAdapter/ExpoSQLiteDatabase.js +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/esm/SQLiteAdapter/SQLiteAdapter.d.ts +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/esm/SQLiteAdapter/SQLiteAdapter.js +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/esm/SQLiteAdapter/SQLiteDatabase.d.ts +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/esm/SQLiteAdapter/SQLiteDatabase.js +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/esm/common/CommonSQLiteAdapter.d.ts +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/esm/common/CommonSQLiteAdapter.js +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/esm/common/SQLiteUtils.d.ts +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/esm/common/SQLiteUtils.js +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/esm/common/constants.d.ts +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/esm/common/constants.js +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/esm/common/types.d.ts +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/esm/common/types.js +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/esm/index.d.ts +0 -0
- /package/.rollup.cache/{home/runner/work/amplify-js/amplify-js → Volumes/workplace}/amplify-js/packages/datastore-storage-adapter/dist/esm/index.js +0 -0
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
|
|
2
|
+
<!DOCTYPE html>
|
|
3
|
+
<html>
|
|
4
|
+
<head>
|
|
5
|
+
<title>CommonSQLiteAdapter.ts</title>
|
|
6
|
+
<link href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css" type="text/css" rel="stylesheet">
|
|
7
|
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.58.2/codemirror.min.js" type="text/javascript" charset="utf-8"></script>
|
|
8
|
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.58.2/mode/javascript/javascript.min.js" type="text/javascript" charset="utf-8"></script>
|
|
9
|
+
<link href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.58.2/codemirror.min.css" type="text/css" rel="stylesheet">
|
|
10
|
+
<script src="../../../assets/source-file.js" type="text/javascript" charset="utf-8"></script>
|
|
11
|
+
<link href="../../../assets/source-file.css" type="text/css" rel="stylesheet">
|
|
12
|
+
</head>
|
|
13
|
+
<body>
|
|
14
|
+
<div style="margin-top:3em" class="ui container"><h1 class="ui header"><a href="../../../index.html">TypeScript coverage report</a></h1><table style="margin-top:2em" class="ui celled table"><thead class=""><tr class=""><th class="">Filename</th><th class="">Percent</th><th class="">Threshold</th><th class="">Total</th><th class="">Covered</th><th class="">Uncovered</th></tr></thead><tbody class=""><tr class="positive"><td class="">src/common/CommonSQLiteAdapter.ts</td><td class="">96.60%</td><td class="">94.16%</td><td class="">765</td><td class="">739</td><td class="">26</td></tr></tbody></table><textarea id="editor" readonly="" style="margin-top:3em">// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
15
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
16
|
+
import { ConsoleLogger as Logger } from '@aws-amplify/core';
|
|
17
|
+
import {
|
|
18
|
+
generateSchemaStatements,
|
|
19
|
+
queryByIdStatement,
|
|
20
|
+
modelUpdateStatement,
|
|
21
|
+
modelInsertStatement,
|
|
22
|
+
queryAllStatement,
|
|
23
|
+
queryOneStatement,
|
|
24
|
+
deleteByIdStatement,
|
|
25
|
+
deleteByPredicateStatement,
|
|
26
|
+
} from '../common/SQLiteUtils';
|
|
27
|
+
|
|
28
|
+
import {
|
|
29
|
+
StorageAdapter,
|
|
30
|
+
ModelInstanceCreator,
|
|
31
|
+
ModelPredicateCreator,
|
|
32
|
+
ModelSortPredicateCreator,
|
|
33
|
+
InternalSchema,
|
|
34
|
+
isPredicateObj,
|
|
35
|
+
ModelPredicate,
|
|
36
|
+
NamespaceResolver,
|
|
37
|
+
NAMESPACES,
|
|
38
|
+
OpType,
|
|
39
|
+
PaginationInput,
|
|
40
|
+
PersistentModel,
|
|
41
|
+
PersistentModelConstructor,
|
|
42
|
+
PredicateObject,
|
|
43
|
+
PredicatesGroup,
|
|
44
|
+
QueryOne,
|
|
45
|
+
utils,
|
|
46
|
+
} from '@aws-amplify/datastore';
|
|
47
|
+
import {
|
|
48
|
+
CommonSQLiteDatabase,
|
|
49
|
+
ParameterizedStatement,
|
|
50
|
+
ModelInstanceMetadataWithId,
|
|
51
|
+
} from './types';
|
|
52
|
+
|
|
53
|
+
const { traverseModel, validatePredicate, isModelConstructor } = utils;
|
|
54
|
+
|
|
55
|
+
const logger = new Logger('DataStore');
|
|
56
|
+
|
|
57
|
+
export class CommonSQLiteAdapter implements StorageAdapter {
|
|
58
|
+
private schema: InternalSchema;
|
|
59
|
+
private namespaceResolver: NamespaceResolver;
|
|
60
|
+
private modelInstanceCreator: ModelInstanceCreator;
|
|
61
|
+
private getModelConstructorByModelName: (
|
|
62
|
+
namsespaceName: string,
|
|
63
|
+
modelName: string
|
|
64
|
+
) => PersistentModelConstructor<any>;
|
|
65
|
+
private db: CommonSQLiteDatabase;
|
|
66
|
+
private initPromise: Promise<void>;
|
|
67
|
+
private resolve: (value?: any) => void;
|
|
68
|
+
private reject: (value?: any) => void;
|
|
69
|
+
|
|
70
|
+
constructor(db: CommonSQLiteDatabase) {
|
|
71
|
+
this.db = db;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
public async setUp(
|
|
75
|
+
theSchema: InternalSchema,
|
|
76
|
+
namespaceResolver: NamespaceResolver,
|
|
77
|
+
modelInstanceCreator: ModelInstanceCreator,
|
|
78
|
+
getModelConstructorByModelName: (
|
|
79
|
+
namsespaceName: NAMESPACES,
|
|
80
|
+
modelName: string
|
|
81
|
+
) => PersistentModelConstructor<any>
|
|
82
|
+
) {
|
|
83
|
+
if (!this.initPromise) {
|
|
84
|
+
this.initPromise = new Promise((res, rej) => {
|
|
85
|
+
this.resolve = res;
|
|
86
|
+
this.reject = rej;
|
|
87
|
+
});
|
|
88
|
+
} else {
|
|
89
|
+
await this.initPromise;
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
this.schema = theSchema;
|
|
93
|
+
this.namespaceResolver = namespaceResolver;
|
|
94
|
+
this.modelInstanceCreator = modelInstanceCreator;
|
|
95
|
+
this.getModelConstructorByModelName = getModelConstructorByModelName;
|
|
96
|
+
|
|
97
|
+
try {
|
|
98
|
+
const usesCPKCodegen = Object.values(
|
|
99
|
+
this.schema.namespaces.user.models
|
|
100
|
+
).some(model =>
|
|
101
|
+
Object.values(model.fields).some(field =>
|
|
102
|
+
field.association?.hasOwnProperty('targetNames')
|
|
103
|
+
)
|
|
104
|
+
);
|
|
105
|
+
if (usesCPKCodegen) {
|
|
106
|
+
logger.error(
|
|
107
|
+
'The SQLite adapter does not support schemas using custom primary key. Set `graphQLTransformer.respectPrimaryKeyAttributesOnConnectionField in `amplify/cli.json` to false to disable custom primary key. To regenerate your API, add or remove an empty newline to your GraphQL schema (to change the computed hash) then run `amplify push`.'
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
await this.db.init();
|
|
111
|
+
const statements = generateSchemaStatements(this.schema);
|
|
112
|
+
await this.db.createSchema(statements);
|
|
113
|
+
|
|
114
|
+
this.resolve();
|
|
115
|
+
} catch (error) {
|
|
116
|
+
this.reject(error);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async clear(): Promise<void> {
|
|
121
|
+
await this.db.clear();
|
|
122
|
+
|
|
123
|
+
this.initPromise = undefined;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async save<T extends PersistentModel>(
|
|
127
|
+
model: T,
|
|
128
|
+
condition?: ModelPredicate<T>
|
|
129
|
+
): Promise<[T, OpType.INSERT | OpType.UPDATE][]> {
|
|
130
|
+
const modelConstructor = Object.getPrototypeOf(model)
|
|
131
|
+
.constructor as PersistentModelConstructor<T>;
|
|
132
|
+
const { name: tableName } = modelConstructor;
|
|
133
|
+
const connectedModels = traverseModel(
|
|
134
|
+
modelConstructor.name,
|
|
135
|
+
model,
|
|
136
|
+
this.schema.namespaces[this.namespaceResolver(modelConstructor)],
|
|
137
|
+
this.modelInstanceCreator,
|
|
138
|
+
this.getModelConstructorByModelName
|
|
139
|
+
);
|
|
140
|
+
const connectionStoreNames = Object.values(connectedModels).map(
|
|
141
|
+
({ modelName, item, instance }) => {
|
|
142
|
+
return { modelName, item, instance };
|
|
143
|
+
}
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
const [queryStatement, params] = queryByIdStatement(model.id, tableName);
|
|
147
|
+
|
|
148
|
+
const fromDB = await this.db.get(queryStatement, params);
|
|
149
|
+
|
|
150
|
+
if (condition && fromDB) {
|
|
151
|
+
const predicates = ModelPredicateCreator.getPredicates(condition);
|
|
152
|
+
const { predicates: predicateObjs, type } = predicates;
|
|
153
|
+
|
|
154
|
+
const isValid = validatePredicate(fromDB, type, predicateObjs);
|
|
155
|
+
|
|
156
|
+
if (!isValid) {
|
|
157
|
+
const msg = 'Conditional update failed';
|
|
158
|
+
logger.error(msg, { model: fromDB, condition: predicateObjs });
|
|
159
|
+
|
|
160
|
+
throw new Error(msg);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const result: [T, OpType.INSERT | OpType.UPDATE][] = [];
|
|
165
|
+
const saveStatements = new Set<ParameterizedStatement>();
|
|
166
|
+
|
|
167
|
+
for await (const resItem of connectionStoreNames) {
|
|
168
|
+
const { modelName, item, instance } = resItem;
|
|
169
|
+
const { id } = item;
|
|
170
|
+
|
|
171
|
+
const [queryStatement, params] = queryByIdStatement(id, modelName);
|
|
172
|
+
const fromDB = await this.db.get(queryStatement, params);
|
|
173
|
+
|
|
174
|
+
const opType: OpType =
|
|
175
|
+
fromDB === undefined ? OpType.INSERT : OpType.UPDATE;
|
|
176
|
+
|
|
177
|
+
const saveStatement = fromDB
|
|
178
|
+
? modelUpdateStatement(instance, modelName)
|
|
179
|
+
: modelInsertStatement(instance, modelName);
|
|
180
|
+
|
|
181
|
+
if (id === model.id || opType === OpType.INSERT) {
|
|
182
|
+
saveStatements.add(saveStatement);
|
|
183
|
+
result.push([instance, opType]);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
await this.db.batchSave(saveStatements);
|
|
188
|
+
|
|
189
|
+
return result;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private async load<T>(
|
|
193
|
+
namespaceName: string,
|
|
194
|
+
srcModelName: string,
|
|
195
|
+
records: T[]
|
|
196
|
+
): Promise<T[]> {
|
|
197
|
+
const namespace = this.schema.namespaces[namespaceName];
|
|
198
|
+
const relations = namespace.relationships[srcModelName].relationTypes;
|
|
199
|
+
const connectionTableNames = relations.map(({ modelName }) => modelName);
|
|
200
|
+
|
|
201
|
+
const modelConstructor = this.getModelConstructorByModelName(
|
|
202
|
+
namespaceName,
|
|
203
|
+
srcModelName
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
if (connectionTableNames.length === 0) {
|
|
207
|
+
return records.map(record =>
|
|
208
|
+
this.modelInstanceCreator(modelConstructor, record)
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Remove related-model fields. They're all `null` in the database,
|
|
213
|
+
// and any that happen to be required will result in a false validation
|
|
214
|
+
// error when DataStore attempts to initialize with `null`.
|
|
215
|
+
// These fields aren't actually needed here. DataStore will use the FK's
|
|
216
|
+
// from the schema model.
|
|
217
|
+
return records.map(record => {
|
|
218
|
+
for (const r of relations) {
|
|
219
|
+
delete record[r.fieldName];
|
|
220
|
+
}
|
|
221
|
+
return this.modelInstanceCreator(modelConstructor, record);
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async query<T extends PersistentModel>(
|
|
226
|
+
modelConstructor: PersistentModelConstructor<T>,
|
|
227
|
+
predicate?: ModelPredicate<T>,
|
|
228
|
+
pagination?: PaginationInput<T>
|
|
229
|
+
): Promise<T[]> {
|
|
230
|
+
const { name: tableName } = modelConstructor;
|
|
231
|
+
const namespaceName = this.namespaceResolver(modelConstructor);
|
|
232
|
+
|
|
233
|
+
const predicates =
|
|
234
|
+
predicate && ModelPredicateCreator.getPredicates(predicate);
|
|
235
|
+
const sortPredicates =
|
|
236
|
+
pagination &&
|
|
237
|
+
pagination.sort &&
|
|
238
|
+
ModelSortPredicateCreator.getPredicates(pagination.sort);
|
|
239
|
+
const limit = pagination && pagination.limit;
|
|
240
|
+
const page = limit && pagination.page;
|
|
241
|
+
|
|
242
|
+
const queryById = predicates && this.idFromPredicate(predicates);
|
|
243
|
+
|
|
244
|
+
const records: T[] = <T[]>await (async () => {
|
|
245
|
+
if (queryById) {
|
|
246
|
+
const record = await this.getById(tableName, queryById);
|
|
247
|
+
return record ? [record] : [];
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const [queryStatement, params] = queryAllStatement(
|
|
251
|
+
tableName,
|
|
252
|
+
predicates,
|
|
253
|
+
sortPredicates,
|
|
254
|
+
limit,
|
|
255
|
+
page
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
return await this.db.getAll(queryStatement, params);
|
|
259
|
+
})();
|
|
260
|
+
|
|
261
|
+
return await this.load(namespaceName, modelConstructor.name, records);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
private async getById<T extends PersistentModel>(
|
|
265
|
+
tableName: string,
|
|
266
|
+
id: string
|
|
267
|
+
): Promise<T> {
|
|
268
|
+
const [queryStatement, params] = queryByIdStatement(id, tableName);
|
|
269
|
+
const record = await this.db.get<T>(queryStatement, params);
|
|
270
|
+
|
|
271
|
+
return record;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
private idFromPredicate<T extends PersistentModel>(
|
|
275
|
+
predicates: PredicatesGroup<T>
|
|
276
|
+
) {
|
|
277
|
+
const { predicates: predicateObjs } = predicates;
|
|
278
|
+
const idPredicate =
|
|
279
|
+
predicateObjs.length === 1 &&
|
|
280
|
+
(predicateObjs.find(
|
|
281
|
+
p => isPredicateObj(p) && p.field === 'id' && p.operator === 'eq'
|
|
282
|
+
) as PredicateObject<T>);
|
|
283
|
+
|
|
284
|
+
return idPredicate && idPredicate.operand;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
async queryOne<T extends PersistentModel>(
|
|
288
|
+
modelConstructor: PersistentModelConstructor<T>,
|
|
289
|
+
firstOrLast: QueryOne = QueryOne.FIRST
|
|
290
|
+
): Promise<T | undefined> {
|
|
291
|
+
const { name: tableName } = modelConstructor;
|
|
292
|
+
const [queryStatement, params] = queryOneStatement(firstOrLast, tableName);
|
|
293
|
+
|
|
294
|
+
const result = await this.db.get<T>(queryStatement, params);
|
|
295
|
+
|
|
296
|
+
const modelInstance =
|
|
297
|
+
result && this.modelInstanceCreator(modelConstructor, result);
|
|
298
|
+
|
|
299
|
+
return modelInstance;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Currently does not cascade
|
|
303
|
+
// TODO: use FKs in relations and have `ON DELETE CASCADE` set
|
|
304
|
+
// For Has Many and Has One relations to have SQL handle cascades automatically
|
|
305
|
+
async delete<T extends PersistentModel>(
|
|
306
|
+
modelOrModelConstructor: T | PersistentModelConstructor<T>,
|
|
307
|
+
condition?: ModelPredicate<T>
|
|
308
|
+
): Promise<[T[], T[]]> {
|
|
309
|
+
if (isModelConstructor(modelOrModelConstructor)) {
|
|
310
|
+
const modelConstructor = modelOrModelConstructor;
|
|
311
|
+
const namespaceName = this.namespaceResolver(modelConstructor);
|
|
312
|
+
const { name: tableName } = modelConstructor;
|
|
313
|
+
|
|
314
|
+
const predicates =
|
|
315
|
+
condition && ModelPredicateCreator.getPredicates(condition);
|
|
316
|
+
|
|
317
|
+
const queryStatement = queryAllStatement(tableName, predicates);
|
|
318
|
+
const deleteStatement = deleteByPredicateStatement(tableName, predicates);
|
|
319
|
+
|
|
320
|
+
const models = await this.db.selectAndDelete(
|
|
321
|
+
queryStatement,
|
|
322
|
+
deleteStatement
|
|
323
|
+
);
|
|
324
|
+
|
|
325
|
+
const modelInstances = await this.load(
|
|
326
|
+
namespaceName,
|
|
327
|
+
modelConstructor.name,
|
|
328
|
+
models
|
|
329
|
+
);
|
|
330
|
+
|
|
331
|
+
return [modelInstances, modelInstances];
|
|
332
|
+
} else {
|
|
333
|
+
const model = modelOrModelConstructor as T;
|
|
334
|
+
const modelConstructor = Object.getPrototypeOf(model)
|
|
335
|
+
.constructor as PersistentModelConstructor<T>;
|
|
336
|
+
const { name: tableName } = modelConstructor;
|
|
337
|
+
|
|
338
|
+
if (condition) {
|
|
339
|
+
const [queryStatement, params] = queryByIdStatement(
|
|
340
|
+
model.id,
|
|
341
|
+
tableName
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
const fromDB = await this.db.get(queryStatement, params);
|
|
345
|
+
|
|
346
|
+
if (fromDB === undefined) {
|
|
347
|
+
const msg = 'Model instance not found in storage';
|
|
348
|
+
logger.warn(msg, { model });
|
|
349
|
+
|
|
350
|
+
return [[model], []];
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const predicates = ModelPredicateCreator.getPredicates(condition);
|
|
354
|
+
const { predicates: predicateObjs, type } = predicates;
|
|
355
|
+
|
|
356
|
+
const isValid = validatePredicate(fromDB, type, predicateObjs);
|
|
357
|
+
|
|
358
|
+
if (!isValid) {
|
|
359
|
+
const msg = 'Conditional update failed';
|
|
360
|
+
logger.error(msg, { model: fromDB, condition: predicateObjs });
|
|
361
|
+
|
|
362
|
+
throw new Error(msg);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const [deleteStatement, deleteParams] = deleteByIdStatement(
|
|
366
|
+
model.id,
|
|
367
|
+
tableName
|
|
368
|
+
);
|
|
369
|
+
await this.db.save(deleteStatement, deleteParams);
|
|
370
|
+
|
|
371
|
+
return [[model], [model]];
|
|
372
|
+
} else {
|
|
373
|
+
const [deleteStatement, params] = deleteByIdStatement(
|
|
374
|
+
model.id,
|
|
375
|
+
tableName
|
|
376
|
+
);
|
|
377
|
+
await this.db.save(deleteStatement, params);
|
|
378
|
+
|
|
379
|
+
return [[model], [model]];
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
async batchSave<T extends PersistentModel>(
|
|
385
|
+
modelConstructor: PersistentModelConstructor<any>,
|
|
386
|
+
items: ModelInstanceMetadataWithId[]
|
|
387
|
+
): Promise<[T, OpType][]> {
|
|
388
|
+
const { name: tableName } = modelConstructor;
|
|
389
|
+
const result: [T, OpType][] = [];
|
|
390
|
+
|
|
391
|
+
const itemsToSave: T[] = [];
|
|
392
|
+
// To determine whether an item should result in an insert or update operation
|
|
393
|
+
// We first need to query the local DB on the item id
|
|
394
|
+
const queryStatements = new Set<ParameterizedStatement>();
|
|
395
|
+
// Deletes don't need to be queried first, because if the item doesn't exist,
|
|
396
|
+
// the delete operation will be a no-op
|
|
397
|
+
const deleteStatements = new Set<ParameterizedStatement>();
|
|
398
|
+
const saveStatements = new Set<ParameterizedStatement>();
|
|
399
|
+
|
|
400
|
+
for (const item of items) {
|
|
401
|
+
const connectedModels = traverseModel(
|
|
402
|
+
modelConstructor.name,
|
|
403
|
+
this.modelInstanceCreator(modelConstructor, item),
|
|
404
|
+
this.schema.namespaces[this.namespaceResolver(modelConstructor)],
|
|
405
|
+
this.modelInstanceCreator,
|
|
406
|
+
this.getModelConstructorByModelName
|
|
407
|
+
);
|
|
408
|
+
|
|
409
|
+
const { id, _deleted } = item;
|
|
410
|
+
|
|
411
|
+
const { instance } = connectedModels.find(
|
|
412
|
+
({ instance }) => instance.id === id
|
|
413
|
+
);
|
|
414
|
+
|
|
415
|
+
if (_deleted) {
|
|
416
|
+
// create the delete statements right away
|
|
417
|
+
const deleteStatement = deleteByIdStatement(instance.id, tableName);
|
|
418
|
+
deleteStatements.add(deleteStatement);
|
|
419
|
+
result.push([<T>(<unknown>item), OpType.DELETE]);
|
|
420
|
+
} else {
|
|
421
|
+
// query statements for the saves at first
|
|
422
|
+
const queryStatement = queryByIdStatement(id, tableName);
|
|
423
|
+
queryStatements.add(queryStatement);
|
|
424
|
+
// combination of insert and update items
|
|
425
|
+
itemsToSave.push(instance);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// returns the query results for each of the save items
|
|
430
|
+
const queryResponses = await this.db.batchQuery(queryStatements);
|
|
431
|
+
|
|
432
|
+
queryResponses.forEach((response, idx) => {
|
|
433
|
+
if (response === undefined) {
|
|
434
|
+
const insertStatement = modelInsertStatement(
|
|
435
|
+
itemsToSave[idx],
|
|
436
|
+
tableName
|
|
437
|
+
);
|
|
438
|
+
saveStatements.add(insertStatement);
|
|
439
|
+
result.push([<T>(<unknown>itemsToSave[idx]), OpType.INSERT]);
|
|
440
|
+
} else {
|
|
441
|
+
const updateStatement = modelUpdateStatement(
|
|
442
|
+
itemsToSave[idx],
|
|
443
|
+
tableName
|
|
444
|
+
);
|
|
445
|
+
saveStatements.add(updateStatement);
|
|
446
|
+
result.push([<T>(<unknown>itemsToSave[idx]), OpType.UPDATE]);
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
// perform all of the insert/update/delete operations in a single transaction
|
|
451
|
+
await this.db.batchSave(saveStatements, deleteStatements);
|
|
452
|
+
|
|
453
|
+
return result;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
</textarea><pre id="annotations" style="display:none">[{"file":"src/common/CommonSQLiteAdapter.ts","line":53,"character":19,"text":"value","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":54,"character":18,"text":"value","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":102,"character":15,"text":"error","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":101,"character":11,"text":"error","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":132,"character":60,"text":"id","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":140,"character":9,"text":"isValid","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":142,"character":8,"text":"isValid","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":155,"character":11,"text":"id","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":167,"character":7,"text":"id","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":167,"character":20,"text":"id","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":228,"character":8,"text":"queryById","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":231,"character":7,"text":"queryById","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":270,"character":36,"text":"operand","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":326,"character":11,"text":"id","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":342,"character":10,"text":"isValid","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":344,"character":9,"text":"isValid","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":352,"character":11,"text":"id","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":360,"character":11,"text":"id","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":397,"character":11,"text":"instance","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":398,"character":7,"text":"instance","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":398,"character":22,"text":"instance","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":398,"character":31,"text":"id","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":403,"character":48,"text":"instance","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":403,"character":57,"text":"id","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":418,"character":26,"text":"response","kind":1},{"file":"src/common/CommonSQLiteAdapter.ts","line":419,"character":7,"text":"response","kind":1}]</pre></div>
|
|
457
|
+
<p class="footer-text">TypeScript Coverage Report generated by <a href="https://github.com/plantain-00/type-coverage">type-coverage</a> and <a href="https://github.com/alexcanessa/typescript-coverage-report">typescript-coverage-report</a> at Mon, 20 Apr 2026 10:42:30 GMT</p>
|
|
458
|
+
</body>
|
|
459
|
+
</html>
|
|
460
|
+
|