@univerjs-pro/collaboration-file-database-sqlite 1.4.0 → 1.6.0

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.cjs CHANGED
@@ -1,28 +1,30 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require("libsql");c=s(c,1);let l=require("@univerjs-pro/collaboration-file-service");var u=`file`,d=1,f=class{_database;_disposed=!1;constructor(e){if(!e.filename)throw TypeError(`SQLiteFileDatabaseAdapter requires a filename`);let t=e.busyTimeoutMs??5e3;this._database=new c.default(e.filename,{timeout:t});try{this._database.exec(`PRAGMA busy_timeout = ${t};`),this._initializeSchema()}catch(e){throw this._database.close(),e}}async create(e,t){this._assertOpen();try{this._database.prepare(`INSERT INTO collaboration_files
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,s)=>(s=n==null?{}:e(i(n)),o(r||!n||!n.__esModule||!a.call(n,`default`)?t(s,`default`,{value:n,enumerable:!0}):s,n));let c=require("libsql");c=s(c,1);let l=require("@univerjs-pro/collaboration-file-service");var u=`file`,d=`
2
+ CREATE TABLE IF NOT EXISTS collaboration_schema_versions (
3
+ component TEXT PRIMARY KEY,
4
+ version INTEGER NOT NULL CHECK (version >= 1)
5
+ );
6
+ `,f=`
7
+ CREATE TABLE collaboration_files (
8
+ file_id TEXT PRIMARY KEY,
9
+ name TEXT NOT NULL,
10
+ size INTEGER NOT NULL,
11
+ content_type TEXT NOT NULL,
12
+ source INTEGER NOT NULL,
13
+ assign TEXT NOT NULL,
14
+ blob_key TEXT NOT NULL,
15
+ created_at INTEGER NOT NULL
16
+ );
17
+ CREATE INDEX collaboration_files_source_created
18
+ ON collaboration_files(source, created_at, file_id);
19
+ CREATE INDEX collaboration_files_source_assign_created
20
+ ON collaboration_files(source, assign, created_at, file_id);
21
+ CREATE INDEX collaboration_files_blob_key
22
+ ON collaboration_files(blob_key);
23
+ `,p=class{_database;_disposed=!1;constructor(e){if(!e.filename)throw TypeError(`SQLiteFileDatabaseAdapter requires a filename`);let t=e.busyTimeoutMs??5e3;this._database=new c.default(e.filename,{timeout:t});try{this._database.exec(`PRAGMA busy_timeout = ${t};`),this._initializeSchema()}catch(e){throw this._database.close(),e}}async create(e,t){this._assertOpen();try{this._database.prepare(`INSERT INTO collaboration_files
2
24
  (file_id, name, size, content_type, source, assign, blob_key, created_at)
3
- VALUES (?, ?, ?, ?, ?, ?, ?, ?)`).run(e.fileID,e.name,e.size,e.contentType,e.source,e.assign,e.blobKey,e.createdAt)}catch(t){throw _(t)?new l.FileError(`INVALID_REQUEST`,`File ${e.fileID} already exists`):t}}async get(e,t){return this._assertOpen(),this._get(e)}async delete(e,t){return this._assertOpen(),this._transaction(()=>{let t=this._get(e);return t?(this._database.prepare(`DELETE FROM collaboration_files WHERE file_id = ?`).run(e),{record:t,releasedBlobKey:t.blobKey}):null})}async list(e,t){this._assertOpen();let n=[`source = ?`],r=[e.source];if(e.assign!==void 0&&(n.push(`assign = ?`),r.push(e.assign)),e.createdBefore!==void 0&&(n.push(`created_at < ?`),r.push(e.createdBefore)),e.cursor!==void 0){let t=g(e.cursor);n.push(`(created_at > ? OR (created_at = ? AND file_id > ?))`),r.push(t.createdAt,t.createdAt,t.fileID)}let i=this._database.prepare(`${p} WHERE ${n.join(` AND `)} ORDER BY created_at, file_id LIMIT ?`).all(...r,e.limit+1),a=i.slice(0,e.limit).map(m),o=a.at(-1);return{records:a,nextCursor:i.length>e.limit&&o?h(o):null}}async dispose(){this._disposed||(this._disposed=!0,this._database.close())}_get(e){let t=this._database.prepare(`${p} WHERE file_id = ?`).get(e);return t?m(t):null}_initializeSchema(){this._transaction(()=>{this._database.exec(`
4
- CREATE TABLE IF NOT EXISTS collaboration_schema_versions (
5
- component TEXT PRIMARY KEY,
6
- version INTEGER NOT NULL CHECK (version >= 1)
7
- );
8
- `);let e=this._readSchemaVersion();if(e!==d){if(e!==void 0)throw Error(`File schema version ${e} is not supported by this adapter (expected ${d})`);this._createSchema()}})}_readSchemaVersion(){return this._database.prepare(`SELECT version FROM collaboration_schema_versions WHERE component = ?`).get(u)?.version}_createSchema(){this._database.exec(`
9
- CREATE TABLE collaboration_files (
10
- file_id TEXT PRIMARY KEY,
11
- name TEXT NOT NULL,
12
- size INTEGER NOT NULL,
13
- content_type TEXT NOT NULL,
14
- source INTEGER NOT NULL,
15
- assign TEXT NOT NULL,
16
- blob_key TEXT NOT NULL,
17
- created_at INTEGER NOT NULL
18
- );
19
- CREATE INDEX collaboration_files_source_created
20
- ON collaboration_files(source, created_at, file_id);
21
- CREATE INDEX collaboration_files_source_assign_created
22
- ON collaboration_files(source, assign, created_at, file_id);
23
- CREATE INDEX collaboration_files_blob_key
24
- ON collaboration_files(blob_key);
25
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)`).run(e.fileID,e.name,e.size,e.contentType,e.source,e.assign,e.blobKey,e.createdAt)}catch(t){throw v(t)?new l.FileError(`INVALID_REQUEST`,`File ${e.fileID} already exists`):t}}async get(e,t){return this._assertOpen(),this._get(e)}async delete(e,t){return this._assertOpen(),this._transaction(()=>{let t=this._get(e);return t?(this._database.prepare(`DELETE FROM collaboration_files WHERE file_id = ?`).run(e),{record:t,releasedBlobKey:t.blobKey}):null})}async list(e,t){this._assertOpen();let n=[`source = ?`],r=[e.source];if(e.assign!==void 0&&(n.push(`assign = ?`),r.push(e.assign)),e.createdBefore!==void 0&&(n.push(`created_at < ?`),r.push(e.createdBefore)),e.cursor!==void 0){let t=_(e.cursor);n.push(`(created_at > ? OR (created_at = ? AND file_id > ?))`),r.push(t.createdAt,t.createdAt,t.fileID)}let i=this._database.prepare(`${m} WHERE ${n.join(` AND `)} ORDER BY created_at, file_id LIMIT ?`).all(...r,e.limit+1),a=i.slice(0,e.limit).map(h),o=a.at(-1);return{records:a,nextCursor:i.length>e.limit&&o?g(o):null}}async dispose(){this._disposed||(this._disposed=!0,this._database.close())}_get(e){let t=this._database.prepare(`${m} WHERE file_id = ?`).get(e);return t?h(t):null}_initializeSchema(){this._transaction(()=>{this._database.exec(d);let e=this._readSchemaVersion();if(e!==1){if(e!==void 0)throw Error(`File schema version ${e} is not supported by this adapter (expected 1)`);this._createSchema()}})}_readSchemaVersion(){return this._database.prepare(`SELECT version FROM collaboration_schema_versions WHERE component = ?`).get(u)?.version}_createSchema(){this._database.exec(`
26
+ ${f}
25
27
  INSERT INTO collaboration_schema_versions (component, version)
26
- VALUES ('${u}', ${d});
27
- `)}_transaction(e){this._database.exec(`BEGIN IMMEDIATE;`);try{let t=e();return this._database.exec(`COMMIT;`),t}catch(e){try{this._database.exec(`ROLLBACK;`)}catch{}throw e}}_assertOpen(){if(this._disposed)throw Error(`SQLite File Database Adapter is disposed`)}},p=`SELECT file_id, name, size, content_type, source, assign, blob_key, created_at
28
- FROM collaboration_files`;function m(e){return{fileID:e.file_id,name:e.name,size:Number(e.size),contentType:e.content_type,source:Number(e.source),assign:e.assign,blobKey:e.blob_key,createdAt:Number(e.created_at)}}function h(e){return Buffer.from(JSON.stringify([e.createdAt,e.fileID])).toString(`base64url`)}function g(e){try{let t=JSON.parse(Buffer.from(e,`base64url`).toString(`utf8`));if(Array.isArray(t)&&t.length===2&&Number.isSafeInteger(t[0])&&typeof t[1]==`string`)return{createdAt:t[0],fileID:t[1]}}catch{}throw new l.FileError(`INVALID_REQUEST`,`Invalid list cursor`)}function _(e){let t=e?.code;return typeof t==`string`&&t.startsWith(`SQLITE_CONSTRAINT`)}exports.SQLiteFileDatabaseAdapter=f;
28
+ VALUES ('${u}', 1);
29
+ `)}_transaction(e){this._database.exec(`BEGIN IMMEDIATE;`);try{let t=e();return this._database.exec(`COMMIT;`),t}catch(e){try{this._database.exec(`ROLLBACK;`)}catch{}throw e}}_assertOpen(){if(this._disposed)throw Error(`SQLite File Database Adapter is disposed`)}},m=`SELECT file_id, name, size, content_type, source, assign, blob_key, created_at
30
+ FROM collaboration_files`;function h(e){return{fileID:e.file_id,name:e.name,size:Number(e.size),contentType:e.content_type,source:Number(e.source),assign:e.assign,blobKey:e.blob_key,createdAt:Number(e.created_at)}}function g(e){return Buffer.from(JSON.stringify([e.createdAt,e.fileID])).toString(`base64url`)}function _(e){try{let t=JSON.parse(Buffer.from(e,`base64url`).toString(`utf8`));if(Array.isArray(t)&&t.length===2&&Number.isSafeInteger(t[0])&&typeof t[1]==`string`)return{createdAt:t[0],fileID:t[1]}}catch{}throw new l.FileError(`INVALID_REQUEST`,`Invalid list cursor`)}function v(e){let t=e?.code;return typeof t==`string`&&t.startsWith(`SQLITE_CONSTRAINT`)}exports.SQLiteFileDatabaseAdapter=p;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { DeleteFileDatabaseResult, FileDatabaseContext, FileRecord, IFileDatabaseAdapter, ListFilesQuery, ListFilesResult } from "@univerjs-pro/collaboration-file-service";
2
2
 
3
- //#region src/sqlite-file-database-adapter.d.ts
3
+ // src/sqlite-file-database-adapter.d.ts
4
4
  /** @public */
5
5
  interface SQLiteFileDatabaseAdapterOptions {
6
6
  /** SQLite filename or `:memory:`. The parent directory must already exist. */
@@ -30,5 +30,4 @@ declare class SQLiteFileDatabaseAdapter implements IFileDatabaseAdapter {
30
30
  private _transaction;
31
31
  private _assertOpen;
32
32
  }
33
- //#endregion
34
33
  export { SQLiteFileDatabaseAdapter, type SQLiteFileDatabaseAdapterOptions };
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "engines": {
12
12
  "node": ">=22"
13
13
  },
14
- "version": "1.4.0",
14
+ "version": "1.6.0",
15
15
  "main": "./dist/index.cjs",
16
16
  "module": "./dist/index.mjs",
17
17
  "types": "./dist/index.d.ts",
@@ -29,6 +29,6 @@
29
29
  "libsql": "^0.5.29"
30
30
  },
31
31
  "peerDependencies": {
32
- "@univerjs-pro/collaboration-file-service": "1.4.0"
32
+ "@univerjs-pro/collaboration-file-service": "1.6.0"
33
33
  }
34
34
  }