kopia lustrzana https://github.com/cloudflare/wildebeest
				
				
				
			neon: fix switch to JSONB
							rodzic
							
								
									175312789b
								
							
						
					
					
						commit
						c308293d7d
					
				|  | @ -224,14 +224,24 @@ export async function updateActorProperty(db: Database, actorId: URL, key: strin | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export async function setActorAlias(db: Database, actorId: URL, alias: URL) { | export async function setActorAlias(db: Database, actorId: URL, alias: URL) { | ||||||
| 	const { success, error } = await db | 	if (db.client === 'neon') { | ||||||
| 		.prepare( | 		const { success, error } = await db | ||||||
| 			`UPDATE actors SET properties=${db.qb.jsonSet('properties', 'alsoKnownAs', db.qb.jsonArray('?1'))} WHERE id=?2` | 			.prepare(`UPDATE actors SET properties=${db.qb.jsonSet('properties', 'alsoKnownAs,0', '?1')} WHERE id=?2`) | ||||||
| 		) | 			.bind('"' + alias.toString() + '"', actorId.toString()) | ||||||
| 		.bind(alias.toString(), actorId.toString()) | 			.run() | ||||||
| 		.run() | 		if (!success) { | ||||||
| 	if (!success) { | 			throw new Error('SQL error: ' + error) | ||||||
| 		throw new Error('SQL error: ' + error) | 		} | ||||||
|  | 	} else { | ||||||
|  | 		const { success, error } = await db | ||||||
|  | 			.prepare( | ||||||
|  | 				`UPDATE actors SET properties=${db.qb.jsonSet('properties', 'alsoKnownAs', 'json_array(?1)')} WHERE id=?2` | ||||||
|  | 			) | ||||||
|  | 			.bind(alias.toString(), actorId.toString()) | ||||||
|  | 			.run() | ||||||
|  | 		if (!success) { | ||||||
|  | 			throw new Error('SQL error: ' + error) | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -246,7 +256,16 @@ export async function getActorById(db: Database, id: URL): Promise<Actor | null> | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export function personFromRow(row: any): Person { | export function personFromRow(row: any): Person { | ||||||
| 	const properties = JSON.parse(row.properties) as PersonProperties | 	let properties | ||||||
|  | 	if (typeof row.properties === 'object') { | ||||||
|  | 		// neon uses JSONB for properties which is returned as a deserialized
 | ||||||
|  | 		// object.
 | ||||||
|  | 		properties = row.properties as PersonProperties | ||||||
|  | 	} else { | ||||||
|  | 		// D1 uses a string for JSON properties
 | ||||||
|  | 		properties = JSON.parse(row.properties) as PersonProperties | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	const icon = properties.icon ?? { | 	const icon = properties.icon ?? { | ||||||
| 		type: 'Image', | 		type: 'Image', | ||||||
| 		mediaType: 'image/jpeg', | 		mediaType: 'image/jpeg', | ||||||
|  |  | ||||||
|  | @ -26,10 +26,6 @@ const qb: QueryBuilder = { | ||||||
| 		return '' | 		return '' | ||||||
| 	}, | 	}, | ||||||
| 
 | 
 | ||||||
| 	jsonArray(r: string): string { |  | ||||||
| 		return `json_array(${r})` |  | ||||||
| 	}, |  | ||||||
| 
 |  | ||||||
| 	jsonSet(obj: string, field: string, value: string): string { | 	jsonSet(obj: string, field: string, value: string): string { | ||||||
| 		return `json_set(${obj}, '$.${field}', ${value})` | 		return `json_set(${obj}, '$.${field}', ${value})` | ||||||
| 	}, | 	}, | ||||||
|  |  | ||||||
|  | @ -33,7 +33,6 @@ export interface QueryBuilder { | ||||||
| 	epoch(): string | 	epoch(): string | ||||||
| 	insertOrIgnore(q: string): string | 	insertOrIgnore(q: string): string | ||||||
| 	psqlOnly(raw: string): string | 	psqlOnly(raw: string): string | ||||||
| 	jsonArray(r: string): string |  | ||||||
| 	jsonSet(obj: string, field: string, value: string): string | 	jsonSet(obj: string, field: string, value: string): string | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -12,7 +12,7 @@ function sqliteToPsql(query: string): string { | ||||||
| 
 | 
 | ||||||
| const qb: QueryBuilder = { | const qb: QueryBuilder = { | ||||||
| 	jsonExtract(obj: string, prop: string): string { | 	jsonExtract(obj: string, prop: string): string { | ||||||
| 		return `json_extract_path(${obj}::json, '${prop}')::text` | 		return `jsonb_extract_path(${obj}, '${prop}')::text` | ||||||
| 	}, | 	}, | ||||||
| 
 | 
 | ||||||
| 	jsonExtractIsNull(obj: string, prop: string): string { | 	jsonExtractIsNull(obj: string, prop: string): string { | ||||||
|  | @ -35,12 +35,8 @@ const qb: QueryBuilder = { | ||||||
| 		return q | 		return q | ||||||
| 	}, | 	}, | ||||||
| 
 | 
 | ||||||
| 	jsonArray(r: string): string { |  | ||||||
| 		return `json_array_elements_text(${r})` |  | ||||||
| 	}, |  | ||||||
| 
 |  | ||||||
| 	jsonSet(obj: string, field: string, value: string): string { | 	jsonSet(obj: string, field: string, value: string): string { | ||||||
| 		return `jsonb_set(${obj}, '${field}', ${value})` | 		return `jsonb_set(${obj}, '{${field}}', ${value})` | ||||||
| 	}, | 	}, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Ładowanie…
	
		Reference in New Issue
	
	 Sven Sauleau
						Sven Sauleau