streamby-core 0.24.0 → 0.24.1

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.
@@ -44,8 +44,9 @@ exports.getConnection = getConnection;
44
44
  const ensureTablesExist = async (pool) => {
45
45
  try {
46
46
  await pool.query(`
47
+ CREATE SCHEMA IF NOT EXISTS streamby;
47
48
  CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
48
- CREATE TABLE IF NOT EXISTS projects (
49
+ CREATE TABLE IF NOT EXISTS streamby.projects (
49
50
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
50
51
  name VARCHAR(255) NOT NULL,
51
52
  description TEXT,
@@ -57,21 +58,21 @@ const ensureTablesExist = async (pool) => {
57
58
  console.log('✅ "projects" table ensured to exist.');
58
59
  // Ensure dbType column exists and is correctly configured
59
60
  await pool.query(`
60
- ALTER TABLE projects ADD COLUMN IF NOT EXISTS "dbType" VARCHAR(50);
61
+ ALTER TABLE streamby.projects ADD COLUMN IF NOT EXISTS "dbType" VARCHAR(50);
61
62
  `);
62
63
  await pool.query(`
63
- UPDATE projects SET "dbType" = 'nosql' WHERE "dbType" IS NULL;
64
+ UPDATE streamby.projects SET "dbType" = 'nosql' WHERE "dbType" IS NULL;
64
65
  `);
65
66
  await pool.query(`
66
- ALTER TABLE projects ALTER COLUMN "dbType" SET NOT NULL;
67
+ ALTER TABLE streamby.projects ALTER COLUMN "dbType" SET NOT NULL;
67
68
  `);
68
69
  await pool.query(`
69
- ALTER TABLE projects ALTER COLUMN "dbType" SET DEFAULT 'nosql';
70
+ ALTER TABLE streamby.projects ALTER COLUMN "dbType" SET DEFAULT 'nosql';
70
71
  `);
71
72
  await pool.query(`
72
- CREATE TABLE IF NOT EXISTS exports (
73
+ CREATE TABLE IF NOT EXISTS streamby.exports (
73
74
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
74
- "projectId" UUID NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
75
+ "projectId" UUID NOT NULL REFERENCES streamby.projects(id) ON DELETE CASCADE,
75
76
  status VARCHAR(50) NOT NULL DEFAULT 'pending',
76
77
  "filePath" TEXT,
77
78
  "createdAt" TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
@@ -80,9 +81,9 @@ const ensureTablesExist = async (pool) => {
80
81
  `);
81
82
  console.log('✅ "exports" table ensured to exist.');
82
83
  await pool.query(`
83
- CREATE TABLE IF NOT EXISTS project_members (
84
+ CREATE TABLE IF NOT EXISTS streamby.project_members (
84
85
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
85
- "projectId" UUID NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
86
+ "projectId" UUID NOT NULL REFERENCES streamby.projects(id) ON DELETE CASCADE,
86
87
  "userId" VARCHAR(255) NOT NULL,
87
88
  role VARCHAR(255) NOT NULL DEFAULT 'member',
88
89
  archived BOOLEAN NOT NULL DEFAULT FALSE,
@@ -10,7 +10,7 @@ export declare const createSQLRawExportTable: (connection: Pool, projectId: stri
10
10
  }>;
11
11
  export declare const sqlAdapter: {
12
12
  find: (connection: Pool, tableName: string, filter: any) => Promise<any[]>;
13
- findOne: (connection: Pool, tableName: string, filter: any) => Promise<any | null>;
13
+ findOne: (connection: Pool, tableName: string, filter: any, schema?: string) => Promise<any | null>;
14
14
  create: (connection: Pool, tableName: string, data: any) => Promise<any>;
15
15
  update: (connection: Pool, tableName: string, filter: any, data: any) => Promise<any | null>;
16
16
  delete: (connection: Pool, tableName: string, filter: any) => Promise<number>;
@@ -55,11 +55,12 @@ exports.sqlAdapter = {
55
55
  const result = await connection.query(query, values);
56
56
  return result.rows;
57
57
  },
58
- findOne: async (connection, tableName, filter) => {
58
+ findOne: async (connection, tableName, filter, schema) => {
59
59
  const keys = Object.keys(filter);
60
60
  const values = Object.values(filter);
61
- const where = keys.map((key, i) => `"${key}" = $${i + 1}`).join(' AND ');
62
- const result = await connection.query(`SELECT * FROM "${tableName}" WHERE ${where} LIMIT 1`, values);
61
+ const where = keys.map((key, i) => `"${key}" = ${i + 1}`).join(' AND ');
62
+ const fullTableName = schema ? `"${schema}"."${tableName}"` : `"${tableName}"`;
63
+ const result = await connection.query(`SELECT * FROM ${fullTableName} WHERE ${where} LIMIT 1`, values);
63
64
  return result.rows[0] || null;
64
65
  },
65
66
  create: async (connection, tableName, data) => {
@@ -58,7 +58,7 @@ function projectRouter(config) {
58
58
  return res.status(500).json({ message: 'Main database not configured' });
59
59
  }
60
60
  const userDbType = mainDb.type;
61
- const User = (0, manager_1.getModel)('users', userDbType);
61
+ const User = (0, manager_1.getModel)('users', userDbType, 'accounts');
62
62
  const user = await User.findOne({ _id: auth.userId });
63
63
  if (!user) {
64
64
  return res.status(404).json({ message: 'User not found' });
@@ -80,6 +80,7 @@ function projectRouter(config) {
80
80
  res.status(200).json({ success: true, projects: projects, projectId: newProject._id || newProject.id, message: 'Project created successfully' });
81
81
  }
82
82
  catch (err) {
83
+ console.error('Error creating project:', err);
83
84
  res.status(500).json({ message: 'Failed to create project', details: err.message });
84
85
  }
85
86
  });
@@ -248,7 +249,7 @@ function projectRouter(config) {
248
249
  return res.status(500).json({ message: 'Main database not configured' });
249
250
  }
250
251
  const userDbType = mainDb.type;
251
- const User = (0, manager_1.getModel)('users', userDbType);
252
+ const User = (0, manager_1.getModel)('users', userDbType, 'accounts');
252
253
  const membersWithUsernames = await Promise.all(project.members.map(async (member) => {
253
254
  const user = await User.findOne({ _id: member.userId });
254
255
  return {
@@ -2,7 +2,10 @@ import { Document } from 'mongodb';
2
2
  export declare class Model<T extends Document> {
3
3
  private connectionIds;
4
4
  private tableName;
5
- constructor(connectionIds: string[], tableName: string);
5
+ private schema?;
6
+ constructor(connectionIds: string[], tableName: string, schema?: string);
7
+ getConnectionIds(): string[];
8
+ getTableName(): string;
6
9
  useDbType(dbType: string): Model<T>;
7
10
  find(filter: any): Promise<T[]>;
8
11
  private transformResult;
@@ -6,9 +6,17 @@ const mongodb_1 = require("mongodb");
6
6
  const sql_1 = require("../adapters/database/sql");
7
7
  const nosql_1 = require("../adapters/database/nosql");
8
8
  class Model {
9
- constructor(connectionIds, tableName) {
9
+ constructor(connectionIds, tableName, schema) {
10
10
  this.connectionIds = connectionIds;
11
11
  this.tableName = tableName;
12
+ this.schema = schema;
13
+ }
14
+ // Add these getter methods
15
+ getConnectionIds() {
16
+ return this.connectionIds;
17
+ }
18
+ getTableName() {
19
+ return this.tableName;
12
20
  }
13
21
  useDbType(dbType) {
14
22
  const filteredConnectionIds = this.connectionIds.filter(id => {
@@ -96,7 +104,14 @@ class Model {
96
104
  processedFilter.id = processedFilter._id;
97
105
  delete processedFilter._id;
98
106
  }
99
- const result = await sql_1.sqlAdapter.findOne(connection, this.tableName, processedFilter);
107
+ // Special handling for 'users' table with raw query
108
+ if (this.tableName === 'users' && processedFilter.id) {
109
+ const schema = this.schema || 'public'; // Default to 'public' if schema not provided
110
+ const query = `SELECT * FROM "${schema}"."${this.tableName}" WHERE "id" = $1::uuid LIMIT 1`;
111
+ const result = await connection.query(query, [processedFilter.id]);
112
+ return result.rows[0] || null;
113
+ }
114
+ const result = await sql_1.sqlAdapter.findOne(connection, this.tableName, processedFilter, this.schema); // Pass this.schema
100
115
  if (result) {
101
116
  const transformedResult = this.transformResult(result);
102
117
  if (this.tableName === 'projects') {
@@ -1,3 +1,3 @@
1
1
  import { Model } from './Model';
2
- export declare const defineModel: (name: string, connectionIds: string[], tableName: string) => void;
3
- export declare const getModel: (name: string, dbType?: string) => Model<any>;
2
+ export declare const defineModel: (name: string, connectionIds: string[], tableName: string, schema?: string) => void;
3
+ export declare const getModel: (name: string, dbType?: string, schema?: string) => Model<any>;
@@ -3,15 +3,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getModel = exports.defineModel = void 0;
4
4
  const Model_1 = require("./Model");
5
5
  const models = {};
6
- const defineModel = (name, connectionIds, tableName) => {
7
- models[name] = new Model_1.Model(connectionIds, tableName);
6
+ const defineModel = (name, connectionIds, tableName, schema) => {
7
+ models[name] = new Model_1.Model(connectionIds, tableName, schema);
8
8
  };
9
9
  exports.defineModel = defineModel;
10
- const getModel = (name, dbType) => {
10
+ const getModel = (name, dbType, schema) => {
11
11
  const model = models[name];
12
12
  if (!model) {
13
13
  throw new Error(`Model ${name} not defined.`);
14
14
  }
15
+ // If a schema is provided, create a new Model instance with the specified schema
16
+ if (schema) {
17
+ return new Model_1.Model(model.getConnectionIds(), model.getTableName(), schema); // Use getters
18
+ }
15
19
  if (dbType) {
16
20
  return model.useDbType(dbType);
17
21
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "streamby-core",
3
- "version": "0.24.0",
3
+ "version": "0.24.1",
4
4
  "description": "StreamBy middleware framework for media storage management",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",