1- import { conditionalDrop , SQLDatabase , SQLStatement } from "./abstract-sql.js" ;
1+ // import { conditionalDrop, SQLDatabase, SQLStatement } from "./abstract-sql.js";
22import { ByConnection } from "./meta-merger.js" ;
3- import { TenantLedgerSql } from "./tenant-ledger.js" ;
3+ // import { TenantLedgerSql } from "./tenant-ledger.js";
4+ import { foreignKey , primaryKey , sqliteTable , text } from "drizzle-orm/sqlite-core" ;
5+ import { sqlTenantLedger } from "./tenant-ledger.js" ;
6+ import { LibSQLDatabase } from "drizzle-orm/libsql" ;
7+ import { eq , and , inArray } from "drizzle-orm/expressions" ;
48
59export interface KeysForTenantLedger {
610 readonly tenant : string ;
@@ -9,102 +13,138 @@ export interface KeysForTenantLedger {
913 readonly createdAt : Date ;
1014}
1115
12- export interface SQLMetaByTenantLedgerRow {
13- readonly tenant : string ;
14- readonly ledger : string ;
15- readonly key : string ;
16- readonly createdAt : string ;
17- }
16+ // export interface SQLMetaByTenantLedgerRow {
17+ // readonly tenant: string;
18+ // readonly ledger: string;
19+ // readonly key: string;
20+ // readonly createdAt: string;
21+ // }
22+
23+ export const sqlKeyByTenantLedger = sqliteTable (
24+ "KeyByTenantLedger" ,
25+ {
26+ tenant : text ( ) . notNull ( ) ,
27+ ledger : text ( ) . notNull ( ) ,
28+ key : text ( ) . notNull ( ) ,
29+ createdAt : text ( ) . notNull ( ) ,
30+ } ,
31+ ( table ) => [
32+ primaryKey ( { columns : [ table . tenant , table . ledger , table . key ] } ) ,
33+ foreignKey ( {
34+ columns : [ table . tenant , table . ledger ] ,
35+ foreignColumns : [ sqlTenantLedger . tenant , sqlTenantLedger . ledger ] ,
36+ } ) ,
37+ ] ,
38+ ) ;
1839
1940export class KeyByTenantLedgerSql {
20- static schema ( drop = false ) {
21- return [
22- ...conditionalDrop (
23- drop ,
24- "KeyByTenantLedger" ,
25- `
26- CREATE TABLE IF NOT EXISTS KeyByTenantLedger(
27- tenant TEXT NOT NULL,
28- ledger TEXT NOT NULL,
29- key TEXT NOT NULL,
30- createdAt TEXT NOT NULL,
31- PRIMARY KEY (tenant, ledger, key),
32- FOREIGN KEY (tenant, ledger) REFERENCES TenantLedger(tenant, ledger)
33- )
34- ` ,
35- ) ,
36- ] ;
37- }
41+ // static schema(drop = false) {
42+ // return [
43+ // ...conditionalDrop(
44+ // drop,
45+ // "KeyByTenantLedger",
46+ // `
47+ // CREATE TABLE IF NOT EXISTS KeyByTenantLedger(
48+ // tenant TEXT NOT NULL,
49+ // ledger TEXT NOT NULL,
50+ // key TEXT NOT NULL,
51+ // createdAt TEXT NOT NULL,
52+ // PRIMARY KEY (tenant, ledger, key),
53+ // FOREIGN KEY (tenant, ledger) REFERENCES TenantLedger(tenant, ledger)
54+ // )
55+ // `,
56+ // ),
57+ // ];
58+ // }
3859
39- readonly db : SQLDatabase ;
40- readonly tenantLedgerSql : TenantLedgerSql ;
60+ readonly db : LibSQLDatabase ;
61+ // readonly tenantLedgerSql: TenantLedgerSql;
4162 readonly id : string ;
42- constructor ( id : string , db : SQLDatabase , tenantLedgerSql : TenantLedgerSql ) {
63+ constructor ( id : string , db : LibSQLDatabase ) {
4364 this . db = db ;
44- this . tenantLedgerSql = tenantLedgerSql ;
4565 this . id = id ;
4666 }
4767
4868 // readonly #sqlCreateMetaByTenantLedger = new ResolveOnce();
49- sqlCreateKeyByTenantLedger ( ) : SQLStatement [ ] {
50- // return this.#sqlCreateMetaByTenantLedger.once(() => {
51- return KeyByTenantLedgerSql . schema ( ) . map ( ( i ) => this . db . prepare ( i ) ) ;
52- // });
53- }
69+ // sqlCreateKeyByTenantLedger(): SQLStatement[] {
70+ // // return this.#sqlCreateMetaByTenantLedger.once(() => {
71+ // return KeyByTenantLedgerSql.schema().map((i) => this.db.prepare(i));
72+ // // });
73+ // }
5474
5575 // readonly #sqlInsertMetaByTenantLedger = new ResolveOnce();
56- sqlEnsureKeyByTenantLedger ( ) : SQLStatement {
57- // return this.#sqlInsertMetaByTenantLedger.once(() => {
58- return this . db . prepare ( `
59- INSERT INTO KeyByTenantLedger(tenant, ledger, key, createdAt)
60- SELECT ?, ?, ?, ? WHERE NOT EXISTS (
61- SELECT 1 FROM KeyByTenantLedger WHERE key = ? and tenant = ? and ledger = ?
62- )
63- ` ) ;
64- // });
65- }
6676
6777 // readonly #sqlDeleteByConnection = new ResolveOnce();
68- sqlDeleteByTenantLedgerKey ( ) : SQLStatement {
69- // return this.#sqlDeleteByConnection.once(() => {
70- return this . db . prepare ( `
71- DELETE FROM KeyByTenantLedger
72- WHERE
73- tenant = ?
74- AND
75- ledger = ?
76- AND
77- key = ?
78- ` ) ;
79- // });
80- }
78+ // sqlDeleteByTenantLedgerKey(): SQLStatement {
79+ // // return this.#sqlDeleteByConnection.once(() => {
80+ // return this.db.prepare(`
81+ // DELETE FROM KeyByTenantLedger
82+ // WHERE
83+ // tenant = ?
84+ // AND
85+ // ledger = ?
86+ // AND
87+ // key = ?
88+ // `);
89+ // // });
90+ // }
8191
82- async deleteByTenantLedgerKey ( t : Omit < KeysForTenantLedger , "createdAt" > ) : Promise < void > {
83- const stmt = this . sqlDeleteByTenantLedgerKey ( ) ;
84- for ( const key of t . keys ) {
85- await stmt . run ( t . tenant , t . ledger , key ) ;
86- }
92+ async deleteByTenantLedgerKey ( t : Omit < KeysForTenantLedger , "createdAt" > ) {
93+ return this . db
94+ . delete ( sqlKeyByTenantLedger )
95+ . where (
96+ and (
97+ eq ( sqlKeyByTenantLedger . tenant , t . tenant ) ,
98+ eq ( sqlKeyByTenantLedger . ledger , t . ledger ) ,
99+ inArray ( sqlKeyByTenantLedger . key , t . keys ) ,
100+ ) ,
101+ )
102+ . run ( ) ;
87103 }
88104
89- async ensure ( t : KeysForTenantLedger ) : Promise < void > {
90- const stmt = this . sqlEnsureKeyByTenantLedger ( ) ;
91- for ( const key of t . keys ) {
92- await stmt . run ( t . tenant , t . ledger , key , t . createdAt , key , t . tenant , t . ledger ) ;
93- }
94- }
105+ // sqlEnsureKeyByTenantLedger(): SQLStatement {
106+ // // return this.#sqlInsertMetaByTenantLedger.once(() => {
107+ // return this.db.prepare(`
108+ // INSERT INTO KeyByTenantLedger(tenant, ledger, key, createdAt)
109+ // SELECT ?, ?, ?, ? WHERE NOT EXISTS (
110+ // SELECT 1 FROM KeyByTenantLedger WHERE key = ? and tenant = ? and ledger = ?
111+ // )
112+ // `);
113+ // // });
114+ // }
95115
96- sqlSelectByTenantLedger ( ) : SQLStatement {
97- return this . db . prepare ( `
98- SELECT tenant, ledger, key, createdAt
99- FROM KeyByTenantLedger
100- WHERE tenant = ? AND ledger = ?
101- ORDER BY key
102- ` ) ;
116+ async ensure ( t : KeysForTenantLedger ) {
117+ return this . db
118+ . insert ( sqlKeyByTenantLedger )
119+ . values (
120+ t . keys . map ( ( key ) => ( {
121+ tenant : t . tenant ,
122+ ledger : t . ledger ,
123+ key : key ,
124+ createdAt : t . createdAt . toISOString ( ) ,
125+ } ) ) ,
126+ )
127+ . onConflictDoNothing ( )
128+ . run ( ) ;
103129 }
104130
131+ // sqlSelectByTenantLedger(): SQLStatement {
132+ // return this.db.prepare(`
133+ // SELECT tenant, ledger, key, createdAt
134+ // FROM KeyByTenantLedger
135+ // WHERE tenant = ? AND ledger = ?
136+ // ORDER BY key
137+ // `);
138+ // }
139+
105140 async selectKeysByTenantLedger ( conn : ByConnection ) : Promise < Omit < KeysForTenantLedger , "createdAt" > > {
106- const stmt = this . sqlSelectByTenantLedger ( ) ;
107- const rows = await stmt . all < SQLMetaByTenantLedgerRow > ( conn . tenant , conn . ledger ) ;
141+ // const stmt = this.sqlSelectByTenantLedger();
142+ const rows = await this . db
143+ . select ( )
144+ . from ( sqlKeyByTenantLedger )
145+ . where ( and ( eq ( sqlKeyByTenantLedger . tenant , conn . tenant ) , eq ( sqlKeyByTenantLedger . ledger , conn . ledger ) ) )
146+ . orderBy ( sqlKeyByTenantLedger . key )
147+ . all ( ) ;
108148 return {
109149 tenant : conn . tenant ,
110150 ledger : conn . ledger ,
0 commit comments