Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 13, 2024
1 parent a620cc2 commit 19da90b
Showing 1 changed file with 40 additions and 51 deletions.
91 changes: 40 additions & 51 deletions src/drivers/db0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,81 +21,67 @@ const DEFAULT_TABLE_NAME = "unstorage";
export default defineDriver((opts: DB0DriverOptions) => {
opts.tableName = opts.tableName || DEFAULT_TABLE_NAME;

/**
* This promise is used to ensure that only one table creation is attempted
* at a time and that all queries are queued until the table is created.
*/
let createTablePromise: Promise<void> | undefined;

/**
* Creates a wrapper around a query function that will automatically
* try to create the database table when a query fails.
*/
const withCreateTable = <TResult, TArgs extends unknown[]>(
query: (...args: TArgs) => Promise<TResult>
) => {
return async (...args: TArgs): Promise<TResult> => {
if (createTablePromise === undefined) {
try {
return await query(...args);
} catch {
createTablePromise = createTable(opts);
}
}

await createTablePromise;

return await query(...args);
};
let setupPromise: Promise<void> | undefined;
let setupDone = false;
const ensureTable = () => {
if (setupDone) {
return;
}
if (!setupPromise) {
setupPromise = setupTable(opts).then(() => {
setupDone = true;
setupPromise = undefined;
});
}
return setupPromise;
};

return {
name: DRIVER_NAME,
options: opts,
getInstance: () => opts.database,
hasItem: withCreateTable(async (key) => {
async hasItem(key) {
await ensureTable();
const { rows } = await opts.database.sql<ResultSchema>/* sql */ `
SELECT EXISTS (
SELECT 1 FROM {${opts.tableName}}
WHERE key = ${key}
) AS value
`;

return rows?.[0]?.value == "1";
}),
getItem: withCreateTable(async (key) => {
},
getItem: async (key) => {
await ensureTable();
const { rows } = await opts.database.sql<ResultSchema>/* sql */ `
SELECT value
FROM {${opts.tableName}}
WHERE key = ${key}
SELECT value FROM {${opts.tableName}} WHERE key = ${key}
`;

return rows?.[0]?.value ?? null;
}),
setItem: withCreateTable(async (key, value) => {
},
setItem: async (key, value) => {
await ensureTable();
if (opts.database.dialect === "mysql") {
await opts.database.sql/* sql */ `
INSERT INTO {${opts.tableName}} (key, value)
VALUES (${key}, ${value})
ON DUPLICATE KEY UPDATE value = ${value}

Check warning on line 66 in src/drivers/db0.ts

View check run for this annotation

Codecov / codecov/patch

src/drivers/db0.ts#L63-L66

Added lines #L63 - L66 were not covered by tests
`;

return;
}

await opts.database.sql/* sql */ `
} else {
await opts.database.sql/* sql */ `
INSERT INTO {${opts.tableName}} (key, value)
VALUES (${key}, ${value})
ON CONFLICT(key) DO UPDATE
SET value = ${value}, updated_at = CURRENT_TIMESTAMP
`;
}),
removeItem: withCreateTable(async (key) => {
}
},
removeItem: async (key) => {
await ensureTable();
await opts.database.sql/* sql */ `
DELETE FROM {${opts.tableName}} WHERE key=${key}
`;
}),
getMeta: withCreateTable(async (key) => {
},
getMeta: async (key) => {
await ensureTable();
const { rows } = await opts.database.sql<ResultSchema>/* sql */ `
SELECT created_at, updated_at
FROM {${opts.tableName}}
Expand All @@ -106,25 +92,28 @@ export default defineDriver((opts: DB0DriverOptions) => {
birthtime: toDate(rows?.[0]?.created_at),
mtime: toDate(rows?.[0]?.updated_at),
};
}),
getKeys: withCreateTable(async (base = "") => {
},
getKeys: async (base = "") => {
await ensureTable();
const { rows } = await opts.database.sql<ResultSchema>/* sql */ `
SELECT key
FROM {${opts.tableName}}
WHERE key LIKE ${base + "%"}
`;

return rows?.map((r) => r.key);
}),
clear: withCreateTable(async () => {
},
clear: async () => {
await ensureTable();
await opts.database.sql/* sql */ `
DELETE FROM {${opts.tableName}}
`;
}),
},
};
});

async function createTable(opts: DB0DriverOptions) {
/** Run database init/migration once */
async function setupTable(opts: DB0DriverOptions) {
switch (opts.database.dialect) {
case "sqlite":
case "libsql": {
Expand Down

0 comments on commit 19da90b

Please sign in to comment.