Skip to content

Commit

Permalink
Update connections to use latest schema models
Browse files Browse the repository at this point in the history
  • Loading branch information
Brayden committed Aug 28, 2024
1 parent 1e43c05 commit a830648
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 175 deletions.
195 changes: 131 additions & 64 deletions playground/index.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,141 @@
import { CloudflareD1Connection, Outerbase, NeonHttpConnection, OuterbaseConnection, equalsNumber, equals } from '../dist/index.js';
import express from 'express';
import { ColumnDataType } from '../dist/query-builder/index.js';

const app = express();
const port = 4000;

app.get('/test/cloudflare', async (req, res) => {
// Establish connection to your provider database
const d1 = new CloudflareD1Connection({
apiKey: '',
accountId: '',
databaseId: ''
});

const db = Outerbase(d1);
// const dbSchema = await d1.fetchDatabaseSchema()

// const { data, query } = await db.selectFrom([
// { table: 'test2', columns: ['*'] }
// ]).query()

// let { data, query } = await db
// .insert({ fname: 'John' })
// .into('test2')
// .returning(['id'])
// .query();
// import { CloudflareD1Connection, Outerbase, NeonHttpConnection, OuterbaseConnection, equalsNumber, equals } from '../dist/index.js';
// import express from 'express';
// import { ColumnDataType } from '../dist/query-builder/index.js';

// let { data, query } = await db
// .update({ fname: 'Johnny' })
// .into('test2')
// .where(equals('id', '3', d1.dialect))
// .query();
// const app = express();
// const port = 4000;

// let { data, query } = await db
// .deleteFrom('test2')
// .where(equals('id', '3'))
// .query();
// app.get('/test/cloudflare', async (req, res) => {
// // Establish connection to your provider database
// const d1 = new CloudflareD1Connection({
// apiKey: '',
// accountId: '',
// databaseId: ''
// });

// const db = Outerbase(d1);
// // const dbSchema = await d1.fetchDatabaseSchema()

// // const { data, query } = await db.selectFrom([
// // { table: 'test2', columns: ['*'] }
// // ]).query()

// // let { data, query } = await db
// // .insert({ fname: 'John' })
// // .into('test2')
// // .returning(['id'])
// // .query();

// // let { data, query } = await db
// // .update({ fname: 'Johnny' })
// // .into('test2')
// // .where(equals('id', '3', d1.dialect))
// // .query();

// // let { data, query } = await db
// // .deleteFrom('test2')
// // .where(equals('id', '3'))
// // .query();

// let data = {}
// let query = await db
// .createTable('test3')
// .schema('public')
// .columns([
// { name: 'id', type: ColumnDataType.NUMBER, primaryKey: true },
// { name: 'fname', type: ColumnDataType.STRING }
// ])
// .toString();

// // let data = {}
// // let query = await db
// // .renameTable('test3', 'test4')
// // .toString();

// // let data = {}
// // let query = await db
// // .dropTable('test4')
// // .toString();

// console.log('Running Query: ', query)

let data = {}
let query = await db
.createTable('test3')
.schema('public')
.columns([
{ name: 'id', type: ColumnDataType.NUMBER, primaryKey: true },
{ name: 'fname', type: ColumnDataType.STRING }
// // db.
// // - ACTION
// // - CONDITIONS
// // - RETURNING
// // - query() / toString()

// // let { data } = await db.queryRaw('SELECT * FROM playing_with_neon WHERE id = $1', ['1']);
// res.json(data);
// });

// app.listen(port, () => {
// console.log(`Server is running on http://localhost:${port}`);
// });

import { DuckDBConnection, CloudflareD1Connection, Outerbase, equals } from '../dist/index.js';

(async () => {
const connection = new DuckDBConnection({
path: 'md:my_db',
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImNhbGViQG91dGVyYmFzZS5jb20iLCJzZXNzaW9uIjoiY2FsZWIub3V0ZXJiYXNlLmNvbSIsInBhdCI6IjR3b2dqTEQ0RDFoU3hnV01iRXhFR29Qc1d0QjU0X0UyRmRFNUJuektwYUkiLCJ1c2VySWQiOiI5YjU4Mjc5Mi0zY2IwLTRmNzMtODg2ZC1jMTA5ZGE1YzU1ZmIiLCJpc3MiOiJtZF9wYXQiLCJpYXQiOjE3MjQ3MDM3MTh9.OVv8UU5yA5AHYLz44UiAn3UXQJUV4Chg3sTCC5oVVK8'
})
// const response = await connection.query({query: 'SELECT ?::INTEGER AS fortytwo', parameters: [52]})
const db = Outerbase(connection);
// const { data, query, error } = await db.selectFrom([
// { table: 'sample_data.nyc.rideshare', columns: ['bcf'] }
// ]).limit(5).query()

// const schema = await connection.fetchDatabaseSchema()
// console.log('Schema: ', JSON.stringify(schema))

const { data, query, error } = await db
.selectFrom([
{ table: 'sample_data.hn.hacker_news', columns: ['text', 'by', 'type'] }
])
.toString();
.limit(5)
.query()

// let data = {}
// let query = await db
// .renameTable('test3', 'test4')
// .toString();
// const { data, query, error } = await db
// .insert({ text: 'My own post into HN', by: 'Brayden', type: 'comment' })
// .into('sample_data.hn.hacker_news')
// .returning(['text'])
// .query();

// const d1 = new CloudflareD1Connection({
// apiKey: '3_e_AAps3LFAtuaeIVeyc-P70I33jNEsuZnTwxK6',
// accountId: '68599e3cad89422e9c74bd9b829754bd',
// databaseId: '1f4583f6-9853-4a05-be16-f1ff71be14b8'
// });
// const db = Outerbase(d1);
// const { data, query, error } = await db.queryRaw({
// query: 'SELECT ?::INTEGER AS fortytwo',
// parameters: [52]
// })

// const { data:a, query:b, error:c } = await db.selectFrom([
// { table: 'users', columns: ['name'] }
// ]).limit(5).query()

// let data = {}
// let query = await db
// .dropTable('test4')
// .toString();
// console.log({a, b, c})

console.log('Running Query: ', query)
// let { data, query, error } = await db
// .insert({ id: 1, name: 'Caleb' })
// .into('users')
// .returning(['bcf'])
// .query();

// db.
// - ACTION
// - CONDITIONS
// - RETURNING
// - query() / toString()
// let { data, query, error } = await db
// .update({ name: 'Brayden' })
// .into('users')
// .where(equals('id', '1'))
// .query();

// let { data } = await db.queryRaw('SELECT * FROM playing_with_neon WHERE id = $1', ['1']);
res.json(data);
});
// let { data, query, error } = await db
// .deleteFrom('users')
// .where(equals('id', '1'))
// .query();
// console.table({data, query, error})

app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
console.log(data, query, error)
})()
54 changes: 31 additions & 23 deletions src/connections/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,28 +116,25 @@ export class CloudflareD1Connection implements Connection {

public async fetchDatabaseSchema(): Promise<Database> {
const exclude_tables = ['_cf_kv']

let database: Database = []
let schema: Schema = {
tables: []
}

const schemaMap: { [key: string]: Table[] } = {}

const { data } = await this.query({ query: `SELECT * FROM sqlite_master WHERE type='table' AND name NOT LIKE '_lite%' AND name NOT LIKE 'sqlite_%'` })

for (const table of data) {
// Skip excluded tables
if (exclude_tables.includes(table.name?.toLowerCase())) continue;

// Add tables to the schema

if (table.type === 'table') {
const { data: tableData } = await this.query({ query: `SELECT * FROM PRAGMA_TABLE_INFO('${table.name}')`})

const { data: tableData } = await this.query({ query: `PRAGMA table_info('${table.name}')` })
// TODO: This is not returning any data. Need to investigate why.
const { data: indexData } = await this.query({ query: `PRAGMA index_list('${table.name}')`})

const { data: indexData } = await this.query({ query: `PRAGMA index_list('${table.name}')` })
console.log('Table: ', tableData)
console.log('Index: ', indexData)

let constraints: TableIndex[] = []
let columns = tableData.map((column: any) => {
if (column.pk === 1) {
Expand All @@ -147,7 +144,7 @@ export class CloudflareD1Connection implements Connection {
columns: [column.name]
})
}

const currentColumn: TableColumn = {
name: column.name,
type: column.type,
Expand All @@ -159,23 +156,34 @@ export class CloudflareD1Connection implements Connection {
// TODO: Need to support foreign key references
references: []
}

return currentColumn
})

let current: Table = {
name: table.name,
columns: columns,
indexes: constraints
}

schema.tables.push(current)

// Use the table's schema name (assuming all tables belong to the same schema, e.g., "main")
const schemaName = 'main' // Replace with the actual schema name if needed

if (!schemaMap[schemaName]) {
schemaMap[schemaName] = []
}

schemaMap[schemaName].push(current)
}
}

// Add schema to database
database.push(schema)


// Transform schemaMap into the final Database structure
database = Object.entries(schemaMap).map(([schemaName, tables]) => {
return {
[schemaName]: tables
}
})

return database
}
}
}
Loading

0 comments on commit a830648

Please sign in to comment.