@potonz/shortlinks-manager-cf-d1 0.2.2 → 0.2.3

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/dist/index.d.ts CHANGED
@@ -103,28 +103,35 @@ export interface IShortLinksManagerBackend {
103
103
  * @param {string} shortId
104
104
  * @returns the short ID or null if not found
105
105
  */
106
- getTargetUrl: (shortId: string) => string | null | Promise<string | null>;
106
+ getTargetUrl(shortId: string): string | null | Promise<string | null>;
107
107
  /**
108
108
  * Create a short link map with the given short ID and target URL
109
109
  * @param {string} shortId
110
110
  * @param {string} targetUrl
111
111
  */
112
- createShortLink: (shortId: string, targetUrl: string) => void | Promise<void>;
112
+ createShortLink(shortId: string, targetUrl: string): void | Promise<void>;
113
113
  /**
114
114
  * Check the provided list of short IDs and return the ones that already exist.
115
115
  * @param {string[]} shortIds
116
116
  */
117
- checkShortIdsExist: (shortIds: string[]) => string[] | Promise<string[]>;
117
+ checkShortIdsExist(shortIds: string[]): string[] | Promise<string[]>;
118
118
  /**
119
119
  * Update last accessed time to current timestamp
120
120
  * @param shortId
121
+ * @param time Unix timestamp or a Date object
121
122
  */
122
- updateShortLinkLastAccessTime(shortId: string): void | Promise<void>;
123
+ updateShortLinkLastAccessTime(shortId: string, time?: number | Date): void | Promise<void>;
123
124
  /**
124
125
  * Remove unused links that are older than the given maxAge
125
126
  * @param maxAge number of days the record should be kept
127
+ * @returns an array of short IDs that have been cleaned
126
128
  */
127
- cleanUnusedLinks(maxAge: number): void | Promise<void>;
129
+ cleanUnusedLinks(maxAge: number): string[] | Promise<string[]>;
130
+ /**
131
+ * Remove a short link by its ID
132
+ * @param shortId the short ID to remove
133
+ */
134
+ removeShortLink(shortId: string): void | Promise<void>;
128
135
  }
129
136
  export interface IShortLinksManagerD1Backend extends IShortLinksManagerBackend {
130
137
  setupTables: () => Promise<void>;
package/dist/index.js CHANGED
@@ -33,7 +33,7 @@ WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
33
33
  MERCHANTABLITY OR NON-INFRINGEMENT.
34
34
  See the Apache Version 2.0 License for specific language governing permissions
35
35
  and limitations under the License.
36
- ***************************************************************************** */function F(j){let w=null,y=null,z=null,B=null,C=null;return{async setupTables(){await j.prepare(`
36
+ ***************************************************************************** */function j($){let p=$.getFullYear(),y=String($.getMonth()+1).padStart(2,"0"),F=String($.getDate()).padStart(2,"0"),H=String($.getHours()).padStart(2,"0"),T=String($.getMinutes()).padStart(2,"0"),Y=String($.getSeconds()).padStart(2,"0");return`${p}-${y}-${F} ${H}:${T}:${Y}`}function w($){let p=null,y=null,F=null,H=null,T=null,Y=null;return{async setupTables(){await $.prepare(`
37
37
  CREATE TABLE IF NOT EXISTS sl_links_map (
38
38
  short_id VARCHAR(255) NOT NULL PRIMARY KEY,
39
39
  target_url VARCHAR(65535) NOT NULL,
@@ -44,4 +44,4 @@ CREATE TABLE IF NOT EXISTS sl_links_map (
44
44
  CREATE INDEX IF NOT EXISTS idx_sl_links_map_last_accessed_at ON sl_links_map(last_accessed_at);
45
45
 
46
46
  PRAGMA optimize;
47
- `).run()},async getTargetUrl(f){if(!w)w=j.prepare("SELECT target_url FROM sl_links_map WHERE short_id = ? LIMIT 1");return(await w.bind(f).first())?.target_url??null},async createShortLink(f,q){if(!z)z=j.prepare("INSERT INTO sl_links_map (short_id, target_url) VALUES (?, ?)");await z.bind(f,q).run()},async checkShortIdsExist(f){if(!y){let D=Array.from("?".repeat(f.length)).join(",");y=j.prepare(`SELECT short_id FROM sl_links_map WHERE short_id IN (${D})`)}let q=await y.bind(...f).all();if(!q.success)return[];return q.results.map((D)=>D.short_id)},async updateShortLinkLastAccessTime(f){if(!B)B=j.prepare("UPDATE sl_links_map SET last_accessed_at = CURRENT_TIMESTAMP WHERE short_id = ?");await B.bind(f).run()},async cleanUnusedLinks(f){if(!C)C=j.prepare("DELETE FROM sl_links_map WHERE last_accessed_at < datetime(CURRENT_TIMESTAMP, ?)");await C.bind(`-${f} days`).run()}}}var J={};export{J as default,F as createD1Backend};
47
+ `).run()},async getTargetUrl(f){if(!p)p=$.prepare("SELECT target_url FROM sl_links_map WHERE short_id = ? LIMIT 1");return(await p.bind(f).first())?.target_url??null},async createShortLink(f,g){if(!F)F=$.prepare("INSERT INTO sl_links_map (short_id, target_url) VALUES (?, ?)");await F.bind(f,g).run()},async checkShortIdsExist(f){if(!y){let D=Array.from("?".repeat(f.length)).join(",");y=$.prepare(`SELECT short_id FROM sl_links_map WHERE short_id IN (${D})`)}let g=await y.bind(...f).all();if(!g.success)return[];return g.results.map((D)=>D.short_id)},async updateShortLinkLastAccessTime(f,g=new Date){if(!H)H=$.prepare("UPDATE sl_links_map SET last_accessed_at = ? WHERE short_id = ?");let D=g;if(typeof D==="number")D=new Date(D);await H.bind(j(D),f).run()},async cleanUnusedLinks(f){if(!T)T=$.prepare("DELETE FROM sl_links_map WHERE last_accessed_at < datetime(CURRENT_TIMESTAMP, ?) RETURNING short_id");return(await T.bind(`-${f} days`).all()).results.map((D)=>D.short_id)},async removeShortLink(f){if(!Y)Y=$.prepare("DELETE FROM sl_links_map WHERE short_id = ?");await Y.bind(f).run()}}}var C={};export{C as default,w as createD1Backend};
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "type": "git",
12
12
  "directory": "packages/shortlinks-manager-cf-d1"
13
13
  },
14
- "version": "0.2.2",
14
+ "version": "0.2.3",
15
15
  "type": "module",
16
16
  "license": "MIT",
17
17
  "dependencies": {