MCPcopy Index your code
hub / github.com/tinyplex/tinybase / getMockedDatabase

Function getMockedDatabase

test/unit/persisters/common/mocks.ts:224–286  ·  view source on GitHub ↗
(
  getLocation: () => Promise<Location>,
  getLocationMethod: GetLocationMethod<Location>,
  getPersister: (
    store: Store,
    location: Location,
    storeTableOrConfig: DatabasePersisterConfig,
  ) => Promise<Persister>,
  cmd: (
    location: Location,
    sql: string,
    args?: any[],
  ) => Promise<{[id: string]: any}[]>,
  close: (location: Location) => Promise<void>,
  autoLoadPause = 2,
  autoLoadIntervalSeconds = 0.001,
  _isPostgres = false,
  _supportsMultipleConnections = false,
  _skipSqlChecks = false,
)

Source from the content-addressed store, hash-verified

222};
223
224const getMockedDatabase = <Location>(
225 getLocation: () => Promise<Location>,
226 getLocationMethod: GetLocationMethod<Location>,
227 getPersister: (
228 store: Store,
229 location: Location,
230 storeTableOrConfig: DatabasePersisterConfig,
231 ) => Promise<Persister>,
232 cmd: (
233 location: Location,
234 sql: string,
235 args?: any[],
236 ) => Promise<{[id: string]: any}[]>,
237 close: (location: Location) => Promise<void>,
238 autoLoadPause = 2,
239 autoLoadIntervalSeconds = 0.001,
240 _isPostgres = false,
241 _supportsMultipleConnections = false,
242 _skipSqlChecks = false,
243): Persistable<Location> => {
244 const mockDatabase = {
245 getLocation,
246 getLocationMethod,
247 getPersister: (store: Store, location: Location) =>
248 getPersister(store, location, {
249 mode: 'json',
250 autoLoadIntervalSeconds,
251 }),
252 get: async (location: Location): Promise<Content | void> =>
253 JSON.parse(
254 (
255 await cmd(location, 'SELECT store FROM tinybase WHERE _id = $1', [
256 '_',
257 ])
258 )[0]['store'],
259 ),
260 set: async (location: Location, rawContent: any): Promise<void> =>
261 await mockDatabase.write(location, JSON.stringify(rawContent)),
262 write: async (location: Location, rawContent: any): Promise<void> => {
263 await cmd(
264 location,
265 'CREATE TABLE IF NOT EXISTS tinybase ' +
266 '(_id text PRIMARY KEY, store text);',
267 );
268 await cmd(
269 location,
270 'INSERT INTO tinybase (_id, store) VALUES ($1, $2) ' +
271 'ON CONFLICT (_id) DO UPDATE SET store=excluded.store',
272 ['_', rawContent],
273 );
274 },
275 del: async (location: Location) => {
276 try {
277 await close(location);
278 } catch {}
279 },
280 afterEach: (location: Location) => mockDatabase.del(location),
281 autoLoadPause,

Callers 1

getMockDatabasesFunction · 0.85

Calls 1

getPersisterFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…